反向代理 功能调通

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

View File

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

View File

@ -187,7 +187,8 @@ namespace ServerCore.Manager
cinfo.IsOffline = true; cinfo.IsOffline = true;
cinfo.LogOutDT = DateTime.Now; 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) public void RemoveClientForSocket(Socket sk)

View File

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

View File

@ -1,324 +1,355 @@
//using AxibugProtobuf; using AxibugProtobuf;
//using Google.Protobuf; using Google.Protobuf;
//using NoSugarNet.Adapter; using NoSugarNet.Adapter;
//using NoSugarNet.Adapter.DataHelper; using NoSugarNet.Adapter.DataHelper;
//using NoSugarNet.ServerCore.Common; using NoSugarNet.ServerCore.Common;
//using ServerCore.Common; using ServerCore.Common;
//using ServerCore.NetWork; using ServerCore.NetWork;
//using System.Net.Sockets; using System.Net.Sockets;
//namespace ServerCore.Manager namespace ServerCore.Manager
//{ {
// public class ReverseLocalClientManager public class ReverseLocalClientManager
// { {
// Dictionary<long, ForwardLocalListener> mDictCommKey2LocalListeners = new Dictionary<long, ForwardLocalListener>(); Dictionary<long, ForwardLocalListener> mDictCommKey2LocalListeners = new Dictionary<long, ForwardLocalListener>();
// public long tReciveAllLenght { get; private set; } public long tReciveAllLenght { get; private set; }
// public long tSendAllLenght { get; private set; } public long tSendAllLenght { get; private set; }
// static long GetCommKey(long Uid, int Tunnel) static long GetCommKey(long Uid, int Tunnel)
// { {
// return (Uid * 10000000) + (Tunnel * 10000); return (Uid * 10000000) + (Tunnel * 10000);
// } }
// static long GetUidForCommKey(long CommKey) static long GetUidForCommKey(long CommKey)
// { {
// return CommKey / 10000000; 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() #region
// { public void Recive_CmdCfgs(Socket sk, byte[] reqData)
// //注册网络消息 {
// NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SReverseConnect, Recive_TunnelC2SConnect); ServerManager.g_Log.Debug("Reverse->Recive_CmdCfgs");
// NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SReverseDisconnect, Recive_TunnelC2SDisconnect); ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
// NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelC2SReverseData, Recive_TunnelC2SData); 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> #region
// /// 初始化连接 /// <summary>
// /// </summary> /// 当客户端本地端口连接
// void InitListenerMode(long UID) /// </summary>
// { /// <param name="uid"></param>
// if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo _c)) /// <param name="tunnelId"></param>
// return; 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;
// //初始化压缩适配器,代表压缩类型 byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_Connect()
// ServerManager.g_Log.Info("初始化压缩适配器" + _c.e_CompressAdapter); {
// foreach (var cfg in _c._cfgs) TunnelID = tunnelId,
// { Idx = _Idx,
// ForwardLocalListener listener = new ForwardLocalListener(256, 1024, cfg.Key, UID); Connected = 1
// 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> ServerManager.g_ClientMgr.ClientSend(client, (int)CommandID.CmdTunnelS2CReverseConnect, (int)ErrorCode.ErrorOk, respData);
// /// 追加监听者 }
// /// </summary> /// <summary>
// /// <param name="tunnelId"></param> /// 当客户端本地端口连接断开
// /// <param name="serverClient"></param> /// </summary>
// void AddLocalListener(long UID,ForwardLocalListener _listener) /// <param name="uid"></param>
// { /// <param name="tunnelId"></param>
// long Key = GetCommKey(UID, _listener.mTunnelID); public void OnLocalDisconnect(long UID, byte tunnelId, byte _Idx)
// lock (mDictCommKey2LocalListeners) {
// { ServerManager.g_Log.Debug($"Reverse->OnLocalDisconnect {UID},{tunnelId},{_Idx}");
// mDictCommKey2LocalListeners[Key] = _listener; if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
// } return;
// } if (!GetLocalListener(UID, tunnelId, out ForwardLocalListener _listener))
// /// <summary> return;
// /// 删除监听 if (!client._cfgs.ContainsKey(tunnelId))
// /// </summary> return;
// /// <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]; byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_Disconnect()
// return true; {
// } TunnelID = tunnelId,
// public void StopAllByUid(long UID) Idx = _Idx,
// { });
// 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
//告知给服务端,来自客户端本地的连接断开
ServerManager.g_ClientMgr.ClientSend(client, (int)CommandID.CmdTunnelS2CReverseDisconnect, (int)ErrorCode.ErrorOk, respData);
}
// #region 解析客户端下行数据 /// <summary>
// public void Recive_CmdCfgs(Socket sk, byte[] reqData) /// 当服务端本地端口连接
// { /// </summary>
// ServerManager.g_Log.Debug("Recive_CmdCfgs"); /// <param name="tunnelId"></param>
// ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk); public void OnRemoteLocalConnect(long UID, byte tunnelId, byte Idx)
// Protobuf_Cfgs msg = ProtoBufHelper.DeSerizlize<Protobuf_Cfgs>(reqData); {
ServerManager.g_Log.Debug($"Reverse->OnRemoteLocalConnect {UID},{tunnelId},{Idx}");
// Dictionary<byte, Protobuf_Cfgs_Single> tempDictTunnelID2Cfg = new Dictionary<byte, Protobuf_Cfgs_Single>(); if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
// for (int i = 0; i < msg.Cfgs.Count; i++) return;
// {
// 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 (!GetLocalListener(UID,tunnelId, out ForwardLocalListener _listener))
return;
// #region 两端本地端口连接事件通知 //维护状态
// /// <summary> _listener.SetRemoteConnectd(Idx, true);
// /// 当客户端本地端口连接 if (_listener.GetDictMsgQueue(Idx, out List<IdxWithMsg> msglist))
// /// </summary> {
// /// <param name="uid"></param> for (int i = 0; i < msglist.Count; i++)
// /// <param name="tunnelId"></param> {
// public void OnLocalConnect(long UID, byte tunnelId, byte _Idx) IdxWithMsg msg = msglist[i];
// { //投递给服务端,来自客户端本地的连接数据
// ServerManager.g_Log.Debug($"OnLocalConnect {UID},{tunnelId},{_Idx}"); ServerManager.g_ClientMgr.ClientSend(client, (int)CommandID.CmdTunnelS2CReverseData, (int)ErrorCode.ErrorOk, msg.data);
//发送后回收
LocalMsgQueuePool._localMsgPool.Enqueue(msg);
}
}
}
// if (!mDictTunnelID2Cfg.ContainsKey(tunnelId)) /// <summary>
// return; /// 当服务端本地端口连接断开
/// </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() if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
// { return;
// TunnelID = tunnelId,
// Idx = _Idx,
// });
// //告知给服务端,来自客户端本地的连接建立 if (!GetLocalListener(UID, tunnelId, out ForwardLocalListener _listener))
// AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardConnect, respData); return;
// }
// /// <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;
// byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_Disconnect() _listener.SetRemoteConnectd(Idx, false);
// { _listener.CloseConnectByIdx(Idx);
// TunnelID = tunnelId, }
// Idx = _Idx, #endregion
// });
// //告知给服务端,来自客户端本地的连接断开 #region
// AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardDisconnect, respData); /// <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> if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
// /// 当服务端本地端口连接 return;
// /// </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
// #region 数据投递 if (!GetLocalListener(UID, tunnelId, out ForwardLocalListener _listener))
// /// <summary> return;
// /// 来自服务端本地连接投递的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}");
// int SlienLenght = 1000; //记录压缩前数据长度
// //判断数据量大时分包 tReciveAllLenght += data.Length;
// if (data.Length > SlienLenght) //解压
// { data = CompressAdapterSelector.Adapter(client.e_CompressAdapter).Decompress(data);
// Span<byte> tempSpan = data; _listener.SendSocketByIdx(Idx, data);
// Span<byte> tempSpanSlien = null; }
// int PageCount = (int)(data.Length / SlienLenght); /// <summary>
// if (data.Length % SlienLenght > 0) /// 来自客户端本地连接投递的Tunnel数据
// { /// </summary>
// PageCount++; /// <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 SlienLenght = 1000;
// { //判断数据量大时分包
// int StartIdx = i * SlienLenght; if (data.Length > SlienLenght)
// if (i != PageCount - 1)//不是最后一个包 {
// tempSpanSlien = tempSpan.Slice(StartIdx, SlienLenght); Span<byte> tempSpan = data;
// else//最后一个 Span<byte> tempSpanSlien = null;
// tempSpanSlien = tempSpan.Slice(StartIdx); int PageCount = (int)(data.Length / SlienLenght);
if (data.Length % SlienLenght > 0)
{
PageCount++;
}
// SendDataToRemote(tunnelId, Idx, tempSpanSlien.ToArray()); for (int i = 0; i < PageCount; i++)
// } {
// return; int StartIdx = i * SlienLenght;
// } if (i != PageCount - 1)//不是最后一个包
// SendDataToRemote(tunnelId, Idx, data); tempSpanSlien = tempSpan.Slice(StartIdx, SlienLenght);
// } else//最后一个
tempSpanSlien = tempSpan.Slice(StartIdx);
// void SendDataToRemote(long UID, byte tunnelId, byte Idx, byte[] data) SendDataToRemote(UID,tunnelId, Idx, tempSpanSlien.ToArray());
// { }
// //压缩 return;
// data = mCompressAdapter.Compress(data); }
// //记录压缩后数据长度 SendDataToRemote(UID, tunnelId, Idx, data);
// tSendAllLenght += data.Length; }
// byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_DATA() void SendDataToRemote(long UID, byte tunnelId, byte Idx, byte[] data)
// { {
// TunnelID = tunnelId, ServerManager.g_Log.Debug($"Reverse->SendDataToRemote {UID},{tunnelId},{Idx},Data长度{data.Length}");
// Idx = Idx,
// HunterNetCoreData = ByteString.CopyFrom(data)
// });
// if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener)) if (!ServerManager.g_ClientMgr.GetClientByUID(UID, out ClientInfo client))
// return; return;
// //远程未连接,添加到缓存 if (!GetLocalListener(UID, tunnelId, out ForwardLocalListener _listener))
// if (!_listener.CheckRemoteConnect(Idx)) return;
// {
// _listener.EnqueueIdxWithMsg(Idx, respData); //压缩
// return; data = CompressAdapterSelector.Adapter(client.e_CompressAdapter).Compress(data);
// } //记录压缩后数据长度
// //投递给服务端,来自客户端本地的连接数据 tSendAllLenght += data.Length;
// AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardData, respData);
// } byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_DATA()
// #endregion {
// } 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 LoginManager g_Login;
public static ChatManager g_Chat; public static ChatManager g_Chat;
public static ForwardLocalClientManager g_ForwardLocal; public static ForwardLocalClientManager g_ForwardLocal;
public static ReverseLocalClientManager g_ReverseLocal;
public static IOCPNetWork g_SocketMgr; public static IOCPNetWork g_SocketMgr;
public static System.Timers.Timer _SpeedCheckTimeTimer;//速度检测计时器 public static System.Timers.Timer _SpeedCheckTimeTimer;//速度检测计时器
public static int TimerInterval = 1000;//计时器间隔 public static int TimerInterval = 1000;//计时器间隔
@ -35,6 +36,7 @@ namespace ServerCore.Manager
g_Login = new LoginManager(); g_Login = new LoginManager();
g_Chat = new ChatManager(); g_Chat = new ChatManager();
g_ForwardLocal = new ForwardLocalClientManager((E_CompressAdapter)compressAdapterType); g_ForwardLocal = new ForwardLocalClientManager((E_CompressAdapter)compressAdapterType);
g_ReverseLocal = new ReverseLocalClientManager();
//g_SocketMgr = new IOCPNetWork(1024, 1024); //g_SocketMgr = new IOCPNetWork(1024, 1024);
g_SocketMgr = new IOCPNetWork(1024, 4096); g_SocketMgr = new IOCPNetWork(1024, 4096);