From 49283f28a8a9036ab45e1db0d3d58499e719b051 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Mon, 8 Aug 2022 11:25:08 +0800 Subject: [PATCH] =?UTF-8?q?GameClient=20=E5=A2=9E=E5=8A=A0Tcp=E5=BC=82?= =?UTF-8?q?=E6=AD=A5ReciveData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GameClient 增加Tcp异步ReciveData --- .../Runtime/Net/ClientSocket/TcpConnection.cs | 61 +++++++++++++++++-- Assets/TEngine/Runtime/Net/GameClient.cs | 2 +- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/Assets/TEngine/Runtime/Net/ClientSocket/TcpConnection.cs b/Assets/TEngine/Runtime/Net/ClientSocket/TcpConnection.cs index f3b58403..f65e0a66 100644 --- a/Assets/TEngine/Runtime/Net/ClientSocket/TcpConnection.cs +++ b/Assets/TEngine/Runtime/Net/ClientSocket/TcpConnection.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using System.Net.Sockets; using TEngineProto; @@ -19,7 +20,7 @@ namespace TEngine.Net this.gameClient = gameClient; } - public bool Connect(string host, int port) + public bool Connect(string host, int port ,bool async = false) { if (socket == null) { @@ -38,9 +39,36 @@ namespace TEngine.Net gameClient.Status = GameClientStatus.StatusInit; try { - socket.Connect(host, port); - StartReceive(); - gameClient.Status = GameClientStatus.StatusConnect; + if (async) + { + IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(host), port); + SocketAsyncEventArgs args = new SocketAsyncEventArgs(); + args.RemoteEndPoint = ipPoint; + + args.Completed += (obj, socketError) => + { + if (socketError.SocketError == SocketError.Success) + { + TLogger.LogInfoSuccessd("connect server[{0}:{1}] success!!!", host, port); + SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs(); + receiveArgs.SetBuffer(message.Buffer, 0, message.Buffer.Length); + receiveArgs.Completed += ReceiveCallBackAsync; + this.socket.ReceiveAsync(receiveArgs); + gameClient.Status = GameClientStatus.StatusConnect; + } + else + { + TLogger.LogError("connect server failed" + socketError.SocketError); + } + }; + socket.ConnectAsync(args); + } + else + { + socket.Connect(host, port); + StartReceive(); + gameClient.Status = GameClientStatus.StatusConnect; + } } catch (Exception e) { @@ -49,12 +77,35 @@ namespace TEngine.Net return false; } - TLogger.LogInfoSuccessd("connect server[{0}:{1}] success!!!", host, port); + //TLogger.LogInfoSuccessd("connect server[{0}:{1}] success!!!", host, port); m_Host = host; m_Port = port; return true; } + void ReceiveCallBackAsync(object obj, SocketAsyncEventArgs args) + { + if (args.SocketError == SocketError.Success) + { + message.ReadBuffer(args.BytesTransferred, gameClient.HandleResponse); + + args.SetBuffer(message.StartIndex, message.RemSize); + if (this.socket != null && this.socket.Connected) + { + socket.ReceiveAsync(args); + } + else + { + Close(); + } + } + else + { + TLogger.LogError("socket receive error" + args.SocketError); + Close(); + } + } + void StartReceive() { socket.BeginReceive(message.Buffer, message.StartIndex, message.RemSize, SocketFlags.None, ReceiveCallback, null); diff --git a/Assets/TEngine/Runtime/Net/GameClient.cs b/Assets/TEngine/Runtime/Net/GameClient.cs index 96286c58..6d7e5eb6 100644 --- a/Assets/TEngine/Runtime/Net/GameClient.cs +++ b/Assets/TEngine/Runtime/Net/GameClient.cs @@ -136,7 +136,7 @@ namespace TEngine.Net m_lastPort = port; Status = reconnect ? GameClientStatus.StatusReconnect : GameClientStatus.StatusInit; TLogger.LogInfo("Start connect server {0}:{1} Reconnect:{2}", host, port, reconnect); - return m_connect.Connect(host, port); + return m_connect.Connect(host, port,false); } public void Shutdown()