From 6e6d3b2050f8450ca359fc0bd06960ae1bba577a Mon Sep 17 00:00:00 2001 From: lishuo Date: Fri, 10 May 2024 16:32:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=B9=E6=88=98=E6=88=BF?= =?UTF-8?q?=E9=97=B4=E6=8C=87=E4=BB=A4=E6=97=A0=E5=8F=8D=E5=BA=94=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/TcpService.cs | 15 +++++---------- ViewModels/MainWindowViewModel.cs | 6 +++--- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Services/TcpService.cs b/Services/TcpService.cs index 09e227a..101b8dc 100644 --- a/Services/TcpService.cs +++ b/Services/TcpService.cs @@ -38,7 +38,6 @@ namespace OnlineUserPool.Services if (session is { IsConnected: true }) { session.SendAsync(dgram); - dgram = null; } } @@ -69,8 +68,7 @@ namespace OnlineUserPool.Services { try { - var item = this.Sessions.Values.FirstOrDefault(t => - t.Socket.Connected && t.Socket.RemoteEndPoint.ToString() == ip.ToString()); + var item = this.Sessions.Values.FirstOrDefault(t => t.Socket.Connected && t.Socket.RemoteEndPoint.ToString() == ip.ToString()); return item; } catch (Exception e) @@ -122,17 +120,17 @@ namespace OnlineUserPool.Services var data = string.Empty; try { + this._dataBuffer.Clear(); data = Encoding.UTF8.GetString(buffer, (int)offset, (int)size); foreach (var item in data) { this._dataBuffer.Append(item); if (item != '}') continue; - Debug.WriteLine("收到消息:" + this._dataBuffer); var s = this._dataBuffer.ToString(); var msg = JsonConvert.DeserializeObject(s); var ipEndPoint = IPEndPoint.Parse(this.Socket.RemoteEndPoint.ToString()); switch (msg.CommandType) - { + { case 1: _action(ipEndPoint, JsonConvert.DeserializeObject(s)); break; @@ -170,23 +168,20 @@ namespace OnlineUserPool.Services _action(ipEndPoint, msg); break; } + this._dataBuffer.Clear(); - s = null; } } catch (Exception ex) { Log.Error($"接受到的数据处理时出错:{data}\r\n{ex.Message}\r\n{ex.StackTrace}"); } - - buffer = null; - data = null; } protected override void OnError(SocketError error) { //base.OnError(error); - Debug.WriteLine(error); + Log.Error($"OnError:{error}"); } } } diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index 9a20c04..96db667 100644 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -289,7 +289,7 @@ namespace OnlineUserPool.ViewModels int total = 0; var G = $"{total}l[{gameRoom},detail{{{ss}}}]"; var temp = string.Join("|", list.Select(m => m.ToString(2))) + "|"; - var strV21 = $"*l{{{ temp }}};g{{{ G}}};#"; + var strV21 = $"l{{{ temp }}};g{{{ G}}};"; var ddd = new NetworkData(strV21); var needList = Clients.Where(c => c.RoomId == gameRoom.RoomId).ToList(); @@ -316,7 +316,7 @@ namespace OnlineUserPool.ViewModels foreach (var item in needlist) { var G = GameRoomMessageHandler(item); - var strV21 = $"*l{{{ temp }}};g{{{G}}};#"; + var strV21 = $"l{{{ temp }}};g{{{G}}};"; var ddd = new NetworkData(strV21); bool isZip = item.Encoding == "gzip"; item.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, item.IPEndPoint); @@ -369,7 +369,7 @@ namespace OnlineUserPool.ViewModels roomInfo += $"[{r.ToString()},detail{{{ss}}}]"; } - var strV21 = $"*l{{{ temp }}};g{{{roomInfo}}};#"; + var strV21 = $"l{{{ temp }}};g{{{roomInfo}}};"; var ddd = new NetworkData(strV21); bool isZip = needSend.Encoding == "gzip"; needSend.Service.Send(ddd.GetBytes(isZip), ddd.GetBytes(isZip).Length, needSend.IPEndPoint);