左晓为主开发手持机充值管理机
zuoxiao
2023-11-14 5a843ba11b991722f8f2af386f0e546e2769f7c3
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.easysocket.connection.connect;
 
import com.easysocket.config.DefaultX509ProtocolTrustManager;
import com.easysocket.config.SocketSSLConfig;
import com.easysocket.connection.action.SocketStatus;
import com.easysocket.entity.SocketAddress;
import com.easysocket.utils.LogUtil;
import com.easysocket.utils.Utils;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.SecureRandom;
 
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
 
/**
 * Author:Alex
 * Date:2019/5/29
 * Note:tcp连接
 */
public class TcpConnection extends SuperConnection {
    /**
     * socket对象
     */
    private Socket socket;
 
    public TcpConnection(SocketAddress socketAddress) {
        super(socketAddress);
    }
 
    @Override
    protected void openConnection() throws Exception {
            try {
                socket = getSocket();
            } catch (Exception e) {
                e.printStackTrace();
                connectionStatus.set(SocketStatus.SOCKET_DISCONNECTED); // 设置为未连接
                throw new RuntimeException("创建socket失败");
            }
 
            // 进行socket连接
            socket.connect(new InetSocketAddress(socketAddress.getIp(), socketAddress.getPort()), socketOptions.getConnectTimeout());
 
            // 关闭Nagle算法,无论TCP数据报大小,立即发送
            socket.setTcpNoDelay(true);
            // 连接已经打开
            if (socket.isConnected() && !socket.isClosed()) {
                onConnectionOpened();
            }
    }
 
    @Override
    protected void closeConnection() throws IOException {
        if (socket != null)
            socket.close();
    }
 
    /**
     * 根据配置信息获取对应的socket
     *
     * @return
     */
    private synchronized Socket getSocket() throws Exception {
        // 自定义的socket生成工厂
        if (socketOptions.getSocketFactory() != null) {
            return socketOptions.getSocketFactory().createSocket(socketAddress, socketOptions);
        }
        // 默认操作
        SocketSSLConfig config = socketOptions.getEasySSLConfig();
        if (config == null) {
            return new Socket();
        }
        // 获取SSL配置工厂
        SSLSocketFactory factory = config.getCustomSSLFactory();
        if (factory == null) {
            String protocol = "SSL";
            if (!Utils.isStringEmpty(config.getProtocol())) {
                protocol = config.getProtocol();
            }
 
            TrustManager[] trustManagers = config.getTrustManagers();
            if (trustManagers == null || trustManagers.length == 0) {
                // 缺省信任所有证书
                trustManagers = new TrustManager[]{new DefaultX509ProtocolTrustManager()};
            }
 
            try {
                SSLContext sslContext = SSLContext.getInstance(protocol);
                sslContext.init(config.getKeyManagers(), trustManagers, new SecureRandom());
                return sslContext.getSocketFactory().createSocket();
            } catch (Exception e) {
                if (socketOptions.isDebug()) {
                    e.printStackTrace();
                }
                LogUtil.e(e.getMessage());
                return new Socket();
            }
 
        } else {
            try {
                return factory.createSocket();
            } catch (IOException e) {
                if (socketOptions.isDebug()) {
                    e.printStackTrace();
                }
                LogUtil.e(e.getMessage());
                return new Socket();
            }
        }
    }
 
 
    @Override
    public InputStream getInputStream() {
        if (socket != null && socket.isConnected() && !socket.isClosed()) {
            try {
                return socket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
    @Override
    public OutputStream getOutStream() {
        if (socket != null && socket.isConnected() && !socket.isClosed()) {
            try {
                return socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}