-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerNetworkChannel.cs
151 lines (132 loc) · 5.88 KB
/
ServerNetworkChannel.cs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Steamworks;
using UnityEngine;
using Valve.Sockets;
namespace SharkkitDedicated
{
public class ServerNetworkChannel : NetworkChannel
{
/// <summary>
/// A PollGroup is used to efficiently poll multiple connections at once (here: all clients)
/// </summary>
public uint PollGroup;
/// <summary>
/// The Listen socket of the server
/// </summary>
public uint ServerSocket;
// Internal stuff
public readonly Dictionary<CSteamID, uint> ClientSockets = new Dictionary<CSteamID, uint>();
public readonly Dictionary<CSteamID, Queue<NetworkingMessage>[]> PacketBufs =
new Dictionary<CSteamID, Queue<NetworkingMessage>[]>();
public StatusCallback ServerStatusCallback;
/// <summary>
/// Connections that have been made, but that still wait for the steam auth token package to verify.
/// </summary>
public readonly HashSet<uint> PendingConnections = new HashSet<uint>();
public override bool IsServer => true;
public override bool TryGetConnectionFor(CSteamID remoteId, out uint socket)
{
return ClientSockets.TryGetValue(remoteId, out socket);
}
public override void Deinitialize(NetworkingSockets networkingSockets)
{
networkingSockets.DestroyPollGroup(PollGroup);
// TODO: For all _networkingSockets.CloseConnection()
networkingSockets.CloseListenSocket(ServerSocket);
}
protected override void EnqueueMessages(int nbMsgs)
{
for (var i = 0; i < nbMsgs; i++)
{
var msg = _messages[i];
try
{
if (PendingConnections.Contains(msg.connection))
{
HandleAuthMessage(msg);
}
else
{
var con = ClientSockets.FirstOrDefault(x => x.Value == msg.connection);
if (!PacketBufs.ContainsKey(con.Key))
{
// TODO: Determine the maximum channel size dynamically
var quArray = new Queue<NetworkingMessage>[2];
quArray[0] = new Queue<NetworkingMessage>();
quArray[1] = new Queue<NetworkingMessage>();
PacketBufs.Add(con.Key, quArray);
}
var buf = PacketBufs[con.Key];
var nChannel = Marshal.ReadByte(msg.data);
if (!PacketBufs.ContainsKey(con.Key))
{
Debug.LogWarning($"Received a packet from an unknown connection {con.Key}");
}
if (nChannel >= buf.Length)
{
Debug.LogWarning($"Skipping a message of size {msg.length} because the channel {nChannel} is not available");
continue;
}
PacketBufs[con.Key][nChannel].Enqueue(msg);
}
}
catch (KeyNotFoundException knf)
{
Debug.LogError($"Could not find a connection for the Id {msg.connection}. {knf}");
}
catch (ArgumentOutOfRangeException ex)
{
Debug.LogError(ex);
}
}
}
private void HandleAuthMessage(NetworkingMessage m)
{
var buf = new byte[m.length];
var ticket = new byte[m.length - 8];
Marshal.Copy(m.data, buf, 0, m.length);
var steamId = new CSteamID(BitConverter.ToUInt64(buf, 0));
Array.Copy(buf, 8, ticket, 0, ticket.Length);
var res = NetworkWrapper._serverList.BeginAuthentication(ticket, steamId);
Debug.Log($"Steam Authentication Result for {steamId}: {res}");
if (res == EBeginAuthSessionResult.k_EBeginAuthSessionResultOK ||
res == EBeginAuthSessionResult.k_EBeginAuthSessionResultGameMismatch /* spacewars */)
{
PendingConnections.Remove(m.connection);
NetworkWrapper._networkingSockets.SendMessageToConnection(m.connection,
BitConverter.GetBytes(SteamGameServerList.ServerId.m_SteamID), SendFlags.Reliable);
ClientSockets.Add(steamId, m.connection);
}
else
{
Debug.LogError("Error validating the Auth Ticket!");
NetworkWrapper._networkingSockets.SendMessageToConnection(m.connection, BitConverter.GetBytes(0UL),
SendFlags.Reliable);
}
}
public override void Tick(NetworkingSockets networkingSockets)
{
EnqueueMessages(networkingSockets.ReceiveMessagesOnPollGroup(PollGroup, _messages, _messages.Length));
}
public override bool SendMessage(CSteamID steamIDRemote, byte[] data, SendFlags flags, int channel)
{
return ClientSockets.TryGetValue(steamIDRemote, out var con) &&
NetworkWrapper.SendBufToConnection(data, flags, channel, con);
}
public class QueueablePacket
{
public byte[] Data;
public SendFlags Flags;
public int Channel;
public QueueablePacket(byte[] pubData, SendFlags flags, int nChannel)
{
Data = pubData;
Flags = flags;
Channel = nChannel;
}
}
}
}