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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
using System;
using System.Collections.Generic;
using System.Threading;
using Steamworks;
public class ZSteamSocket : IDisposable, ISocket
{
private static List<ZSteamSocket> m_sockets = new List<ZSteamSocket>();
private static Callback<P2PSessionRequest_t> m_SessionRequest;
private static Callback<P2PSessionConnectFail_t> m_connectionFailed;
private Queue<ZSteamSocket> m_pendingConnections = new Queue<ZSteamSocket>();
private CSteamID m_peerID = CSteamID.Nil;
private bool m_listner;
private Queue<ZPackage> m_pkgQueue = new Queue<ZPackage>();
private Queue<byte[]> m_sendQueue = new Queue<byte[]>();
private int m_totalSent;
private int m_totalRecv;
private bool m_gotData;
public ZSteamSocket()
{
m_sockets.Add(this);
RegisterGlobalCallbacks();
}
public ZSteamSocket(CSteamID peerID)
{
m_sockets.Add(this);
m_peerID = peerID;
RegisterGlobalCallbacks();
}
private static void RegisterGlobalCallbacks()
{
if (m_connectionFailed == null)
{
ZLog.Log("ZSteamSocket Registering global callbacks");
m_connectionFailed = Callback<P2PSessionConnectFail_t>.Create(OnConnectionFailed);
}
if (m_SessionRequest == null)
{
m_SessionRequest = Callback<P2PSessionRequest_t>.Create(OnSessionRequest);
}
}
private static void UnregisterGlobalCallbacks()
{
ZLog.Log("ZSteamSocket UnregisterGlobalCallbacks, existing sockets:" + m_sockets.Count);
if (m_connectionFailed != null)
{
m_connectionFailed.Dispose();
m_connectionFailed = null;
}
if (m_SessionRequest != null)
{
m_SessionRequest.Dispose();
m_SessionRequest = null;
}
}
private static void OnConnectionFailed(P2PSessionConnectFail_t data)
{
ZLog.Log("Got connection failed callback: " + data.m_steamIDRemote);
foreach (ZSteamSocket socket in m_sockets)
{
if (socket.IsPeer(data.m_steamIDRemote))
{
socket.Close();
}
}
}
private static void OnSessionRequest(P2PSessionRequest_t data)
{
ZLog.Log("Got session request from " + data.m_steamIDRemote);
if (SteamNetworking.AcceptP2PSessionWithUser(data.m_steamIDRemote))
{
GetListner()?.QueuePendingConnection(data.m_steamIDRemote);
}
}
public void Dispose()
{
ZLog.Log("Disposing socket");
Close();
m_pkgQueue.Clear();
m_sockets.Remove(this);
if (m_sockets.Count == 0)
{
ZLog.Log("Last socket, unregistering callback");
UnregisterGlobalCallbacks();
}
}
public void Close()
{
ZLog.Log("Closing socket " + GetEndPointString());
if (m_peerID != CSteamID.Nil)
{
Flush();
ZLog.Log(" send queue size:" + m_sendQueue.Count);
Thread.Sleep(100);
SteamNetworking.GetP2PSessionState(m_peerID, out var pConnectionState);
ZLog.Log(" P2P state, bytes in send queue:" + pConnectionState.m_nBytesQueuedForSend);
SteamNetworking.CloseP2PSessionWithUser(m_peerID);
SteamUser.EndAuthSession(m_peerID);
m_peerID = CSteamID.Nil;
}
m_listner = false;
}
public bool StartHost()
{
m_listner = true;
m_pendingConnections.Clear();
return true;
}
private ZSteamSocket QueuePendingConnection(CSteamID id)
{
foreach (ZSteamSocket pendingConnection in m_pendingConnections)
{
if (pendingConnection.IsPeer(id))
{
return pendingConnection;
}
}
ZSteamSocket zSteamSocket = new ZSteamSocket(id);
m_pendingConnections.Enqueue(zSteamSocket);
return zSteamSocket;
}
public ISocket Accept()
{
if (!m_listner)
{
return null;
}
if (m_pendingConnections.Count > 0)
{
return m_pendingConnections.Dequeue();
}
return null;
}
public bool IsConnected()
{
return m_peerID != CSteamID.Nil;
}
public void Send(ZPackage pkg)
{
if (pkg.Size() != 0 && IsConnected())
{
byte[] array = pkg.GetArray();
byte[] bytes = BitConverter.GetBytes(array.Length);
byte[] array2 = new byte[array.Length + bytes.Length];
bytes.CopyTo(array2, 0);
array.CopyTo(array2, 4);
m_sendQueue.Enqueue(array);
SendQueuedPackages();
}
}
public bool Flush()
{
SendQueuedPackages();
return m_sendQueue.Count == 0;
}
private void SendQueuedPackages()
{
if (!IsConnected())
{
return;
}
while (m_sendQueue.Count > 0)
{
byte[] array = m_sendQueue.Peek();
if (SteamNetworking.SendP2PPacket(m_peerID, array, (uint)array.Length, EP2PSend.k_EP2PSendReliable))
{
m_totalSent += array.Length;
m_sendQueue.Dequeue();
continue;
}
break;
}
}
public static void Update()
{
foreach (ZSteamSocket socket in m_sockets)
{
socket.SendQueuedPackages();
}
ReceivePackages();
}
private static void ReceivePackages()
{
uint pcubMsgSize;
while (SteamNetworking.IsP2PPacketAvailable(out pcubMsgSize))
{
byte[] array = new byte[pcubMsgSize];
if (SteamNetworking.ReadP2PPacket(array, pcubMsgSize, out var _, out var psteamIDRemote))
{
QueueNewPkg(psteamIDRemote, array);
continue;
}
break;
}
}
private static void QueueNewPkg(CSteamID sender, byte[] data)
{
foreach (ZSteamSocket socket in m_sockets)
{
if (socket.IsPeer(sender))
{
socket.QueuePackage(data);
return;
}
}
ZSteamSocket listner = GetListner();
if (listner != null)
{
ZLog.Log("Got package from unconnected peer " + sender);
listner.QueuePendingConnection(sender).QueuePackage(data);
}
else
{
ZLog.Log(string.Concat("Got package from unkown peer ", sender, " but no active listner"));
}
}
private static ZSteamSocket GetListner()
{
foreach (ZSteamSocket socket in m_sockets)
{
if (socket.IsHost())
{
return socket;
}
}
return null;
}
private void QueuePackage(byte[] data)
{
ZPackage item = new ZPackage(data);
m_pkgQueue.Enqueue(item);
m_gotData = true;
m_totalRecv += data.Length;
}
public ZPackage Recv()
{
if (!IsConnected())
{
return null;
}
if (m_pkgQueue.Count > 0)
{
return m_pkgQueue.Dequeue();
}
return null;
}
public string GetEndPointString()
{
return m_peerID.ToString();
}
public string GetHostName()
{
return m_peerID.ToString();
}
public CSteamID GetPeerID()
{
return m_peerID;
}
public bool IsPeer(CSteamID peer)
{
if (!IsConnected())
{
return false;
}
return peer == m_peerID;
}
public bool IsHost()
{
return m_listner;
}
public bool IsSending()
{
if (!IsConnected())
{
return false;
}
return m_sendQueue.Count > 0;
}
public void GetAndResetStats(out int totalSent, out int totalRecv)
{
totalSent = m_totalSent;
totalRecv = m_totalRecv;
m_totalSent = 0;
m_totalRecv = 0;
}
public bool GotNewData()
{
bool gotData = m_gotData;
m_gotData = false;
return gotData;
}
public int GetHostPort()
{
if (IsHost())
{
return 1;
}
return -1;
}
}
|