diff --git a/Lib/HaoYueNet.ClientNetwork.dll b/Lib/HaoYueNet.ClientNetwork.dll index 6eb54c8..bc3795c 100644 Binary files a/Lib/HaoYueNet.ClientNetwork.dll and b/Lib/HaoYueNet.ClientNetwork.dll differ diff --git a/Lib/HaoYueNet.ServerNetwork.dll b/Lib/HaoYueNet.ServerNetwork.dll index d448aeb..fe89567 100644 Binary files a/Lib/HaoYueNet.ServerNetwork.dll and b/Lib/HaoYueNet.ServerNetwork.dll differ diff --git a/NoSugarNet.ClientCore/Manager/AppLocalClient.cs b/NoSugarNet.ClientCore/Manager/AppLocalClient.cs index 07589c0..7c328a4 100644 --- a/NoSugarNet.ClientCore/Manager/AppLocalClient.cs +++ b/NoSugarNet.ClientCore/Manager/AppLocalClient.cs @@ -52,6 +52,7 @@ namespace ServerCore.Manager AppNoSugarNet.log.Debug($"开始监听配置 Tunnel:{cfg.Key},Port:{cfg.Value.Port}"); listener.Init(); listener.Start(new IPEndPoint(IPAddress.Any.Address, (int)cfg.Value.Port)); + //listener.Init((int)cfg.Value.Port); AddLocalListener(listener); } @@ -103,7 +104,7 @@ namespace ServerCore.Manager { LocalListener _listener = mDictTunnelID2Listeners[keys[i]]; _listener.StopAll(); - _listener.Stop(); + //_listener.Stop(); RemoveLocalListener(_listener); } } diff --git a/NoSugarNet.ClientCore/Manager/LocalClient/LocalListener_Source.cs b/NoSugarNet.ClientCore/Manager/LocalClient/LocalListener_Source.cs new file mode 100644 index 0000000..0ed1064 --- /dev/null +++ b/NoSugarNet.ClientCore/Manager/LocalClient/LocalListener_Source.cs @@ -0,0 +1,290 @@ +using HaoYueNet.ClientNetwork.OtherMode; +using HaoYueNet.ServerNetwork; +using System.Net.Sockets; + +namespace NoSugarNet.ClientCore +{ + public class LocalListener_Source : NetworkHelperCore_ListenerMode + { + public byte mTunnelID; + public long mReciveAllLenght; + public long mSendAllLenght; + public LocalListener_Source(int numConnections, int receiveBufferSize, byte TunnelID) + : base() + { + OnConnected += ClientNumberChange; + OnReceive += ReceiveData; + OnDisconnected += OnDisconnectClient; + OnNetLog += OnShowNetLog; + + mTunnelID = TunnelID; + } + + private void ClientNumberChange(Socket socket) + { + Console.WriteLine("Client数发生变化"); + //增加连接数 + int Idx = AddDictSocket(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; + SendToClient(_localClientInfo._socket, data); + } + //TODO连接前缓存数据 + } + + /// + /// 接受包回调 + /// + /// 协议ID + /// 错误编号 + /// 业务数据 + private void ReceiveData(Socket sk, byte[] data) + { + DataCallBack(sk, data); + } + + public void DataCallBack(Socket sk, byte[] data) + { + //AppNoSugarNet.log.Debug("收到消息 数据长度=>" + data.Length); + //记录接受长度 + mReciveAllLenght += data.Length; + if (!GetSocketIdxBySocket(sk, out int Idx)) + return; + try + { + if (GetMsgQueueByIdx(sk.Handle, out Queue _queue)) + { + lock (_queue) + { + _queue.Enqueue(data); + while (_queue.Count > 0) + { + AppNoSugarNet.local.OnClientTunnelDataCallBack(mTunnelID, (byte)Idx, _queue.Dequeue()); + } + } + } + + ////抛出网络数据 + //AppNoSugarNet.local.OnClientTunnelDataCallBack(mTunnelID, (byte)Idx, data); + } + catch (Exception ex) + { + Console.WriteLine("逻辑处理错误:" + ex.ToString()); + } + } + + public void CloseConnectByIdx(byte Idx) + { + if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInf)) + { + _localClientInf._socket.Shutdown(SocketShutdown.Both); + } + } + + /// + /// 断开连接 + /// + /// + public void OnDisconnectClient(Socket sk) + { + AppNoSugarNet.log.Debug("断开连接"); + + if (!GetSocketIdxBySocket(sk, out int Idx)) + return; + + AppNoSugarNet.local.OnClientLocalDisconnect(mTunnelID, (byte)Idx); + RemoveDictSocket(sk); + } + + public void OnShowNetLog(string msg) + { + AppNoSugarNet.log.Debug(msg); + } + + #region 一个轻量级无用户连接管理 + Dictionary DictSocketHandle2Idx = new Dictionary(); + Dictionary> DictSocketHandle2Msg = 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(); + } + + int GetNextIdx() + { + if (FreeIdxs.Count > 0) + { + int Idx = FreeIdxs[0]; + FreeIdxs.RemoveAt(0); + return Idx; + } + return mSeedIdx++; + } + + /// + /// 追加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}; + DictSocketHandle2Msg[socket.Handle] = new Queue(); + 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); + + if (DictSocketHandle2Msg.ContainsKey(socket.Handle)) + DictSocketHandle2Msg.Remove(socket.Handle); + + DictSocketHandle2Idx.Remove(socket.Handle); + } + } + + bool GetSocketByIdx(int Idx, out LocalClientInfo _localClientInfo) + { + if (!DictIdx2LocalClientInfo.ContainsKey(Idx)) + { + _localClientInfo = null; + return false; + } + + _localClientInfo = DictIdx2LocalClientInfo[Idx]; + return true; + } + + bool GetMsgQueueByIdx(nint handle, out Queue _queue) + { + if (!DictSocketHandle2Msg.ContainsKey(handle)) + { + _queue = null; + return false; + } + + _queue = DictSocketHandle2Msg[handle]; + 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) + AppNoSugarNet.log.Debug("远端本地连接已连接!!!!"); + else + AppNoSugarNet.log.Debug("远端本地连接已断开连接!!!!"); + _localClientInfo.bRemoteConnect = bConnected; + } + + public void StopAll() + { + lock (DictIdx2LocalClientInfo) + { + int[] Idxs = DictIdx2LocalClientInfo.Keys.ToArray(); + for (int i = 0; i < Idxs.Length; i++) + { + CloseConnectByIdx((byte)Idxs[i]); + } + DictIdx2LocalClientInfo.Clear(); + } + } + + #endregion + + + #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; + } + + MsgList = new List(); + lock (_localClientInfo.msgQueue) + { + while (_localClientInfo.msgQueue.Count > 0) + { + IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue(); + MsgList.Add(msg); + } + return true; + } + } + #endregion + } +}