营养数据计算优化;人脸增加数据处理;其它优化;
This commit is contained in:
@@ -0,0 +1,300 @@
|
||||
package com.sw.dualscreen.socket;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 局域网多客户端通信管理器:支持并发、心跳、认证、广播、点对点发送
|
||||
* 可用于 Android 和 JVM 程序。
|
||||
*/
|
||||
public class LanCommunicationManager {
|
||||
|
||||
// ============ 监听配置 ============
|
||||
private final int port;
|
||||
private final long HEARTBEAT_TIMEOUT_MS;
|
||||
private final int MAX_CLIENT_THREADS;
|
||||
|
||||
// ============ 状态 ============
|
||||
private volatile boolean running = false;
|
||||
private ServerSocket serverSocket;
|
||||
|
||||
// ============ 线程池 ============
|
||||
private final ExecutorService acceptExecutor = Executors.newSingleThreadExecutor();
|
||||
private final ExecutorService clientExecutor;
|
||||
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
// ============ 客户端会话 ============
|
||||
private final ConcurrentHashMap<String, ClientSession> clients = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<Socket, ClientSession> unAuthSessions = new ConcurrentHashMap<>();
|
||||
|
||||
// ============ 回调接口 ============
|
||||
public interface Listener {
|
||||
// 新客户端完成 AUTH / 认证
|
||||
void onClientConnected(String clientId);
|
||||
|
||||
// 客户端断开
|
||||
void onClientDisconnected(String clientId);
|
||||
|
||||
// 收到业务消息(type != heartbeat/auth)
|
||||
void onMessageReceived(String clientId, JSONObject message);
|
||||
}
|
||||
|
||||
private Listener listener;
|
||||
|
||||
// ============ 构造 ============
|
||||
public LanCommunicationManager(int port, int maxClientThreads, long heartbeatTimeoutMs) {
|
||||
this.port = port;
|
||||
this.MAX_CLIENT_THREADS = maxClientThreads;
|
||||
this.HEARTBEAT_TIMEOUT_MS = heartbeatTimeoutMs;
|
||||
this.clientExecutor = Executors.newFixedThreadPool(Math.max(2, maxClientThreads));
|
||||
}
|
||||
|
||||
public void setListener(Listener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
// ============ 启动服务端 ============
|
||||
@SuppressLint("DiscouragedApi")
|
||||
public void start() throws IOException {
|
||||
if (running) return;
|
||||
running = true;
|
||||
|
||||
serverSocket = new ServerSocket(port);
|
||||
serverSocket.setSoTimeout(2000);
|
||||
|
||||
acceptExecutor.execute(this::acceptLoop);
|
||||
scheduler.scheduleAtFixedRate(this::heartbeatCheck,
|
||||
HEARTBEAT_TIMEOUT_MS,
|
||||
HEARTBEAT_TIMEOUT_MS,
|
||||
TimeUnit.MILLISECONDS);
|
||||
|
||||
System.out.println("LanCommunicationManager started on port " + port);
|
||||
}
|
||||
|
||||
private void acceptLoop() {
|
||||
while (running) {
|
||||
try {
|
||||
Socket socket = serverSocket.accept();
|
||||
socket.setSoTimeout((int) HEARTBEAT_TIMEOUT_MS * 2);
|
||||
|
||||
ClientSession session = new ClientSession(socket);
|
||||
unAuthSessions.put(socket, session);
|
||||
|
||||
clientExecutor.execute(() -> clientReadLoop(session));
|
||||
|
||||
} catch (SocketTimeoutException ignore) {
|
||||
} catch (Exception e) {
|
||||
if (running) e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============ 处理客户端数据读取 ============
|
||||
private void clientReadLoop(ClientSession session) {
|
||||
Socket socket = session.socket;
|
||||
|
||||
try (DataInputStream in = new DataInputStream(socket.getInputStream())) {
|
||||
while (running && !socket.isClosed()) {
|
||||
|
||||
int len;
|
||||
try {
|
||||
len = in.readInt();
|
||||
} catch (SocketTimeoutException ste) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (len <= 0 || len > 10 * 1024 * 1024) break;
|
||||
|
||||
byte[] buf = new byte[len];
|
||||
in.readFully(buf);
|
||||
session.updateLastSeen();
|
||||
|
||||
JSONObject msg = new JSONObject(new String(buf));
|
||||
handleMessage(session, msg);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
} finally {
|
||||
closeSession(session);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMessage(ClientSession session, JSONObject msg) {
|
||||
String type = msg.optString("type", "");
|
||||
|
||||
switch (type) {
|
||||
case "auth":
|
||||
handleAuth(session, msg);
|
||||
break;
|
||||
case "heartbeat":
|
||||
session.updateLastSeen();
|
||||
break;
|
||||
default:
|
||||
if (listener != null && session.clientId != null) {
|
||||
listener.onMessageReceived(session.clientId, msg);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleAuth(ClientSession session, JSONObject msg) {
|
||||
String clientId = msg.optString("clientId", null);
|
||||
if (clientId == null) return;
|
||||
|
||||
session.clientId = clientId;
|
||||
|
||||
// 移动到已认证 map
|
||||
unAuthSessions.remove(session.socket);
|
||||
clients.put(clientId, session);
|
||||
|
||||
if (listener != null) listener.onClientConnected(clientId);
|
||||
|
||||
sendToSession(session, ack("auth_ok"));
|
||||
}
|
||||
|
||||
// ============ 心跳超时 ============
|
||||
private void heartbeatCheck() {
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
for (Map.Entry<String, ClientSession> e : clients.entrySet()) {
|
||||
ClientSession s = e.getValue();
|
||||
if (now - s.lastSeen > HEARTBEAT_TIMEOUT_MS) {
|
||||
closeSession(s);
|
||||
}
|
||||
}
|
||||
|
||||
for (ClientSession s : unAuthSessions.values()) {
|
||||
if (now - s.lastSeen > HEARTBEAT_TIMEOUT_MS * 2) {
|
||||
closeSession(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============ 发送 ============
|
||||
public boolean sendToClient(String clientId, JSONObject json) {
|
||||
ClientSession s = clients.get(clientId);
|
||||
return s != null && sendToSession(s, json);
|
||||
}
|
||||
|
||||
public void broadcast(JSONObject json) {
|
||||
for (ClientSession s : clients.values()) {
|
||||
sendToSession(s, json);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean sendToSession(ClientSession s, JSONObject json) {
|
||||
try {
|
||||
DataOutputStream out = s.out;
|
||||
|
||||
synchronized (out) {
|
||||
byte[] data = json.toString().getBytes();
|
||||
out.writeInt(data.length);
|
||||
out.write(data);
|
||||
out.flush();
|
||||
}
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
closeSession(s);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ============ ACK ============
|
||||
private JSONObject ack(String type) {
|
||||
JSONObject j = new JSONObject();
|
||||
try {
|
||||
j.put("type", "ack");
|
||||
j.put("ack", type);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
// ============ 停止 ============
|
||||
public void stop() {
|
||||
running = false;
|
||||
|
||||
try {
|
||||
serverSocket.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
for (ClientSession s : clients.values()) closeSession(s);
|
||||
for (ClientSession s : unAuthSessions.values()) closeSession(s);
|
||||
|
||||
acceptExecutor.shutdownNow();
|
||||
clientExecutor.shutdownNow();
|
||||
scheduler.shutdownNow();
|
||||
|
||||
System.out.println("LanCommunicationManager stopped");
|
||||
}
|
||||
|
||||
// ============ 会话类 ============
|
||||
public static class ClientSession {
|
||||
public final Socket socket;
|
||||
public final DataOutputStream out;
|
||||
public volatile long lastSeen = System.currentTimeMillis();
|
||||
public volatile String clientId;
|
||||
|
||||
public ClientSession(Socket socket) throws IOException {
|
||||
this.socket = socket;
|
||||
this.out = new DataOutputStream(socket.getOutputStream());
|
||||
}
|
||||
|
||||
public void updateLastSeen() {
|
||||
lastSeen = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
|
||||
public void closeSession(ClientSession session) {
|
||||
if (session == null) return;
|
||||
|
||||
try {
|
||||
Socket socket = session.socket;
|
||||
|
||||
// 1. 从已认证表移除
|
||||
if (session.clientId != null) {
|
||||
ClientSession removed = clients.remove(session.clientId);
|
||||
if (removed != null && listener != null) {
|
||||
listener.onClientDisconnected(session.clientId);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 从未认证表移除
|
||||
unAuthSessions.remove(socket);
|
||||
|
||||
// 3. 关闭输出流
|
||||
try {
|
||||
session.out.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
// 4. 关闭 socket
|
||||
try {
|
||||
if (!socket.isClosed()) socket.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.sw.dualscreen.socket;
|
||||
|
||||
public class LanServer {
|
||||
|
||||
private static volatile LanCommunicationManager instance;
|
||||
|
||||
public static LanCommunicationManager getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (LanServer.class) {
|
||||
if (instance == null) {
|
||||
instance = new LanCommunicationManager(
|
||||
5000, // 监听端口
|
||||
20, // 最大客户端数
|
||||
10_000 // 心跳超时时间 10 秒
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.sw.face.collect.view
|
||||
|
||||
import android.util.Log
|
||||
import com.sw.dualscreen.socket.LanCommunicationManager
|
||||
import org.json.JSONObject
|
||||
|
||||
open class LanServerListenerImpl: LanCommunicationManager.Listener {
|
||||
companion object {
|
||||
private const val TAG = "LanServerListenerImpl"
|
||||
}
|
||||
override fun onClientConnected(clientId: String?) {
|
||||
Log.d(TAG, "addSocketListener,onClientConnected: clientId = $clientId")
|
||||
}
|
||||
|
||||
override fun onClientDisconnected(clientId: String?) {
|
||||
Log.d(TAG, "addSocketListener,onClientDisconnected: clientId = $clientId")
|
||||
}
|
||||
|
||||
override fun onMessageReceived(clientId: String?, message: JSONObject?) {
|
||||
Log.d(TAG, "addSocketListener,onMessageReceived: clientId = $clientId,message = $message")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
package com.sw.dualscreen.socket;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* TcpClient - Android ready
|
||||
* <p>
|
||||
* Features:
|
||||
* - length-prefix protocol (int length + bytes)
|
||||
* - separate read thread and write queue & writer thread
|
||||
* - auto-reconnect with exponential backoff
|
||||
* - heartbeat scheduler
|
||||
* - send queue with optional callback for send result
|
||||
* - auto send "auth" JSON after connection
|
||||
*/
|
||||
public class TcpClient {
|
||||
|
||||
private static final String TAG = "TcpClient";
|
||||
|
||||
// configuration
|
||||
private final String serverIp;
|
||||
private final int serverPort;
|
||||
private final String clientId; // will be sent in auth message
|
||||
private final int connectTimeoutMs;
|
||||
private final long heartbeatIntervalMs;
|
||||
private final long heartbeatTimeoutMs;
|
||||
|
||||
// socket + streams
|
||||
private Socket socket;
|
||||
private DataOutputStream out;
|
||||
private DataInputStream in;
|
||||
|
||||
// threads & executors
|
||||
private final ExecutorService writerExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "TcpClient-Writer"));
|
||||
private final ExecutorService readerExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "TcpClient-Reader"));
|
||||
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(r -> new Thread(r, "TcpClient-Scheduler"));
|
||||
private final ExecutorService connectExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "TcpClient-Connect"));
|
||||
|
||||
// send queue
|
||||
private final BlockingQueue<JSONObject> sendQueue = new LinkedBlockingQueue<>();
|
||||
|
||||
// state
|
||||
private final AtomicBoolean running = new AtomicBoolean(false);
|
||||
private final AtomicBoolean connected = new AtomicBoolean(false);
|
||||
private final AtomicBoolean authSent = new AtomicBoolean(false);
|
||||
|
||||
// reconnection/backoff
|
||||
private final long baseReconnectDelayMs = 1000; // 1s
|
||||
private final long maxReconnectDelayMs = 30_000; // 30s
|
||||
private final AtomicInteger reconnectAttempt = new AtomicInteger(0);
|
||||
|
||||
// heartbeat task future
|
||||
private ScheduledFuture<?> heartbeatFuture;
|
||||
|
||||
// listener
|
||||
public interface Listener {
|
||||
void onConnected();
|
||||
|
||||
void onDisconnected(Exception e);
|
||||
|
||||
void onMessage(JSONObject json);
|
||||
|
||||
void onSendSuccess(JSONObject json);
|
||||
|
||||
void onSendFailed(JSONObject json, Exception e);
|
||||
}
|
||||
|
||||
private Listener listener;
|
||||
|
||||
public void setListener(Listener l) {
|
||||
this.listener = l;
|
||||
}
|
||||
|
||||
// ctor
|
||||
public TcpClient(String serverIp, int serverPort, String clientId,
|
||||
int connectTimeoutMs, long heartbeatIntervalMs, long heartbeatTimeoutMs) {
|
||||
this.serverIp = serverIp;
|
||||
this.serverPort = serverPort;
|
||||
this.clientId = clientId;
|
||||
this.connectTimeoutMs = connectTimeoutMs;
|
||||
this.heartbeatIntervalMs = heartbeatIntervalMs;
|
||||
this.heartbeatTimeoutMs = heartbeatTimeoutMs;
|
||||
}
|
||||
|
||||
// start client (will attempt connect)
|
||||
public void start() {
|
||||
if (running.getAndSet(true)) return;
|
||||
scheduleConnect(0);
|
||||
// writer thread drains sendQueue
|
||||
writerExecutor.execute(this::writerLoop);
|
||||
}
|
||||
|
||||
// stop client and cleanup
|
||||
public void stop() {
|
||||
running.set(false);
|
||||
cancelHeartbeat();
|
||||
closeSocketQuiet();
|
||||
writerExecutor.shutdownNow();
|
||||
readerExecutor.shutdownNow();
|
||||
scheduler.shutdownNow();
|
||||
connectExecutor.shutdownNow();
|
||||
sendQueue.clear();
|
||||
}
|
||||
|
||||
// send JSON (queued). Non-blocking.
|
||||
public void send(JSONObject json) {
|
||||
if (!running.get()) return;
|
||||
sendQueue.offer(json);
|
||||
}
|
||||
|
||||
// AUTH shortcut (immediately send auth JSON)
|
||||
private void sendAuth() {
|
||||
try {
|
||||
JSONObject auth = new JSONObject();
|
||||
auth.put("type", "auth");
|
||||
auth.put("clientId", clientId);
|
||||
sendQueue.offer(auth);
|
||||
authSent.set(true);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
// writer thread loop (serializes sends)
|
||||
private void writerLoop() {
|
||||
while (running.get()) {
|
||||
try {
|
||||
JSONObject json = sendQueue.take(); // blocks
|
||||
if (connected.get() && out != null) {
|
||||
try {
|
||||
byte[] data = json.toString().getBytes();
|
||||
synchronized (out) {
|
||||
out.writeInt(data.length);
|
||||
out.write(data);
|
||||
out.flush();
|
||||
}
|
||||
if (listener != null) listener.onSendSuccess(json);
|
||||
} catch (Exception e) {
|
||||
if (listener != null) listener.onSendFailed(json, e);
|
||||
// on write failure, attempt reconnect
|
||||
safeCloseAndScheduleReconnect(e);
|
||||
}
|
||||
} else {
|
||||
// not connected: requeue it and wait for connection
|
||||
sendQueue.offer(json);
|
||||
Thread.sleep(500); // avoid busy loop
|
||||
}
|
||||
} catch (InterruptedException ignored) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// reader loop (runs in readerExecutor)
|
||||
private void startReaderLoop() {
|
||||
readerExecutor.execute(() -> {
|
||||
try {
|
||||
while (running.get() && connected.get() && in != null) {
|
||||
int length;
|
||||
try {
|
||||
length = in.readInt(); // will throw SocketTimeoutException if set
|
||||
} catch (SocketTimeoutException ste) {
|
||||
// used to detect socket liveness; continue loop
|
||||
continue;
|
||||
}
|
||||
if (length <= 0 || length > 10 * 1024 * 1024) {
|
||||
// invalid length, break
|
||||
throw new RuntimeException("Invalid message length: " + length);
|
||||
}
|
||||
byte[] buf = new byte[length];
|
||||
in.readFully(buf);
|
||||
String s = new String(buf);
|
||||
try {
|
||||
JSONObject json = new JSONObject(s);
|
||||
// update last seen time via heartbeat ack if needed
|
||||
if ("heartbeat".equals(json.optString("type"))) {
|
||||
// optionally respond or update time
|
||||
} else if ("auth_ok".equals(json.optString("type")) || "ack".equals(json.optString("type"))) {
|
||||
// ignore or process ack
|
||||
} else {
|
||||
if (listener != null) listener.onMessage(json);
|
||||
}
|
||||
} catch (Exception je) {
|
||||
Log.w(TAG, "Invalid JSON from server: " + s, je);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (running.get()) {
|
||||
safeCloseAndScheduleReconnect(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// schedule connect attempt with delay (ms)
|
||||
private void scheduleConnect(long delayMs) {
|
||||
connectExecutor.execute(() -> {
|
||||
try {
|
||||
if (delayMs > 0) Thread.sleep(delayMs);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
if (!running.get()) return;
|
||||
tryConnect();
|
||||
});
|
||||
}
|
||||
|
||||
// connect logic
|
||||
private void tryConnect() {
|
||||
if (!running.get()) return;
|
||||
closeSocketQuiet(); // ensure closed
|
||||
try {
|
||||
Socket s = new Socket();
|
||||
s.connect(new InetSocketAddress(serverIp, serverPort), connectTimeoutMs);
|
||||
s.setSoTimeout((int) Math.max(heartbeatTimeoutMs, 5_000));
|
||||
socket = s;
|
||||
out = new DataOutputStream(socket.getOutputStream());
|
||||
in = new DataInputStream(socket.getInputStream());
|
||||
connected.set(true);
|
||||
reconnectAttempt.set(0);
|
||||
authSent.set(false);
|
||||
// start reader
|
||||
startReaderLoop();
|
||||
// send auth immediately
|
||||
sendAuth();
|
||||
// start heartbeat
|
||||
startHeartbeat();
|
||||
if (listener != null) listener.onConnected();
|
||||
Log.i(TAG, "Connected to " + serverIp + ":" + serverPort);
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "Connect failed: " + e.getMessage());
|
||||
scheduleReconnectWithBackoff();
|
||||
}
|
||||
}
|
||||
|
||||
// start heartbeat scheduler
|
||||
private void startHeartbeat() {
|
||||
cancelHeartbeat();
|
||||
heartbeatFuture = scheduler.scheduleAtFixedRate(() -> {
|
||||
if (!running.get() || !connected.get()) return;
|
||||
try {
|
||||
JSONObject hb = new JSONObject();
|
||||
hb.put("type", "heartbeat");
|
||||
hb.put("time", System.currentTimeMillis());
|
||||
sendQueue.offer(hb);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}, 0, heartbeatIntervalMs, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private void cancelHeartbeat() {
|
||||
if (heartbeatFuture != null && !heartbeatFuture.isCancelled()) {
|
||||
heartbeatFuture.cancel(true);
|
||||
heartbeatFuture = null;
|
||||
}
|
||||
}
|
||||
|
||||
// close socket quietly and notify listener
|
||||
private void safeCloseAndScheduleReconnect(Exception cause) {
|
||||
closeSocketQuiet();
|
||||
if (listener != null) listener.onDisconnected(cause);
|
||||
scheduleReconnectWithBackoff();
|
||||
}
|
||||
|
||||
private void scheduleReconnectWithBackoff() {
|
||||
int attempt = reconnectAttempt.incrementAndGet();
|
||||
long delay = Math.min(maxReconnectDelayMsFromAttempt(attempt), maxReconnectDelayMs);
|
||||
Log.i(TAG, "Scheduling reconnect attempt " + attempt + " after " + delay + "ms");
|
||||
scheduleConnect(delay);
|
||||
}
|
||||
|
||||
// compute exponential backoff
|
||||
private long maxReconnectDelayMsFromAttempt(int attempt) {
|
||||
long d = baseReconnectDelayMs * (1L << Math.min(attempt, 30));
|
||||
if (d < 0) d = maxReconnectDelayMs;
|
||||
return Math.min(d, maxReconnectDelayMs);
|
||||
}
|
||||
|
||||
// close socket and streams
|
||||
private void closeSocketQuiet() {
|
||||
connected.set(false);
|
||||
cancelHeartbeat();
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try {
|
||||
if (socket != null && !socket.isClosed()) {
|
||||
socket.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
out = null;
|
||||
in = null;
|
||||
socket = null;
|
||||
}
|
||||
|
||||
// helper: when manually call reconnect (immediately)
|
||||
public void reconnectNow() {
|
||||
scheduleConnect(0);
|
||||
}
|
||||
|
||||
// helper to set immediate send of a JSON and wait (blocking) until it is queued (not until delivered)
|
||||
public boolean sendBlocking(JSONObject json, long timeoutMs) throws InterruptedException {
|
||||
return sendQueue.offer(json, timeoutMs, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.sw.face.collect.view
|
||||
|
||||
import android.util.Log
|
||||
import com.sw.dualscreen.socket.TcpClient
|
||||
import org.json.JSONObject
|
||||
import java.lang.Exception
|
||||
|
||||
open class TcpClientListenerImpl : TcpClient.Listener {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "TcpClientListenerImpl"
|
||||
}
|
||||
|
||||
override fun onConnected() {
|
||||
Log.d(TAG, "addSocketListener,onConnected: ")
|
||||
}
|
||||
|
||||
override fun onDisconnected(e: Exception?) {
|
||||
Log.d(TAG, "addSocketListener,onDisconnected: ${e?.toString()}")
|
||||
}
|
||||
|
||||
override fun onMessage(json: JSONObject?) {
|
||||
Log.d(TAG, "addSocketListener,onMessage: $json")
|
||||
}
|
||||
|
||||
override fun onSendSuccess(json: JSONObject?) {
|
||||
Log.d(TAG, "addSocketListener,onSendSuccess: $json")
|
||||
}
|
||||
|
||||
override fun onSendFailed(json: JSONObject?, e: Exception?) {
|
||||
Log.d(TAG, "addSocketListener,onSendFailed: $json,e:${e?.toString()}")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.sw.dualscreen.socket;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
|
||||
/**
|
||||
* Simple UDP discovery client:
|
||||
* Sends "DISCOVER_SERVER" broadcast and waits for first reply "SERVER_FOUND:serverName:ip"
|
||||
*/
|
||||
public class UdpDiscoveryClient {
|
||||
|
||||
public interface Listener {
|
||||
void onFound(String ip, String serverName);
|
||||
|
||||
void onError(Exception e);
|
||||
}
|
||||
|
||||
public void discover(int discoveryPort, int timeoutMs, Listener listener) {
|
||||
new Thread(() -> {
|
||||
try (DatagramSocket socket = new DatagramSocket()) {
|
||||
socket.setBroadcast(true);
|
||||
byte[] data = "DISCOVER_SERVER".getBytes();
|
||||
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName("255.255.255.255"), discoveryPort);
|
||||
socket.send(packet);
|
||||
|
||||
socket.setSoTimeout(timeoutMs);
|
||||
byte[] buf = new byte[512];
|
||||
DatagramPacket resp = new DatagramPacket(buf, buf.length);
|
||||
socket.receive(resp);
|
||||
|
||||
String msg = new String(resp.getData(), 0, resp.getLength());
|
||||
if (msg.startsWith("SERVER_FOUND")) {
|
||||
// format: SERVER_FOUND:serverName:ip
|
||||
String[] parts = msg.split(":", 3);
|
||||
if (parts.length >= 3) {
|
||||
listener.onFound(parts[2], parts[1]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
listener.onError(new Exception("Invalid response"));
|
||||
} catch (Exception e) {
|
||||
listener.onError(e);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.sw.dualscreen.socket;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class test {
|
||||
LanCommunicationManager manager = new LanCommunicationManager(
|
||||
9999, // 端口
|
||||
20, // 最大客户端线程
|
||||
30_000 // 心跳超时 30 秒
|
||||
);
|
||||
|
||||
public void test() {
|
||||
manager.setListener(new LanCommunicationManager.Listener() {
|
||||
@Override
|
||||
public void onClientConnected(String clientId) {
|
||||
System.out.println("新的客户端上线:" + clientId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClientDisconnected(String clientId) {
|
||||
System.out.println("客户端离线:" + clientId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(String clientId, JSONObject msg) {
|
||||
System.out.println("收到 " + clientId + " 的消息:" + msg);
|
||||
}
|
||||
});
|
||||
|
||||
// 启动
|
||||
try {
|
||||
manager.start();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// 给某个客户端发送
|
||||
JSONObject j = new JSONObject();
|
||||
try {
|
||||
j.put("type", "cmd");
|
||||
j.put("content", "hello");
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
manager.sendToClient("device123", j);
|
||||
|
||||
// 广播
|
||||
manager.broadcast(j);
|
||||
}
|
||||
|
||||
|
||||
public void clientTets() {
|
||||
// 1) discover server (optional)
|
||||
UdpDiscoveryClient disc = new UdpDiscoveryClient();
|
||||
disc.discover(9876, 3000, new UdpDiscoveryClient.Listener() {
|
||||
@Override
|
||||
public void onFound(String ip, String serverName) {
|
||||
startClient(ip);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) { /* fallback to manual IP */ }
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 2) start client
|
||||
private TcpClient client;
|
||||
|
||||
private void startClient(String serverIp) {
|
||||
client = new TcpClient(
|
||||
serverIp,
|
||||
9999,
|
||||
"device123", // clientId
|
||||
5000, // connectTimeoutMs
|
||||
10_000, // heartbeatIntervalMs
|
||||
30_000 // heartbeatTimeoutMs
|
||||
);
|
||||
|
||||
client.setListener(new TcpClient.Listener() {
|
||||
@Override
|
||||
public void onConnected() {
|
||||
Log.i("APP", "connected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnected(Exception e) {
|
||||
Log.i("APP", "disconnected", e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(JSONObject json) {
|
||||
Log.i("APP", "msg:" + json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSendSuccess(JSONObject json) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSendFailed(JSONObject json, Exception e) {
|
||||
}
|
||||
});
|
||||
|
||||
client.start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user