This commit is contained in:
sin365 2024-01-19 21:34:08 +08:00
parent 2cd5aa4383
commit a5cc4b0521
8 changed files with 1593 additions and 238 deletions

Binary file not shown.

Binary file not shown.

View File

@ -11,6 +11,7 @@ namespace ServerCore.Manager
public Socket _socket { get; set; }
public bool IsOffline { get; set; } = false;
public DateTime LogOutDT { get; set; }
}
public class ClientManager
@ -64,7 +65,6 @@ namespace ServerCore.Manager
public ClientInfo JoinNewClient(Protobuf_Login data, Socket _socket)
{
//也许这个函数需加lock
ClientInfo cinfo = GetClientForSocket(_socket);
//如果连接还在
if (cinfo != null)

View File

@ -1,5 +1,5 @@
using AxibugProtobuf;
using ClientCore.Network;
using NoSugarNet.ClientCore.Network;
using Google.Protobuf;
using NoSugarNet.ServerCore.Common;
using ServerCore.Common;
@ -17,9 +17,14 @@ namespace ServerCore.Manager
}
Dictionary<byte, TunnelClientData> mDictTunnelID2Cfg = new Dictionary<byte, TunnelClientData>();
Dictionary<long, Dictionary<byte, ServerLocalClient>> mDictUid2ServerLocalClients = new Dictionary<long, Dictionary<byte, ServerLocalClient>>();
Dictionary<long, ServerLocalClient> mDictCommKey2ServerLocalClients = new Dictionary<long, ServerLocalClient>();
CompressAdapter mCompressAdapter;
static long GetCommKey(long Uid, int Tunnel, int Idx)
{
return (Uid * 10000000) + (Tunnel * 10000) + Idx;
}
public LocalClientManager()
{
//初始化压缩适配器暂时使用0代表压缩类型
@ -37,84 +42,78 @@ namespace ServerCore.Manager
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
/// <param name="serverClient"></param>
void AddServerLocalClient(long uid, byte tunnelId, ServerLocalClient serverClient)
void AddServerLocalClient(long uid, byte tunnelId,byte Idx, ServerLocalClient serverClient)
{
lock (mDictUid2ServerLocalClients)
long CommKey = GetCommKey(uid, tunnelId, Idx);
lock (mDictCommKey2ServerLocalClients)
{
if (!mDictUid2ServerLocalClients.ContainsKey(uid))
mDictUid2ServerLocalClients[uid] = new Dictionary<byte, ServerLocalClient>();
mDictUid2ServerLocalClients[uid][tunnelId] = serverClient;
mDictCommKey2ServerLocalClients[CommKey] = serverClient;
}
}
/// <summary>
/// 删除连接
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
void RemoveServerLocalClient(long uid, byte tunnelId)
void RemoveServerLocalClient(long uid, byte tunnelId, byte Idx)
{
lock (mDictUid2ServerLocalClients)
lock (mDictCommKey2ServerLocalClients)
{
if (!mDictUid2ServerLocalClients.ContainsKey(uid))
long CommKey = GetCommKey(uid, tunnelId, Idx);
if (!mDictCommKey2ServerLocalClients.ContainsKey(CommKey))
return;
if (!mDictUid2ServerLocalClients[uid].ContainsKey(tunnelId))
return;
mDictUid2ServerLocalClients[uid].Remove(tunnelId);
if (mDictUid2ServerLocalClients[uid].Count < 1)
mDictUid2ServerLocalClients.Remove(uid);
mDictCommKey2ServerLocalClients.Remove(CommKey);
}
}
bool GetServerLocalClient(long uid, byte tunnelId,out ServerLocalClient serverLocalClient)
bool GetServerLocalClient(long uid, byte tunnelId, byte Idx,out ServerLocalClient serverLocalClient)
{
serverLocalClient = null;
if (!mDictUid2ServerLocalClients.ContainsKey(uid))
long CommKey = GetCommKey(uid, tunnelId, Idx);
if (!mDictCommKey2ServerLocalClients.ContainsKey(CommKey))
return false;
if (!mDictUid2ServerLocalClients[uid].ContainsKey(tunnelId))
return false;
serverLocalClient = mDictUid2ServerLocalClients[uid][tunnelId];
serverLocalClient = mDictCommKey2ServerLocalClients[CommKey];
return true;
}
#endregion
#region
public void Recive_TunnelC2SConnect(Socket sk, byte[] reqData)
{
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
ServerManager.g_Log.Debug("OnTunnelC2SConnect");
Protobuf_C2S_Connect msg = ProtoBufHelper.DeSerizlize<Protobuf_C2S_Connect>(reqData);
OnClientLocalConnect(_c.UID, (byte)msg.TunnelID);
OnClientLocalConnect(_c.UID, (byte)msg.TunnelID, (byte)msg.Idx);
}
public void Recive_TunnelC2SDisconnect(Socket sk, byte[] reqData)
{
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
ServerManager.g_Log.Debug("Recive_TunnelC2SDisconnect");
Protobuf_C2S_Disconnect msg = ProtoBufHelper.DeSerizlize<Protobuf_C2S_Disconnect>(reqData);
OnClientLocalDisconnect(_c.UID, (byte)msg.TunnelID);
OnClientLocalDisconnect(_c.UID, (byte)msg.TunnelID,(byte)msg.Idx);
}
public void Recive_TunnelC2SData(Socket sk, byte[] reqData)
{
ClientInfo _c = ServerManager.g_ClientMgr.GetClientForSocket(sk);
ServerManager.g_Log.Debug("OnTunnelC2SData");
Protobuf_C2S_DATA msg = ProtoBufHelper.DeSerizlize<Protobuf_C2S_DATA>(reqData);
OnClientTunnelDataCallBack(_c.UID, (byte)msg.TunnelID, msg.HunterNetCoreData.ToArray());
OnClientTunnelDataCallBack(_c.UID, (byte)msg.TunnelID, (byte)msg.Idx, msg.HunterNetCoreData.ToArray());
}
#endregion
#region
/// <summary>
/// 当客户端本地端口连接
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
void OnClientLocalConnect(long uid, byte tunnelId)
void OnClientLocalConnect(long uid, byte tunnelId,int Idx)
{
if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client))
return;
@ -127,7 +126,7 @@ namespace ServerCore.Manager
{
//服务器本地局域网连接指定端口
TunnelClientData tunnelDataCfg = mDictTunnelID2Cfg[tunnelId];
ServerLocalClient serverLocalClient = new ServerLocalClient(tunnelId);
ServerLocalClient serverLocalClient = new ServerLocalClient(tunnelId, (byte)Idx);
//连接成功
if (!serverLocalClient.Init(tunnelDataCfg.IP, tunnelDataCfg.Port))
{
@ -142,17 +141,17 @@ namespace ServerCore.Manager
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
void OnClientLocalDisconnect(long uid, byte tunnelId)
void OnClientLocalDisconnect(long uid, byte tunnelId,byte Idx)
{
if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client))
return;
//隧道ID定位投递服务端本地连接
if (!GetServerLocalClient(uid, tunnelId, out ServerLocalClient serverLocalClient))
if (!GetServerLocalClient(uid, tunnelId, Idx, out ServerLocalClient serverLocalClient))
return;
//清楚服务器数据
RemoveServerLocalClient(uid, tunnelId);
RemoveServerLocalClient(uid, tunnelId, Idx);
//断开服务端本地客户端连接
serverLocalClient.CloseConntect();
}
@ -161,17 +160,18 @@ namespace ServerCore.Manager
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
public void OnServerLocalConnect(long uid, byte tunnelId, ServerLocalClient serverLocalClient)
public void OnServerLocalConnect(long uid, byte tunnelId, byte Idx, ServerLocalClient serverLocalClient)
{
if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client))
return;
//添加到服务端本地连接列表
AddServerLocalClient(uid, tunnelId, serverLocalClient);
AddServerLocalClient(uid, tunnelId, Idx, serverLocalClient);
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_S2C_Connect()
{
TunnelID = tunnelId,
Idx = Idx,
});
//发送给客户端,指定服务端本地端口已连接
ServerManager.g_ClientMgr.ClientSend(client, (int)CommandID.CmdTunnelS2CConnect, (int)ErrorCode.ErrorOk, respData);
@ -181,16 +181,17 @@ namespace ServerCore.Manager
/// </summary>
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
public void OnServerLocalDisconnect(long uid, byte tunnelId, ServerLocalClient serverLocalClient)
public void OnServerLocalDisconnect(long uid, byte tunnelId, byte Idx, ServerLocalClient serverLocalClient)
{
if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client))
return;
//添加到服务端本地连接列表
RemoveServerLocalClient(uid, tunnelId);
RemoveServerLocalClient(uid, tunnelId, Idx);
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_S2C_Disconnect()
{
TunnelID = tunnelId,
Idx= Idx,
});
//发送给客户端,指定服务端本地端口连接已断开
ServerManager.g_ClientMgr.ClientSend(client, (int)CommandID.CmdTunnelS2CDisconnect, (int)ErrorCode.ErrorOk, respData);
@ -204,7 +205,7 @@ namespace ServerCore.Manager
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
/// <param name="data"></param>
public void OnServerLocalDataCallBack(long uid, byte tunnelId, byte[] data)
public void OnServerLocalDataCallBack(long uid, byte tunnelId,byte Idx, byte[] data)
{
if (!ServerManager.g_ClientMgr.GetClientByUID(uid, out ClientInfo client))
return;
@ -214,6 +215,7 @@ namespace ServerCore.Manager
byte[] respData = ProtoBufHelper.Serizlize(new Protobuf_C2S_DATA()
{
TunnelID = tunnelId,
Idx = Idx,
HunterNetCoreData = ByteString.CopyFrom(data)
});
@ -226,10 +228,10 @@ namespace ServerCore.Manager
/// <param name="uid"></param>
/// <param name="tunnelId"></param>
/// <param name="data"></param>
public void OnClientTunnelDataCallBack(long uid, byte tunnelId, byte[] data)
public void OnClientTunnelDataCallBack(long uid, byte tunnelId, byte Idx, byte[] data)
{
//隧道ID定位投递服务端本地连接
if (!GetServerLocalClient(uid, tunnelId, out ServerLocalClient serverLocalClient))
if (!GetServerLocalClient(uid, tunnelId, Idx, out ServerLocalClient serverLocalClient))
return;
//解压

View File

@ -1,7 +1,7 @@
using HaoYueNet.ClientNetwork;
using HaoYueNet.ClientNetwork.OtherMode;
using ServerCore.Manager;
namespace ClientCore.Network
namespace NoSugarNet.ClientCore.Network
{
/// <summary>
/// 继承网络库,以支持网络功能
@ -10,9 +10,11 @@ namespace ClientCore.Network
{
public long mUID;
public byte mTunnelID;
public ServerLocalClient(byte TunnelID)
public byte mIdx;
public ServerLocalClient(byte TunnelID, byte Idx)
{
mTunnelID = TunnelID;
mIdx = Idx;
//指定接收服务器数据事件
OnReceiveData += GetDataCallBack;
//断开连接
@ -27,7 +29,7 @@ namespace ClientCore.Network
NetworkDeBugLog($"NetworkConnected:{IsConnect}");
if (IsConnect)
{
ServerManager.g_Local.OnServerLocalConnect(mUID, mTunnelID,this);
ServerManager.g_Local.OnServerLocalConnect(mUID, mTunnelID, mIdx, this);
}
else
{
@ -55,7 +57,7 @@ namespace ClientCore.Network
try
{
//抛出网络数据
ServerManager.g_Local.OnServerLocalDataCallBack(mUID, mTunnelID, data);
ServerManager.g_Local.OnServerLocalDataCallBack(mUID, mTunnelID, mIdx, data);
}
catch (Exception ex)
{
@ -70,7 +72,7 @@ namespace ClientCore.Network
public void OnConnectClose()
{
NetworkDeBugLog("OnConnectClose");
ServerManager.g_Local.OnServerLocalDisconnect(mUID, mTunnelID,this);
ServerManager.g_Local.OnServerLocalDisconnect(mUID, mTunnelID,mIdx,this);
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,11 @@ enum CommandID
{
CMD_DEFAUL = 0;//使
CMD_LOGIN = 2001; // | Protobuf_Login | Protobuf_Login_RESP
CMD_CFGS = 3001; // Protobuf_Cfgs
CMD_CHATMSG = 4001; //广 | Protobuf_ChatMsg | Protobuf_ChatMsg_RESP
CMD_TUNNEL_C2S_CONNECT = 5000; // Protobuf_C2S_Connect
@ -66,6 +69,18 @@ message Protobuf_Login_RESP
LoginResultStatus Status = 4;// [1][0]
}
//
message Protobuf_Cfgs
{
repeated Protobuf_Cfgs_Single cfgs = 1;//
}
message Protobuf_Cfgs_Single
{
uint32 TunnelID = 1;//TunnelID
string IP = 2;//IP
int32 Port = 3;//
}
//
message Protobuf_ChatMsg
@ -78,38 +93,44 @@ message Protobuf_ChatMsg_RESP
{
string NickName = 1;//
string ChatMsg = 2;//
int64 Date = 3;//
int64 Date = 3;//
}
message Protobuf_C2S_Connect
{
int32 TunnelID = 1;//TunnelID
uint32 TunnelID = 1;//TunnelID
uint32 Idx = 2;//
}
message Protobuf_S2C_Connect
{
int32 TunnelID = 1;//TunnelID
uint32 TunnelID = 1;//TunnelID
uint32 Idx = 2;//
}
message Protobuf_C2S_Disconnect
{
int32 TunnelID = 1;//TunnelID
uint32 TunnelID = 1;//TunnelID
uint32 Idx = 2;//
}
message Protobuf_S2C_Disconnect
{
int32 TunnelID = 1;//TunnelID
uint32 TunnelID = 1;//TunnelID
uint32 Idx = 2;//
}
message Protobuf_C2S_DATA
{
int32 TunnelID = 1;//TunnelID
bytes HunterNetCore_Data = 2;
uint32 TunnelID = 1;//TunnelID
uint32 Idx = 2;//
bytes HunterNetCore_Data = 3;
}
message Protobuf_S2C_DATA
{
int32 TunnelID = 1;//TunnelID
bytes HunterNetCore_Data = 2;
uint32 TunnelID = 1;//TunnelID
uint32 Idx = 2;//
bytes HunterNetCore_Data = 3;
}