From 11cea3249fd58d59d7c1d644b1cfff6a547921c2 Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期三, 16 二月 2022 13:07:10 +0800 Subject: [PATCH] Merge branch 'dev-hxb' --- HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java | 61 ++++++++++++++++++++---------- 1 files changed, 40 insertions(+), 21 deletions(-) diff --git a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java index 4e9d13c..db3bbcc 100644 --- a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java +++ b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java @@ -3,6 +3,7 @@ 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; import com.hdl.sdk.socket.SocketOptions; @@ -15,6 +16,8 @@ import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; +import java.net.SocketAddress; +import java.util.ArrayList; import java.util.List; @@ -31,12 +34,32 @@ private Socket mSocket; - private byte[] readBuffer; + private final static List<TcpClient> tcpClientList = new ArrayList(); + + /** + * 浠庤繛鎺ユ睜涓壘鍑哄綋鍓岻P鍙婄鍙g殑杩炴帴瀹㈡埛绔� + * @param ipAdderss 杩炴帴IP鍦板潃 + * @param port 杩炴帴绔彛 + * @return + */ + public static TcpClient getTcpClientByIP(String ipAdderss,int port) { + for(TcpClient tcpClient:tcpClientList){ + if(tcpClient.ip.equals(ipAdderss)&&tcpClient.port==port) + { + return tcpClient; + } + } + return null; + } + + private byte[] readBuffer = new byte[4*1024]; private TcpClient(String ip, int port, SocketOptions socketOptions) { this.socketOptions = socketOptions; this.ip = ip; this.port = port; + socketOptions.setIp(ip); + socketOptions.setPort(port); } public static SocketBoot init(String ip, int port, SocketOptions options) { @@ -46,14 +69,19 @@ @Override public void connect() throws Exception { + mSocket = getSocket(); - SocketOptions options = getOptions(); - mSocket.connect(new InetSocketAddress(ip, port)); +// SocketOptions options = getOptions(); + mSocket.connect(new InetSocketAddress(ip, port), 3 * 1000); mSocket.setTcpNoDelay(true); mSocket.setReuseAddress(true); mSocket.setKeepAlive(true); - readBuffer = new byte[options.getReadMaxBufferSize()]; + mSocket.setSoTimeout(20 * 1000); + + tcpClientList.add(this); } + + @Override @@ -68,16 +96,6 @@ } @Override - public boolean isConnect() { - if (mSocket == null) { - return false; - } - - return mSocket.isConnected() && !mSocket.isClosed(); - } - - - @Override public synchronized SocketOptions getOptions() { if (socketOptions == null) { socketOptions = new SocketOptions(); @@ -89,12 +107,17 @@ public void onHandleResponse() throws Exception { final InputStream stream = getInputStream(); if (stream != null && getOptions() != null) { - int len=0; - while ( (len=getInputStream().read(readBuffer)) != -1) { + while ( true) { + int len=getInputStream().read(readBuffer); + if(len<=0){ + throw new Exception("鎺ユ敹寮傚父锛屾帴鏀舵暟鎹暱搴en="+len); + } + IHandleMessage handleMessage = getOptions().getHandleMessage(); if (handleMessage != null) { byte []bytes = new byte[len]; System.arraycopy(readBuffer,0,bytes,0,len); + LogUtils.i("鏀跺埌TCP鏁版嵁",new String(bytes)); //瀹屾暣鐨勬暟鎹墠鍥炶皟 handleMessage.read(bytes); } @@ -106,14 +129,10 @@ public void sendMsg(byte[] msg) throws Exception { final OutputStream outputStream = getOutStream(); if (outputStream != null && getOptions() != null) { - try { IHandleMessage handleMessage = getOptions().getHandleMessage(); handleMessage.write(handleMessage.write(msg)); - getOutStream().write(msg); - - } finally { + outputStream.write(msg); outputStream.flush(); - } } } -- Gitblit v1.8.0