2023-06-15 15:45:58 +08:00
|
|
|
|
using AxibugProtobuf;
|
|
|
|
|
using ClientCore.Common;
|
|
|
|
|
using ClientCore.Enum;
|
|
|
|
|
using ClientCore.Event;
|
|
|
|
|
using ClientCore.Network;
|
2023-06-16 18:04:08 +08:00
|
|
|
|
using HaoYueNet.ClientNetwork;
|
2023-06-15 15:45:58 +08:00
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Timers;
|
2023-06-16 18:04:08 +08:00
|
|
|
|
using static System.Runtime.CompilerServices.RuntimeHelpers;
|
2023-06-15 15:45:58 +08:00
|
|
|
|
|
|
|
|
|
namespace ClientCore.Manager
|
|
|
|
|
{
|
|
|
|
|
public class ClientManager
|
|
|
|
|
{
|
|
|
|
|
bool bTryReConTcpTunnelServer = false;
|
|
|
|
|
string IP;
|
|
|
|
|
int port;
|
|
|
|
|
int tcptunnelport;
|
|
|
|
|
int LastlocalPort = 0;
|
|
|
|
|
|
2023-06-16 18:04:08 +08:00
|
|
|
|
Dictionary<long, NetworkP2PHelper> DictUID2TcpTaret = new Dictionary<long, NetworkP2PHelper>();
|
2023-06-15 15:45:58 +08:00
|
|
|
|
|
|
|
|
|
public ClientManager()
|
|
|
|
|
{
|
|
|
|
|
//注册事件
|
|
|
|
|
EventSystem.Instance.RegisterEvent<ServerType, bool>(EEvent.OnSocketConnect, OnSocketConnect);
|
|
|
|
|
EventSystem.Instance.RegisterEvent<ServerType, long>(EEvent.OnSocketDisconnect, OnSocketDisconnect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetIpData(string IP, int port, int tcptunnelport)
|
|
|
|
|
{
|
|
|
|
|
this.IP = IP;
|
|
|
|
|
this.port = port;
|
|
|
|
|
this.tcptunnelport = tcptunnelport;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnSocketConnect(ServerType servertype, bool IsOk)
|
|
|
|
|
{
|
|
|
|
|
switch (servertype)
|
|
|
|
|
{
|
|
|
|
|
case ServerType.MainServer:
|
|
|
|
|
{
|
|
|
|
|
if (IsOk)
|
|
|
|
|
{
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("MainServer连接成功");
|
2023-06-15 15:45:58 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("MainServer连接失败");
|
2023-06-15 15:45:58 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ServerType.TcpTunnelServer:
|
|
|
|
|
{
|
|
|
|
|
bTryReConTcpTunnelServer = false;
|
|
|
|
|
if (IsOk)
|
|
|
|
|
{
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("TcpTunnelServer连接成功");
|
2023-06-15 15:45:58 +08:00
|
|
|
|
App.p2ptcp.SendHellToSev();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("TcpTunnelServer连接失败");
|
2023-06-15 15:45:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ServerType.TcpP2PTarget:
|
|
|
|
|
{
|
|
|
|
|
if (IsOk)
|
|
|
|
|
{
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("TcpP2PTarget连接成功");
|
2023-06-15 15:45:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void OnSocketDisconnect(ServerType servertype, long uid)
|
|
|
|
|
{
|
|
|
|
|
switch (servertype)
|
|
|
|
|
{
|
|
|
|
|
case ServerType.MainServer:
|
|
|
|
|
{
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("MainServer连接断开");
|
2023-06-15 15:45:58 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ServerType.TcpTunnelServer:
|
|
|
|
|
{
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("TcpTunnelServer连接断开");
|
|
|
|
|
App.log.Info("TcpTunnelServer,尝试重连");
|
2023-06-15 15:45:58 +08:00
|
|
|
|
ReConnectTcpTunnelServer();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ServerType.TcpP2PTarget:
|
|
|
|
|
{
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("TcpP2PTarget连接断开");
|
|
|
|
|
RemoveP2PTargetSocket(uid);
|
2023-06-15 15:45:58 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConnectTcpTunnelServer()
|
|
|
|
|
{
|
|
|
|
|
App.networkTcp2S = new NetworkHelper(Enum.ServerType.TcpTunnelServer);
|
|
|
|
|
App.networkTcp2S.Init(IP, tcptunnelport);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReConnectTcpTunnelServer()
|
|
|
|
|
{
|
|
|
|
|
//TODO 因TEST 暂不重连
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (bTryReConTcpTunnelServer)
|
|
|
|
|
return;
|
|
|
|
|
if (App.networkTcp2S != null && App.networkTcp2S.GetClientSocket() != null && App.networkTcp2S.GetClientSocket().Connected)
|
|
|
|
|
return;
|
|
|
|
|
bTryReConTcpTunnelServer = true;
|
|
|
|
|
|
|
|
|
|
Thread thread = new Thread(ReConnectTcpTunnelServerThread);
|
|
|
|
|
thread.IsBackground = true;
|
|
|
|
|
thread.Start();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReConnectTcpTunnelServerThread()
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
App.networkTcp2S.Init(IP, tcptunnelport);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region P2PTarget 管理
|
2024-04-26 17:51:55 +08:00
|
|
|
|
public void AddP2PTargetSocket(NetworkP2PHelper targetSocket)
|
2023-06-15 15:45:58 +08:00
|
|
|
|
{
|
|
|
|
|
DictUID2TcpTaret[targetSocket.mUID] = targetSocket;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-26 17:51:55 +08:00
|
|
|
|
public void SetP2PTargetState(long UID,E_P2P_STATE state)
|
|
|
|
|
{
|
|
|
|
|
if (!DictUID2TcpTaret.ContainsKey(UID))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
DictUID2TcpTaret[UID].e_P2P_STATE = state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool GetP2PTargetState(long UID, out E_P2P_STATE state)
|
|
|
|
|
{
|
|
|
|
|
if (DictUID2TcpTaret.ContainsKey(UID))
|
|
|
|
|
{
|
|
|
|
|
state = DictUID2TcpTaret[UID].e_P2P_STATE;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
state = E_P2P_STATE.None;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveP2PTargetSocket(long UID)
|
2023-06-15 15:45:58 +08:00
|
|
|
|
{
|
|
|
|
|
if (DictUID2TcpTaret.ContainsKey(UID))
|
|
|
|
|
{
|
|
|
|
|
DictUID2TcpTaret.Remove(UID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 18:04:08 +08:00
|
|
|
|
NetworkP2PHelper GetTargetSocket(long UID)
|
2023-06-15 15:45:58 +08:00
|
|
|
|
{
|
|
|
|
|
if (DictUID2TcpTaret.ContainsKey(UID))
|
|
|
|
|
{
|
|
|
|
|
return DictUID2TcpTaret[UID];
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-06-16 18:04:08 +08:00
|
|
|
|
|
2023-06-15 15:45:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发送给指定UID消息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="UID"></param>
|
|
|
|
|
/// <param name="CMDID"></param>
|
|
|
|
|
/// <param name="data"></param>
|
2023-06-16 18:04:08 +08:00
|
|
|
|
public void SendToTargetSocket(long UID, int CMDID, int ERRCODE, byte[] data)
|
2023-06-15 15:45:58 +08:00
|
|
|
|
{
|
2023-06-16 18:04:08 +08:00
|
|
|
|
NetworkP2PHelper target = GetTargetSocket(UID);
|
2023-06-15 15:45:58 +08:00
|
|
|
|
if (target == null)
|
|
|
|
|
return;
|
2023-06-16 18:04:08 +08:00
|
|
|
|
target.SendToSocket((int)CMDID, ERRCODE, data);
|
2023-06-15 15:45:58 +08:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region TCP打洞对连
|
|
|
|
|
|
|
|
|
|
public void ConnectTCPTargetP2P(Protobuf_TcpTunnel_DoTunnel_RESP msg, int localPort)
|
|
|
|
|
{
|
|
|
|
|
LastlocalPort = localPort;
|
|
|
|
|
Thread thread = new Thread(ConnectTCPTargetP2PThread);
|
|
|
|
|
thread.IsBackground = true;
|
|
|
|
|
thread.Start(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConnectTCPTargetP2PThread(object obj)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(1000);
|
2024-04-26 17:51:55 +08:00
|
|
|
|
|
2023-06-15 15:45:58 +08:00
|
|
|
|
int userBindPort = LastlocalPort;
|
|
|
|
|
Protobuf_TcpTunnel_DoTunnel_RESP msg = (Protobuf_TcpTunnel_DoTunnel_RESP)obj;
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("LocalEndPoint Port:" + userBindPort);
|
|
|
|
|
NetworkP2PHelper targetSocket = GetTargetSocket(msg.TargetUID);
|
|
|
|
|
targetSocket.Init(true, userBindPort);
|
|
|
|
|
//尝试5次连接
|
|
|
|
|
for (int j = 0; j < 5; j++)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!targetSocket.Connect(msg.OtherIP, msg.OtherPort))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
App.log.Info($"Connect:成功 目标用户UID:{msg.TargetUID},{msg.OtherIP},{msg.OtherPort}");
|
|
|
|
|
//AddTargetSocket(targetSocket);
|
|
|
|
|
SetP2PTargetState(msg.TargetUID, E_P2P_STATE.Success);
|
|
|
|
|
EventSystem.Instance.PostEvent(EEvent.TcpTunnelP2PStateUpdate, msg.TargetUID);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
App.log.Info("Connect:失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SetP2PTargetState(msg.TargetUID, E_P2P_STATE.Fail);
|
|
|
|
|
EventSystem.Instance.PostEvent(EEvent.TcpTunnelP2PStateUpdate, msg.TargetUID);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
int userBindPort = LastlocalPort;
|
|
|
|
|
Protobuf_TcpTunnel_DoTunnel_RESP msg = (Protobuf_TcpTunnel_DoTunnel_RESP)obj;
|
|
|
|
|
App.log.Info("LocalEndPoint Port:" + userBindPort);
|
2023-06-16 18:04:08 +08:00
|
|
|
|
NetworkP2PHelper targetSocket = new NetworkP2PHelper(Enum.ServerType.TcpP2PTarget, msg.TargetUID);
|
2023-06-15 15:45:58 +08:00
|
|
|
|
targetSocket.Init(msg.OtherIP, msg.OtherPort, true, userBindPort);
|
|
|
|
|
//尝试5次连接
|
|
|
|
|
for (int j = 0; j < 5; j++)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
targetSocket.Connect(msg.OtherIP, msg.OtherPort);
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info($"Connect:成功 目标用户UID:{msg.TargetUID},{msg.OtherIP},{msg.OtherPort}");
|
2023-06-15 15:45:58 +08:00
|
|
|
|
AddTargetSocket(targetSocket);
|
2024-04-26 17:51:55 +08:00
|
|
|
|
return;
|
2023-06-15 15:45:58 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2024-04-26 17:51:55 +08:00
|
|
|
|
App.log.Info("Connect:失败");
|
2023-06-15 15:45:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-26 17:51:55 +08:00
|
|
|
|
|
|
|
|
|
EventSystem.Instance.PostEvent(EEvent.TcpTunnelFail, msg.TargetUID);
|
|
|
|
|
*/
|
2023-06-15 15:45:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|