反向代理 功能调通

This commit is contained in:
sin365 2024-06-26 14:10:05 +08:00
parent 53ae9c96c1
commit 086d16e8c8
7 changed files with 374 additions and 321 deletions

View File

@ -0,0 +1,18 @@
using NoSugarNet.Adapter.DataHelper;
namespace NoSugarNet.Adapter.DataHelper
{
public static class CompressAdapterSelector
{
static Dictionary<E_CompressAdapter, CompressAdapter> mDictAdapter = new Dictionary<E_CompressAdapter, CompressAdapter>();
public static CompressAdapter Adapter(E_CompressAdapter adptType)
{
if(mDictAdapter.ContainsKey(adptType))
return mDictAdapter[adptType];
mDictAdapter[adptType] = new CompressAdapter(adptType);
return mDictAdapter[adptType];
}
}
}

View File

@ -15,7 +15,7 @@ namespace ServerCore.Manager
{
Dictionary<byte, Protobuf_Cfgs_Single> mDictTunnelID2Cfg = new Dictionary<byte, Protobuf_Cfgs_Single>();
Dictionary<byte, ForwardLocalListener> mDictTunnelID2Listeners = new Dictionary<byte, ForwardLocalListener>();
NoSugarNet.Adapter.DataHelper.CompressAdapter mCompressAdapter;
//NoSugarNet.Adapter.DataHelper.CompressAdapter mCompressAdapter;
NoSugarNet.Adapter.DataHelper.E_CompressAdapter compressAdapterType;
//public LocalMsgQueuePool _localMsgPool = new LocalMsgQueuePool(1000);
@ -75,8 +75,8 @@ namespace ServerCore.Manager
void InitListenerMode()
{
AppNoSugarNet.log.Info("初始化压缩适配器" + compressAdapterType);
//初始化压缩适配器,代表压缩类型
mCompressAdapter = new NoSugarNet.Adapter.DataHelper.CompressAdapter(compressAdapterType);
////初始化压缩适配器,代表压缩类型
//mCompressAdapter = new NoSugarNet.Adapter.DataHelper.CompressAdapter(compressAdapterType);
foreach (var cfg in mDictTunnelID2Cfg)
{
ForwardLocalListener listener = new ForwardLocalListener(256, 1024, cfg.Key,AppNoSugarNet.user.userdata.UID);
@ -279,7 +279,7 @@ namespace ServerCore.Manager
//记录压缩前数据长度
tReciveAllLenght += data.Length;
//解压
data = mCompressAdapter.Decompress(data);
data = CompressAdapterSelector.Adapter(compressAdapterType).Decompress(data);
_listener.SendSocketByIdx(Idx,data);
}
/// <summary>
@ -322,7 +322,7 @@ namespace ServerCore.Manager
void SendDataToRemote(byte tunnelId, byte Idx, byte[] data)
{
//压缩
data = mCompressAdapter.Compress(data);
data = CompressAdapterSelector.Adapter(compressAdapterType).Compress(data);
//记录压缩后数据长度
tSendAllLenght += data.Length;

View File

@ -29,7 +29,7 @@ namespace ServerCore.Manager
public AppReverseLocalClient(NoSugarNet.Adapter.DataHelper.E_CompressAdapter compressAdapterType)
{
AppNoSugarNet.log.Debug("初始化压缩适配器" + compressAdapterType);
AppNoSugarNet.log.Debug("Reverse->初始化压缩适配器" + compressAdapterType);
//初始化压缩适配器暂时使用0代表压缩类型
mCompressAdapter = new CompressAdapter(compressAdapterType);
@ -43,7 +43,7 @@ namespace ServerCore.Manager
public void Send_ClientCfg()
{
AppNoSugarNet.log.Debug("-->Send_ClientCfg");
AppNoSugarNet.log.Debug("Reverse->-->Send_ClientCfg");
_Protobuf_Cfgs.CompressAdapterType = (int)Config.compressAdapterType;
_Protobuf_Cfgs.Cfgs.Clear();
@ -58,7 +58,7 @@ namespace ServerCore.Manager
#region
public void Recive_TunnelS2CConnect(byte[] reqData)
{
AppNoSugarNet.log.Debug("Recive_TunnelS2CConnect");
AppNoSugarNet.log.Debug("Reverse->Recive_TunnelS2CConnect");
Protobuf_Tunnel_Connect msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_Connect>(reqData);
if (msg.Connected == 1)
OnServerLocalConnect((byte)msg.TunnelID, (byte)msg.Idx);
@ -67,7 +67,7 @@ namespace ServerCore.Manager
}
public void Recive_TunnelS2CDisconnect(byte[] reqData)
{
AppNoSugarNet.log.Debug("Recive_TunnelS2CDisconnect");
AppNoSugarNet.log.Debug("Reverse->Recive_TunnelS2CDisconnect");
Protobuf_Tunnel_Disconnect msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_Disconnect>(reqData);
OnServerLocalDisconnect((byte)msg.TunnelID, (byte)msg.Idx);
}
@ -88,7 +88,7 @@ namespace ServerCore.Manager
/// <param name="tunnelId"></param>
public void OnServerLocalConnect(byte tunnelId, byte Idx)
{
AppNoSugarNet.log.Debug($"OnClientLocalConnect!!!!!! {AppNoSugarNet.user.userdata.UID},{tunnelId},{Idx}");
AppNoSugarNet.log.Debug($"Reverse->OnClientLocalConnect!!!!!! {AppNoSugarNet.user.userdata.UID},{tunnelId},{Idx}");
if (!Config.cfgs.ContainsKey(tunnelId))
return;
@ -112,7 +112,7 @@ namespace ServerCore.Manager
Connected = 0//失败
});
//发送给客户端,指定服务端本地端口已连接
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelS2CForwardConnect, respData);
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SReverseConnect, respData);
}
});
thread.Start();
@ -124,7 +124,7 @@ namespace ServerCore.Manager
/// <param name="tunnelId"></param>
public void OnServerLocalDisconnect(byte tunnelId, byte Idx)
{
AppNoSugarNet.log.Debug($"OnServerLocalDisconnect,收到客户端断开链接!!!!!! {AppNoSugarNet.user.userdata.UID},{tunnelId},{Idx}");
AppNoSugarNet.log.Debug($"Reverse->OnServerLocalDisconnect,收到客户端断开链接!!!!!! {AppNoSugarNet.user.userdata.UID},{tunnelId},{Idx}");
//隧道ID定位投递服务端本地连接
if (!GetClientLocalClient(AppNoSugarNet.user.userdata.UID, tunnelId, Idx, out BackwardLocalClient LocalClient))
@ -141,7 +141,7 @@ namespace ServerCore.Manager
/// <param name="tunnelId"></param>
public void OnClientLocalConnect(long uid, byte tunnelId, byte Idx, BackwardLocalClient serverLocalClient)
{
AppNoSugarNet.log.Debug($"OnServerLocalConnect {uid},{tunnelId},{Idx}");
AppNoSugarNet.log.Debug($"Reverse->OnServerLocalConnect {uid},{tunnelId},{Idx}");
//添加到服务端本地连接列表
AddClientLocalClient(uid, tunnelId, Idx, serverLocalClient);
@ -153,7 +153,7 @@ namespace ServerCore.Manager
Connected = 1
});
//发送给客户端,指定服务端本地端口已连接
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelS2CForwardConnect, respData);
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SReverseConnect, respData);
}
/// <summary>
/// 当服务端本地端口连接断开
@ -162,7 +162,7 @@ namespace ServerCore.Manager
/// <param name="tunnelId"></param>
public void OnClientLocalDisconnect(long uid, byte tunnelId, byte Idx, BackwardLocalClient serverLocalClient)
{
AppNoSugarNet.log.Debug($"OnServerLocalDisconnect {uid},{tunnelId},{Idx}");
AppNoSugarNet.log.Debug($"Reverse->OnClientLocalDisconnect {uid},{tunnelId},{Idx}");
//移除到服务端本地连接列表
RemoveClientLocalClient(uid, tunnelId, Idx);
@ -172,7 +172,7 @@ namespace ServerCore.Manager
Idx = Idx,
});
//发送给客户端,指定服务端本地端口连接已断开
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelS2CForwardDisconnect, respData);
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SReverseDisconnect, respData);
}
#endregion
@ -281,7 +281,7 @@ namespace ServerCore.Manager
/// <param name="data"></param>
public void OnServerTunnelDataCallBack(long uid, byte tunnelId, byte Idx, byte[] data)
{
//ServerManager.g_Log.Debug($"OnClientTunnelDataCallBack {uid},{tunnelId},{Idx}");
AppNoSugarNet.log.Debug($"Reverse->OnServerTunnelDataCallBack {uid},{tunnelId},{Idx},data -> {data.Length}");
//隧道ID定位投递服务端本地连接
if (!GetClientLocalClient(uid, tunnelId, Idx, out BackwardLocalClient serverLocalClient))
return;
@ -302,6 +302,7 @@ namespace ServerCore.Manager
/// <param name="data"></param>
public void OnClientLocalDataCallBack(long uid, byte tunnelId, byte Idx, byte[] data)
{
AppNoSugarNet.log.Debug($"Reverse->OnClientLocalDataCallBack {uid},{tunnelId},{Idx},data -> {data.Length}");
//ServerManager.g_Log.Debug($"OnServerLocalDataCallBack {uid},{tunnelId},{Idx}");
//if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client))
// return;
@ -350,7 +351,7 @@ namespace ServerCore.Manager
});
//发送给客户端指定客户端本地隧道ID
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelS2CForwardData, respData);
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SReverseData, respData);
}
#endregion
}

View File

@ -187,7 +187,8 @@ namespace ServerCore.Manager
cinfo.IsOffline = true;
cinfo.LogOutDT = DateTime.Now;
//断开所有连接
ServerManager.g_ForwardLocal.StopAll(cinfo.UID);
ServerManager.g_ForwardLocal.StopAllByUid(cinfo.UID);
ServerManager.g_ReverseLocal.StopAllByUid(cinfo.UID);
}
public void RemoveClientForSocket(Socket sk)

View File

@ -12,7 +12,8 @@ namespace ServerCore.Manager
public class ForwardLocalClientManager
{
Dictionary<long, BackwardLocalClient> mDictCommKey2ServerLocalClients = new Dictionary<long, BackwardLocalClient>();
CompressAdapter mCompressAdapter;
E_CompressAdapter compressAdapterType;
//CompressAdapter mCompressAdapter;
public long tReciveAllLenght { get; private set; }
public long tSendAllLenght { get;private set; }
@ -27,11 +28,10 @@ namespace ServerCore.Manager
return CommKey / 10000000;
}
public ForwardLocalClientManager(E_CompressAdapter compressAdapterType)
public ForwardLocalClientManager(E_CompressAdapter _compressAdapterType)
{
ServerManager.g_Log.Debug("初始化压缩适配器" + compressAdapterType);
//初始化压缩适配器暂时使用0代表压缩类型
mCompressAdapter = new CompressAdapter(compressAdapterType);
ServerManager.g_Log.Debug("初始化压缩适配器" + _compressAdapterType);
compressAdapterType = _compressAdapterType;
//注册网络消息
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SForwardConnect, Recive_TunnelC2SConnect);
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SForwardDisconnect, Recive_TunnelC2SDisconnect);
@ -120,7 +120,7 @@ namespace ServerCore.Manager
ClientUserCount = TempHadLocalConnetList.Count;
}
public void StopAll(long Uid)
public void StopAllByUid(long Uid)
{
List<long> TempRemoveCommIDList = new List<long>();
lock (mDictCommKey2ServerLocalClients)
@ -289,7 +289,7 @@ namespace ServerCore.Manager
//记录数据长度
tReciveAllLenght += data.Length;
//解压
data = mCompressAdapter.Decompress(data);
data = CompressAdapterSelector.Adapter(compressAdapterType).Decompress(data);
//记录数据长度
serverLocalClient.mSendAllLenght += data.LongLength;
//发送给对应服务端本地连接数据
@ -339,7 +339,7 @@ namespace ServerCore.Manager
return;
//压缩
data = mCompressAdapter.Compress(data);
data = CompressAdapterSelector.Adapter(compressAdapterType).Compress(data);
//记录压缩后数据长度
tSendAllLenght += data.Length;

View File

@ -1,324 +1,355 @@
//using AxibugProtobuf;
//using Google.Protobuf;
//using NoSugarNet.Adapter;
//using NoSugarNet.Adapter.DataHelper;
//using NoSugarNet.ServerCore.Common;
//using ServerCore.Common;
//using ServerCore.NetWork;
//using System.Net.Sockets;
using AxibugProtobuf;
using Google.Protobuf;
using NoSugarNet.Adapter;
using NoSugarNet.Adapter.DataHelper;
using NoSugarNet.ServerCore.Common;
using ServerCore.Common;
using ServerCore.NetWork;
using System.Net.Sockets;
//namespace ServerCore.Manager
//{
// public class ReverseLocalClientManager
// {
// Dictionary<long, ForwardLocalListener> mDictCommKey2LocalListeners = new Dictionary<long, ForwardLocalListener>();
namespace ServerCore.Manager
{
public class ReverseLocalClientManager
{
Dictionary<long, ForwardLocalListener> mDictCommKey2LocalListeners = new Dictionary<long, ForwardLocalListener>();
// public long tReciveAllLenght { get; private set; }
// public long tSendAllLenght { get; private set; }
public long tReciveAllLenght { get; private set; }
public long tSendAllLenght { get; private set; }
// static long GetCommKey(long Uid, int Tunnel)
// {
// return (Uid * 10000000) + (Tunnel * 10000);
// }
static long GetCommKey(long Uid, int Tunnel)
{
return (Uid * 10000000) + (Tunnel * 10000);
}
// static long GetUidForCommKey(long CommKey)
// {
// return CommKey / 10000000;
// }
static long GetUidForCommKey(long CommKey)
{
return CommKey / 10000000;
}
public ReverseLocalClientManager()
{
//注册网络消息
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdClientCfgs, Recive_CmdCfgs);
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SReverseConnect, Recive_TunnelC2SConnect);
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SReverseDisconnect, Recive_TunnelC2SDisconnect);
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SReverseData, Recive_TunnelC2SData);
}
/// <summary>
/// 初始化连接
/// </summary>
void InitListenerMode(long UID)
{
if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo _c))
return;
//初始化压缩适配器,代表压缩类型
ServerManager.g_Log.Info("初始化压缩适配器" + _c.e_CompressAdapter);
foreach (var cfg in _c._cfgs)
{
ForwardLocalListener listener = new ForwardLocalListener(256, 1024, cfg.Key, UID);
ServerManager.g_Log.Info($"开始监听配置 Tunnel:{cfg.Key},Port:{cfg.Value.Port}");
listener.BandEvent(ServerManager.g_Log.Log, OnLocalConnect, OnLocalDisconnect, OnTunnelDataCallBack);
listener.StartListener((uint)cfg.Value.Port);
AddLocalListener(UID, listener);
}
}
#region
/// <summary>
/// 追加监听者
/// </summary>
/// <param name="tunnelId"></param>
/// <param name="serverClient"></param>
void AddLocalListener(long UID, ForwardLocalListener _listener)
{
long Key = GetCommKey(UID, _listener.mTunnelID);
lock (mDictCommKey2LocalListeners)
{
mDictCommKey2LocalListeners[Key] = _listener;
}
}
/// <summary>
/// 删除监听
/// </summary>
/// <param name="tunnelId"></param>
/// <param name="serverClient"></param>
void RemoveLocalListener(long UID, ForwardLocalListener _listener)
{
long Key = GetCommKey(UID, _listener.mTunnelID);
lock (mDictCommKey2LocalListeners)
{
if (mDictCommKey2LocalListeners.ContainsKey(Key))
mDictCommKey2LocalListeners.Remove(Key);
}
}
bool GetLocalListener(long UID, byte tunnelId, out ForwardLocalListener _listener)
{
long Key = GetCommKey(UID, tunnelId);
_listener = null;
if (!mDictCommKey2LocalListeners.ContainsKey(Key))
return false;
_listener = mDictCommKey2LocalListeners[Key];
return true;
}
public void StopAllByUid(long UID)
{
lock (mDictCommKey2LocalListeners)
{
long[] keys = mDictCommKey2LocalListeners.Keys.ToArray();
for (int i = 0; i < keys.Length; i++)
{
ForwardLocalListener _listener = mDictCommKey2LocalListeners[keys[i]];
if (_listener.mUid != UID)
continue;
_listener.StopAllLocalClient();
_listener.StopWithClear();
//_listener.Stop();
RemoveLocalListener(UID, _listener);
}
//服务端得按用户分开
//mDictCommKey2ServerLocalClients.Clear();
}
}
#endregion
// public ReverseLocalClientManager()
// {
// //注册网络消息
// NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SReverseConnect, Recive_TunnelC2SConnect);
// NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SReverseDisconnect, Recive_TunnelC2SDisconnect);
// NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SReverseData, Recive_TunnelC2SData);
// }
#region
public void Recive_CmdCfgs(Socket sk, byte[] reqData)
{
ServerManager.g_Log.Debug("Reverse->Recive_CmdCfgs");
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
Protobuf_Cfgs msg = ProtoBufHelper.DeSerizlize<Protobuf_Cfgs>(reqData);
Dictionary<byte, Protobuf_Cfgs_Single> tempDictTunnelID2Cfg = new Dictionary<byte, Protobuf_Cfgs_Single>();
for (int i = 0; i < msg.Cfgs.Count; i++)
{
Protobuf_Cfgs_Single cfg = msg.Cfgs[i];
tempDictTunnelID2Cfg[(byte)cfg.TunnelID] = cfg;
}
//设置玩家的设置
ServerManager.g_ClientMgr.SetUserCfg(_c.UID, (E_CompressAdapter)msg.CompressAdapterType, tempDictTunnelID2Cfg);
InitListenerMode(_c.UID);
}
public void Recive_TunnelC2SConnect(Socket sk, byte[] reqData)
{
ServerManager.g_Log.Debug("Reverse->Recive_TunnelC2SConnect");
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
Protobuf_Tunnel_Connect msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_Connect>(reqData);
if (msg.Connected == 1)
OnRemoteLocalConnect(_c.UID,(byte)msg.TunnelID, (byte)msg.Idx);
else
OnRemoteLocalDisconnect(_c.UID, (byte)msg.TunnelID, (byte)msg.Idx);
}
public void Recive_TunnelC2SDisconnect(Socket sk, byte[] reqData)
{
ServerManager.g_Log.Debug("Reverse->Recive_TunnelC2CDisconnect");
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
Protobuf_Tunnel_Connect msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_Connect>(reqData);
OnRemoteLocalDisconnect(_c.UID,(byte)msg.TunnelID, (byte)msg.Idx);
}
public void Recive_TunnelC2SData(Socket sk, byte[] reqData)
{
ServerManager.g_Log.Debug("Reverse->Recive_TunnelC2SData");
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
Protobuf_Tunnel_DATA msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_DATA>(reqData);
OnRemoteLocalDataCallBack(_c.UID, (byte)msg.TunnelID, (byte)msg.Idx, msg.HunterNetCoreData.ToArray());
}
#endregion
// /// <summary>
// /// 初始化连接
// /// </summary>
// void InitListenerMode(long UID)
// {
// if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo _c))
// return;
#region
/// <summary>
/// 当客户端本地端口连接
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
public void OnLocalConnect(long UID, byte tunnelId, byte _Idx)
{
ServerManager.g_Log.Debug($"Reverse->OnLocalConnect {UID},{tunnelId},{_Idx}");
if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
return;
if (!GetLocalListener(UID, tunnelId, out ForwardLocalListener _listener))
return;
if (!client._cfgs.ContainsKey(tunnelId))
return;
// //初始化压缩适配器,代表压缩类型
// ServerManager.g_Log.Info("初始化压缩适配器" + _c.e_CompressAdapter);
// foreach (var cfg in _c._cfgs)
// {
// ForwardLocalListener listener = new ForwardLocalListener(256, 1024, cfg.Key, UID);
// ServerManager.g_Log.Info($"开始监听配置 Tunnel:{cfg.Key},Port:{cfg.Value.Port}");
// listener.BandEvent(ServerManager.g_Log.Log, OnLocalConnect, OnLocalDisconnect, OnTunnelDataCallBack);
// listener.StartListener((uint)cfg.Value.Port);
// AddLocalListener(UID,listener);
// }
// }
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_Connect()
{
TunnelID = tunnelId,
Idx = _Idx,
Connected = 1
});
// #region 连接字典管理
// /// <summary>
// /// 追加监听者
// /// </summary>
// /// <param name="tunnelId"></param>
// /// <param name="serverClient"></param>
// void AddLocalListener(long UID,ForwardLocalListener _listener)
// {
// long Key = GetCommKey(UID, _listener.mTunnelID);
// lock (mDictCommKey2LocalListeners)
// {
// mDictCommKey2LocalListeners[Key] = _listener;
// }
// }
// /// <summary>
// /// 删除监听
// /// </summary>
// /// <param name="tunnelId"></param>
// /// <param name="serverClient"></param>
// void RemoveLocalListener(long UID, ForwardLocalListener _listener)
// {
// long Key = GetCommKey(UID, _listener.mTunnelID);
// lock (mDictCommKey2LocalListeners)
// {
// if (mDictCommKey2LocalListeners.ContainsKey(Key))
// mDictCommKey2LocalListeners.Remove(Key);
// }
// }
// bool GetLocalListener(long UID, byte tunnelId, out ForwardLocalListener _listener)
// {
// long Key = GetCommKey(UID, tunnelId);
// _listener = null;
// if (!mDictCommKey2LocalListeners.ContainsKey(Key))
// return false;
//告知给服务端,来自客户端本地的连接建立
ServerManager.g_ClientMgr.ClientSend(client, (int)CommandID.CmdTunnelS2CReverseConnect, (int)ErrorCode.ErrorOk, respData);
}
/// <summary>
/// 当客户端本地端口连接断开
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
public void OnLocalDisconnect(long UID, byte tunnelId, byte _Idx)
{
ServerManager.g_Log.Debug($"Reverse->OnLocalDisconnect {UID},{tunnelId},{_Idx}");
if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
return;
if (!GetLocalListener(UID, tunnelId, out ForwardLocalListener _listener))
return;
if (!client._cfgs.ContainsKey(tunnelId))
return;
// _listener = mDictCommKey2LocalListeners[Key];
// return true;
// }
// public void StopAllByUid(long UID)
// {
// lock (mDictCommKey2LocalListeners)
// {
// long[] keys = mDictCommKey2LocalListeners.Keys.ToArray();
// for (int i = 0; i < keys.Length; i++)
// {
// ForwardLocalListener _listener = mDictCommKey2LocalListeners[keys[i]];
// if (_listener.mUid != UID)
// continue;
// _listener.StopAllLocalClient();
// _listener.StopWithClear();
// //_listener.Stop();
// RemoveLocalListener(UID,_listener);
// }
// //服务端得按用户分开
// //mDictCommKey2ServerLocalClients.Clear();
// }
// }
// #endregion
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_Disconnect()
{
TunnelID = tunnelId,
Idx = _Idx,
});
//告知给服务端,来自客户端本地的连接断开
ServerManager.g_ClientMgr.ClientSend(client, (int)CommandID.CmdTunnelS2CReverseDisconnect, (int)ErrorCode.ErrorOk, respData);
}
// #region 解析客户端下行数据
// public void Recive_CmdCfgs(Socket sk, byte[] reqData)
// {
// ServerManager.g_Log.Debug("Recive_CmdCfgs");
// ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
// Protobuf_Cfgs msg = ProtoBufHelper.DeSerizlize<Protobuf_Cfgs>(reqData);
/// <summary>
/// 当服务端本地端口连接
/// </summary>
/// <param name="tunnelId"></param>
public void OnRemoteLocalConnect(long UID, byte tunnelId, byte Idx)
{
ServerManager.g_Log.Debug($"Reverse->OnRemoteLocalConnect {UID},{tunnelId},{Idx}");
// Dictionary<byte, Protobuf_Cfgs_Single> tempDictTunnelID2Cfg = new Dictionary<byte, Protobuf_Cfgs_Single>();
// for (int i = 0; i < msg.Cfgs.Count; i++)
// {
// Protobuf_Cfgs_Single cfg = msg.Cfgs[i];
// tempDictTunnelID2Cfg[(byte)cfg.TunnelID] = cfg;
// }
// ServerManager.g_ClientMgr.SetUserCfg(_c.UID, (NoSugarNet.Adapter.DataHelper.E_CompressAdapter)msg.CompressAdapterType, tempDictTunnelID2Cfg);
// InitListenerMode(_c.UID);
// }
// public void Recive_TunnelS2CConnect(byte[] reqData)
// {
// ServerManager.g_Log.Debug("Recive_TunnelS2CConnect");
// Protobuf_Tunnel_Connect msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_Connect>(reqData);
// if (msg.Connected == 1)
// OnServerLocalConnect((byte)msg.TunnelID, (byte)msg.Idx);
// else
// OnServerLocalDisconnect((byte)msg.TunnelID, (byte)msg.Idx);
// }
// public void Recive_TunnelS2CDisconnect(byte[] reqData)
// {
// ServerManager.g_Log.Debug("Recive_TunnelS2CDisconnect");
// Protobuf_Tunnel_Disconnect msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_Disconnect>(reqData);
// OnServerLocalDisconnect((byte)msg.TunnelID, (byte)msg.Idx);
// }
// public void Recive_TunnelS2CData(byte[] reqData)
// {
// //ServerManager.g_Log.Debug("Recive_TunnelS2CData");
// Protobuf_Tunnel_DATA msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_DATA>(reqData);
// OnServerLocalDataCallBack((byte)msg.TunnelID, (byte)msg.Idx, msg.HunterNetCoreData.ToArray());
// }
// #endregion
if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
return;
if (!GetLocalListener(UID,tunnelId, out ForwardLocalListener _listener))
return;
// #region 两端本地端口连接事件通知
// /// <summary>
// /// 当客户端本地端口连接
// /// </summary>
// /// <param name="uid"></param>
// /// <param name="tunnelId"></param>
// public void OnLocalConnect(long UID, byte tunnelId, byte _Idx)
// {
// ServerManager.g_Log.Debug($"OnLocalConnect {UID},{tunnelId},{_Idx}");
//维护状态
_listener.SetRemoteConnectd(Idx, true);
if (_listener.GetDictMsgQueue(Idx, out List<IdxWithMsg> msglist))
{
for (int i = 0; i < msglist.Count; i++)
{
IdxWithMsg msg = msglist[i];
//投递给服务端,来自客户端本地的连接数据
ServerManager.g_ClientMgr.ClientSend(client, (int)CommandID.CmdTunnelS2CReverseData, (int)ErrorCode.ErrorOk, msg.data);
//发送后回收
LocalMsgQueuePool._localMsgPool.Enqueue(msg);
}
}
}
// if (!mDictTunnelID2Cfg.ContainsKey(tunnelId))
// return;
/// <summary>
/// 当服务端本地端口连接断开
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
public void OnRemoteLocalDisconnect(long UID, byte tunnelId, byte Idx)
{
ServerManager.g_Log.Debug($"Reverse->OnRemoteLocalDisconnect {UID},{tunnelId},{Idx}");
// byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_Connect()
// {
// TunnelID = tunnelId,
// Idx = _Idx,
// });
if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
return;
// //告知给服务端,来自客户端本地的连接建立
// AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardConnect, respData);
// }
// /// <summary>
// /// 当客户端本地端口连接断开
// /// </summary>
// /// <param name="uid"></param>
// /// <param name="tunnelId"></param>
// public void OnLocalDisconnect(long UID, byte tunnelId, byte _Idx)
// {
// AppNoSugarNet.log.Debug($"OnClientLocalDisconnect {tunnelId},{_Idx}");
// //隧道ID定位投递服务端本地连接
// if (!mDictTunnelID2Cfg.ContainsKey(tunnelId))
// return;
if (!GetLocalListener(UID, tunnelId, out ForwardLocalListener _listener))
return;
// byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_Disconnect()
// {
// TunnelID = tunnelId,
// Idx = _Idx,
// });
_listener.SetRemoteConnectd(Idx, false);
_listener.CloseConnectByIdx(Idx);
}
#endregion
// //告知给服务端,来自客户端本地的连接断开
// AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardDisconnect, respData);
// }
#region
/// <summary>
/// 来自服务端本地连接投递的Tunnel数据
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
/// <param name="data"></param>
public void OnRemoteLocalDataCallBack(long UID, byte tunnelId, byte Idx, byte[] data)
{
ServerManager.g_Log.Debug($"Reverse->OnRemoteLocalDataCallBack {UID},{tunnelId},{Idx},Data长度{data.Length}");
// /// <summary>
// /// 当服务端本地端口连接
// /// </summary>
// /// <param name="tunnelId"></param>
// public void OnRemoteLocalConnect(long UID, byte tunnelId, byte Idx)
// {
// AppNoSugarNet.log.Debug($"OnServerLocalConnect {tunnelId},{Idx}");
// if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener))
// return;
// //维护状态
// _listener.SetRemoteConnectd(Idx, true);
// if (_listener.GetDictMsgQueue(Idx, out List<IdxWithMsg> msglist))
// {
// for (int i = 0; i < msglist.Count; i++)
// {
// IdxWithMsg msg = msglist[i];
// //投递给服务端,来自客户端本地的连接数据
// AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardData, msg.data);
// //发送后回收
// LocalMsgQueuePool._localMsgPool.Enqueue(msg);
// }
// }
// }
// /// <summary>
// /// 当服务端本地端口连接断开
// /// </summary>
// /// <param name="uid"></param>
// /// <param name="tunnelId"></param>
// public void OnRemoteLocalDisconnect(long UID, byte tunnelId, byte Idx)
// {
// AppNoSugarNet.log.Debug($"OnServerLocalDisconnect {tunnelId},{Idx}");
// if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener))
// return;
// _listener.SetRemoteConnectd(Idx, false);
// _listener.CloseConnectByIdx(Idx);
// }
// #endregion
if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
return;
// #region 数据投递
// /// <summary>
// /// 来自服务端本地连接投递的Tunnel数据
// /// </summary>
// /// <param name="uid"></param>
// /// <param name="tunnelId"></param>
// /// <param name="data"></param>
// public void OnRemoteLocalDataCallBack(long UID, byte tunnelId, byte Idx, byte[] data)
// {
// //AppNoSugarNet.log.Info($"OnServerLocalDataCallBack {tunnelId},{Idx},Data长度{data.Length}");
// if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener))
// return;
// //记录压缩前数据长度
// tReciveAllLenght += data.Length;
// //解压
// data = mCompressAdapter.Decompress(data);
// _listener.SendSocketByIdx(Idx, data);
// }
// /// <summary>
// /// 来自客户端本地连接投递的Tunnel数据
// /// </summary>
// /// <param name="uid"></param>
// /// <param name="tunnelId"></param>
// /// <param name="data"></param>
// public void OnTunnelDataCallBack(long UID, byte tunnelId, byte Idx, byte[] data)
// {
// //AppNoSugarNet.log.Info($"OnClientTunnelDataCallBack {tunnelId},{Idx} data.Length->{data.Length}");
if (!GetLocalListener(UID, tunnelId, out ForwardLocalListener _listener))
return;
// int SlienLenght = 1000;
// //判断数据量大时分包
// if (data.Length > SlienLenght)
// {
// Span<byte> tempSpan = data;
// Span<byte> tempSpanSlien = null;
// int PageCount = (int)(data.Length / SlienLenght);
// if (data.Length % SlienLenght > 0)
// {
// PageCount++;
// }
//记录压缩前数据长度
tReciveAllLenght += data.Length;
//解压
data = CompressAdapterSelector.Adapter(client.e_CompressAdapter).Decompress(data);
_listener.SendSocketByIdx(Idx, data);
}
/// <summary>
/// 来自客户端本地连接投递的Tunnel数据
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
/// <param name="data"></param>
public void OnTunnelDataCallBack(long UID, byte tunnelId, byte Idx, byte[] data)
{
ServerManager.g_Log.Debug($"Reverse->OnTunnelDataCallBack {UID},{tunnelId},{Idx},Data长度{data.Length}");
// for (int i = 0; i < PageCount; i++)
// {
// int StartIdx = i * SlienLenght;
// if (i != PageCount - 1)//不是最后一个包
// tempSpanSlien = tempSpan.Slice(StartIdx, SlienLenght);
// else//最后一个
// tempSpanSlien = tempSpan.Slice(StartIdx);
int SlienLenght = 1000;
//判断数据量大时分包
if (data.Length > SlienLenght)
{
Span<byte> tempSpan = data;
Span<byte> tempSpanSlien = null;
int PageCount = (int)(data.Length / SlienLenght);
if (data.Length % SlienLenght > 0)
{
PageCount++;
}
// SendDataToRemote(tunnelId, Idx, tempSpanSlien.ToArray());
// }
// return;
// }
// SendDataToRemote(tunnelId, Idx, data);
// }
for (int i = 0; i < PageCount; i++)
{
int StartIdx = i * SlienLenght;
if (i != PageCount - 1)//不是最后一个包
tempSpanSlien = tempSpan.Slice(StartIdx, SlienLenght);
else//最后一个
tempSpanSlien = tempSpan.Slice(StartIdx);
// void SendDataToRemote(long UID, byte tunnelId, byte Idx, byte[] data)
// {
// //压缩
// data = mCompressAdapter.Compress(data);
// //记录压缩后数据长度
// tSendAllLenght += data.Length;
SendDataToRemote(UID,tunnelId, Idx, tempSpanSlien.ToArray());
}
return;
}
SendDataToRemote(UID, tunnelId, Idx, data);
}
// byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_DATA()
// {
// TunnelID = tunnelId,
// Idx = Idx,
// HunterNetCoreData = ByteString.CopyFrom(data)
// });
void SendDataToRemote(long UID, byte tunnelId, byte Idx, byte[] data)
{
ServerManager.g_Log.Debug($"Reverse->SendDataToRemote {UID},{tunnelId},{Idx},Data长度{data.Length}");
// if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener))
// return;
if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
return;
// //远程未连接,添加到缓存
// if (!_listener.CheckRemoteConnect(Idx))
// {
// _listener.EnqueueIdxWithMsg(Idx, respData);
// return;
// }
// //投递给服务端,来自客户端本地的连接数据
// AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardData, respData);
// }
// #endregion
// }
//}
if (!GetLocalListener(UID, tunnelId, out ForwardLocalListener _listener))
return;
//压缩
data = CompressAdapterSelector.Adapter(client.e_CompressAdapter).Compress(data);
//记录压缩后数据长度
tSendAllLenght += data.Length;
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_DATA()
{
TunnelID = tunnelId,
Idx = Idx,
HunterNetCoreData = ByteString.CopyFrom(data)
});
//远程未连接,添加到缓存
if (!_listener.CheckRemoteConnect(Idx))
{
_listener.EnqueueIdxWithMsg(Idx, respData);
return;
}
//投递给服务端,来自客户端本地的连接数据
ServerManager.g_ClientMgr.ClientSend(client, (int)CommandID.CmdTunnelS2CReverseData, (int)ErrorCode.ErrorOk, respData);
}
#endregion
}
}

View File

@ -14,6 +14,7 @@ namespace ServerCore.Manager
public static LoginManager g_Login;
public static ChatManager g_Chat;
public static ForwardLocalClientManager g_ForwardLocal;
public static ReverseLocalClientManager g_ReverseLocal;
public static IOCPNetWork g_SocketMgr;
public static System.Timers.Timer _SpeedCheckTimeTimer;//速度检测计时器
public static int TimerInterval = 1000;//计时器间隔
@ -35,6 +36,7 @@ namespace ServerCore.Manager
g_Login = new LoginManager();
g_Chat = new ChatManager();
g_ForwardLocal = new ForwardLocalClientManager((E_CompressAdapter)compressAdapterType);
g_ReverseLocal = new ReverseLocalClientManager();
//g_SocketMgr = new IOCPNetWork(1024, 1024);
g_SocketMgr = new IOCPNetWork(1024, 4096);