| | |
| | |
|
| | | private final IClient client;
|
| | | private IHeartbeat iHeartbeat;
|
| | |
|
| | | public void SetHeartbeat(IHeartbeat iHeartbeat) {
|
| | | this.iHeartbeat = iHeartbeat;
|
| | | public void SetHeartbeat(IHeartbeat iHeartbeat){
|
| | | this.iHeartbeat=iHeartbeat;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 当前接收到数据的时间
|
| | | */
|
| | | private long time = System.currentTimeMillis();
|
| | | private long time=System.currentTimeMillis();
|
| | | /**
|
| | | * tcp是否已经连接
|
| | | */
|
| | | private boolean connected = false;
|
| | | private boolean connected=false;
|
| | |
|
| | | public IClient getClient() {
|
| | | return client;
|
| | |
| | |
|
| | | private final ArrayMap<String, SendListener> sendMap = new ArrayMap<>();
|
| | |
|
| | | public TcpSocketBoot(IClient client) {
|
| | | public TcpSocketBoot(IClient client) {
|
| | | TCP_SOCKET_BOOT_LIST.add(this);
|
| | | this.client = client;
|
| | | initConnectThread();
|
| | |
| | | /**
|
| | | * 记录所有SocketBoot
|
| | | */
|
| | | final static List<TcpSocketBoot> TCP_SOCKET_BOOT_LIST = new ArrayList<>();
|
| | | 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;
|
| | | 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) {
|
| | | for(TcpSocketBoot tcpSocketBoot : TCP_SOCKET_BOOT_LIST){
|
| | | if(ipAddress.equals(tcpSocketBoot.getClient().getOptions().getIp())&& tcpSocketBoot.getClient().getOptions().getPort()==port)
|
| | | {
|
| | | return tcpSocketBoot;
|
| | | }
|
| | | }
|
| | | return null;
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | */
|
| | | private synchronized void connect() {
|
| | | try {
|
| | | LogUtils.i("TCP连接:" + this.getClient().getOptions().getIp());
|
| | | LogUtils.i("TCP连接:"+this.getClient().getOptions().getIp());
|
| | | client.onConnectStatus(ConnectStatus.CONNECTING);
|
| | | // Thread.sleep(700);
|
| | | client.connect();
|
| | | LogUtils.i("TCP连接成功:" + this.getClient().getOptions().getIp());
|
| | | connected = true;
|
| | | LogUtils.i("TCP连接成功:"+this.getClient().getOptions().getIp());
|
| | | connected=true;
|
| | | client.onConnectStatus(ConnectStatus.CONNECTED);
|
| | | } catch (Exception e) {
|
| | | }catch(Exception e) {
|
| | | LogUtils.e(e.getMessage());
|
| | | }
|
| | | }
|
| | |
| | | public void run() {
|
| | | while (true) {
|
| | | try {
|
| | | if (!connected) {
|
| | | if(connected==false){
|
| | | Thread.sleep(100);
|
| | | continue;
|
| | | }
|
| | |
| | | if (connected) {
|
| | | //读取数据
|
| | | client.onHandleResponse();
|
| | | time = System.currentTimeMillis();
|
| | | time= System.currentTimeMillis();
|
| | | } else {
|
| | | try {
|
| | | Thread.sleep(1000);
|
| | |
| | | if (!connected) {
|
| | | reconect();
|
| | | }
|
| | | Thread.sleep(5 * 1000);
|
| | | Thread.sleep(5*1000);
|
| | | } catch (Exception e) {
|
| | |
|
| | | LogUtils.e("定时连接线程异常:" + e.getMessage());
|
| | |
| | | });
|
| | | }
|
| | | }
|
| | |
|
| | | /**
|
| | | * 重新连接
|
| | | */
|
| | |
| | |
|
| | | /**
|
| | | * 发送无需回调
|
| | | *
|
| | | * @param msg 发送的数据
|
| | | */
|
| | | public void sendMsg(byte[] msg) {
|
| | |
| | | }
|
| | | }
|
| | |
|
| | |
|
| | | // /**
|
| | | // * 断开全部的Link网关连接
|
| | | // */
|