左晓为主开发手持机充值管理机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.easysocket.connection.iowork;
 
import com.easysocket.config.EasySocketOptions;
import com.easysocket.interfaces.config.IOptions;
import com.easysocket.interfaces.conn.IConnectionManager;
import com.easysocket.interfaces.conn.ISocketActionDispatch;
import com.easysocket.interfaces.io.IIOManager;
import com.easysocket.interfaces.io.IReader;
import com.easysocket.interfaces.io.IWriter;
 
/**
 * Author:Alex
 * Date:2019/5/28
 * Note:
 */
public class IOManager implements IIOManager, IOptions {
    /**
     * socket行为回调
     */
    private ISocketActionDispatch actionDispatch;
    /**
     * 连接管理
     */
    private IConnectionManager connectionManager;
    /**
     * 写
     */
    private IWriter writer;
    /**
     * 读
     */
    private IReader reader;
 
    public IOManager(IConnectionManager connectionManager,
                     ISocketActionDispatch connActionDispatch) {
        this.connectionManager = connectionManager;
        this.actionDispatch = connActionDispatch;
        initIO();
    }
 
    //  初始化io
    private void initIO() {
        //makesureHeaderProtocolNotEmpty();
        reader = new EasyReader(connectionManager, actionDispatch); //  读
        writer = new EasyWriter(connectionManager, actionDispatch); //  写
    }
 
    @Override
    public void sendBytes(byte[] bytes) {
        if (writer != null)
            writer.offer(bytes);
    }
 
    @Override
    public void startIO() {
        if (writer != null)
            writer.openWriter();
        if (reader != null)
            reader.openReader();
    }
 
    @Override
    public void closeIO() {
        if (writer != null)
            writer.closeWriter();
        if (reader != null)
            reader.closeReader();
    }
 
    @Override
    public Object setOptions(EasySocketOptions socketOptions) {
        //makesureHeaderProtocolNotEmpty();
        if (writer != null)
            writer.setOption(socketOptions);
        if (reader != null)
            reader.setOption(socketOptions);
        return this;
    }
 
    @Override
    public EasySocketOptions getOptions() {
        return connectionManager.getOptions();
    }
 
    /**
     * 确保包结构协议不为空
     */
//    private void makesureHeaderProtocolNotEmpty() {
//        IMessageProtocol protocol = connectionManager.getOptions().getMessageProtocol();
//        if (protocol == null) {
//            throw new NoNullException("The reader protocol can not be Null.");
//        }
//
//        if (protocol.getHeaderLength() == 0) {
//            throw new NoNullException("The header length can not be zero.");
//        }
//    }
}