From d194b86c2d32f749732dfe10d3dc1a6b71825b94 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Mon, 23 May 2022 18:07:25 +0800 Subject: [PATCH] Update UdpConnection Update UdpConnection --- .../Net/ClientSocket/UdpConnection.cs | 89 ++++++++++++++++++- .../src/TEngineCore/Net/GameClient.cs | 5 +- .../src/TEngineCore/Thread/Loom.cs | 2 +- 3 files changed, 88 insertions(+), 8 deletions(-) diff --git a/TEngineHotUpdate/src/TEngineCore/Net/ClientSocket/UdpConnection.cs b/TEngineHotUpdate/src/TEngineCore/Net/ClientSocket/UdpConnection.cs index 31b6862e..b90790a3 100644 --- a/TEngineHotUpdate/src/TEngineCore/Net/ClientSocket/UdpConnection.cs +++ b/TEngineHotUpdate/src/TEngineCore/Net/ClientSocket/UdpConnection.cs @@ -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); + } + } } } diff --git a/TEngineHotUpdate/src/TEngineCore/Net/GameClient.cs b/TEngineHotUpdate/src/TEngineCore/Net/GameClient.cs index 5032e3fc..9ebece84 100644 --- a/TEngineHotUpdate/src/TEngineCore/Net/GameClient.cs +++ b/TEngineHotUpdate/src/TEngineCore/Net/GameClient.cs @@ -219,12 +219,11 @@ namespace TEngineCore.Net } } /// - /// Udp网络消息回调,非主线程 + /// Udp网络消息回调,Loom多线程回调到此处,主线程 /// /// - private void UdpHandleResponse(MainPack pack) + public void UdpHandleResponse(MainPack pack) { - //Debug.Log(pack); List listHandle; if (m_mapCmdHandle.TryGetValue((int)pack.Actioncode, out listHandle)) diff --git a/TEngineHotUpdate/src/TEngineCore/Thread/Loom.cs b/TEngineHotUpdate/src/TEngineCore/Thread/Loom.cs index 5d0ad62c..e073c3eb 100644 --- a/TEngineHotUpdate/src/TEngineCore/Thread/Loom.cs +++ b/TEngineHotUpdate/src/TEngineCore/Thread/Loom.cs @@ -116,7 +116,7 @@ namespace TEngineCore } } - public static Thread RunAsync(string actionName,Action action) + public static Thread RunAsync(Action action) { Initialize(); while (_numThreads >= MaxThreads)