HaoYueTunnel/ClientCore/Manager/P2PTcp.cs
2024-04-26 17:51:55 +08:00

71 lines
2.5 KiB
C#

using AxibugProtobuf;
using ClientCore.Common;
using ClientCore.Enum;
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,
TargetUID = targetUID,
};
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 打洞
App.log.Info($"打洞目标信息 {msg.TargetUID} {msg.OtherIP} {msg.OtherPort}");
int localPort = ((IPEndPoint)App.networkTcp2S.GetClientSocket().LocalEndPoint).Port;
NetworkP2PHelper targetSocket = new NetworkP2PHelper(Enum.ServerType.TcpP2PTarget, msg.TargetUID);
App.clientMgr.AddP2PTargetSocket(targetSocket);
App.clientMgr.SetP2PTargetState(msg.TargetUID, E_P2P_STATE.Doing);
//断开连接
App.networkTcp2S.CloseConntect();
//TODO重新连接打洞服务器
//进行打洞事件
EventSystem.Instance.PostEvent(EEvent.TcpTunnelP2PStateUpdate, msg.TargetUID);
//P2P连接尝试
App.clientMgr.ConnectTCPTargetP2P(msg, localPort);
}
}
}