GameClient 增加Tcp异步ReciveData

GameClient 增加Tcp异步ReciveData
This commit is contained in:
ALEXTANG
2022-08-08 11:25:08 +08:00
parent 6c56eac2c8
commit 49283f28a8
2 changed files with 57 additions and 6 deletions

View File

@@ -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)
{
@@ -37,11 +38,38 @@ namespace TEngine.Net
gameClient.Status = GameClientStatus.StatusInit;
try
{
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)
{
TLogger.LogError(e.Message);
@@ -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);

View File

@@ -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()