hxb
2022-09-08 2a01ef5e49422cca49bc7476fc1b8be8c8556561
HDLSDK/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/TcpSocketBoot.java
File was renamed from HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/SocketBoot.java
@@ -1,14 +1,18 @@
package com.hdl.sdk.socket;
package com.hdl.sdk.link.socket;
import com.hdl.sdk.common.utils.LogUtils;
import com.hdl.sdk.common.utils.TextUtils;
import com.hdl.sdk.common.utils.ThreadToolUtils;
import com.hdl.sdk.socket.annotation.ConnectStatus;
import com.hdl.sdk.socket.client.IClient;
import com.hdl.sdk.socket.client.IHeartbeat;
import com.hdl.sdk.socket.listener.SendListener;
import com.hdl.sdk.link.HDLLinkLocalSdk;
import com.hdl.sdk.link.common.utils.LogUtils;
import com.hdl.sdk.link.common.utils.TextUtils;
import com.hdl.sdk.link.common.utils.ThreadToolUtils;
import com.hdl.sdk.link.socket.client.IClient;
import com.hdl.sdk.link.socket.client.IHeartbeat;
import com.hdl.sdk.link.socket.listener.SendListener;
import com.hdl.sdk.link.socket.annotation.ConnectStatus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
@@ -18,7 +22,7 @@
 * Created by Tong on 2021/9/26.
 * Tcp/Udp 启动器
 */
public class SocketBoot {
public class TcpSocketBoot {
    private ExecutorService connectThread;
    private ExecutorService sendThread;
@@ -33,8 +37,7 @@
    /**
     * 当前接收到数据的时间
     */
    private static long time=System.currentTimeMillis();
    private  long time=System.currentTimeMillis();
    /**
     * tcp是否已经连接
     */
@@ -48,12 +51,37 @@
    private final Map<String, SendListener> sendMap = new HashMap<>();
    public SocketBoot(IClient client) {
    public  TcpSocketBoot(IClient client) {
        TCP_SOCKET_BOOT_LIST.add(this);
        this.client = client;
        initConnectThread();
        initReceiveThread();
        initSendThread();
        initHeartbeat();
    }
    /**
     * 记录所有SocketBoot
     */
    final static List<TcpSocketBoot> TCP_SOCKET_BOOT_LIST = new ArrayList();
    /**
     * 根据IP地址及端口获取当前socketBoot
     * @param ipAddress
     * @param port
     * @return
     */
    public static TcpSocketBoot getByEndPoint(String ipAddress, int port){
        if(ipAddress==null){
            return  null;
        }
        for(TcpSocketBoot tcpSocketBoot : TCP_SOCKET_BOOT_LIST){
            if(ipAddress.equals(tcpSocketBoot.getClient().getOptions().getIp())&& tcpSocketBoot.getClient().getOptions().getPort()==port)
            {
                return tcpSocketBoot;
            }
        }
        return  null;
    }
    /**
@@ -63,13 +91,13 @@
        try {
            LogUtils.i("TCP连接");
            client.onConnectStatus(ConnectStatus.CONNECTING);
            Thread.sleep(1000);
//            Thread.sleep(700);
            client.connect();
            connected=true;
            LogUtils.i("TCP连接成功");
            connected=true;
            client.onConnectStatus(ConnectStatus.CONNECTED);
        }catch(Exception e) {
            LogUtils.e("连接异常"+e.getMessage());
            LogUtils.e(e.getMessage());
        }
    }
@@ -85,9 +113,11 @@
                public void run() {
                    while (true) {
                        try {
                            if(connected==false){
                                Thread.sleep(100);
                                continue;
                            }
                            SocketRequest socketRequest = mMessageQueue.take();
//                            final String sendStr = new String(socketRequest.getData(), 0, socketRequest.getData().length);
//                            LogUtils.i("发送数据:" + sendStr);
                            final String action = socketRequest.getAction();
                            try {
                                client.sendMsg(socketRequest.getData());
@@ -108,7 +138,7 @@
                                }
                            }
                        } catch (Exception e) {
                            LogUtils.e("发送失败1:" + e.getMessage());
                        }
                    }
                }
@@ -163,9 +193,9 @@
                            if (!connected) {
                                reconect();
                            }
                            Thread.sleep(3*1000);
                            Thread.sleep(5*1000);
                        } catch (Exception e) {
                            LogUtils.e("定时连接失败:" + e.getMessage());
                        }
                    }
                }
@@ -184,14 +214,14 @@
                public void run() {
                    while (true) {
                        try {
                            //10秒
                            if (connected && 10 * 1000 < (System.currentTimeMillis() - time)) {
                            //5秒
                            if (connected && 5 * 1000 < (System.currentTimeMillis() - time)) {
                                time = System.currentTimeMillis();
                                //心跳检测
                                if (iHeartbeat != null)
                                    iHeartbeat.heartbeat();
                            }
                            Thread.sleep(1000);
                            Thread.sleep(10);
                        } catch (Exception e) {
                            LogUtils.e("定时心跳检测网关异常:" + e.getMessage());
                        }
@@ -200,8 +230,6 @@
            });
        }
    }
    /**
     * 重新连接
     */
@@ -230,7 +258,7 @@
            }
            mMessageQueue.put(request);
        } catch (Exception e) {
            LogUtils.e("发送数据失败:" + e.getMessage());
        }
    }