mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
Update UdpConnection
Update UdpConnection
This commit is contained in:
@@ -1,13 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using TEngineProto;
|
||||
|
||||
|
||||
namespace TEngineCore.Net
|
||||
{
|
||||
public enum UdpState
|
||||
{
|
||||
None,
|
||||
Start,
|
||||
Close,
|
||||
}
|
||||
|
||||
public class UdpConnection
|
||||
{
|
||||
private Thread udpThread;
|
||||
private Socket udpClient;
|
||||
private EndPoint _ePoint;
|
||||
private IPEndPoint ipEndPoint;
|
||||
private Byte[] buffer = new Byte[2048];
|
||||
private GameClient client;
|
||||
|
||||
private UdpState udpState = UdpState.None;
|
||||
|
||||
public UdpState State
|
||||
{
|
||||
get
|
||||
{
|
||||
return udpState;
|
||||
}
|
||||
set
|
||||
{
|
||||
udpState = value;
|
||||
}
|
||||
}
|
||||
|
||||
public UdpConnection(GameClient client)
|
||||
{
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public void ConnectUdp(string host,int port)
|
||||
{
|
||||
udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
ipEndPoint = new IPEndPoint(IPAddress.Parse(host), port + 1);
|
||||
_ePoint = ipEndPoint;
|
||||
udpState = UdpState.Start;
|
||||
try
|
||||
{
|
||||
TLogger.LogInfo("start connect udp server[{0}:{1}]...", host, port);
|
||||
udpClient.Connect(_ePoint);
|
||||
}
|
||||
catch
|
||||
{
|
||||
TLogger.LogError("UDP connect failed!...".ToColor("FF0000"));
|
||||
return;
|
||||
}
|
||||
TLogger.LogInfo("start connect udp server[{0}:{1}] successed...".ToColor("10FD00"), host, port);
|
||||
Loom.RunAsync(() =>
|
||||
{
|
||||
udpThread = new Thread(ReceiveMsg);
|
||||
udpThread.Start();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void ReceiveMsg()
|
||||
{
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (client == null || udpState != UdpState.Start)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int len = udpClient.ReceiveFrom(buffer, ref _ePoint);
|
||||
MainPack pack = (MainPack)MainPack.Descriptor.Parser.ParseFrom(buffer, 0, len);
|
||||
Loom.QueueOnMainThread((param) =>
|
||||
{
|
||||
client.UdpHandleResponse(pack);
|
||||
}, null);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
TLogger.LogError(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -219,12 +219,11 @@ namespace TEngineCore.Net
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Udp网络消息回调,非主线程
|
||||
/// Udp网络消息回调,Loom多线程回调到此处,主线程
|
||||
/// </summary>
|
||||
/// <param name="pack"></param>
|
||||
private void UdpHandleResponse(MainPack pack)
|
||||
public void UdpHandleResponse(MainPack pack)
|
||||
{
|
||||
//Debug.Log(pack);
|
||||
List<CsMsgDelegate> listHandle;
|
||||
|
||||
if (m_mapCmdHandle.TryGetValue((int)pack.Actioncode, out listHandle))
|
||||
|
@@ -116,7 +116,7 @@ namespace TEngineCore
|
||||
}
|
||||
}
|
||||
|
||||
public static Thread RunAsync(string actionName,Action action)
|
||||
public static Thread RunAsync(Action action)
|
||||
{
|
||||
Initialize();
|
||||
while (_numThreads >= MaxThreads)
|
||||
|
Reference in New Issue
Block a user