From 99bc815e07e39354f51421b77f4012ffd35594d8 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 28 六月 2023 18:03:00 +0800
Subject: [PATCH] 2023年06月28日18:02:58
---
HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/TcpClient.java | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 176 insertions(+), 0 deletions(-)
diff --git a/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/TcpClient.java b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/TcpClient.java
new file mode 100644
index 0000000..d5e6f38
--- /dev/null
+++ b/HDLLinkLocalSdk/src/main/java/com/hdl/sdk/link/socket/client/TcpClient.java
@@ -0,0 +1,176 @@
+package com.hdl.sdk.link.socket.client;
+
+
+
+import com.hdl.sdk.link.common.utils.LogUtils;
+import com.hdl.sdk.link.common.utils.ThreadToolUtils;
+import com.hdl.sdk.link.core.protocol.LinkMessageDecoder;
+import com.hdl.sdk.link.socket.TcpSocketBoot;
+import com.hdl.sdk.link.socket.SocketOptions;
+import com.hdl.sdk.link.socket.annotation.ConnectStatus;
+import com.hdl.sdk.link.socket.bean.Packet;
+import com.hdl.sdk.link.socket.codec.IHandleMessage;
+import com.hdl.sdk.link.socket.listener.ConnectStatusListener;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.List;
+
+
+/**
+ * Created by Tong on 2021/9/15.
+ */
+public final class TcpClient implements IClient {
+
+
+ private SocketOptions socketOptions;
+
+ private final String ip;
+ private final int port;
+
+ private Socket mSocket;
+
+ 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 TcpSocketBoot init(String ip, int port, SocketOptions options) {
+ return new TcpSocketBoot(new TcpClient(ip, port, options));
+ }
+
+
+
+
+ @Override
+ public void connect() throws Exception {
+ mSocket = getSocket();
+ mSocket.connect(new InetSocketAddress(ip, port), 3 * 1000);
+ mSocket.setTcpNoDelay(true);
+ mSocket.setReuseAddress(true);
+// mSocket.setKeepAlive(true);
+// mSocket.setSoTimeout(12 * 1000);
+ }
+
+
+
+
+ @Override
+ public void disconnect() {
+ if (mSocket != null) {
+ try {
+ mSocket.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ @Override
+ public synchronized SocketOptions getOptions() {
+ if (socketOptions == null) {
+ socketOptions = new SocketOptions();
+ }
+ return socketOptions;
+ }
+
+ @Override
+ public void onHandleResponse() throws Exception {
+ final InputStream stream = getInputStream();
+ if (stream != null && getOptions() != null) {
+ int len = 0;
+
+ if ((len = stream.read(readBuffer)) == -1) {
+ throw new Exception("缃戝叧鏂紑");
+ }
+
+ byte[] bytes = new byte[len];
+ System.arraycopy(readBuffer, 0, bytes, 0, len);
+ socketOptions.getHandleMessage().read(new Packet(bytes, mSocket));
+ }
+ }
+
+ @Override
+ public synchronized 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.flush();
+ }
+ }
+ }
+
+
+ /**
+ * 澶勭悊杩炴帴鐘舵��
+ */
+ public void onConnectStatus(int status) {
+ ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ final List<ConnectStatusListener> list = getOptions().getConnectStatusListener();
+ if (list != null && !list.isEmpty()) {
+ for (ConnectStatusListener listener : list) {
+ switch (status) {
+ case ConnectStatus
+ .CONNECTING:
+ listener.onConnecting();
+ break;
+ case ConnectStatus
+ .CONNECTED:
+ listener.onConnected();
+ break;
+ case ConnectStatus
+ .DISCONNECT:
+ listener.onConnectFailed();
+ break;
+ }
+ }
+ }
+ }
+ });
+ }
+
+
+ private synchronized Socket getSocket() {
+ return new Socket();
+ }
+
+ private InputStream getInputStream() {
+ if (mSocket != null && mSocket.isConnected() && !mSocket.isClosed()) {
+ try {
+ return mSocket.getInputStream();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }
+
+
+ private OutputStream getOutStream() {
+ if (mSocket != null && mSocket.isConnected() && !mSocket.isClosed()) {
+ try {
+ return mSocket.getOutputStream();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }
+
+
+}
--
Gitblit v1.8.0