Tool.Net 支持的 P2P 服务器模型实例
- 说明
- 服务器部分相关代码
- 相关调用实例
- Tcp版本
- Udp版本
- 最后附一张思维图
说明
- 当前文章,仅是Tool.Net 开源库的一个缩影。
- 本次更新V5.0版本以上提供支持。
- 可以提供简单实现P2P功能用于业务开发。
服务器部分相关代码
完整代码(适用于自定义的场景)
.
using Tool.Sockets.Kernels;
using Tool.Sockets.P2PHelpr;
using Tool.Sockets.TcpHelper;
using Tool.Sockets.UdpHelper;await Console.Out.WriteLineAsync("P2P服务器·支持“TCP” OR “UDP”");static async ValueTask Completed(UserKey a1, EnServer b1, DateTime c1, string name)
{await Console.Out.WriteLineAsync($"[{name}]IP:{a1} \t{b1} \t{c1:yyyy/MM/dd HH:mm:ss:fffffff}");
}EnumEventQueue.OnInterceptor(EnServer.SendMsg, false);
EnumEventQueue.OnInterceptor(EnServer.Receive, false);
EnumEventQueue.OnInterceptor(EnServer.HeartBeat, false);TcpServerAsync tcpServerAsync = new(NetBufferSize.Size8K, true);
UdpServerAsync udpClientAsync = new(NetBufferSize.Size8K, true);string ip = "0.0.0.0";
int port = 11111;tcpServerAsync.SetCompleted((a1, b1, c1) => Completed(a1, b1, c1, "TCP"));
udpClientAsync.SetCompleted((a1, b1, c1) => Completed(a1, b1, c1, "UDP"));tcpServerAsync.SetReceived(async (receive) =>
{using (receive){if (P2pServerAsync.IsP2pAuth(receive.Span, out var ipnum)){string ok = $"[TCP]{ipnum}->{receive.Key} \t{DateTime.Now:yyyy/MM/dd HH:mm:ss:fffffff}";using var sendBytes = tcpServerAsync.CreateSendBytes(receive.Client, 16);sendBytes.SetMemory(P2pServerAsync.TcpTop);sendBytes.SetMemory(ipnum.Span, 4);sendBytes.SetMemory(receive.Key.Span, 10);await tcpServerAsync.SendAsync(sendBytes);await Console.Out.WriteLineAsync(ok);}}
});udpClientAsync.SetReceived(async (receive) =>
{using (receive){if (P2pServerAsync.IsP2pAuth(receive.Span, out var ipnum)){string ok = $"[UDP]{ipnum}->{receive.Key} \t{DateTime.Now:yyyy/MM/dd HH:mm:ss:fffffff}";using var sendBytes = udpClientAsync.CreateSendBytes(receive.Client, 16);sendBytes.SetMemory(P2pServerAsync.UdpTop);sendBytes.SetMemory(ipnum.Span, 4);sendBytes.SetMemory(receive.Key.Span, 10);await udpClientAsync.SendAsync(sendBytes);await Console.Out.WriteLineAsync(ok);}}
});await tcpServerAsync.StartAsync(ip, port);
await udpClientAsync.StartAsync(ip, port);while (Console.ReadKey(true).Key != ConsoleKey.Escape) ;
相关调用实例
Tcp版本
P2pServerAsync p2PServerAsync0 = await P2pServerAsync.GetFreeTcp();
P2pServerAsync p2PServerAsync1 = await P2pServerAsync.GetFreeTcp();TcpClientAsync p2PClientAsync0 = new(NetBufferSize.Default, true);
var task0 = p2PClientAsync0.P2PConnectAsync(p2PServerAsync0.LocalEP, p2PServerAsync1.RemoteEP);TcpClientAsync p2PClientAsync1 = new(NetBufferSize.Default, true);
var task1 = p2PClientAsync1.P2PConnectAsync(p2PServerAsync1.LocalEP, p2PServerAsync0.RemoteEP);Task.WaitAll(task0, task1);
Udp版本
P2pServerAsync p2PServerAsync0 = await P2pServerAsync.GetFreeUdp();
P2pServerAsync p2PServerAsync1 = await P2pServerAsync.GetFreeUdp();UdpClientAsync p2PClientAsync0 = new(NetBufferSize.Default, true);
var task0 = p2PClientAsync0.P2PConnectAsync(p2PServerAsync0.LocalEP, p2PServerAsync1.RemoteEP);UdpClientAsync p2PClientAsync1 = new(NetBufferSize.Default, true);
var task1 = p2PClientAsync1.P2PConnectAsync(p2PServerAsync1.LocalEP, p2PServerAsync0.RemoteEP);Task.WaitAll(task0, task1);