diff --git a/Assets/GameScripts/HotFix/GameLogic/Network/GameClient.cs b/Assets/GameScripts/HotFix/GameLogic/Network/GameClient.cs index 372e1117..344fc76b 100644 --- a/Assets/GameScripts/HotFix/GameLogic/Network/GameClient.cs +++ b/Assets/GameScripts/HotFix/GameLogic/Network/GameClient.cs @@ -1,4 +1,5 @@ -using GameBase; +using System.Net.Sockets; +using GameBase; using GameProto; using TEngine; using CSPkg = GameProto.CSPkg; @@ -72,6 +73,33 @@ namespace GameLogic m_connectWatcher = new ClientConnectWatcher(this); m_dispatcher = new MsgDispatcher(); m_dispatcher.SetTimeout(5f); + GameEvent.AddEventListener(NetworkEvent.NetworkConnectedEvent,OnNetworkConnected); + GameEvent.AddEventListener(NetworkEvent.NetworkClosedEvent,OnNetworkClosed); + GameEvent.AddEventListener(NetworkEvent.NetworkErrorEvent,OnNetworkError); + GameEvent.AddEventListener(NetworkEvent.NetworkCustomErrorEvent,OnNetworkCustomError); + } + + private void OnNetworkConnected(INetworkChannel channel, object userdata) + { + bool isReconnect = (m_status == GameClientStatus.StatusReconnect); + //准备登录 + Status = GameClientStatus.StatusLogin; + // TODO Reconnected + } + + private void OnNetworkClosed(INetworkChannel channel) + { + + } + + private void OnNetworkError(INetworkChannel channel, NetworkErrorCode networkErrorCode, SocketError socketError, string errorMessage) + { + + } + + private void OnNetworkCustomError(INetworkChannel channel, object userData) + { + } public void Connect(string host, int port, bool reconnect = false) @@ -94,6 +122,8 @@ namespace GameLogic _lastHost = host; _lastPort = port; + Status = reconnect ? GameClientStatus.StatusReconnect : GameClientStatus.StatusInit; + Channel = TEngine.Network.CreateNetworkChannel("GameClient", ServiceType.Tcp, new NetworkChannelHelper()); Channel.Connect(host, port); } @@ -203,13 +233,13 @@ namespace GameLogic private bool IsIgnoreLog(uint msgId) { bool ignoreLog = false; - switch (msgId) + /*switch (msgId) { case (uint)CSMsgID.CsCmdHeatbeatReq: case (uint)CSMsgID.CsCmdHeatbeatRes: ignoreLog = true; break; - } + }*/ return ignoreLog; } diff --git a/Assets/GameScripts/HotFix/GameLogic/Network/NetworkChannelHelper.cs b/Assets/GameScripts/HotFix/GameLogic/Network/NetworkChannelHelper.cs index 75d1f353..8bddc4af 100644 --- a/Assets/GameScripts/HotFix/GameLogic/Network/NetworkChannelHelper.cs +++ b/Assets/GameScripts/HotFix/GameLogic/Network/NetworkChannelHelper.cs @@ -1,9 +1,10 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Net.Sockets; -using System.Reflection; using GameProto; +using Google.Protobuf; using TEngine; namespace GameLogic @@ -88,9 +89,7 @@ namespace GameLogic _cachedStream.SetLength(_cachedStream.Capacity); // 此行防止 Array.Copy 的数据无法写入 _cachedStream.Position = 0L; - - global::ProtobufUtility.ToStream(packet,destination); - + global::ProtobufUtility.ToStreamWithHead(packet,_cachedStream); _cachedStream.WriteTo(destination); return true; } diff --git a/Assets/GameScripts/HotFix/GameLogic/Network/NetworkCore/Network.cs b/Assets/GameScripts/HotFix/GameLogic/Network/NetworkCore/Network.cs index b2767580..887cf0d5 100644 --- a/Assets/GameScripts/HotFix/GameLogic/Network/NetworkCore/Network.cs +++ b/Assets/GameScripts/HotFix/GameLogic/Network/NetworkCore/Network.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Net.Sockets; using UnityEngine; @@ -10,7 +11,7 @@ namespace TEngine [DisallowMultipleComponent] public sealed class Network : GameFrameworkModuleBase { - private static INetworkManager m_NetworkManager = null; + private static NetworkManager m_NetworkManager = null; /// /// 获取网络频道数量。 @@ -24,7 +25,8 @@ namespace TEngine { base.Awake(); - m_NetworkManager = GameFrameworkEntry.GetModule(); + // m_NetworkManager = GameFrameworkEntry.GetModule(); + m_NetworkManager = new NetworkManager(); if (m_NetworkManager == null) { Log.Fatal("Network manager is invalid."); @@ -38,6 +40,11 @@ namespace TEngine m_NetworkManager.NetworkCustomError += OnNetworkCustomError; } + private void Update() + { + m_NetworkManager.Update(GameTime.deltaTime, GameTime.unscaledDeltaTime); + } + /// /// 检查是否存在网络频道。 /// diff --git a/Assets/GameScripts/HotFix/GameLogic/Network/NetworkCore/NetworkManager.NetworkChannelBase.cs b/Assets/GameScripts/HotFix/GameLogic/Network/NetworkCore/NetworkManager.NetworkChannelBase.cs index ea9a5937..4509a54b 100644 --- a/Assets/GameScripts/HotFix/GameLogic/Network/NetworkCore/NetworkManager.NetworkChannelBase.cs +++ b/Assets/GameScripts/HotFix/GameLogic/Network/NetworkCore/NetworkManager.NetworkChannelBase.cs @@ -462,7 +462,6 @@ namespace TEngine /// /// 向远程主机发送消息包并注册消息回调。 /// - /// 消息包类型。 /// 要发送的消息包。 /// 要注册的回调。 /// 是否需要等待UI。 diff --git a/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs b/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs index d90653f3..05171715 100644 --- a/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs +++ b/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs @@ -1,6 +1,8 @@ using System; using System.ComponentModel; using System.IO; +using System.Linq; +using GameProto; using Google.Protobuf; @@ -9,6 +11,8 @@ using Google.Protobuf; /// public partial class ProtobufUtility { + private const int BufferHead = 4; + /// /// 消息压入内存流。 /// @@ -28,6 +32,20 @@ public partial class ProtobufUtility { ((IMessage)message).WriteTo(stream); } + + /// + /// 消息压入内存流。 + /// + /// + /// + public static void ToStreamWithHead(CSPkg packet, MemoryStream stream) + { + byte[] data = packet.ToByteArray(); + byte[] head = BitConverter.GetBytes(data.Length); + byte[] ret = head.Concat(data).ToArray(); + stream.Write(ret); + ((MemoryStream)stream).SetLength(BufferHead + data.Length); + } /// /// 比特流解析。