From 2c11d70b688d2a3cbcfde4a54c1df85c2e37438b Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Tue, 25 Jun 2024 13:18:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E8=B1=A1Forward=E5=92=8CBackward?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NoSugarNet.Adapter/BackwardLocalClient.cs | 112 ++++ .../DataHelper/CompressAdapter.cs | 94 ++++ NoSugarNet.Adapter/ForwardLocalListener.cs | 342 ++++++++++++ NoSugarNet.Adapter/LocalMsgQueuePool.cs | 55 ++ NoSugarNet.Adapter/NoSugarNet.Adapter.csproj | 18 + .../NoSugarNet.ClientCore.Standard2.csproj | 3 - .../Manager/AppLocalClient.cs | 42 +- NoSugarNet.ClientCore/Manager/AppLogin.cs | 1 + .../Manager/LocalClient/LocalListener.cs | 522 +++++++++--------- .../Manager/LocalClient/LocalMsgQueuePool.cs | 94 ++-- NoSugarNet.ClientCore/Manager/LogManager.cs | 5 + .../NoSugarNet.ClientCore.csproj | 4 + NoSugarNet.ServerCore/Common/Config.cs | 3 +- .../Manager/LocalClientManager.cs | 27 +- NoSugarNet.ServerCore/Manager/LogManager.cs | 5 + .../ServerLocalClient/ServerLocalClient.cs | 158 +++--- .../Manager/ServerManager.cs | 2 +- .../NoSugarNet.ServerCore.csproj | 2 +- .../PublishProfiles/FolderProfile.pubxml.user | 2 +- NoSugarNet.sln | 12 +- .../PublishProfiles/FolderProfile.pubxml.user | 2 +- 21 files changed, 1071 insertions(+), 434 deletions(-) create mode 100644 NoSugarNet.Adapter/BackwardLocalClient.cs create mode 100644 NoSugarNet.Adapter/DataHelper/CompressAdapter.cs create mode 100644 NoSugarNet.Adapter/ForwardLocalListener.cs create mode 100644 NoSugarNet.Adapter/LocalMsgQueuePool.cs create mode 100644 NoSugarNet.Adapter/NoSugarNet.Adapter.csproj diff --git a/NoSugarNet.Adapter/BackwardLocalClient.cs b/NoSugarNet.Adapter/BackwardLocalClient.cs new file mode 100644 index 0000000..fa995b5 --- /dev/null +++ b/NoSugarNet.Adapter/BackwardLocalClient.cs @@ -0,0 +1,112 @@ +using HaoYueNet.ClientNetwork.OtherMode; + +namespace NoSugarNet.Adapter +{ + /// + /// 继承网络库,以支持网络功能 + /// + public class BackwardLocalClient : NetworkHelperCore_SourceMode + { + public long mUID; + public byte mTunnelID; + public byte mIdx; + public long mReciveAllLenght; + public long mSendAllLenght; + + public delegate void OnBackwardLogOutHandler(int LogLevel, string Msg); + public delegate void OnBackwardConnectHandler(long uid, byte tunnelId, byte Idx, BackwardLocalClient serverLocalClient); + public delegate void OnBackwardDisconnectHandler(long uid, byte tunnelId, byte Idx, BackwardLocalClient serverLocalClient); + public delegate void OnBackwardDataCallBackHandler(long uid, byte tunnelId, byte Idx, byte[] data); + + public event OnBackwardLogOutHandler OnBackwardLogOut; + public event OnBackwardConnectHandler OnBackwardConnect; + public event OnBackwardDisconnectHandler OnBackwardDisconnect; + public event OnBackwardDataCallBackHandler OnBackwardDataCallBack; + + public BackwardLocalClient(long UID,byte TunnelID, byte Idx) + { + mUID = UID; + mTunnelID = TunnelID; + mIdx = Idx; + //指定接收服务器数据事件 + OnReceiveData += GetDataCallBack; + //断开连接 + OnClose += OnConnectClose; + OnConnected += NetworkConnected; + //网络库调试信息输出事件,用于打印网络内容 + OnLogOut += NetworkDeBugLog; + } + + public void BandEvent( + OnBackwardLogOutHandler _OnBackwardLogOut, + OnBackwardConnectHandler _OnConnect, + OnBackwardDisconnectHandler _OnDisconnect, + OnBackwardDataCallBackHandler _OnDataCallBack + ) + { + OnBackwardLogOut += _OnBackwardLogOut; + OnBackwardConnect += _OnConnect; + OnBackwardDisconnect += _OnDisconnect; + OnBackwardDataCallBack += _OnDataCallBack; + } + + public void NetworkConnected(bool IsConnect) + { + NetworkDeBugLog($"NetworkConnected:{IsConnect}"); + if (IsConnect) + { + OnBackwardConnect?.Invoke(mUID, mTunnelID, mIdx, this); + } + else + { + //连接失败 + NetworkDeBugLog("连接失败!"); + } + } + + public void NetworkDeBugLog(string str) + { + OnBackwardLogOut?.Invoke(1 ,"NetCoreDebug >> " + str); + } + + /// + /// 接受包回调 + /// + /// 协议ID + /// 错误编号 + /// 业务数据 + public void GetDataCallBack(byte[] data) + { + //NetworkDeBugLog("收到消息 数据长度=>" + data.Length); + try + { + //记录接收数据长度 + mReciveAllLenght += data.Length; + //抛出网络数据 + OnBackwardDataCallBack?.Invoke(mUID, mTunnelID, mIdx, data); + } + catch (Exception ex) + { + NetworkDeBugLog("逻辑处理错误:" + ex.ToString()); + } + + } + + /// + /// 关闭连接 + /// + void OnConnectClose() + { + NetworkDeBugLog("OnConnectClose"); + OnBackwardDisconnect?.Invoke(mUID, mTunnelID,mIdx,this); + } + + public void Release() + { + OnBackwardLogOut -= OnBackwardLogOut; + OnBackwardConnect -= OnBackwardConnect; + OnBackwardDisconnect -= OnBackwardDisconnect; + OnBackwardDataCallBack -= OnBackwardDataCallBack; + } + } +} diff --git a/NoSugarNet.Adapter/DataHelper/CompressAdapter.cs b/NoSugarNet.Adapter/DataHelper/CompressAdapter.cs new file mode 100644 index 0000000..2bce95e --- /dev/null +++ b/NoSugarNet.Adapter/DataHelper/CompressAdapter.cs @@ -0,0 +1,94 @@ +using System.IO; +using System.IO.Compression; + +namespace NoSugarNet.Adapter.DataHelper +{ + public enum E_CompressAdapter + { + //不压缩 + None = 0, + //GIPZ + GZIP_Plan1 = 1, + } + + /// + /// 压缩适配器 + /// + public class CompressAdapter + { + IDataCompress mIDataCompress; + public CompressAdapter(E_CompressAdapter type) + { + switch (type) + { + //不压缩 + case E_CompressAdapter.None: + mIDataCompress = new NoCompress(); + break; + //GZIP Plan1 + case E_CompressAdapter.GZIP_Plan1: + mIDataCompress = new GZipCompress(); + break; + //TODO 其他压缩对比 + //…… + default: + mIDataCompress = new NoCompress(); + break; + } + } + + public byte[] Compress(byte[] data) + { + return mIDataCompress.Compress(data); + } + public byte[] Decompress(byte[] data) + { + return mIDataCompress.Decompress(data); + } + } + + + public interface IDataCompress + { + byte[] Compress(byte[] data); + byte[] Decompress(byte[] data); + } + + public class NoCompress : IDataCompress + { + public byte[] Compress(byte[] data) + { + return data; + } + public byte[] Decompress(byte[] data) + { + return data; + } + } + + public class GZipCompress : IDataCompress + { + public byte[] Compress(byte[] data) + { + using (var compressedStream = new MemoryStream()) + using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress)) + { + zipStream.Write(data, 0, data.Length); + zipStream.Close(); + return compressedStream.ToArray(); + } + } + + public byte[] Decompress(byte[] data) + { + using (var compressedStream = new MemoryStream(data)) + using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress)) + using (var resultStream = new MemoryStream()) + { + zipStream.CopyTo(resultStream); + return resultStream.ToArray(); + } + } + } + +} diff --git a/NoSugarNet.Adapter/ForwardLocalListener.cs b/NoSugarNet.Adapter/ForwardLocalListener.cs new file mode 100644 index 0000000..521cef1 --- /dev/null +++ b/NoSugarNet.Adapter/ForwardLocalListener.cs @@ -0,0 +1,342 @@ +using HaoYueNet.ServerNetwork; +using System.Net; +using System.Net.Sockets; + +namespace NoSugarNet.Adapter +{ + public class ForwardLocalListener : TcpSaeaServer_SourceMode + { + public byte mTunnelID; + public long mReciveAllLenght; + public long mSendAllLenght; + public long currSeed; + static long Seed; + + public enum AdptLogLevel + { + Debug, + Info, + Warning, + Error + } + + public delegate void OnLogOutHandler(int LogLevel,string Msg); + public delegate void OnClientLocalConnectHandler(byte tunnelId, byte _Idx); + public delegate void OnClientLocalDisconnectHandler(byte tunnelId, byte _Idx); + public delegate void OnClientTunnelDataCallBackHandler(byte tunnelId, byte Idx, byte[] data); + + public event OnLogOutHandler OnForwardLogOut; + public event OnClientLocalConnectHandler OnClientLocalConnect; + public event OnClientLocalDisconnectHandler OnClientLocalDisconnect; + public event OnClientTunnelDataCallBackHandler OnClientTunnelDataCallBack; + + public ForwardLocalListener(int numConnections, int receiveBufferSize, byte TunnelID) + : base(numConnections, receiveBufferSize) + { + OnClientNumberChange += ClientNumberChange; + OnReceive += ReceiveData; + OnDisconnected += OnDisconnect; + OnNetLog += OnShowNetLog; + + mTunnelID = TunnelID; + + currSeed = Seed++; + } + + + + public event OnLogOutHandler OnForwardLogOut2; + + public void BandEvent( + OnLogOutHandler _OnLogOut, + OnClientLocalConnectHandler _OnClientLocalConnect, + OnClientLocalDisconnectHandler _OnClientLocalDisconnect, + OnClientTunnelDataCallBackHandler _ClientTunnelDataCall + ) + { + OnForwardLogOut += _OnLogOut; + OnClientLocalConnect += _OnClientLocalConnect; + OnClientLocalDisconnect += _OnClientLocalDisconnect; + OnClientTunnelDataCallBack += _ClientTunnelDataCall; + } + + public void StartListener(uint port) + { + Init(); + Start(new IPEndPoint(IPAddress.Any.Address, (int)port)); + } + + private void ClientNumberChange(int num, AsyncUserToken token) + { + OnForwardLogOut?.Invoke((int)AdptLogLevel.Info, "Client数发生变化"); + //增加连接数stsc + if (num > 0) + { + int Idx = AddDictSocket(token.Socket); + if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInf)) + { + OnClientLocalConnect?.Invoke(mTunnelID, (byte)Idx); + } + } + } + + /// + /// 通过下标发送 + /// + /// + /// + public void SendSocketByIdx(int Idx, byte[] data) + { + if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) + { + mSendAllLenght += data.Length; + SendToSocket(_localClientInfo._socket, data); + } + //TODO连接前缓存数据 + } + + /// + /// 接受包回调 + /// + /// 协议ID + /// 错误编号 + /// 业务数据 + private void ReceiveData(AsyncUserToken token, byte[] data) + { + DataCallBack(token.Socket, data); + } + + public void DataCallBack(Socket sk, byte[] data) + { + //AppNoSugarNet.log.Info("收到消息 数据长度=>" + data.Length); + //记录接受长度 + mReciveAllLenght += data.Length; + if (!GetSocketIdxBySocket(sk, out int Idx)) + return; + try + { + //抛出网络数据 + OnClientTunnelDataCallBack?.Invoke(mTunnelID, (byte)Idx, data); + } + catch (Exception ex) + { + OnForwardLogOut?.Invoke((int)AdptLogLevel.Error,"逻辑处理错误:" + ex.ToString()); + } + } + + public void CloseConnectByIdx(byte Idx) + { + if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) + { + //把未发送消息队列回收了 + while (_localClientInfo.msgQueue.Count > 0) + { + IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue(); + LocalMsgQueuePool._localMsgPool.Enqueue(msg); + } + + _localClientInfo._socket.Shutdown(SocketShutdown.Both); + } + } + + /// + /// 断开连接 + /// + /// + public void OnDisconnect(AsyncUserToken token) + { + OnForwardLogOut?.Invoke((int)AdptLogLevel.Info,"断开连接"); + + if (!GetSocketIdxBySocket(token.Socket, out int Idx)) + return; + + OnClientLocalDisconnect?.Invoke(mTunnelID, (byte)Idx); + RemoveDictSocket(token.Socket); + } + + public void OnShowNetLog(string msg) + { + OnForwardLogOut?.Invoke((int)AdptLogLevel.Info, msg); + } + + #region 一个轻量级无用户连接管理 + Dictionary DictSocketHandle2Idx = new Dictionary(); + Dictionary DictIdx2LocalClientInfo = new Dictionary(); + int mSeedIdx = 0; + List FreeIdxs = new List(); + public class LocalClientInfo + { + public Socket _socket; + public bool bRemoteConnect; + public bool bLocalConnect => _socket.Connected; + public Queue msgQueue = new Queue(); + } + + public Dictionary GetDictIdx2LocalClientInfo() + { + return DictIdx2LocalClientInfo; + } + + int GetNextIdx() + { + if (FreeIdxs.Count > 0) + { + int Idx = FreeIdxs[0]; + FreeIdxs.RemoveAt(0); + return Idx; + } + return mSeedIdx++; + } + + void ResetFree() + { + FreeIdxs.Clear(); + mSeedIdx = 0; + } + + /// + /// 追加Socket返回下标 + /// + /// + /// + public int AddDictSocket(Socket socket) + { + if (socket == null) + return -1; + lock (DictSocketHandle2Idx) + { + int Idx = GetNextIdx(); + DictSocketHandle2Idx[socket.Handle] = Idx; + DictIdx2LocalClientInfo[Idx] = new LocalClientInfo() { _socket = socket,bRemoteConnect = false}; + OnForwardLogOut?.Invoke((int)AdptLogLevel.Debug, $"AddDictSocket mTunnelID->{mTunnelID} Idx->{Idx} socket.Handle{socket.Handle}"); + return Idx; + } + } + + public void RemoveDictSocket(Socket socket) + { + if (socket == null) + return; + lock (DictSocketHandle2Idx) + { + if (!DictSocketHandle2Idx.ContainsKey(socket.Handle)) + return; + int Idx = DictSocketHandle2Idx[socket.Handle]; + FreeIdxs.Add(Idx); + if (DictIdx2LocalClientInfo.ContainsKey(Idx)) + DictIdx2LocalClientInfo.Remove(Idx); + DictSocketHandle2Idx.Remove(socket.Handle); + OnForwardLogOut?.Invoke((int)AdptLogLevel.Debug, $"RemoveDictSocket mTunnelID->{mTunnelID} Idx->{Idx} socket.Handle{socket.Handle}"); + } + } + + bool GetSocketByIdx(int Idx, out LocalClientInfo _localClientInfo) + { + if (!DictIdx2LocalClientInfo.ContainsKey(Idx)) + { + _localClientInfo = null; + return false; + } + + _localClientInfo = DictIdx2LocalClientInfo[Idx]; + return true; + } + + public bool GetSocketIdxBySocket(Socket _socket, out int Idx) + { + if (_socket == null) + { + Idx = -1; + return false; + } + + if (!DictSocketHandle2Idx.ContainsKey(_socket.Handle)) + { + Idx = -1; + return false; + } + + Idx = DictSocketHandle2Idx[_socket.Handle]; + return true; + } + + public bool CheckRemoteConnect(int Idx) + { + if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) + return false; + return _localClientInfo.bRemoteConnect; + } + + public void SetRemoteConnectd(int Idx,bool bConnected) + { + if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) + return; + if (bConnected) + OnForwardLogOut?.Invoke((int)AdptLogLevel.Info,"远端本地连接已连接!!!!"); + else + OnForwardLogOut?.Invoke((int)AdptLogLevel.Info, "远端本地连接已断开连接!!!!"); + _localClientInfo.bRemoteConnect = bConnected; + } + + public void StopAllLocalClient() + { + lock (DictIdx2LocalClientInfo) + { + int[] Idxs = DictIdx2LocalClientInfo.Keys.ToArray(); + for (int i = 0; i < Idxs.Length; i++) + { + CloseConnectByIdx((byte)Idxs[i]); + } + DictIdx2LocalClientInfo.Clear(); + DictSocketHandle2Idx.Clear(); + ResetFree(); + } + } + + public void StopWithClear() + { + base.Stop(); + //清理事件 + OnForwardLogOut -= OnForwardLogOut; + OnClientLocalConnect -= OnClientLocalConnect; + OnClientLocalDisconnect -= OnClientLocalDisconnect; + OnClientTunnelDataCallBack -= OnClientTunnelDataCallBack; + } + + #endregion + + + #region 缓存 + public void EnqueueIdxWithMsg(byte Idx, byte[] data) + { + if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) + return; + + IdxWithMsg Msg = LocalMsgQueuePool._localMsgPool.Dequeue(); + Msg.Idx = Idx; + Msg.data = data; + _localClientInfo.msgQueue.Enqueue(Msg); + } + public bool GetDictMsgQueue(byte Idx,out List MsgList) + { + if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo) || _localClientInfo.msgQueue.Count < 1) + { + MsgList = null; + return false; + } + + MsgList = new List(); + lock (_localClientInfo.msgQueue) + { + while (_localClientInfo.msgQueue.Count > 0) + { + IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue(); + MsgList.Add(msg); + } + return true; + } + } + #endregion + } + +} diff --git a/NoSugarNet.Adapter/LocalMsgQueuePool.cs b/NoSugarNet.Adapter/LocalMsgQueuePool.cs new file mode 100644 index 0000000..7de0cf3 --- /dev/null +++ b/NoSugarNet.Adapter/LocalMsgQueuePool.cs @@ -0,0 +1,55 @@ +namespace NoSugarNet.Adapter +{ + public class IdxWithMsg + { + public byte Idx; + public byte[] data; + } + + public class LocalMsgQueuePool + { + public static LocalMsgQueuePool _localMsgPool = new LocalMsgQueuePool(1000); + + Queue msg_pool; + + public LocalMsgQueuePool(int capacity) + { + msg_pool = new Queue(capacity); + } + + /// + /// 向 Queue 的末尾添加一个对象。 + /// + /// + public void Enqueue(IdxWithMsg item) + { + lock (msg_pool) + { + item.Idx = 0; + item.data = null; + msg_pool.Enqueue(item); + } + } + + //移除并返回在 Queue 的开头的对象。 + public IdxWithMsg Dequeue() + { + lock (msg_pool) + { + if(msg_pool.Count > 0) + return msg_pool.Dequeue(); + return new IdxWithMsg(); + } + } + + public int Count + { + get { return msg_pool.Count; } + } + + public void Clear() + { + msg_pool.Clear(); + } + } +} diff --git a/NoSugarNet.Adapter/NoSugarNet.Adapter.csproj b/NoSugarNet.Adapter/NoSugarNet.Adapter.csproj new file mode 100644 index 0000000..bb4967f --- /dev/null +++ b/NoSugarNet.Adapter/NoSugarNet.Adapter.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + + + + + ..\Lib\HaoYueNet.ClientNetwork.dll + + + ..\Lib\HaoYueNet.ServerNetwork.dll + + + + diff --git a/NoSugarNet.ClientCore.Standard2/NoSugarNet.ClientCore.Standard2.csproj b/NoSugarNet.ClientCore.Standard2/NoSugarNet.ClientCore.Standard2.csproj index 46ead48..59561e8 100644 --- a/NoSugarNet.ClientCore.Standard2/NoSugarNet.ClientCore.Standard2.csproj +++ b/NoSugarNet.ClientCore.Standard2/NoSugarNet.ClientCore.Standard2.csproj @@ -19,8 +19,5 @@ ..\Lib\NetLib_Standard2\HaoYueNet.ServerNetwork.Standard2.dll - - ..\Lib\NetLib_Standard2\NoSugarNet.DataHelper.dll - \ No newline at end of file diff --git a/NoSugarNet.ClientCore/Manager/AppLocalClient.cs b/NoSugarNet.ClientCore/Manager/AppLocalClient.cs index e319675..2434df5 100644 --- a/NoSugarNet.ClientCore/Manager/AppLocalClient.cs +++ b/NoSugarNet.ClientCore/Manager/AppLocalClient.cs @@ -1,6 +1,8 @@ using AxibugProtobuf; using Google.Protobuf; using HaoYueNet.ServerNetwork; +using NoSugarNet.Adapter; +using NoSugarNet.Adapter.DataHelper; using NoSugarNet.ClientCore; using NoSugarNet.ClientCore.Common; using NoSugarNet.ClientCore.Network; @@ -12,10 +14,10 @@ namespace ServerCore.Manager public class AppLocalClient { Dictionary mDictTunnelID2Cfg = new Dictionary(); - Dictionary mDictTunnelID2Listeners = new Dictionary(); - CompressAdapter mCompressAdapter; - E_CompressAdapter compressAdapterType; - public LocalMsgQueuePool _localMsgPool = new LocalMsgQueuePool(1000); + Dictionary mDictTunnelID2Listeners = new Dictionary(); + NoSugarNet.Adapter.DataHelper.CompressAdapter mCompressAdapter; + NoSugarNet.Adapter.DataHelper.E_CompressAdapter compressAdapterType; + //public LocalMsgQueuePool _localMsgPool = new LocalMsgQueuePool(1000); public long tReciveAllLenght { get; private set; } public long tSendAllLenght { get; private set; } @@ -74,13 +76,15 @@ namespace ServerCore.Manager { AppNoSugarNet.log.Info("初始化压缩适配器" + compressAdapterType); //初始化压缩适配器,代表压缩类型 - mCompressAdapter = new CompressAdapter(compressAdapterType); + mCompressAdapter = new NoSugarNet.Adapter.DataHelper.CompressAdapter(compressAdapterType); foreach (var cfg in mDictTunnelID2Cfg) { - LocalListener listener = new LocalListener(256, 1024, cfg.Key); + ForwardLocalListener listener = new ForwardLocalListener(256, 1024, cfg.Key); AppNoSugarNet.log.Info($"开始监听配置 Tunnel:{cfg.Key},Port:{cfg.Value.Port}"); - listener.Init(); - listener.Start(new IPEndPoint(IPAddress.Any.Address, (int)cfg.Value.Port)); + listener.BandEvent(AppNoSugarNet.log.Log, OnClientLocalConnect, OnClientLocalDisconnect, OnClientTunnelDataCallBack); + listener.StartListener((uint)cfg.Value.Port); + //listener.Init(); + //listener.Start(new IPEndPoint(IPAddress.Any.Address, (int)cfg.Value.Port)); //listener.Init((int)cfg.Value.Port); AddLocalListener(listener); } @@ -93,7 +97,7 @@ namespace ServerCore.Manager /// /// /// - void AddLocalListener(LocalListener _listener) + void AddLocalListener(ForwardLocalListener _listener) { lock (mDictTunnelID2Listeners) { @@ -105,7 +109,7 @@ namespace ServerCore.Manager /// /// /// - void RemoveLocalListener(LocalListener _listener) + void RemoveLocalListener(ForwardLocalListener _listener) { lock (mDictTunnelID2Listeners) { @@ -115,7 +119,7 @@ namespace ServerCore.Manager } } } - bool GetLocalListener(byte tunnelId,out LocalListener _listener) + bool GetLocalListener(byte tunnelId,out ForwardLocalListener _listener) { _listener = null; if (!mDictTunnelID2Listeners.ContainsKey(tunnelId)) @@ -131,9 +135,9 @@ namespace ServerCore.Manager byte[] keys = mDictTunnelID2Listeners.Keys.ToArray(); for (int i = 0; i < keys.Length; i++) { - LocalListener _listener = mDictTunnelID2Listeners[keys[i]]; + ForwardLocalListener _listener = mDictTunnelID2Listeners[keys[i]]; _listener.StopAllLocalClient(); - _listener.Stop(); + _listener.StopWithClear(); //_listener.Stop(); RemoveLocalListener(_listener); } @@ -153,7 +157,7 @@ namespace ServerCore.Manager Protobuf_Cfgs_Single cfg = msg.Cfgs[i]; mDictTunnelID2Cfg[(byte)cfg.TunnelID] = cfg; } - compressAdapterType = (E_CompressAdapter)msg.CompressAdapterType; + compressAdapterType = (NoSugarNet.Adapter.DataHelper.E_CompressAdapter)msg.CompressAdapterType; InitListenerMode(); } public void Recive_TunnelS2CConnect(byte[] reqData) @@ -229,7 +233,7 @@ namespace ServerCore.Manager public void OnServerLocalConnect(byte tunnelId,byte Idx) { AppNoSugarNet.log.Debug($"OnServerLocalConnect {tunnelId},{Idx}"); - if (!GetLocalListener(tunnelId, out LocalListener _listener)) + if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener)) return; //维护状态 _listener.SetRemoteConnectd(Idx, true); @@ -241,7 +245,7 @@ namespace ServerCore.Manager //投递给服务端,来自客户端本地的连接数据 AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SData, msg.data); //发送后回收 - AppNoSugarNet.local._localMsgPool.Enqueue(msg); + LocalMsgQueuePool._localMsgPool.Enqueue(msg); } } } @@ -253,7 +257,7 @@ namespace ServerCore.Manager public void OnServerLocalDisconnect(byte tunnelId, byte Idx) { AppNoSugarNet.log.Debug($"OnServerLocalDisconnect {tunnelId},{Idx}"); - if (!GetLocalListener(tunnelId, out LocalListener _listener)) + if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener)) return; _listener.SetRemoteConnectd(Idx,false); _listener.CloseConnectByIdx(Idx); @@ -270,7 +274,7 @@ namespace ServerCore.Manager public void OnServerLocalDataCallBack(byte tunnelId,byte Idx, byte[] data) { //AppNoSugarNet.log.Info($"OnServerLocalDataCallBack {tunnelId},{Idx},Data长度:{data.Length}"); - if (!GetLocalListener(tunnelId, out LocalListener _listener)) + if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener)) return; //记录压缩前数据长度 tReciveAllLenght += data.Length; @@ -329,7 +333,7 @@ namespace ServerCore.Manager HunterNetCoreData = ByteString.CopyFrom(data) }); - if (!GetLocalListener(tunnelId, out LocalListener _listener)) + if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener)) return; //远程未连接,添加到缓存 diff --git a/NoSugarNet.ClientCore/Manager/AppLogin.cs b/NoSugarNet.ClientCore/Manager/AppLogin.cs index d1ad4de..20f3768 100644 --- a/NoSugarNet.ClientCore/Manager/AppLogin.cs +++ b/NoSugarNet.ClientCore/Manager/AppLogin.cs @@ -14,6 +14,7 @@ namespace NoSugarNet.ClientCore.Manager public void Login() { + AppNoSugarNet.log.Debug("-->Login"); if(string.IsNullOrEmpty(LastLoginGuid)) LastLoginGuid = Guid.NewGuid().ToString(); diff --git a/NoSugarNet.ClientCore/Manager/LocalClient/LocalListener.cs b/NoSugarNet.ClientCore/Manager/LocalClient/LocalListener.cs index 247f6a7..d65ce85 100644 --- a/NoSugarNet.ClientCore/Manager/LocalClient/LocalListener.cs +++ b/NoSugarNet.ClientCore/Manager/LocalClient/LocalListener.cs @@ -1,295 +1,295 @@ -using HaoYueNet.ServerNetwork; -using System.Net.Sockets; +//using HaoYueNet.ServerNetwork; +//using System.Net.Sockets; -namespace NoSugarNet.ClientCore -{ - public class LocalListener : TcpSaeaServer_SourceMode - { - public byte mTunnelID; - public long mReciveAllLenght; - public long mSendAllLenght; - public long currSeed; - static long Seed; +//namespace NoSugarNet.ClientCore +//{ +// public class LocalListener : TcpSaeaServer_SourceMode +// { +// public byte mTunnelID; +// public long mReciveAllLenght; +// public long mSendAllLenght; +// public long currSeed; +// static long Seed; - public LocalListener(int numConnections, int receiveBufferSize, byte TunnelID) - : base(numConnections, receiveBufferSize) - { - OnClientNumberChange += ClientNumberChange; - OnReceive += ReceiveData; - OnDisconnected += OnDisconnect; - OnNetLog += OnShowNetLog; +// public LocalListener(int numConnections, int receiveBufferSize, byte TunnelID) +// : base(numConnections, receiveBufferSize) +// { +// OnClientNumberChange += ClientNumberChange; +// OnReceive += ReceiveData; +// OnDisconnected += OnDisconnect; +// OnNetLog += OnShowNetLog; - mTunnelID = TunnelID; +// mTunnelID = TunnelID; - currSeed = Seed++; - } +// currSeed = Seed++; +// } - private void ClientNumberChange(int num, AsyncUserToken token) - { - AppNoSugarNet.log.Info("Client数发生变化"); - //增加连接数stsc - if (num > 0) - { - int Idx = AddDictSocket(token.Socket); - if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInf)) - { - AppNoSugarNet.local.OnClientLocalConnect(mTunnelID, (byte)Idx); - } - } - } +// private void ClientNumberChange(int num, AsyncUserToken token) +// { +// AppNoSugarNet.log.Info("Client数发生变化"); +// //增加连接数stsc +// if (num > 0) +// { +// int Idx = AddDictSocket(token.Socket); +// if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInf)) +// { +// AppNoSugarNet.local.OnClientLocalConnect(mTunnelID, (byte)Idx); +// } +// } +// } - /// - /// 通过下标发送 - /// - /// - /// - public void SendSocketByIdx(int Idx, byte[] data) - { - if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) - { - mSendAllLenght += data.Length; - SendToSocket(_localClientInfo._socket, data); - } - //TODO连接前缓存数据 - } +// /// +// /// 通过下标发送 +// /// +// /// +// /// +// public void SendSocketByIdx(int Idx, byte[] data) +// { +// if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) +// { +// mSendAllLenght += data.Length; +// SendToSocket(_localClientInfo._socket, data); +// } +// //TODO连接前缓存数据 +// } - /// - /// 接受包回调 - /// - /// 协议ID - /// 错误编号 - /// 业务数据 - private void ReceiveData(AsyncUserToken token, byte[] data) - { - DataCallBack(token.Socket, data); - } +// /// +// /// 接受包回调 +// /// +// /// 协议ID +// /// 错误编号 +// /// 业务数据 +// private void ReceiveData(AsyncUserToken token, byte[] data) +// { +// DataCallBack(token.Socket, data); +// } - public void DataCallBack(Socket sk, byte[] data) - { - //AppNoSugarNet.log.Info("收到消息 数据长度=>" + data.Length); - //记录接受长度 - mReciveAllLenght += data.Length; - if (!GetSocketIdxBySocket(sk, out int Idx)) - return; - try - { - //抛出网络数据 - AppNoSugarNet.local.OnClientTunnelDataCallBack(mTunnelID, (byte)Idx, data); - } - catch (Exception ex) - { - AppNoSugarNet.log.Info("逻辑处理错误:" + ex.ToString()); - } - } +// public void DataCallBack(Socket sk, byte[] data) +// { +// //AppNoSugarNet.log.Info("收到消息 数据长度=>" + data.Length); +// //记录接受长度 +// mReciveAllLenght += data.Length; +// if (!GetSocketIdxBySocket(sk, out int Idx)) +// return; +// try +// { +// //抛出网络数据 +// AppNoSugarNet.local.OnClientTunnelDataCallBack(mTunnelID, (byte)Idx, data); +// } +// catch (Exception ex) +// { +// AppNoSugarNet.log.Info("逻辑处理错误:" + ex.ToString()); +// } +// } - public void CloseConnectByIdx(byte Idx) - { - if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) - { - //把未发送消息队列回收了 - while (_localClientInfo.msgQueue.Count > 0) - { - IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue(); - AppNoSugarNet.local._localMsgPool.Enqueue(msg); - } +// public void CloseConnectByIdx(byte Idx) +// { +// if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) +// { +// //把未发送消息队列回收了 +// while (_localClientInfo.msgQueue.Count > 0) +// { +// IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue(); +// AppNoSugarNet.local._localMsgPool.Enqueue(msg); +// } - _localClientInfo._socket.Shutdown(SocketShutdown.Both); - } - } +// _localClientInfo._socket.Shutdown(SocketShutdown.Both); +// } +// } - /// - /// 断开连接 - /// - /// - public void OnDisconnect(AsyncUserToken token) - { - AppNoSugarNet.log.Info("断开连接"); +// /// +// /// 断开连接 +// /// +// /// +// public void OnDisconnect(AsyncUserToken token) +// { +// AppNoSugarNet.log.Info("断开连接"); - if (!GetSocketIdxBySocket(token.Socket, out int Idx)) - return; +// if (!GetSocketIdxBySocket(token.Socket, out int Idx)) +// return; - AppNoSugarNet.local.OnClientLocalDisconnect(mTunnelID, (byte)Idx); - RemoveDictSocket(token.Socket); - } +// AppNoSugarNet.local.OnClientLocalDisconnect(mTunnelID, (byte)Idx); +// RemoveDictSocket(token.Socket); +// } - public void OnShowNetLog(string msg) - { - AppNoSugarNet.log.Info(msg); - } +// public void OnShowNetLog(string msg) +// { +// AppNoSugarNet.log.Info(msg); +// } - #region 一个轻量级无用户连接管理 - Dictionary DictSocketHandle2Idx = new Dictionary(); - Dictionary DictIdx2LocalClientInfo = new Dictionary(); - int mSeedIdx = 0; - List FreeIdxs = new List(); - public class LocalClientInfo - { - public Socket _socket; - public bool bRemoteConnect; - public bool bLocalConnect => _socket.Connected; - public Queue msgQueue = new Queue(); - } +// #region 一个轻量级无用户连接管理 +// Dictionary DictSocketHandle2Idx = new Dictionary(); +// Dictionary DictIdx2LocalClientInfo = new Dictionary(); +// int mSeedIdx = 0; +// List FreeIdxs = new List(); +// public class LocalClientInfo +// { +// public Socket _socket; +// public bool bRemoteConnect; +// public bool bLocalConnect => _socket.Connected; +// public Queue msgQueue = new Queue(); +// } - public Dictionary GetDictIdx2LocalClientInfo() - { - return DictIdx2LocalClientInfo; - } +// public Dictionary GetDictIdx2LocalClientInfo() +// { +// return DictIdx2LocalClientInfo; +// } - int GetNextIdx() - { - if (FreeIdxs.Count > 0) - { - int Idx = FreeIdxs[0]; - FreeIdxs.RemoveAt(0); - return Idx; - } - return mSeedIdx++; - } +// int GetNextIdx() +// { +// if (FreeIdxs.Count > 0) +// { +// int Idx = FreeIdxs[0]; +// FreeIdxs.RemoveAt(0); +// return Idx; +// } +// return mSeedIdx++; +// } - void ResetFree() - { - FreeIdxs.Clear(); - mSeedIdx = 0; - } +// void ResetFree() +// { +// FreeIdxs.Clear(); +// mSeedIdx = 0; +// } - /// - /// 追加Socket返回下标 - /// - /// - /// - public int AddDictSocket(Socket socket) - { - if (socket == null) - return -1; - lock (DictSocketHandle2Idx) - { - int Idx = GetNextIdx(); - DictSocketHandle2Idx[socket.Handle] = Idx; - DictIdx2LocalClientInfo[Idx] = new LocalClientInfo() { _socket = socket,bRemoteConnect = false}; - AppNoSugarNet.log.Debug($"AddDictSocket mTunnelID->{mTunnelID} Idx->{Idx} socket.Handle{socket.Handle}"); - return Idx; - } - } +// /// +// /// 追加Socket返回下标 +// /// +// /// +// /// +// public int AddDictSocket(Socket socket) +// { +// if (socket == null) +// return -1; +// lock (DictSocketHandle2Idx) +// { +// int Idx = GetNextIdx(); +// DictSocketHandle2Idx[socket.Handle] = Idx; +// DictIdx2LocalClientInfo[Idx] = new LocalClientInfo() { _socket = socket,bRemoteConnect = false}; +// AppNoSugarNet.log.Debug($"AddDictSocket mTunnelID->{mTunnelID} Idx->{Idx} socket.Handle{socket.Handle}"); +// return Idx; +// } +// } - public void RemoveDictSocket(Socket socket) - { - if (socket == null) - return; - lock (DictSocketHandle2Idx) - { - if (!DictSocketHandle2Idx.ContainsKey(socket.Handle)) - return; - int Idx = DictSocketHandle2Idx[socket.Handle]; - FreeIdxs.Add(Idx); - if (DictIdx2LocalClientInfo.ContainsKey(Idx)) - DictIdx2LocalClientInfo.Remove(Idx); - DictSocketHandle2Idx.Remove(socket.Handle); - AppNoSugarNet.log.Debug($"RemoveDictSocket mTunnelID->{mTunnelID} Idx->{Idx} socket.Handle{socket.Handle}"); - } - } +// public void RemoveDictSocket(Socket socket) +// { +// if (socket == null) +// return; +// lock (DictSocketHandle2Idx) +// { +// if (!DictSocketHandle2Idx.ContainsKey(socket.Handle)) +// return; +// int Idx = DictSocketHandle2Idx[socket.Handle]; +// FreeIdxs.Add(Idx); +// if (DictIdx2LocalClientInfo.ContainsKey(Idx)) +// DictIdx2LocalClientInfo.Remove(Idx); +// DictSocketHandle2Idx.Remove(socket.Handle); +// AppNoSugarNet.log.Debug($"RemoveDictSocket mTunnelID->{mTunnelID} Idx->{Idx} socket.Handle{socket.Handle}"); +// } +// } - bool GetSocketByIdx(int Idx, out LocalClientInfo _localClientInfo) - { - if (!DictIdx2LocalClientInfo.ContainsKey(Idx)) - { - _localClientInfo = null; - return false; - } +// bool GetSocketByIdx(int Idx, out LocalClientInfo _localClientInfo) +// { +// if (!DictIdx2LocalClientInfo.ContainsKey(Idx)) +// { +// _localClientInfo = null; +// return false; +// } - _localClientInfo = DictIdx2LocalClientInfo[Idx]; - return true; - } +// _localClientInfo = DictIdx2LocalClientInfo[Idx]; +// return true; +// } - public bool GetSocketIdxBySocket(Socket _socket, out int Idx) - { - if (_socket == null) - { - Idx = -1; - return false; - } +// public bool GetSocketIdxBySocket(Socket _socket, out int Idx) +// { +// if (_socket == null) +// { +// Idx = -1; +// return false; +// } - if (!DictSocketHandle2Idx.ContainsKey(_socket.Handle)) - { - Idx = -1; - return false; - } +// if (!DictSocketHandle2Idx.ContainsKey(_socket.Handle)) +// { +// Idx = -1; +// return false; +// } - Idx = DictSocketHandle2Idx[_socket.Handle]; - return true; - } +// Idx = DictSocketHandle2Idx[_socket.Handle]; +// return true; +// } - public bool CheckRemoteConnect(int Idx) - { - if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) - return false; - return _localClientInfo.bRemoteConnect; - } +// public bool CheckRemoteConnect(int Idx) +// { +// if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) +// return false; +// return _localClientInfo.bRemoteConnect; +// } - public void SetRemoteConnectd(int Idx,bool bConnected) - { - if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) - return; - if (bConnected) - AppNoSugarNet.log.Info("远端本地连接已连接!!!!"); - else - AppNoSugarNet.log.Info("远端本地连接已断开连接!!!!"); - _localClientInfo.bRemoteConnect = bConnected; - } +// public void SetRemoteConnectd(int Idx,bool bConnected) +// { +// if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) +// return; +// if (bConnected) +// AppNoSugarNet.log.Info("远端本地连接已连接!!!!"); +// else +// AppNoSugarNet.log.Info("远端本地连接已断开连接!!!!"); +// _localClientInfo.bRemoteConnect = bConnected; +// } - public void StopAllLocalClient() - { - lock (DictIdx2LocalClientInfo) - { - int[] Idxs = DictIdx2LocalClientInfo.Keys.ToArray(); - for (int i = 0; i < Idxs.Length; i++) - { - CloseConnectByIdx((byte)Idxs[i]); - } - DictIdx2LocalClientInfo.Clear(); - DictSocketHandle2Idx.Clear(); - ResetFree(); +// public void StopAllLocalClient() +// { +// lock (DictIdx2LocalClientInfo) +// { +// int[] Idxs = DictIdx2LocalClientInfo.Keys.ToArray(); +// for (int i = 0; i < Idxs.Length; i++) +// { +// CloseConnectByIdx((byte)Idxs[i]); +// } +// DictIdx2LocalClientInfo.Clear(); +// DictSocketHandle2Idx.Clear(); +// ResetFree(); - //清理事件 - OnClientNumberChange -= ClientNumberChange; - OnReceive -= ReceiveData; - OnDisconnected -= OnDisconnect; - OnNetLog -= OnShowNetLog; - } - } +// //清理事件 +// OnClientNumberChange -= ClientNumberChange; +// OnReceive -= ReceiveData; +// OnDisconnected -= OnDisconnect; +// OnNetLog -= OnShowNetLog; +// } +// } - #endregion +// #endregion - #region 缓存 - public void EnqueueIdxWithMsg(byte Idx, byte[] data) - { - if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) - return; +// #region 缓存 +// public void EnqueueIdxWithMsg(byte Idx, byte[] data) +// { +// if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo)) +// return; - IdxWithMsg Msg = AppNoSugarNet.local._localMsgPool.Dequeue(); - Msg.Idx = Idx; - Msg.data = data; - _localClientInfo.msgQueue.Enqueue(Msg); - } - public bool GetDictMsgQueue(byte Idx,out List MsgList) - { - if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo) || _localClientInfo.msgQueue.Count < 1) - { - MsgList = null; - return false; - } +// IdxWithMsg Msg = AppNoSugarNet.local._localMsgPool.Dequeue(); +// Msg.Idx = Idx; +// Msg.data = data; +// _localClientInfo.msgQueue.Enqueue(Msg); +// } +// public bool GetDictMsgQueue(byte Idx,out List MsgList) +// { +// if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo) || _localClientInfo.msgQueue.Count < 1) +// { +// MsgList = null; +// return false; +// } - MsgList = new List(); - lock (_localClientInfo.msgQueue) - { - while (_localClientInfo.msgQueue.Count > 0) - { - IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue(); - MsgList.Add(msg); - } - return true; - } - } - #endregion - } -} +// MsgList = new List(); +// lock (_localClientInfo.msgQueue) +// { +// while (_localClientInfo.msgQueue.Count > 0) +// { +// IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue(); +// MsgList.Add(msg); +// } +// return true; +// } +// } +// #endregion +// } +//} diff --git a/NoSugarNet.ClientCore/Manager/LocalClient/LocalMsgQueuePool.cs b/NoSugarNet.ClientCore/Manager/LocalClient/LocalMsgQueuePool.cs index 4f832b0..2e98933 100644 --- a/NoSugarNet.ClientCore/Manager/LocalClient/LocalMsgQueuePool.cs +++ b/NoSugarNet.ClientCore/Manager/LocalClient/LocalMsgQueuePool.cs @@ -1,53 +1,53 @@ -namespace NoSugarNet.ClientCore -{ - public class IdxWithMsg - { - public byte Idx; - public byte[] data; - } +//namespace NoSugarNet.ClientCore +//{ +// public class IdxWithMsg +// { +// public byte Idx; +// public byte[] data; +// } - public class LocalMsgQueuePool - { - Queue msg_pool; +// public class LocalMsgQueuePool +// { +// Queue msg_pool; - public LocalMsgQueuePool(int capacity) - { - msg_pool = new Queue(capacity); - } +// public LocalMsgQueuePool(int capacity) +// { +// msg_pool = new Queue(capacity); +// } - /// - /// 向 Queue 的末尾添加一个对象。 - /// - /// - public void Enqueue(IdxWithMsg item) - { - lock (msg_pool) - { - item.Idx = 0; - item.data = null; - msg_pool.Enqueue(item); - } - } +// /// +// /// 向 Queue 的末尾添加一个对象。 +// /// +// /// +// public void Enqueue(IdxWithMsg item) +// { +// lock (msg_pool) +// { +// item.Idx = 0; +// item.data = null; +// msg_pool.Enqueue(item); +// } +// } - //移除并返回在 Queue 的开头的对象。 - public IdxWithMsg Dequeue() - { - lock (msg_pool) - { - if(msg_pool.Count > 0) - return msg_pool.Dequeue(); - return new IdxWithMsg(); - } - } +// //移除并返回在 Queue 的开头的对象。 +// public IdxWithMsg Dequeue() +// { +// lock (msg_pool) +// { +// if(msg_pool.Count > 0) +// return msg_pool.Dequeue(); +// return new IdxWithMsg(); +// } +// } - public int Count - { - get { return msg_pool.Count; } - } +// public int Count +// { +// get { return msg_pool.Count; } +// } - public void Clear() - { - msg_pool.Clear(); - } - } -} +// public void Clear() +// { +// msg_pool.Clear(); +// } +// } +//} diff --git a/NoSugarNet.ClientCore/Manager/LogManager.cs b/NoSugarNet.ClientCore/Manager/LogManager.cs index 8b2a504..e356779 100644 --- a/NoSugarNet.ClientCore/Manager/LogManager.cs +++ b/NoSugarNet.ClientCore/Manager/LogManager.cs @@ -44,5 +44,10 @@ { OnLog?.Invoke((int)logtype, str); } + + public void Log(int logtype, string str) + { + OnLog?.Invoke(logtype, str); + } } } \ No newline at end of file diff --git a/NoSugarNet.ClientCore/NoSugarNet.ClientCore.csproj b/NoSugarNet.ClientCore/NoSugarNet.ClientCore.csproj index f149ca3..de7f8df 100644 --- a/NoSugarNet.ClientCore/NoSugarNet.ClientCore.csproj +++ b/NoSugarNet.ClientCore/NoSugarNet.ClientCore.csproj @@ -6,6 +6,10 @@ enable + + + + ..\Lib\Google.Protobuf.dll diff --git a/NoSugarNet.ServerCore/Common/Config.cs b/NoSugarNet.ServerCore/Common/Config.cs index 1f45d3c..c0e83e3 100644 --- a/NoSugarNet.ServerCore/Common/Config.cs +++ b/NoSugarNet.ServerCore/Common/Config.cs @@ -1,5 +1,4 @@ -using NoSugarNet.DataHelper; -using System.Text; +using NoSugarNet.Adapter.DataHelper; namespace NoSugarNet.ServerCore.Common { diff --git a/NoSugarNet.ServerCore/Manager/LocalClientManager.cs b/NoSugarNet.ServerCore/Manager/LocalClientManager.cs index 08bed42..074eba6 100644 --- a/NoSugarNet.ServerCore/Manager/LocalClientManager.cs +++ b/NoSugarNet.ServerCore/Manager/LocalClientManager.cs @@ -1,7 +1,7 @@ using AxibugProtobuf; using Google.Protobuf; -using NoSugarNet.ClientCore.Network; -using NoSugarNet.DataHelper; +using NoSugarNet.Adapter; +using NoSugarNet.Adapter.DataHelper; using NoSugarNet.ServerCore.Common; using ServerCore.Common; using ServerCore.NetWork; @@ -11,7 +11,7 @@ namespace ServerCore.Manager { public class LocalClientManager { - Dictionary mDictCommKey2ServerLocalClients = new Dictionary(); + Dictionary mDictCommKey2ServerLocalClients = new Dictionary(); CompressAdapter mCompressAdapter; public long tReciveAllLenght { get; private set; } @@ -58,7 +58,7 @@ namespace ServerCore.Manager /// /// /// - void AddServerLocalClient(long uid, byte tunnelId,byte Idx, ServerLocalClient serverClient) + void AddServerLocalClient(long uid, byte tunnelId,byte Idx, BackwardLocalClient serverClient) { long CommKey = GetCommKey(uid, tunnelId, Idx); lock (mDictCommKey2ServerLocalClients) @@ -79,11 +79,12 @@ namespace ServerCore.Manager if (!mDictCommKey2ServerLocalClients.ContainsKey(CommKey)) return; + mDictCommKey2ServerLocalClients[CommKey].Release(); mDictCommKey2ServerLocalClients.Remove(CommKey); } } - bool GetServerLocalClient(long uid, byte tunnelId, byte Idx,out ServerLocalClient serverLocalClient) + bool GetServerLocalClient(long uid, byte tunnelId, byte Idx,out BackwardLocalClient serverLocalClient) { serverLocalClient = null; @@ -99,7 +100,7 @@ namespace ServerCore.Manager void CloseServerLocalClient(long uid, byte tunnelId, byte Idx) { //隧道ID定位投递服务端本地连接 - if (!GetServerLocalClient(uid, tunnelId, Idx, out ServerLocalClient _serverLocalClient)) + if (!GetServerLocalClient(uid, tunnelId, Idx, out BackwardLocalClient _serverLocalClient)) return; _serverLocalClient.CloseConntect(); RemoveServerLocalClient(uid, tunnelId, Idx); @@ -139,7 +140,7 @@ namespace ServerCore.Manager long CommID = TempRemoveCommIDList[i]; if (!mDictCommKey2ServerLocalClients.ContainsKey(CommID)) continue; - ServerLocalClient _serverLoackClient = mDictCommKey2ServerLocalClients[CommID]; + BackwardLocalClient _serverLoackClient = mDictCommKey2ServerLocalClients[CommID]; _serverLoackClient.CloseConntect(); } } @@ -189,7 +190,8 @@ namespace ServerCore.Manager { //服务器本地局域网连接指定端口 TunnelClientData tunnelDataCfg = Config.cfgs[tunnelId]; - ServerLocalClient serverLocalClient = new ServerLocalClient(uid, tunnelId, (byte)Idx); + BackwardLocalClient serverLocalClient = new BackwardLocalClient(uid, tunnelId, (byte)Idx); + serverLocalClient.BandEvent(ServerManager.g_Log.Log, OnServerLocalConnect, OnServerLocalDisconnect, OnServerLocalDataCallBack); //连接成功 if (!serverLocalClient.Init(tunnelDataCfg.ServerLocalTargetIP, tunnelDataCfg.ServerLocalTargetPort)) { @@ -219,7 +221,7 @@ namespace ServerCore.Manager return; //隧道ID定位投递服务端本地连接 - if (!GetServerLocalClient(uid, tunnelId, Idx, out ServerLocalClient serverLocalClient)) + if (!GetServerLocalClient(uid, tunnelId, Idx, out BackwardLocalClient serverLocalClient)) return; //断开服务端本地客户端连接 @@ -230,7 +232,7 @@ namespace ServerCore.Manager /// /// /// - public void OnServerLocalConnect(long uid, byte tunnelId, byte Idx, ServerLocalClient serverLocalClient) + public void OnServerLocalConnect(long uid, byte tunnelId, byte Idx, BackwardLocalClient serverLocalClient) { ServerManager.g_Log.Debug($"OnServerLocalConnect {uid},{tunnelId},{Idx}"); if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client)) @@ -253,7 +255,7 @@ namespace ServerCore.Manager /// /// /// - public void OnServerLocalDisconnect(long uid, byte tunnelId, byte Idx, ServerLocalClient serverLocalClient) + public void OnServerLocalDisconnect(long uid, byte tunnelId, byte Idx, BackwardLocalClient serverLocalClient) { ServerManager.g_Log.Debug($"OnServerLocalDisconnect {uid},{tunnelId},{Idx}"); if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client)) @@ -282,7 +284,7 @@ namespace ServerCore.Manager { //ServerManager.g_Log.Debug($"OnClientTunnelDataCallBack {uid},{tunnelId},{Idx}"); //隧道ID定位投递服务端本地连接 - if (!GetServerLocalClient(uid, tunnelId, Idx, out ServerLocalClient serverLocalClient)) + if (!GetServerLocalClient(uid, tunnelId, Idx, out BackwardLocalClient serverLocalClient)) return; //记录数据长度 tReciveAllLenght += data.Length; @@ -331,7 +333,6 @@ namespace ServerCore.Manager } SendDataToRemote(uid, tunnelId, Idx, data); } - void SendDataToRemote(long uid, byte tunnelId, byte Idx, byte[] data) { if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client)) diff --git a/NoSugarNet.ServerCore/Manager/LogManager.cs b/NoSugarNet.ServerCore/Manager/LogManager.cs index b1d8e77..f5714e5 100644 --- a/NoSugarNet.ServerCore/Manager/LogManager.cs +++ b/NoSugarNet.ServerCore/Manager/LogManager.cs @@ -16,5 +16,10 @@ { Console.WriteLine(str); } + + public void Log(int logtype, string str) + { + Console.WriteLine(str); + } } } \ No newline at end of file diff --git a/NoSugarNet.ServerCore/Manager/ServerLocalClient/ServerLocalClient.cs b/NoSugarNet.ServerCore/Manager/ServerLocalClient/ServerLocalClient.cs index 4a78469..e4a1c5c 100644 --- a/NoSugarNet.ServerCore/Manager/ServerLocalClient/ServerLocalClient.cs +++ b/NoSugarNet.ServerCore/Manager/ServerLocalClient/ServerLocalClient.cs @@ -1,85 +1,85 @@ -using HaoYueNet.ClientNetwork.OtherMode; -using ServerCore.Manager; -using System; -using System.Security.Cryptography; +//using HaoYueNet.ClientNetwork.OtherMode; +//using ServerCore.Manager; +//using System; +//using System.Security.Cryptography; -namespace NoSugarNet.ClientCore.Network -{ - /// - /// 继承网络库,以支持网络功能 - /// - public class ServerLocalClient : NetworkHelperCore_SourceMode - { - public long mUID; - public byte mTunnelID; - public byte mIdx; - public long mReciveAllLenght; - public long mSendAllLenght; - public ServerLocalClient(long UID,byte TunnelID, byte Idx) - { - mUID = UID; - mTunnelID = TunnelID; - mIdx = Idx; - //指定接收服务器数据事件 - OnReceiveData += GetDataCallBack; - //断开连接 - OnClose += OnConnectClose; - OnConnected += NetworkConnected; - //网络库调试信息输出事件,用于打印网络内容 - OnLogOut += NetworkDeBugLog; - } +//namespace NoSugarNet.ClientCore.Network +//{ +// /// +// /// 继承网络库,以支持网络功能 +// /// +// public class ServerLocalClient : NetworkHelperCore_SourceMode +// { +// public long mUID; +// public byte mTunnelID; +// public byte mIdx; +// public long mReciveAllLenght; +// public long mSendAllLenght; +// public ServerLocalClient(long UID,byte TunnelID, byte Idx) +// { +// mUID = UID; +// mTunnelID = TunnelID; +// mIdx = Idx; +// //指定接收服务器数据事件 +// OnReceiveData += GetDataCallBack; +// //断开连接 +// OnClose += OnConnectClose; +// OnConnected += NetworkConnected; +// //网络库调试信息输出事件,用于打印网络内容 +// OnLogOut += NetworkDeBugLog; +// } - public void NetworkConnected(bool IsConnect) - { - NetworkDeBugLog($"NetworkConnected:{IsConnect}"); - if (IsConnect) - { - ServerManager.g_Local.OnServerLocalConnect(mUID, mTunnelID, mIdx, this); - } - else - { - //连接失败 - NetworkDeBugLog("连接失败!"); - } - } +// public void NetworkConnected(bool IsConnect) +// { +// NetworkDeBugLog($"NetworkConnected:{IsConnect}"); +// if (IsConnect) +// { +// ServerManager.g_Local.OnServerLocalConnect(mUID, mTunnelID, mIdx, this); +// } +// else +// { +// //连接失败 +// NetworkDeBugLog("连接失败!"); +// } +// } - public void NetworkDeBugLog(string str) - { - //用于Unity内的输出 - //Debug.Log("NetCoreDebug >> "+str); - Console.WriteLine("NetCoreDebug >> " + str); - } +// public void NetworkDeBugLog(string str) +// { +// //用于Unity内的输出 +// //Debug.Log("NetCoreDebug >> "+str); +// Console.WriteLine("NetCoreDebug >> " + str); +// } - /// - /// 接受包回调 - /// - /// 协议ID - /// 错误编号 - /// 业务数据 - public void GetDataCallBack(byte[] data) - { - //NetworkDeBugLog("收到消息 数据长度=>" + data.Length); - try - { - //记录接收数据长度 - mReciveAllLenght += data.Length; - //抛出网络数据 - ServerManager.g_Local.OnServerLocalDataCallBack(mUID, mTunnelID, mIdx, data); - } - catch (Exception ex) - { - NetworkDeBugLog("逻辑处理错误:" + ex.ToString()); - } +// /// +// /// 接受包回调 +// /// +// /// 协议ID +// /// 错误编号 +// /// 业务数据 +// public void GetDataCallBack(byte[] data) +// { +// //NetworkDeBugLog("收到消息 数据长度=>" + data.Length); +// try +// { +// //记录接收数据长度 +// mReciveAllLenght += data.Length; +// //抛出网络数据 +// ServerManager.g_Local.OnServerLocalDataCallBack(mUID, mTunnelID, mIdx, data); +// } +// catch (Exception ex) +// { +// NetworkDeBugLog("逻辑处理错误:" + ex.ToString()); +// } - } +// } - /// - /// 关闭连接 - /// - public void OnConnectClose() - { - NetworkDeBugLog("OnConnectClose"); - ServerManager.g_Local.OnServerLocalDisconnect(mUID, mTunnelID,mIdx,this); - } - } -} +// /// +// /// 关闭连接 +// /// +// public void OnConnectClose() +// { +// NetworkDeBugLog("OnConnectClose"); +// ServerManager.g_Local.OnServerLocalDisconnect(mUID, mTunnelID,mIdx,this); +// } +// } +//} diff --git a/NoSugarNet.ServerCore/Manager/ServerManager.cs b/NoSugarNet.ServerCore/Manager/ServerManager.cs index 8542c82..2f49e39 100644 --- a/NoSugarNet.ServerCore/Manager/ServerManager.cs +++ b/NoSugarNet.ServerCore/Manager/ServerManager.cs @@ -1,4 +1,4 @@ -using NoSugarNet.DataHelper; +using NoSugarNet.Adapter.DataHelper; using NoSugarNet.ServerCore; using NoSugarNet.ServerCore.Common; using ServerCore.NetWork; diff --git a/NoSugarNet.ServerCore/NoSugarNet.ServerCore.csproj b/NoSugarNet.ServerCore/NoSugarNet.ServerCore.csproj index 01ca97b..d1ec45a 100644 --- a/NoSugarNet.ServerCore/NoSugarNet.ServerCore.csproj +++ b/NoSugarNet.ServerCore/NoSugarNet.ServerCore.csproj @@ -7,7 +7,7 @@ - + diff --git a/NoSugarNet.ServerCore/Properties/PublishProfiles/FolderProfile.pubxml.user b/NoSugarNet.ServerCore/Properties/PublishProfiles/FolderProfile.pubxml.user index 55d7f13..b2f84cc 100644 --- a/NoSugarNet.ServerCore/Properties/PublishProfiles/FolderProfile.pubxml.user +++ b/NoSugarNet.ServerCore/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - True|2024-04-23T09:13:21.9642037Z;True|2024-04-23T17:02:04.8137007+08:00;True|2024-01-25T17:08:35.3176032+08:00; + True|2024-06-25T05:12:56.0566334Z;True|2024-04-23T17:13:21.9642037+08:00;True|2024-04-23T17:02:04.8137007+08:00;True|2024-01-25T17:08:35.3176032+08:00; \ No newline at end of file diff --git a/NoSugarNet.sln b/NoSugarNet.sln index 859b9df..e11546d 100644 --- a/NoSugarNet.sln +++ b/NoSugarNet.sln @@ -20,8 +20,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NoSugarNet.ClientCli", "Sam EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NoSugarNet.ServerCli", "Sample\NoSugarNet.ServerCli\NoSugarNet.ServerCli.csproj", "{65220036-9A81-49FA-A5BC-DA06783D2E52}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NoSugarNet.DataHelper", "NoSugarNet.DataHelper\NoSugarNet.DataHelper.csproj", "{3C41B685-B46B-4057-826B-C8C6F395BFA8}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NetLib_Standard2", "NetLib_Standard2", "{4A660CAE-CD92-411C-9D8E-7CB677DB3C74}" ProjectSection(SolutionItems) = preProject Lib\NetLib_Standard2\HaoYueNet.ClientNetworkNet.Standard2.dll = Lib\NetLib_Standard2\HaoYueNet.ClientNetworkNet.Standard2.dll @@ -33,6 +31,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NetLib_Standard2", "NetLib_ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NoSugarNet.ClientCore.Standard2", "NoSugarNet.ClientCore.Standard2\NoSugarNet.ClientCore.Standard2.csproj", "{5DB2B608-6F99-430A-99AC-534410393955}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoSugarNet.Adapter", "NoSugarNet.Adapter\NoSugarNet.Adapter.csproj", "{961FA85B-B6A7-4F45-8239-9E91315253B4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -55,14 +55,14 @@ Global {65220036-9A81-49FA-A5BC-DA06783D2E52}.Debug|Any CPU.Build.0 = Debug|Any CPU {65220036-9A81-49FA-A5BC-DA06783D2E52}.Release|Any CPU.ActiveCfg = Release|Any CPU {65220036-9A81-49FA-A5BC-DA06783D2E52}.Release|Any CPU.Build.0 = Release|Any CPU - {3C41B685-B46B-4057-826B-C8C6F395BFA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3C41B685-B46B-4057-826B-C8C6F395BFA8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3C41B685-B46B-4057-826B-C8C6F395BFA8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3C41B685-B46B-4057-826B-C8C6F395BFA8}.Release|Any CPU.Build.0 = Release|Any CPU {5DB2B608-6F99-430A-99AC-534410393955}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5DB2B608-6F99-430A-99AC-534410393955}.Debug|Any CPU.Build.0 = Debug|Any CPU {5DB2B608-6F99-430A-99AC-534410393955}.Release|Any CPU.ActiveCfg = Release|Any CPU {5DB2B608-6F99-430A-99AC-534410393955}.Release|Any CPU.Build.0 = Release|Any CPU + {961FA85B-B6A7-4F45-8239-9E91315253B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {961FA85B-B6A7-4F45-8239-9E91315253B4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {961FA85B-B6A7-4F45-8239-9E91315253B4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {961FA85B-B6A7-4F45-8239-9E91315253B4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Sample/NoSugarNet.ServerCli/Properties/PublishProfiles/FolderProfile.pubxml.user b/Sample/NoSugarNet.ServerCli/Properties/PublishProfiles/FolderProfile.pubxml.user index 4195f51..0b53317 100644 --- a/Sample/NoSugarNet.ServerCli/Properties/PublishProfiles/FolderProfile.pubxml.user +++ b/Sample/NoSugarNet.ServerCli/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - True|2024-06-18T03:37:53.3986849Z;True|2024-06-18T11:21:56.8035265+08:00;True|2024-06-18T11:21:19.5434721+08:00;True|2024-06-18T11:21:01.1589956+08:00;True|2024-06-18T11:13:38.3624463+08:00;True|2024-06-18T11:10:28.0508856+08:00;True|2024-06-18T10:39:23.0033920+08:00;True|2024-06-18T10:28:08.9658896+08:00;True|2024-06-17T14:46:33.2307641+08:00;True|2024-05-20T17:56:34.6581491+08:00;True|2024-04-23T17:02:36.4793408+08:00;True|2024-04-15T15:24:50.3598281+08:00;True|2024-04-15T15:24:34.0374231+08:00;True|2024-01-25T17:09:07.9161603+08:00;True|2024-01-23T18:28:01.1220581+08:00;True|2024-01-23T16:36:21.1141328+08:00; + True|2024-06-25T05:14:05.9372915Z;True|2024-06-18T11:37:53.3986849+08:00;True|2024-06-18T11:21:56.8035265+08:00;True|2024-06-18T11:21:19.5434721+08:00;True|2024-06-18T11:21:01.1589956+08:00;True|2024-06-18T11:13:38.3624463+08:00;True|2024-06-18T11:10:28.0508856+08:00;True|2024-06-18T10:39:23.0033920+08:00;True|2024-06-18T10:28:08.9658896+08:00;True|2024-06-17T14:46:33.2307641+08:00;True|2024-05-20T17:56:34.6581491+08:00;True|2024-04-23T17:02:36.4793408+08:00;True|2024-04-15T15:24:50.3598281+08:00;True|2024-04-15T15:24:34.0374231+08:00;True|2024-01-25T17:09:07.9161603+08:00;True|2024-01-23T18:28:01.1220581+08:00;True|2024-01-23T16:36:21.1141328+08:00; \ No newline at end of file