forked from Chykary/FizzySteamworks
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.cs
More file actions
158 lines (137 loc) · 5.91 KB
/
Server.cs
File metadata and controls
158 lines (137 loc) · 5.91 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using Steamworks;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Mirror.FizzySteam
{
public class Server : Common
{
private event Action<int> OnConnected;
private event Action<int, byte[], int> OnReceivedData;
private event Action<int> OnDisconnected;
private event Action<int, Exception> OnReceivedError;
private BidirectionalDictionary<CSteamID, int> steamToMirrorIds;
private int maxConnections;
private int nextConnectionID;
public static Server CreateServer(FizzySteamworks transport, int maxConnections)
{
Server s = new Server(transport, maxConnections);
s.OnConnected += (id) => transport.OnServerConnected.Invoke(id);
s.OnDisconnected += (id) => transport.OnServerDisconnected.Invoke(id);
s.OnReceivedData += (id, data, channel) => transport.OnServerDataReceived.Invoke(id, new ArraySegment<byte>(data), channel);
s.OnReceivedError += (id, exception) => transport.OnServerError.Invoke(id, exception);
if (!SteamManager.Initialized)
{
Debug.LogError("SteamWorks not initialized.");
}
return s;
}
private Server(FizzySteamworks transport, int maxConnections) : base(transport)
{
this.maxConnections = maxConnections;
steamToMirrorIds = new BidirectionalDictionary<CSteamID, int>();
nextConnectionID = 1;
}
protected override void OnNewConnection(P2PSessionRequest_t result) => SteamNetworking.AcceptP2PSessionWithUser(result.m_steamIDRemote);
protected override void OnReceiveInternalData(InternalMessages type, CSteamID clientSteamID)
{
switch (type)
{
case InternalMessages.CONNECT:
if (steamToMirrorIds.Count >= maxConnections)
{
SendInternal(clientSteamID, InternalMessages.DISCONNECT);
return;
}
SendInternal(clientSteamID, InternalMessages.ACCEPT_CONNECT);
int connectionId = nextConnectionID++;
steamToMirrorIds.Add(clientSteamID, connectionId);
OnConnected.Invoke(connectionId);
Debug.Log($"Client with SteamID {clientSteamID} connected. Assigning connection id {connectionId}");
break;
case InternalMessages.DISCONNECT:
if (steamToMirrorIds.TryGetValue(clientSteamID, out int connId))
{
OnDisconnected.Invoke(connId);
CloseP2PSessionWithUser(clientSteamID);
steamToMirrorIds.Remove(clientSteamID);
Debug.Log($"Client with SteamID {clientSteamID} disconnected.");
}
else
{
OnReceivedError.Invoke(-1, new Exception("ERROR Unknown SteamID"));
}
break;
default:
Debug.Log("Received unknown message type");
break;
}
}
protected override void OnReceiveData(byte[] data, CSteamID clientSteamID, int channel)
{
if (steamToMirrorIds.TryGetValue(clientSteamID, out int connectionId))
{
OnReceivedData.Invoke(connectionId, data, channel);
}
else
{
CloseP2PSessionWithUser(clientSteamID);
Debug.LogError("Data received from steam client thats not known " + clientSteamID);
OnReceivedError.Invoke(-1, new Exception("ERROR Unknown SteamID"));
}
}
public bool Disconnect(int connectionId)
{
if (steamToMirrorIds.TryGetValue(connectionId, out CSteamID steamID))
{
SendInternal(steamID, InternalMessages.DISCONNECT);
return true;
}
else
{
Debug.LogWarning("Trying to disconnect unknown connection id: " + connectionId);
return false;
}
}
public void Shutdown()
{
foreach (KeyValuePair<CSteamID, int> client in steamToMirrorIds)
{
Disconnect(client.Value);
WaitForClose(client.Key);
}
Dispose();
}
public void SendAll(int connectionId, byte[] data, int channelId)
{
if (steamToMirrorIds.TryGetValue(connectionId, out CSteamID steamId))
{
Send(steamId, data, channelId);
}
else
{
Debug.LogError("Trying to send on unknown connection: " + connectionId);
OnReceivedError.Invoke(connectionId, new Exception("ERROR Unknown Connection"));
}
}
public string ServerGetClientAddress(int connectionId)
{
if (steamToMirrorIds.TryGetValue(connectionId, out CSteamID steamId))
{
return steamId.ToString();
}
else
{
Debug.LogError("Trying to get info on unknown connection: " + connectionId);
OnReceivedError.Invoke(connectionId, new Exception("ERROR Unknown Connection"));
return string.Empty;
}
}
protected override void OnConnectionFailed(CSteamID remoteId)
{
int connectionId = steamToMirrorIds.TryGetValue(remoteId, out int connId) ? connId : nextConnectionID++;
OnDisconnected.Invoke(connectionId);
steamToMirrorIds.Remove(remoteId);
}
}
}