using AxibugProtobuf; using ClientCore.Common; using ClientCore.Enum; using ClientCore.Event; using ClientCore.Network; using HaoYueNet.ClientNetwork; using System.Net.Sockets; using System.Timers; using static System.Runtime.CompilerServices.RuntimeHelpers; namespace ClientCore.Manager { public class ClientManager { bool bTryReConTcpTunnelServer = false; string IP; int port; int tcptunnelport; int LastlocalPort = 0; Dictionary DictUID2TcpTaret = new Dictionary(); public ClientManager() { //注册事件 EventSystem.Instance.RegisterEvent(EEvent.OnSocketConnect, OnSocketConnect); EventSystem.Instance.RegisterEvent(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) { App.log.Info("MainServer连接成功"); } else { App.log.Info("MainServer连接失败"); } } break; case ServerType.TcpTunnelServer: { bTryReConTcpTunnelServer = false; if (IsOk) { App.log.Info("TcpTunnelServer连接成功"); App.p2ptcp.SendHellToSev(); } else { App.log.Info("TcpTunnelServer连接失败"); } } break; case ServerType.TcpP2PTarget: { if (IsOk) { App.log.Info("TcpP2PTarget连接成功"); } } break; } } void OnSocketDisconnect(ServerType servertype, long uid) { switch (servertype) { case ServerType.MainServer: { App.log.Info("MainServer连接断开"); } break; case ServerType.TcpTunnelServer: { App.log.Info("TcpTunnelServer连接断开"); App.log.Info("TcpTunnelServer,尝试重连"); ReConnectTcpTunnelServer(); } break; case ServerType.TcpP2PTarget: { App.log.Info("TcpP2PTarget连接断开"); RemoveP2PTargetSocket(uid); } 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 管理 public void AddP2PTargetSocket(NetworkP2PHelper targetSocket) { DictUID2TcpTaret[targetSocket.mUID] = targetSocket; } 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) { if (DictUID2TcpTaret.ContainsKey(UID)) { DictUID2TcpTaret.Remove(UID); } } NetworkP2PHelper GetTargetSocket(long UID) { if (DictUID2TcpTaret.ContainsKey(UID)) { return DictUID2TcpTaret[UID]; } return null; } /// /// 发送给指定UID消息 /// /// /// /// public void SendToTargetSocket(long UID, int CMDID, int ERRCODE, byte[] data) { NetworkP2PHelper target = GetTargetSocket(UID); if (target == null) return; target.SendToSocket((int)CMDID, ERRCODE, data); } #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); int userBindPort = LastlocalPort; Protobuf_TcpTunnel_DoTunnel_RESP msg = (Protobuf_TcpTunnel_DoTunnel_RESP)obj; 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); NetworkP2PHelper targetSocket = new NetworkP2PHelper(Enum.ServerType.TcpP2PTarget, msg.TargetUID); targetSocket.Init(msg.OtherIP, msg.OtherPort, true, userBindPort); //尝试5次连接 for (int j = 0; j < 5; j++) { try { targetSocket.Connect(msg.OtherIP, msg.OtherPort); App.log.Info($"Connect:成功 目标用户UID:{msg.TargetUID},{msg.OtherIP},{msg.OtherPort}"); AddTargetSocket(targetSocket); return; } catch (Exception) { App.log.Info("Connect:失败"); } } EventSystem.Instance.PostEvent(EEvent.TcpTunnelFail, msg.TargetUID); */ } #endregion } }