mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
GameClient 增加Tcp异步ReciveData
GameClient 增加Tcp异步ReciveData
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using TEngineProto;
|
using TEngineProto;
|
||||||
|
|
||||||
@@ -19,7 +20,7 @@ namespace TEngine.Net
|
|||||||
this.gameClient = gameClient;
|
this.gameClient = gameClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Connect(string host, int port)
|
public bool Connect(string host, int port ,bool async = false)
|
||||||
{
|
{
|
||||||
if (socket == null)
|
if (socket == null)
|
||||||
{
|
{
|
||||||
@@ -38,9 +39,36 @@ namespace TEngine.Net
|
|||||||
gameClient.Status = GameClientStatus.StatusInit;
|
gameClient.Status = GameClientStatus.StatusInit;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
socket.Connect(host, port);
|
if (async)
|
||||||
StartReceive();
|
{
|
||||||
gameClient.Status = GameClientStatus.StatusConnect;
|
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)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -49,12 +77,35 @@ namespace TEngine.Net
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
TLogger.LogInfoSuccessd("connect server[{0}:{1}] success!!!", host, port);
|
//TLogger.LogInfoSuccessd("connect server[{0}:{1}] success!!!", host, port);
|
||||||
m_Host = host;
|
m_Host = host;
|
||||||
m_Port = port;
|
m_Port = port;
|
||||||
return true;
|
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()
|
void StartReceive()
|
||||||
{
|
{
|
||||||
socket.BeginReceive(message.Buffer, message.StartIndex, message.RemSize, SocketFlags.None, ReceiveCallback, null);
|
socket.BeginReceive(message.Buffer, message.StartIndex, message.RemSize, SocketFlags.None, ReceiveCallback, null);
|
||||||
|
@@ -136,7 +136,7 @@ namespace TEngine.Net
|
|||||||
m_lastPort = port;
|
m_lastPort = port;
|
||||||
Status = reconnect ? GameClientStatus.StatusReconnect : GameClientStatus.StatusInit;
|
Status = reconnect ? GameClientStatus.StatusReconnect : GameClientStatus.StatusInit;
|
||||||
TLogger.LogInfo("Start connect server {0}:{1} Reconnect:{2}", host, port, reconnect);
|
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()
|
public void Shutdown()
|
||||||
|
Reference in New Issue
Block a user