-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndividualServer.Client.cs
More file actions
130 lines (113 loc) · 5.2 KB
/
IndividualServer.Client.cs
File metadata and controls
130 lines (113 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using RainMeadow.Shared;
namespace RainMeadow.IndividualServer
{
static partial class IndividualServer
{
static List<Client> clients = new();
class Client
{
public readonly ushort routerID;
public readonly IPEndPoint endPoint;
public readonly bool exposeIPAddress;
public Client(IPEndPoint endPoint, ushort routerID, bool exposeIPAddress)
{
RainMeadow.Debug($"New client: {endPoint}, {routerID}");
this.routerID = routerID;
this.endPoint = endPoint;
this.exposeIPAddress = exposeIPAddress;
if (peerManager is null) throw new InvalidProgrammerException("peerManager is null");
clients.Add(this);
peerManager.GetRemotePeer(endPoint, true); // create remotePeer
peerManager.OnPeerForgotten += OnPeerForgotten;
}
private void OnPeerForgotten(IPEndPoint forgottenEndPoint)
{
if (this.endPoint == forgottenEndPoint) RemoveClient();
}
private void RemoveClient()
{
RainMeadow.Debug($"Removing client: {endPoint}, {routerID}");
if (peerManager is null) throw new InvalidProgrammerException("peerManager is null");
peerManager.OnPeerForgotten -= OnPeerForgotten;
if (clients.Contains(this))
{
clients.Remove(this);
Send(new RouterModifyPlayerListPacket(RouterModifyPlayerListPacket.Operation.Remove, new List<ushort> { routerID }),
UDPPeerManager.PacketType.Reliable);
foreach (Client client in clients)
{
client.Send(new RouterModifyPlayerListPacket(RouterModifyPlayerListPacket.Operation.Remove, new List<ushort> { routerID }),
UDPPeerManager.PacketType.Reliable);
}
peerManager.ForgetPeer(endPoint);
}
}
public void Send(Packet packet, UDPPeerManager.PacketType type, bool start_conversation = false)
{
if (peerManager is null) throw new InvalidProgrammerException("peerManager is null");
using (MemoryStream stream = new())
using (BinaryWriter writer = new(stream))
{
Packet.Encode(packet, writer, endPoint);
peerManager.Send(stream.GetBuffer(), endPoint, type, start_conversation);
}
}
}
static void SetupClientEvents()
{
Packet.packetFactory += Packet.RouterFactory;
BeginRouterSession.ProcessAction += BeginRouterSession_ProcessAction;
RouteSessionData.ProcessAction += RouteSessionData_ProcessAction;
}
static void RouteSessionData_ProcessAction(RouteSessionData packet)
{
var srcRoutingID = clients.First(x => UDPPeerManager.CompareIPEndpoints(packet.processingEndpoint, x.endPoint)).routerID;
var destinationClient = clients.First(x => x.routerID == packet.processingRouterID);
destinationClient.Send(new RouteSessionData(srcRoutingID, packet.data, (ushort)(packet.size - sizeof(ushort))), UDPPeerManager.PacketType.Unreliable);
}
const ushort nullrouterID = 0;
static void BeginRouterSession_ProcessAction(BeginRouterSession packet)
{
if (clients.Any(x => x.endPoint == packet.processingEndpoint)) return;
var usedIDs = clients.Select(x => x.routerID);
ushort id = 1;
if (usedIDs.Any())
{
try
{
var possibleIDs = usedIDs.Select(x => (ushort)(x + 1));
id = possibleIDs.Except(usedIDs).Where(x => x != 0).First();
}
catch (InvalidOperationException exception)
{
RainMeadow.Error(exception);
return;
}
}
if (id == nullrouterID)
{
RainMeadow.Error("No available RouterIDs");
return;
}
var newClient = new Client(packet.processingEndpoint, id, packet.exposeIPAddress);
foreach (Client client in clients)
{
if (newClient == client)
{
client.Send(new RouterModifyPlayerListPacket(RouterModifyPlayerListPacket.Operation.Add, clients.Select(x => x.routerID).ToList()),
UDPPeerManager.PacketType.Reliable);
continue;
}
client.Send(new RouterModifyPlayerListPacket(RouterModifyPlayerListPacket.Operation.Add, new List<ushort> { id }),
UDPPeerManager.PacketType.Reliable);
}
newClient.Send(new JoinRouterLobby(id, maxplayers, name, passwordprotected, mode, mods, bannedMods), UDPPeerManager.PacketType.Reliable);
}
}
}