copy from "\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs"
copy to "\344\273\243\347\240\201\347\256\241\347\220\206/Mould/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs"
| Îļþ´Ó ´úÂë¹ÜÀí/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs ¸´ÖÆ |
| | |
| | | using System.Text; |
| | | using System.Text.Json; |
| | | using System.IO; |
| | | using WIDESEAWCS_Tasks.StackerCraneJob; |
| | | |
| | | namespace WIDESEAWCS_Tasks.SocketServer |
| | | { |
| | |
| | | /// <param name="cancellationToken">å¯ç¨äºåæ¶å®¢æ·ç«¯å¤çæä½ç忶令çãå¦æè¯·æ±åæ¶ï¼æ¹æ³å°ç«å³ç»æ¢å¤çã</param> |
| | | /// <param name="stackerState">表示ä¸å®¢æ·ç«¯å
³èçæºå¨äººèµ·éæºçå½åç¶æå¯¹è±¡ãç¨äºä¸ºæ¶æ¯å¤çåäºä»¶è°ç¨æä¾ä¸ä¸æã</param> |
| | | /// <returns>表示å¤ç客æ·ç«¯è¿æ¥ç弿¥æä½çä»»å¡ãå½å®¢æ·ç«¯æå¼è¿æ¥æè¯·æ±åæ¶æ¶ä»»å¡å®æã</returns> |
| | | public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken, StackerSocketState stackerState) |
| | | public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken, StackerSocketState_BTI stackerState) |
| | | { |
| | | using (client) |
| | | using (NetworkStream networkStream = client.GetStream()) |
| | |
| | | |
| | | string messageLower = message.ToLowerInvariant(); |
| | | |
| | | if (TryHandleRegister(messageLower, message, clientId, networkStream, cancellationToken)) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | if (MessageReceived != null) |
| | | if (MessageReceivedBTI != null) |
| | | { |
| | | try |
| | | { |
| | | // 夿æ¯å¦ä¸º JSON æ ¼å¼ |
| | | bool isJsonFormat = TryParseJsonSilent(message); |
| | | _ = MessageReceived.Invoke(message, isJsonFormat, client, stackerState); |
| | | _ = MessageReceivedBTI.Invoke(message, isJsonFormat, client, stackerState); |
| | | } |
| | | catch { } |
| | | } |
| | |
| | | { |
| | | try { localCts?.Cancel(); localCts?.Dispose(); } catch { } |
| | | RemoveClient(clientId); |
| | | try { _ = RobotReceived.Invoke(clientId); } catch { } |
| | | try { _ = StackerReceivedBTI.Invoke(clientId); } catch { } |
| | | } |
| | | } |
| | | } |
| | | |
| | | public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken, StackerSocketState_CP01 stackerState) |
| | | { |
| | | using (client) |
| | | using (NetworkStream networkStream = client.GetStream()) |
| | | using (StreamReader reader = new(networkStream, _textEncoding, false, 1024, true)) |
| | | using (StreamWriter writer = new(networkStream, _textEncoding, 1024, true) { AutoFlush = true }) |
| | | { |
| | | CancellationTokenSource? localCts = null; |
| | | if (_options.EnableHeartbeat || _options.IdleTimeoutSeconds > 0) |
| | | { |
| | | localCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | | } |
| | | |
| | | try |
| | | { |
| | | while (!cancellationToken.IsCancellationRequested && client.Connected) |
| | | { |
| | | string? message; |
| | | try |
| | | { |
| | | var ct = localCts?.Token ?? cancellationToken; |
| | | message = await ReceiveFullMessageAsync(networkStream, _textEncoding, ct); |
| | | //message = await reader.ReadLineAsync().WaitAsync(ct); |
| | | } |
| | | catch (OperationCanceledException) |
| | | { |
| | | break; |
| | | } |
| | | |
| | | if (message == null) |
| | | { |
| | | break; |
| | | } |
| | | |
| | | UpdateClientStatus(clientId, message); |
| | | |
| | | string messageLower = message.ToLowerInvariant(); |
| | | |
| | | //if (TryHandleRegister(messageLower, message, clientId, networkStream, cancellationToken)) |
| | | //{ |
| | | // continue; |
| | | //} |
| | | |
| | | if (MessageReceivedCP01 != null) |
| | | { |
| | | try |
| | | { |
| | | // 夿æ¯å¦ä¸º JSON æ ¼å¼ |
| | | bool isJsonFormat = TryParseJsonSilent(message); |
| | | _ = MessageReceivedCP01.Invoke(message, isJsonFormat, client, stackerState); |
| | | } |
| | | catch { } |
| | | } |
| | | } |
| | | } |
| | | finally |
| | | { |
| | | try { localCts?.Cancel(); localCts?.Dispose(); } catch { } |
| | | RemoveClient(clientId); |
| | | try { _ = StackerReceivedCP01.Invoke(clientId); } catch { } |
| | | } |
| | | } |
| | | } |
| | | |
| | | public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken, StackerSocketState_CP02 stackerState) |
| | | { |
| | | using (client) |
| | | using (NetworkStream networkStream = client.GetStream()) |
| | | using (StreamReader reader = new(networkStream, _textEncoding, false, 1024, true)) |
| | | using (StreamWriter writer = new(networkStream, _textEncoding, 1024, true) { AutoFlush = true }) |
| | | { |
| | | CancellationTokenSource? localCts = null; |
| | | if (_options.EnableHeartbeat || _options.IdleTimeoutSeconds > 0) |
| | | { |
| | | localCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | | } |
| | | |
| | | try |
| | | { |
| | | while (!cancellationToken.IsCancellationRequested && client.Connected) |
| | | { |
| | | string? message; |
| | | try |
| | | { |
| | | var ct = localCts?.Token ?? cancellationToken; |
| | | message = await ReceiveFullMessageAsync(networkStream, _textEncoding, ct); |
| | | //message = await reader.ReadLineAsync().WaitAsync(ct); |
| | | } |
| | | catch (OperationCanceledException) |
| | | { |
| | | break; |
| | | } |
| | | |
| | | if (message == null) |
| | | { |
| | | break; |
| | | } |
| | | |
| | | UpdateClientStatus(clientId, message); |
| | | |
| | | string messageLower = message.ToLowerInvariant(); |
| | | |
| | | //if (TryHandleRegister(messageLower, message, clientId, networkStream, cancellationToken)) |
| | | //{ |
| | | // continue; |
| | | //} |
| | | |
| | | if (MessageReceivedCP02 != null) |
| | | { |
| | | try |
| | | { |
| | | // 夿æ¯å¦ä¸º JSON æ ¼å¼ |
| | | bool isJsonFormat = TryParseJsonSilent(message); |
| | | _ = MessageReceivedCP02.Invoke(message, isJsonFormat, client, stackerState); |
| | | } |
| | | catch { } |
| | | } |
| | | } |
| | | } |
| | | finally |
| | | { |
| | | try { localCts?.Cancel(); localCts?.Dispose(); } catch { } |
| | | RemoveClient(clientId); |
| | | try { _ = StackerReceivedCP02.Invoke(clientId); } catch { } |
| | | } |
| | | } |
| | | } |
| | |
| | | /// <returns>å¦ææ¶æ¯è¢«è¯å«å¹¶ä½ä¸ºæ³¨å请æ±å¤çï¼åè¿å trueï¼å¦åè¿å falseã</returns> |
| | | private bool TryHandleRegister(string messageLower, string message, string clientId, NetworkStream networkStream, CancellationToken cancellationToken) |
| | | { |
| | | if (!messageLower.StartsWith("register,")) |
| | | { |
| | | return false; |
| | | } |
| | | //if (!messageLower.StartsWith("register,")) |
| | | //{ |
| | | // return false; |
| | | //} |
| | | |
| | | string deviceId = message.Substring("register,".Length).Trim(); |
| | | if (!string.IsNullOrEmpty(deviceId)) |
| | | { |
| | | //string deviceId = message.Substring("register,".Length).Trim(); |
| | | //if (!string.IsNullOrEmpty(deviceId)) |
| | | //{ |
| | | lock (_syncRoot) |
| | | { |
| | | _deviceBindings[deviceId] = clientId; |
| | | _deviceBindings[clientId] = clientId; |
| | | } |
| | | |
| | | _ = WriteToClientAsync(clientId, networkStream, $"Registered,{deviceId}", cancellationToken); |
| | | } |
| | | _ = WriteToClientAsync(clientId, networkStream, $"Registered,{clientId}", cancellationToken); |
| | | //} |
| | | |
| | | return true; |
| | | } |