HaoYueTunnel/ClientCore/Manager/P2PTcp.cs

71 lines
2.5 KiB
C#
Raw Permalink Normal View History

2023-06-15 15:45:58 +08:00
using AxibugProtobuf;
using ClientCore.Common;
2024-04-26 17:51:55 +08:00
using ClientCore.Enum;
2023-06-15 15:45:58 +08:00
using ClientCore.Event;
using ClientCore.Network;
using System.Net;
using System.Net.Sockets;
namespace ClientCore.Manager
{
public class P2PTcp
{
bool bRegToSev = false;
public P2PTcp()
{
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTcptunnelHello, RecvTcptunnelHello);
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdTcptunnelDo, RecvCmdTcptunnelDo);
}
public void SendHellToSev()
{
Protobuf_TcpTunnel_HellToSev msg = new Protobuf_TcpTunnel_HellToSev()
{
UID = App.userMgr.MainPlayer.UID
};
App.networkTcp2S.SendToServer((int)CommandID.CmdTcptunnelHello, ProtoBufHelper.Serizlize(msg));
}
public void RecvTcptunnelHello(byte[] reqData)
{
Protobuf_TcpTunnel_HellToSev_RESP msg = ProtoBufHelper.DeSerizlize<Protobuf_TcpTunnel_HellToSev_RESP>(reqData);
bRegToSev = true;
EventSystem.Instance.PostEvent(EEvent.TcpTunnelHelloResp);
}
public void SendDoTunnel(long targetUID)
{
Protobuf_TcpTunnel_DoTunnel msg = new Protobuf_TcpTunnel_DoTunnel()
{
UID = App.userMgr.MainPlayer.UID,
2024-04-26 17:51:55 +08:00
TargetUID = targetUID,
2023-06-15 15:45:58 +08:00
};
App.networkTcp2S.SendToServer((int)CommandID.CmdTcptunnelDo, ProtoBufHelper.Serizlize(msg));
}
public void RecvCmdTcptunnelDo(byte[] reqData)
{
Protobuf_TcpTunnel_DoTunnel_RESP msg = ProtoBufHelper.DeSerizlize<Protobuf_TcpTunnel_DoTunnel_RESP>(reqData);
//TODO 打洞
2024-04-26 17:51:55 +08:00
App.log.Info($"打洞目标信息 {msg.TargetUID} {msg.OtherIP} {msg.OtherPort}");
2023-06-15 15:45:58 +08:00
int localPort = ((IPEndPoint)App.networkTcp2S.GetClientSocket().LocalEndPoint).Port;
2024-04-26 17:51:55 +08:00
NetworkP2PHelper targetSocket = new NetworkP2PHelper(Enum.ServerType.TcpP2PTarget, msg.TargetUID);
App.clientMgr.AddP2PTargetSocket(targetSocket);
App.clientMgr.SetP2PTargetState(msg.TargetUID, E_P2P_STATE.Doing);
2023-06-15 15:45:58 +08:00
//断开连接
App.networkTcp2S.CloseConntect();
2024-04-26 17:51:55 +08:00
//TODO重新连接打洞服务器
//进行打洞事件
EventSystem.Instance.PostEvent(EEvent.TcpTunnelP2PStateUpdate, msg.TargetUID);
2023-06-15 15:45:58 +08:00
2024-04-26 17:51:55 +08:00
//P2P连接尝试
2023-06-15 15:45:58 +08:00
App.clientMgr.ConnectTCPTargetP2P(msg, localPort);
}
}
}