Update Network

Update Network
This commit is contained in:
ALEXTANG
2023-05-19 17:01:04 +08:00
parent 4bc63fafc8
commit ec409c7e15
5 changed files with 64 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
using GameBase; using System.Net.Sockets;
using GameBase;
using GameProto; using GameProto;
using TEngine; using TEngine;
using CSPkg = GameProto.CSPkg; using CSPkg = GameProto.CSPkg;
@@ -72,6 +73,33 @@ namespace GameLogic
m_connectWatcher = new ClientConnectWatcher(this); m_connectWatcher = new ClientConnectWatcher(this);
m_dispatcher = new MsgDispatcher(); m_dispatcher = new MsgDispatcher();
m_dispatcher.SetTimeout(5f); m_dispatcher.SetTimeout(5f);
GameEvent.AddEventListener<INetworkChannel,object>(NetworkEvent.NetworkConnectedEvent,OnNetworkConnected);
GameEvent.AddEventListener<INetworkChannel>(NetworkEvent.NetworkClosedEvent,OnNetworkClosed);
GameEvent.AddEventListener<INetworkChannel,NetworkErrorCode,SocketError,string>(NetworkEvent.NetworkErrorEvent,OnNetworkError);
GameEvent.AddEventListener<INetworkChannel,object>(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) public void Connect(string host, int port, bool reconnect = false)
@@ -94,6 +122,8 @@ namespace GameLogic
_lastHost = host; _lastHost = host;
_lastPort = port; _lastPort = port;
Status = reconnect ? GameClientStatus.StatusReconnect : GameClientStatus.StatusInit;
Channel = TEngine.Network.CreateNetworkChannel("GameClient", ServiceType.Tcp, new NetworkChannelHelper()); Channel = TEngine.Network.CreateNetworkChannel("GameClient", ServiceType.Tcp, new NetworkChannelHelper());
Channel.Connect(host, port); Channel.Connect(host, port);
} }
@@ -203,13 +233,13 @@ namespace GameLogic
private bool IsIgnoreLog(uint msgId) private bool IsIgnoreLog(uint msgId)
{ {
bool ignoreLog = false; bool ignoreLog = false;
switch (msgId) /*switch (msgId)
{ {
case (uint)CSMsgID.CsCmdHeatbeatReq: case (uint)CSMsgID.CsCmdHeatbeatReq:
case (uint)CSMsgID.CsCmdHeatbeatRes: case (uint)CSMsgID.CsCmdHeatbeatRes:
ignoreLog = true; ignoreLog = true;
break; break;
} }*/
return ignoreLog; return ignoreLog;
} }

View File

@@ -1,9 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
using System.Reflection;
using GameProto; using GameProto;
using Google.Protobuf;
using TEngine; using TEngine;
namespace GameLogic namespace GameLogic
@@ -88,9 +89,7 @@ namespace GameLogic
_cachedStream.SetLength(_cachedStream.Capacity); // 此行防止 Array.Copy 的数据无法写入 _cachedStream.SetLength(_cachedStream.Capacity); // 此行防止 Array.Copy 的数据无法写入
_cachedStream.Position = 0L; _cachedStream.Position = 0L;
global::ProtobufUtility.ToStreamWithHead(packet,_cachedStream);
global::ProtobufUtility.ToStream(packet,destination);
_cachedStream.WriteTo(destination); _cachedStream.WriteTo(destination);
return true; return true;
} }

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Net.Sockets; using System.Net.Sockets;
using UnityEngine; using UnityEngine;
@@ -10,7 +11,7 @@ namespace TEngine
[DisallowMultipleComponent] [DisallowMultipleComponent]
public sealed class Network : GameFrameworkModuleBase public sealed class Network : GameFrameworkModuleBase
{ {
private static INetworkManager m_NetworkManager = null; private static NetworkManager m_NetworkManager = null;
/// <summary> /// <summary>
/// 获取网络频道数量。 /// 获取网络频道数量。
@@ -24,7 +25,8 @@ namespace TEngine
{ {
base.Awake(); base.Awake();
m_NetworkManager = GameFrameworkEntry.GetModule<INetworkManager>(); // m_NetworkManager = GameFrameworkEntry.GetModule<INetworkManager>();
m_NetworkManager = new NetworkManager();
if (m_NetworkManager == null) if (m_NetworkManager == null)
{ {
Log.Fatal("Network manager is invalid."); Log.Fatal("Network manager is invalid.");
@@ -38,6 +40,11 @@ namespace TEngine
m_NetworkManager.NetworkCustomError += OnNetworkCustomError; m_NetworkManager.NetworkCustomError += OnNetworkCustomError;
} }
private void Update()
{
m_NetworkManager.Update(GameTime.deltaTime, GameTime.unscaledDeltaTime);
}
/// <summary> /// <summary>
/// 检查是否存在网络频道。 /// 检查是否存在网络频道。
/// </summary> /// </summary>

View File

@@ -462,7 +462,6 @@ namespace TEngine
/// <summary> /// <summary>
/// 向远程主机发送消息包并注册消息回调。 /// 向远程主机发送消息包并注册消息回调。
/// </summary> /// </summary>
/// <typeparam name="T">消息包类型。</typeparam>
/// <param name="packet">要发送的消息包。</param> /// <param name="packet">要发送的消息包。</param>
/// <param name="resHandler">要注册的回调。</param> /// <param name="resHandler">要注册的回调。</param>
/// <param name="needShowWaitUI">是否需要等待UI。</param> /// <param name="needShowWaitUI">是否需要等待UI。</param>

View File

@@ -1,6 +1,8 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Linq;
using GameProto;
using Google.Protobuf; using Google.Protobuf;
@@ -9,6 +11,8 @@ using Google.Protobuf;
/// </summary> /// </summary>
public partial class ProtobufUtility public partial class ProtobufUtility
{ {
private const int BufferHead = 4;
/// <summary> /// <summary>
/// 消息压入内存流。 /// 消息压入内存流。
/// </summary> /// </summary>
@@ -29,6 +33,20 @@ public partial class ProtobufUtility
((IMessage)message).WriteTo(stream); ((IMessage)message).WriteTo(stream);
} }
/// <summary>
/// 消息压入内存流。
/// </summary>
/// <param name="packet"></param>
/// <param name="stream"></param>
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);
}
/// <summary> /// <summary>
/// 比特流解析。 /// 比特流解析。
/// </summary> /// </summary>