562935844@qq.com
2022-05-13 a3f9e5ce8dce3d86a60a79b85ce44424a612c6fa
HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java
@@ -1,8 +1,5 @@
package com.hdl.sdk.socket.client;
import com.hdl.sdk.common.utils.ByteUtils;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.ThreadToolUtils;
import com.hdl.sdk.socket.SocketBoot;
@@ -39,21 +36,21 @@
    /**
     * 从连接池中找出当前IP及端口的连接客户端
     *
     * @param ipAdderss 连接IP地址
     * @param port 连接端口
     * @param port      连接端口
     * @return
     */
    public static TcpClient getTcpClientByIP(String ipAdderss,int port) {
        for(TcpClient tcpClient:tcpClientList){
            if(tcpClient.ip.equals(ipAdderss)&&tcpClient.port==port)
            {
    public static TcpClient getTcpClientByIP(String ipAdderss, int port) {
        for (TcpClient tcpClient : tcpClientList) {
            if (tcpClient.ip.equals(ipAdderss) && tcpClient.port == port) {
                return tcpClient;
            }
        }
        return  null;
        return null;
    }
    private byte[] readBuffer = new byte[4*1024];
    private byte[] readBuffer = new byte[4 * 1024];
    private TcpClient(String ip, int port, SocketOptions socketOptions) {
        this.socketOptions = socketOptions;
@@ -83,8 +80,6 @@
    }
    @Override
    public void disconnect() {
        if (mSocket != null) {
@@ -108,18 +103,18 @@
    public void onHandleResponse() throws Exception {
        final InputStream stream = getInputStream();
        if (stream != null && getOptions() != null) {
            while ( true) {
                int len=getInputStream().read(readBuffer);
                if(len<=0){
                    throw  new Exception("接收异常,接收数据长度len="+len);
            while (true) {
                int len = getInputStream().read(readBuffer);
                if (len <= 0) {
                    throw new Exception("接收异常,接收数据长度len=" + len);
                }
                IHandleMessage handleMessage = getOptions().getHandleMessage();
                if (handleMessage != null) {
                    byte []bytes = new byte[len];
                    System.arraycopy(readBuffer,0,bytes,0,len);
                    byte[] bytes = new byte[len];
                    System.arraycopy(readBuffer, 0, bytes, 0, len);
                    //完整的数据才回调
                    handleMessage.read(bytes);
                    handleMessage.read(bytes, ip);
                }
            }
        }
@@ -129,10 +124,10 @@
    public void sendMsg(byte[] msg) throws Exception {
        final OutputStream outputStream = getOutStream();
        if (outputStream != null && getOptions() != null) {
                IHandleMessage handleMessage = getOptions().getHandleMessage();
                handleMessage.write(handleMessage.write(msg));
                outputStream.write(msg);
                outputStream.flush();
            IHandleMessage handleMessage = getOptions().getHandleMessage();
            handleMessage.write(handleMessage.write(msg));
            outputStream.write(msg);
            outputStream.flush();
        }
    }
@@ -141,6 +136,7 @@
     * 处理连接状态
     */
    public void onConnectStatus(int status) {
        ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
            @Override
            public void run() {
@@ -148,16 +144,13 @@
                if (list != null && !list.isEmpty()) {
                    for (ConnectStatusListener listener : list) {
                        switch (status) {
                            case ConnectStatus
                                    .CONNECTING:
                            case 0:
                                listener.onConnecting();
                                break;
                            case ConnectStatus
                                    .CONNECTED:
                            case 1:
                                listener.onConnected();
                                break;
                            case ConnectStatus
                                    .DISCONNECT:
                            case 2:
                                listener.onConnectFailed();
                                break;
                        }
@@ -165,6 +158,9 @@
                }
            }
        });
        LogUtils.i("TcpClient onConnectStatus status:" + status);
    }