抽象Forward和Backward网络监听

This commit is contained in:
sin365 2024-06-25 13:18:33 +08:00
parent 59ee584c2c
commit 2c11d70b68
21 changed files with 1071 additions and 434 deletions

View File

@ -0,0 +1,112 @@
using HaoYueNet.ClientNetwork.OtherMode;
namespace NoSugarNet.Adapter
{
/// <summary>
/// 继承网络库,以支持网络功能
/// </summary>
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);
}
/// <summary>
/// 接受包回调
/// </summary>
/// <param name="CMDID">协议ID</param>
/// <param name="ERRCODE">错误编号</param>
/// <param name="data">业务数据</param>
public void GetDataCallBack(byte[] data)
{
//NetworkDeBugLog("收到消息 数据长度=>" + data.Length);
try
{
//记录接收数据长度
mReciveAllLenght += data.Length;
//抛出网络数据
OnBackwardDataCallBack?.Invoke(mUID, mTunnelID, mIdx, data);
}
catch (Exception ex)
{
NetworkDeBugLog("逻辑处理错误:" + ex.ToString());
}
}
/// <summary>
/// 关闭连接
/// </summary>
void OnConnectClose()
{
NetworkDeBugLog("OnConnectClose");
OnBackwardDisconnect?.Invoke(mUID, mTunnelID,mIdx,this);
}
public void Release()
{
OnBackwardLogOut -= OnBackwardLogOut;
OnBackwardConnect -= OnBackwardConnect;
OnBackwardDisconnect -= OnBackwardDisconnect;
OnBackwardDataCallBack -= OnBackwardDataCallBack;
}
}
}

View File

@ -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,
}
/// <summary>
/// 压缩适配器
/// </summary>
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();
}
}
}
}

View File

@ -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);
}
}
}
/// <summary>
/// 通过下标发送
/// </summary>
/// <param name="Idx"></param>
/// <param name="data"></param>
public void SendSocketByIdx(int Idx, byte[] data)
{
if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo))
{
mSendAllLenght += data.Length;
SendToSocket(_localClientInfo._socket, data);
}
//TODO连接前缓存数据
}
/// <summary>
/// 接受包回调
/// </summary>
/// <param name="CMDID">协议ID</param>
/// <param name="ERRCODE">错误编号</param>
/// <param name="data">业务数据</param>
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);
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <param name="sk"></param>
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<nint, int> DictSocketHandle2Idx = new Dictionary<nint, int>();
Dictionary<int, LocalClientInfo> DictIdx2LocalClientInfo = new Dictionary<int, LocalClientInfo>();
int mSeedIdx = 0;
List<int> FreeIdxs = new List<int>();
public class LocalClientInfo
{
public Socket _socket;
public bool bRemoteConnect;
public bool bLocalConnect => _socket.Connected;
public Queue<IdxWithMsg> msgQueue = new Queue<IdxWithMsg>();
}
public Dictionary<int, LocalClientInfo> 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;
}
/// <summary>
/// 追加Socket返回下标
/// </summary>
/// <param name="socket"></param>
/// <returns></returns>
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<IdxWithMsg> MsgList)
{
if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo) || _localClientInfo.msgQueue.Count < 1)
{
MsgList = null;
return false;
}
MsgList = new List<IdxWithMsg>();
lock (_localClientInfo.msgQueue)
{
while (_localClientInfo.msgQueue.Count > 0)
{
IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue();
MsgList.Add(msg);
}
return true;
}
}
#endregion
}
}

View File

@ -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<IdxWithMsg> msg_pool;
public LocalMsgQueuePool(int capacity)
{
msg_pool = new Queue<IdxWithMsg>(capacity);
}
/// <summary>
/// 向 Queue 的末尾添加一个对象。
/// </summary>
/// <param name="item"></param>
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();
}
}
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Reference Include="HaoYueNet.ClientNetwork">
<HintPath>..\Lib\HaoYueNet.ClientNetwork.dll</HintPath>
</Reference>
<Reference Include="HaoYueNet.ServerNetwork">
<HintPath>..\Lib\HaoYueNet.ServerNetwork.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@ -19,8 +19,5 @@
<Reference Include="HaoYueNet.ServerNetwork.Standard2">
<HintPath>..\Lib\NetLib_Standard2\HaoYueNet.ServerNetwork.Standard2.dll</HintPath>
</Reference>
<Reference Include="NoSugarNet.DataHelper">
<HintPath>..\Lib\NetLib_Standard2\NoSugarNet.DataHelper.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@ -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<byte, Protobuf_Cfgs_Single> mDictTunnelID2Cfg = new Dictionary<byte, Protobuf_Cfgs_Single>();
Dictionary<byte, LocalListener> mDictTunnelID2Listeners = new Dictionary<byte, LocalListener>();
CompressAdapter mCompressAdapter;
E_CompressAdapter compressAdapterType;
public LocalMsgQueuePool _localMsgPool = new LocalMsgQueuePool(1000);
Dictionary<byte, ForwardLocalListener> mDictTunnelID2Listeners = new Dictionary<byte, ForwardLocalListener>();
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
/// </summary>
/// <param name="tunnelId"></param>
/// <param name="serverClient"></param>
void AddLocalListener(LocalListener _listener)
void AddLocalListener(ForwardLocalListener _listener)
{
lock (mDictTunnelID2Listeners)
{
@ -105,7 +109,7 @@ namespace ServerCore.Manager
/// </summary>
/// <param name="tunnelId"></param>
/// <param name="serverClient"></param>
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;
//远程未连接,添加到缓存

View File

@ -14,6 +14,7 @@ namespace NoSugarNet.ClientCore.Manager
public void Login()
{
AppNoSugarNet.log.Debug("-->Login");
if(string.IsNullOrEmpty(LastLoginGuid))
LastLoginGuid = Guid.NewGuid().ToString();

View File

@ -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);
// }
// }
// }
/// <summary>
/// 通过下标发送
/// </summary>
/// <param name="Idx"></param>
/// <param name="data"></param>
public void SendSocketByIdx(int Idx, byte[] data)
{
if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo))
{
mSendAllLenght += data.Length;
SendToSocket(_localClientInfo._socket, data);
}
//TODO连接前缓存数据
}
// /// <summary>
// /// 通过下标发送
// /// </summary>
// /// <param name="Idx"></param>
// /// <param name="data"></param>
// public void SendSocketByIdx(int Idx, byte[] data)
// {
// if (GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo))
// {
// mSendAllLenght += data.Length;
// SendToSocket(_localClientInfo._socket, data);
// }
// //TODO连接前缓存数据
// }
/// <summary>
/// 接受包回调
/// </summary>
/// <param name="CMDID">协议ID</param>
/// <param name="ERRCODE">错误编号</param>
/// <param name="data">业务数据</param>
private void ReceiveData(AsyncUserToken token, byte[] data)
{
DataCallBack(token.Socket, data);
}
// /// <summary>
// /// 接受包回调
// /// </summary>
// /// <param name="CMDID">协议ID</param>
// /// <param name="ERRCODE">错误编号</param>
// /// <param name="data">业务数据</param>
// 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);
// }
// }
/// <summary>
/// 断开连接
/// </summary>
/// <param name="sk"></param>
public void OnDisconnect(AsyncUserToken token)
{
AppNoSugarNet.log.Info("断开连接");
// /// <summary>
// /// 断开连接
// /// </summary>
// /// <param name="sk"></param>
// 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<nint, int> DictSocketHandle2Idx = new Dictionary<nint, int>();
Dictionary<int, LocalClientInfo> DictIdx2LocalClientInfo = new Dictionary<int, LocalClientInfo>();
int mSeedIdx = 0;
List<int> FreeIdxs = new List<int>();
public class LocalClientInfo
{
public Socket _socket;
public bool bRemoteConnect;
public bool bLocalConnect => _socket.Connected;
public Queue<IdxWithMsg> msgQueue = new Queue<IdxWithMsg>();
}
// #region 一个轻量级无用户连接管理
// Dictionary<nint, int> DictSocketHandle2Idx = new Dictionary<nint, int>();
// Dictionary<int, LocalClientInfo> DictIdx2LocalClientInfo = new Dictionary<int, LocalClientInfo>();
// int mSeedIdx = 0;
// List<int> FreeIdxs = new List<int>();
// public class LocalClientInfo
// {
// public Socket _socket;
// public bool bRemoteConnect;
// public bool bLocalConnect => _socket.Connected;
// public Queue<IdxWithMsg> msgQueue = new Queue<IdxWithMsg>();
// }
public Dictionary<int, LocalClientInfo> GetDictIdx2LocalClientInfo()
{
return DictIdx2LocalClientInfo;
}
// public Dictionary<int, LocalClientInfo> 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;
// }
/// <summary>
/// 追加Socket返回下标
/// </summary>
/// <param name="socket"></param>
/// <returns></returns>
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;
}
}
// /// <summary>
// /// 追加Socket返回下标
// /// </summary>
// /// <param name="socket"></param>
// /// <returns></returns>
// 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<IdxWithMsg> 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<IdxWithMsg> MsgList)
// {
// if (!GetSocketByIdx(Idx, out LocalClientInfo _localClientInfo) || _localClientInfo.msgQueue.Count < 1)
// {
// MsgList = null;
// return false;
// }
MsgList = new List<IdxWithMsg>();
lock (_localClientInfo.msgQueue)
{
while (_localClientInfo.msgQueue.Count > 0)
{
IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue();
MsgList.Add(msg);
}
return true;
}
}
#endregion
}
}
// MsgList = new List<IdxWithMsg>();
// lock (_localClientInfo.msgQueue)
// {
// while (_localClientInfo.msgQueue.Count > 0)
// {
// IdxWithMsg msg = _localClientInfo.msgQueue.Dequeue();
// MsgList.Add(msg);
// }
// return true;
// }
// }
// #endregion
// }
//}

View File

@ -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<IdxWithMsg> msg_pool;
// public class LocalMsgQueuePool
// {
// Queue<IdxWithMsg> msg_pool;
public LocalMsgQueuePool(int capacity)
{
msg_pool = new Queue<IdxWithMsg>(capacity);
}
// public LocalMsgQueuePool(int capacity)
// {
// msg_pool = new Queue<IdxWithMsg>(capacity);
// }
/// <summary>
/// 向 Queue 的末尾添加一个对象。
/// </summary>
/// <param name="item"></param>
public void Enqueue(IdxWithMsg item)
{
lock (msg_pool)
{
item.Idx = 0;
item.data = null;
msg_pool.Enqueue(item);
}
}
// /// <summary>
// /// 向 Queue 的末尾添加一个对象。
// /// </summary>
// /// <param name="item"></param>
// 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();
// }
// }
//}

View File

@ -44,5 +44,10 @@
{
OnLog?.Invoke((int)logtype, str);
}
public void Log(int logtype, string str)
{
OnLog?.Invoke(logtype, str);
}
}
}

View File

@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NoSugarNet.Adapter\NoSugarNet.Adapter.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Google.Protobuf">
<HintPath>..\Lib\Google.Protobuf.dll</HintPath>

View File

@ -1,5 +1,4 @@
using NoSugarNet.DataHelper;
using System.Text;
using NoSugarNet.Adapter.DataHelper;
namespace NoSugarNet.ServerCore.Common
{

View File

@ -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<long, ServerLocalClient> mDictCommKey2ServerLocalClients = new Dictionary<long, ServerLocalClient>();
Dictionary<long, BackwardLocalClient> mDictCommKey2ServerLocalClients = new Dictionary<long, BackwardLocalClient>();
CompressAdapter mCompressAdapter;
public long tReciveAllLenght { get; private set; }
@ -58,7 +58,7 @@ namespace ServerCore.Manager
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
/// <param name="serverClient"></param>
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
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
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
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
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))

View File

@ -16,5 +16,10 @@
{
Console.WriteLine(str);
}
public void Log(int logtype, string str)
{
Console.WriteLine(str);
}
}
}

View File

@ -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
{
/// <summary>
/// 继承网络库,以支持网络功能
/// </summary>
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
//{
// /// <summary>
// /// 继承网络库,以支持网络功能
// /// </summary>
// 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);
// }
/// <summary>
/// 接受包回调
/// </summary>
/// <param name="CMDID">协议ID</param>
/// <param name="ERRCODE">错误编号</param>
/// <param name="data">业务数据</param>
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());
}
// /// <summary>
// /// 接受包回调
// /// </summary>
// /// <param name="CMDID">协议ID</param>
// /// <param name="ERRCODE">错误编号</param>
// /// <param name="data">业务数据</param>
// 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());
// }
}
// }
/// <summary>
/// 关闭连接
/// </summary>
public void OnConnectClose()
{
NetworkDeBugLog("OnConnectClose");
ServerManager.g_Local.OnServerLocalDisconnect(mUID, mTunnelID,mIdx,this);
}
}
}
// /// <summary>
// /// 关闭连接
// /// </summary>
// public void OnConnectClose()
// {
// NetworkDeBugLog("OnConnectClose");
// ServerManager.g_Local.OnServerLocalDisconnect(mUID, mTunnelID,mIdx,this);
// }
// }
//}

View File

@ -1,4 +1,4 @@
using NoSugarNet.DataHelper;
using NoSugarNet.Adapter.DataHelper;
using NoSugarNet.ServerCore;
using NoSugarNet.ServerCore.Common;
using ServerCore.NetWork;

View File

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NoSugarNet.DataHelper\NoSugarNet.DataHelper.csproj" />
<ProjectReference Include="..\NoSugarNet.Adapter\NoSugarNet.Adapter.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<History>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;</History>
<History>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;</History>
<LastFailureDetails />
</PropertyGroup>
</Project>

View File

@ -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

View File

@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<History>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;</History>
<History>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;</History>
<LastFailureDetails />
</PropertyGroup>
</Project>