NoSugarNet/NoSugarNet.ClientCore/Manager/AppForwardLocalClient.cs

351 lines
14 KiB
C#
Raw Normal View History

2024-01-19 21:35:05 +08:00
using AxibugProtobuf;
using Google.Protobuf;
2024-01-22 11:56:02 +08:00
using HaoYueNet.ServerNetwork;
2024-06-25 13:18:33 +08:00
using NoSugarNet.Adapter;
using NoSugarNet.Adapter.DataHelper;
2024-01-19 21:35:05 +08:00
using NoSugarNet.ClientCore;
2024-01-22 11:56:02 +08:00
using NoSugarNet.ClientCore.Common;
using NoSugarNet.ClientCore.Network;
2024-01-25 17:17:16 +08:00
using NoSugarNet.DataHelper;
2024-01-19 21:35:05 +08:00
using System.Net;
namespace ServerCore.Manager
{
2024-06-25 14:35:44 +08:00
public class AppForwardLocalClient
2024-01-19 21:35:05 +08:00
{
Dictionary<byte, Protobuf_Cfgs_Single> mDictTunnelID2Cfg = new Dictionary<byte, Protobuf_Cfgs_Single>();
2024-06-25 13:18:33 +08:00
Dictionary<byte, ForwardLocalListener> mDictTunnelID2Listeners = new Dictionary<byte, ForwardLocalListener>();
2024-06-26 14:10:05 +08:00
//NoSugarNet.Adapter.DataHelper.CompressAdapter mCompressAdapter;
2024-06-25 13:18:33 +08:00
NoSugarNet.Adapter.DataHelper.E_CompressAdapter compressAdapterType;
//public LocalMsgQueuePool _localMsgPool = new LocalMsgQueuePool(1000);
2024-01-19 21:35:05 +08:00
2024-04-08 16:20:37 +08:00
public long tReciveAllLenght { get; private set; }
public long tSendAllLenght { get; private set; }
2024-06-25 14:35:44 +08:00
public AppForwardLocalClient()
2024-01-19 21:35:05 +08:00
{
//注册网络消息
2024-06-25 18:23:08 +08:00
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdServerCfgs, Recive_CmdCfgs);
2024-06-25 14:35:44 +08:00
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelS2CForwardConnect, Recive_TunnelS2CConnect);
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelS2CForwardDisconnect, Recive_TunnelS2CDisconnect);
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTunnelS2CForwardData, Recive_TunnelS2CData);
2024-01-19 21:35:05 +08:00
}
public void GetCurrLenght(out long resultReciveAllLenght, out long resultSendAllLenght)
{
resultReciveAllLenght = 0;
resultSendAllLenght = 0;
byte[] Keys = mDictTunnelID2Listeners.Keys.ToArray();
for (int i = 0; i < Keys.Length; i++)
{
2024-04-08 16:20:37 +08:00
//local和转发 收发相反
resultSendAllLenght += mDictTunnelID2Listeners[Keys[i]].mReciveAllLenght;
resultReciveAllLenght += mDictTunnelID2Listeners[Keys[i]].mSendAllLenght;
}
}
2024-04-08 16:20:37 +08:00
public void GetClientCount(out int ClientUserCount, out int TunnelCount)
{
TunnelCount = mDictTunnelID2Listeners.Count;
ClientUserCount = mDictTunnelID2Listeners.Count;
}
2024-06-21 17:17:32 +08:00
public void GetClientDebugInfo()
{
AppNoSugarNet.log.Debug($"------------ mDictTunnelID2Listeners {mDictTunnelID2Listeners.Count} ------------");
lock (mDictTunnelID2Listeners)
{
foreach (var item in mDictTunnelID2Listeners)
{
var cinfo = item.Value.GetDictIdx2LocalClientInfo();
AppNoSugarNet.log.Debug($"----- TunnelID {item.Key} ObjcurrSeed->{item.Value.currSeed} ClientList->{item.Value.ClientList.Count} Idx2LocalClient->{cinfo.Count} -----");
foreach (var c in cinfo)
{
AppNoSugarNet.log.Debug($"----- Idx {c.Key} bRemoteConnect->{c.Value.bRemoteConnect} msgQueue.Count->{c.Value.msgQueue.Count} -----");
}
}
}
}
2024-01-19 21:35:05 +08:00
/// <summary>
/// 初始化连接,先获取到配置
/// </summary>
void InitListenerMode()
{
2024-04-15 15:16:10 +08:00
AppNoSugarNet.log.Info("初始化压缩适配器" + compressAdapterType);
2024-06-26 14:10:05 +08:00
////初始化压缩适配器,代表压缩类型
//mCompressAdapter = new NoSugarNet.Adapter.DataHelper.CompressAdapter(compressAdapterType);
2024-01-19 21:35:05 +08:00
foreach (var cfg in mDictTunnelID2Cfg)
{
2024-06-26 10:41:16 +08:00
ForwardLocalListener listener = new ForwardLocalListener(256, 1024, cfg.Key,AppNoSugarNet.user.userdata.UID);
2024-04-15 15:16:10 +08:00
AppNoSugarNet.log.Info($"开始监听配置 Tunnel:{cfg.Key},Port:{cfg.Value.Port}");
2024-06-25 13:18:33 +08:00
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));
2024-03-29 18:02:54 +08:00
//listener.Init((int)cfg.Value.Port);
2024-01-19 21:35:05 +08:00
AddLocalListener(listener);
}
2024-01-19 21:35:05 +08:00
}
#region
/// <summary>
/// 追加监听者
/// </summary>
/// <param name="tunnelId"></param>
/// <param name="serverClient"></param>
2024-06-25 13:18:33 +08:00
void AddLocalListener(ForwardLocalListener _listener)
2024-01-19 21:35:05 +08:00
{
lock (mDictTunnelID2Listeners)
{
mDictTunnelID2Listeners[_listener.mTunnelID] = _listener;
}
}
/// <summary>
/// 删除监听
/// </summary>
/// <param name="tunnelId"></param>
/// <param name="serverClient"></param>
2024-06-25 13:18:33 +08:00
void RemoveLocalListener(ForwardLocalListener _listener)
2024-01-19 21:35:05 +08:00
{
lock (mDictTunnelID2Listeners)
{
if (mDictTunnelID2Listeners.ContainsKey(_listener.mTunnelID))
{
2024-01-22 11:56:02 +08:00
mDictTunnelID2Listeners.Remove(_listener.mTunnelID);
2024-01-19 21:35:05 +08:00
}
}
}
2024-06-25 13:18:33 +08:00
bool GetLocalListener(byte tunnelId,out ForwardLocalListener _listener)
2024-01-19 21:35:05 +08:00
{
_listener = null;
if (!mDictTunnelID2Listeners.ContainsKey(tunnelId))
return false;
_listener = mDictTunnelID2Listeners[tunnelId];
return true;
}
2024-01-22 11:56:02 +08:00
public void StopAll()
{
lock (mDictTunnelID2Listeners)
{
byte[] keys = mDictTunnelID2Listeners.Keys.ToArray();
for (int i = 0; i < keys.Length; i++)
{
2024-06-25 13:18:33 +08:00
ForwardLocalListener _listener = mDictTunnelID2Listeners[keys[i]];
2024-06-21 17:17:32 +08:00
_listener.StopAllLocalClient();
2024-06-25 13:18:33 +08:00
_listener.StopWithClear();
2024-03-29 18:02:54 +08:00
//_listener.Stop();
2024-01-22 11:56:02 +08:00
RemoveLocalListener(_listener);
}
2024-06-21 17:17:32 +08:00
mDictTunnelID2Listeners.Clear();
2024-01-22 11:56:02 +08:00
}
}
2024-01-19 21:35:05 +08:00
#endregion
#region
public void Recive_CmdCfgs(byte[] reqData)
{
2024-01-22 11:56:02 +08:00
AppNoSugarNet.log.Debug("Recive_CmdCfgs");
2024-01-19 21:35:05 +08:00
Protobuf_Cfgs msg = ProtoBufHelper.DeSerizlize<Protobuf_Cfgs>(reqData);
2024-01-22 15:08:43 +08:00
for (int i = 0;i < msg.Cfgs.Count;i++)
2024-01-19 21:35:05 +08:00
{
Protobuf_Cfgs_Single cfg = msg.Cfgs[i];
mDictTunnelID2Cfg[(byte)cfg.TunnelID] = cfg;
}
2024-06-25 13:18:33 +08:00
compressAdapterType = (NoSugarNet.Adapter.DataHelper.E_CompressAdapter)msg.CompressAdapterType;
2024-01-19 21:35:05 +08:00
InitListenerMode();
}
public void Recive_TunnelS2CConnect(byte[] reqData)
{
2024-01-22 11:56:02 +08:00
AppNoSugarNet.log.Debug("Recive_TunnelS2CConnect");
2024-06-26 10:41:16 +08:00
Protobuf_Tunnel_Connect msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_Connect>(reqData);
2024-01-22 15:08:43 +08:00
if(msg.Connected == 1)
OnServerLocalConnect((byte)msg.TunnelID,(byte)msg.Idx);
else
OnServerLocalDisconnect((byte)msg.TunnelID, (byte)msg.Idx);
2024-01-19 21:35:05 +08:00
}
public void Recive_TunnelS2CDisconnect(byte[] reqData)
{
2024-01-22 11:56:02 +08:00
AppNoSugarNet.log.Debug("Recive_TunnelS2CDisconnect");
2024-06-26 10:41:16 +08:00
Protobuf_Tunnel_Disconnect msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_Disconnect>(reqData);
2024-01-19 21:35:05 +08:00
OnServerLocalDisconnect((byte)msg.TunnelID,(byte)msg.Idx);
}
public void Recive_TunnelS2CData(byte[] reqData)
{
//AppNoSugarNet.log.Debug("Recive_TunnelS2CData");
2024-06-26 10:41:16 +08:00
Protobuf_Tunnel_DATA msg = ProtoBufHelper.DeSerizlize<Protobuf_Tunnel_DATA>(reqData);
2024-01-19 21:35:05 +08:00
OnServerLocalDataCallBack((byte)msg.TunnelID,(byte)msg.Idx, msg.HunterNetCoreData.ToArray());
}
#endregion
#region
/// <summary>
/// 当客户端本地端口连接
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
2024-06-26 10:41:16 +08:00
public void OnClientLocalConnect(long UID, byte tunnelId,byte _Idx)
2024-01-19 21:35:05 +08:00
{
2024-01-22 15:08:43 +08:00
AppNoSugarNet.log.Debug($"OnClientLocalConnect {tunnelId},{_Idx}");
2024-01-19 21:35:05 +08:00
if (!mDictTunnelID2Cfg.ContainsKey(tunnelId))
return;
2024-06-26 10:41:16 +08:00
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_Connect()
2024-01-19 21:35:05 +08:00
{
TunnelID = tunnelId,
Idx = _Idx,
});
//告知给服务端,来自客户端本地的连接建立
2024-06-25 14:35:44 +08:00
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardConnect, respData);
2024-01-19 21:35:05 +08:00
}
/// <summary>
/// 当客户端本地端口连接断开
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
2024-06-26 10:41:16 +08:00
public void OnClientLocalDisconnect(long UID, byte tunnelId, byte _Idx)
2024-01-19 21:35:05 +08:00
{
2024-01-22 15:08:43 +08:00
AppNoSugarNet.log.Debug($"OnClientLocalDisconnect {tunnelId},{_Idx}");
2024-01-19 21:35:05 +08:00
//隧道ID定位投递服务端本地连接
if (!mDictTunnelID2Cfg.ContainsKey(tunnelId))
return;
2024-06-26 10:41:16 +08:00
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_Disconnect()
2024-01-19 21:35:05 +08:00
{
TunnelID = tunnelId,
Idx= _Idx,
});
2024-01-22 15:08:43 +08:00
2024-01-19 21:35:05 +08:00
//告知给服务端,来自客户端本地的连接断开
2024-06-25 14:35:44 +08:00
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardDisconnect, respData);
2024-01-19 21:35:05 +08:00
}
2024-01-19 21:35:05 +08:00
/// <summary>
/// 当服务端本地端口连接
/// </summary>
/// <param name="tunnelId"></param>
public void OnServerLocalConnect(byte tunnelId,byte Idx)
{
2024-01-22 15:08:43 +08:00
AppNoSugarNet.log.Debug($"OnServerLocalConnect {tunnelId},{Idx}");
2024-06-25 13:18:33 +08:00
if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener))
2024-01-19 21:35:05 +08:00
return;
2024-01-22 11:56:02 +08:00
//维护状态
_listener.SetRemoteConnectd(Idx, true);
if (_listener.GetDictMsgQueue(Idx, out List<IdxWithMsg> msglist))
{
for(int i = 0; i < msglist.Count; i++)
{
2024-01-22 15:08:43 +08:00
IdxWithMsg msg = msglist[i];
2024-01-22 11:56:02 +08:00
//投递给服务端,来自客户端本地的连接数据
2024-06-25 14:35:44 +08:00
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardData, msg.data);
2024-01-22 15:08:43 +08:00
//发送后回收
2024-06-25 13:18:33 +08:00
LocalMsgQueuePool._localMsgPool.Enqueue(msg);
2024-01-22 11:56:02 +08:00
}
}
2024-01-19 21:35:05 +08:00
}
/// <summary>
/// 当服务端本地端口连接断开
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
public void OnServerLocalDisconnect(byte tunnelId, byte Idx)
{
2024-01-22 15:08:43 +08:00
AppNoSugarNet.log.Debug($"OnServerLocalDisconnect {tunnelId},{Idx}");
2024-06-25 13:18:33 +08:00
if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener))
2024-01-19 21:35:05 +08:00
return;
2024-01-22 11:56:02 +08:00
_listener.SetRemoteConnectd(Idx,false);
2024-01-19 21:35:05 +08:00
_listener.CloseConnectByIdx(Idx);
}
#endregion
#region
/// <summary>
/// 来自服务端本地连接投递的Tunnel数据
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
/// <param name="data"></param>
public void OnServerLocalDataCallBack(byte tunnelId,byte Idx, byte[] data)
{
2024-04-15 15:16:10 +08:00
//AppNoSugarNet.log.Info($"OnServerLocalDataCallBack {tunnelId},{Idx},Data长度{data.Length}");
2024-06-25 13:18:33 +08:00
if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener))
2024-01-19 21:35:05 +08:00
return;
2024-04-08 16:20:37 +08:00
//记录压缩前数据长度
tReciveAllLenght += data.Length;
2024-01-19 21:35:05 +08:00
//解压
2024-06-26 14:10:05 +08:00
data = CompressAdapterSelector.Adapter(compressAdapterType).Decompress(data);
2024-01-19 21:35:05 +08:00
_listener.SendSocketByIdx(Idx,data);
}
/// <summary>
/// 来自客户端本地连接投递的Tunnel数据
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
/// <param name="data"></param>
2024-06-26 10:41:16 +08:00
public void OnClientTunnelDataCallBack(long UID, byte tunnelId,byte Idx, byte[] data)
2024-01-19 21:35:05 +08:00
{
2024-06-21 17:17:32 +08:00
//AppNoSugarNet.log.Info($"OnClientTunnelDataCallBack {tunnelId},{Idx} data.Length->{data.Length}");
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++;
}
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);
SendDataToRemote(tunnelId, Idx, tempSpanSlien.ToArray());
}
return;
}
SendDataToRemote(tunnelId, Idx, data);
}
void SendDataToRemote(byte tunnelId, byte Idx, byte[] data)
{
2024-01-19 21:35:05 +08:00
//压缩
2024-06-26 14:10:05 +08:00
data = CompressAdapterSelector.Adapter(compressAdapterType).Compress(data);
2024-04-08 16:20:37 +08:00
//记录压缩后数据长度
tSendAllLenght += data.Length;
2024-06-26 10:41:16 +08:00
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_Tunnel_DATA()
2024-01-19 21:35:05 +08:00
{
TunnelID = tunnelId,
Idx = Idx,
HunterNetCoreData = ByteString.CopyFrom(data)
});
2024-06-25 13:18:33 +08:00
if (!GetLocalListener(tunnelId, out ForwardLocalListener _listener))
2024-01-22 11:56:02 +08:00
return;
//远程未连接,添加到缓存
if (!_listener.CheckRemoteConnect(Idx))
{
_listener.EnqueueIdxWithMsg(Idx, respData);
return;
}
2024-01-19 21:35:05 +08:00
//投递给服务端,来自客户端本地的连接数据
2024-06-25 14:35:44 +08:00
AppNoSugarNet.networkHelper.SendToServer((int)CommandID.CmdTunnelC2SForwardData, respData);
2024-01-19 21:35:05 +08:00
}
#endregion
2024-01-19 21:35:05 +08:00
}
}