forked from FakeByte/EpicOnlineTransport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.cs
288 lines (232 loc) · 13 KB
/
Common.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
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
using Epic.OnlineServices;
using Epic.OnlineServices.P2P;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace EpicTransport {
public abstract class Common {
private PacketReliability[] channels;
private int internal_ch => channels.Length;
protected enum InternalMessages : byte {
CONNECT,
ACCEPT_CONNECT,
DISCONNECT
}
protected struct PacketKey {
public ProductUserId productUserId;
public byte channel;
}
private OnIncomingConnectionRequestCallback OnIncomingConnectionRequest;
ulong incomingNotificationId = 0;
private OnRemoteConnectionClosedCallback OnRemoteConnectionClosed;
ulong outgoingNotificationId = 0;
protected readonly EosTransport transport;
protected List<string> deadSockets;
public bool ignoreAllMessages = false;
// Mapping from PacketKey to a List of Packet Lists
protected Dictionary<PacketKey, List<List<Packet>>> incomingPackets = new Dictionary<PacketKey, List<List<Packet>>>();
protected Common(EosTransport transport) {
channels = transport.Channels;
deadSockets = new List<string>();
AddNotifyPeerConnectionRequestOptions addNotifyPeerConnectionRequestOptions = new AddNotifyPeerConnectionRequestOptions();
addNotifyPeerConnectionRequestOptions.LocalUserId = EOSSDKComponent.LocalUserProductId;
addNotifyPeerConnectionRequestOptions.SocketId = null;
OnIncomingConnectionRequest += OnNewConnection;
OnRemoteConnectionClosed += OnConnectFail;
incomingNotificationId = EOSSDKComponent.GetP2PInterface().AddNotifyPeerConnectionRequest(addNotifyPeerConnectionRequestOptions,
null, OnIncomingConnectionRequest);
AddNotifyPeerConnectionClosedOptions addNotifyPeerConnectionClosedOptions = new AddNotifyPeerConnectionClosedOptions();
addNotifyPeerConnectionClosedOptions.LocalUserId = EOSSDKComponent.LocalUserProductId;
addNotifyPeerConnectionClosedOptions.SocketId = null;
outgoingNotificationId = EOSSDKComponent.GetP2PInterface().AddNotifyPeerConnectionClosed(addNotifyPeerConnectionClosedOptions,
null, OnRemoteConnectionClosed);
if (outgoingNotificationId == 0 || incomingNotificationId == 0) {
Debug.LogError("Couldn't bind notifications with P2P interface");
}
incomingPackets = new Dictionary<PacketKey, List<List<Packet>>>();
this.transport = transport;
}
protected void Dispose() {
EOSSDKComponent.GetP2PInterface().RemoveNotifyPeerConnectionRequest(incomingNotificationId);
EOSSDKComponent.GetP2PInterface().RemoveNotifyPeerConnectionClosed(outgoingNotificationId);
transport.ResetIgnoreMessagesAtStartUpTimer();
}
protected abstract void OnNewConnection(OnIncomingConnectionRequestInfo result);
private void OnConnectFail(OnRemoteConnectionClosedInfo result) {
if (ignoreAllMessages) {
return;
}
OnConnectionFailed(result.RemoteUserId);
switch (result.Reason) {
case ConnectionClosedReason.ClosedByLocalUser:
throw new Exception("Connection cLosed: The Connection was gracecfully closed by the local user.");
case ConnectionClosedReason.ClosedByPeer:
throw new Exception("Connection closed: The connection was gracefully closed by remote user.");
case ConnectionClosedReason.ConnectionClosed:
throw new Exception("Connection closed: The connection was unexpectedly closed.");
case ConnectionClosedReason.ConnectionFailed:
throw new Exception("Connection failed: Failled to establish connection.");
case ConnectionClosedReason.InvalidData:
throw new Exception("Connection failed: The remote user sent us invalid data..");
case ConnectionClosedReason.InvalidMessage:
throw new Exception("Connection failed: The remote user sent us an invalid message.");
case ConnectionClosedReason.NegotiationFailed:
throw new Exception("Connection failed: Negotiation failed.");
case ConnectionClosedReason.TimedOut:
throw new Exception("Connection failed: Timeout.");
case ConnectionClosedReason.TooManyConnections:
throw new Exception("Connection failed: Too many connections.");
case ConnectionClosedReason.UnexpectedError:
throw new Exception("Unexpected Error, connection will be closed");
case ConnectionClosedReason.Unknown:
default:
throw new Exception("Unknown Error, connection has been closed.");
}
}
protected void SendInternal(ProductUserId target, SocketId socketId, InternalMessages type) {
EOSSDKComponent.GetP2PInterface().SendPacket(new SendPacketOptions() {
AllowDelayedDelivery = true,
Channel = (byte) internal_ch,
Data = new byte[] { (byte) type },
LocalUserId = EOSSDKComponent.LocalUserProductId,
Reliability = PacketReliability.ReliableOrdered,
RemoteUserId = target,
SocketId = socketId
});
}
protected void Send(ProductUserId host, SocketId socketId, byte[] msgBuffer, byte channel) {
Result result = EOSSDKComponent.GetP2PInterface().SendPacket(new SendPacketOptions() {
AllowDelayedDelivery = true,
Channel = channel,
Data = msgBuffer,
LocalUserId = EOSSDKComponent.LocalUserProductId,
Reliability = channels[channel],
RemoteUserId = host,
SocketId = socketId
});
if(result != Result.Success) {
Debug.LogError("Send failed " + result);
}
}
private bool Receive(out ProductUserId clientProductUserId, out SocketId socketId, out byte[] receiveBuffer, byte channel) {
Result result = EOSSDKComponent.GetP2PInterface().ReceivePacket(new ReceivePacketOptions() {
LocalUserId = EOSSDKComponent.LocalUserProductId,
MaxDataSizeBytes = P2PInterface.MaxPacketSize,
RequestedChannel = channel
}, out clientProductUserId, out socketId, out channel, out receiveBuffer);
if (result == Result.Success) {
return true;
}
receiveBuffer = null;
clientProductUserId = null;
return false;
}
protected virtual void CloseP2PSessionWithUser(ProductUserId clientUserID, SocketId socketId) {
if (socketId == null) {
Debug.LogWarning("Socket ID == null | " + ignoreAllMessages);
return;
}
if (deadSockets == null) {
Debug.LogWarning("DeadSockets == null");
return;
}
if (deadSockets.Contains(socketId.SocketName)) {
return;
} else {
deadSockets.Add(socketId.SocketName);
}
}
protected void WaitForClose(ProductUserId clientUserID, SocketId socketId) => transport.StartCoroutine(DelayedClose(clientUserID, socketId));
private IEnumerator DelayedClose(ProductUserId clientUserID, SocketId socketId) {
yield return null;
CloseP2PSessionWithUser(clientUserID, socketId);
}
public void ReceiveData() {
try {
// Internal Channel, no fragmentation here
SocketId socketId = new SocketId();
while (transport.enabled && Receive(out ProductUserId clientUserID, out socketId, out byte[] internalMessage, (byte) internal_ch)) {
if (internalMessage.Length == 1) {
OnReceiveInternalData((InternalMessages) internalMessage[0], clientUserID, socketId);
return; // Wait one frame
} else {
Debug.Log("Incorrect package length on internal channel.");
}
}
// Insert new packet at the correct location in the incoming queue
for (int chNum = 0; chNum < channels.Length; chNum++) {
while (transport.enabled && Receive(out ProductUserId clientUserID, out socketId, out byte[] receiveBuffer, (byte) chNum)) {
PacketKey incomingPacketKey = new PacketKey();
incomingPacketKey.productUserId = clientUserID;
incomingPacketKey.channel = (byte)chNum;
Packet packet = new Packet();
packet.FromBytes(receiveBuffer);
if (!incomingPackets.ContainsKey(incomingPacketKey)) {
incomingPackets.Add(incomingPacketKey, new List<List<Packet>>());
}
int packetListIndex = incomingPackets[incomingPacketKey].Count;
for(int i = 0; i < incomingPackets[incomingPacketKey].Count; i++) {
if(incomingPackets[incomingPacketKey][i][0].id == packet.id) {
packetListIndex = i;
break;
}
}
if (packetListIndex == incomingPackets[incomingPacketKey].Count) {
incomingPackets[incomingPacketKey].Add(new List<Packet>());
}
int insertionIndex = -1;
for (int i = 0; i < incomingPackets[incomingPacketKey][packetListIndex].Count; i++) {
if (incomingPackets[incomingPacketKey][packetListIndex][i].fragment > packet.fragment) {
insertionIndex = i;
break;
}
}
if (insertionIndex >= 0) {
incomingPackets[incomingPacketKey][packetListIndex].Insert(insertionIndex, packet);
} else {
incomingPackets[incomingPacketKey][packetListIndex].Add(packet);
}
}
}
// Find fully received packets
List<List<Packet>> emptyPacketLists = new List<List<Packet>>();
foreach(KeyValuePair<PacketKey, List<List<Packet>>> keyValuePair in incomingPackets) {
for(int packetList = 0; packetList < keyValuePair.Value.Count; packetList++) {
bool packetReady = true;
int packetLength = 0;
for (int packet = 0; packet < keyValuePair.Value[packetList].Count; packet++) {
Packet tempPacket = keyValuePair.Value[packetList][packet];
if (tempPacket.fragment != packet || (packet == keyValuePair.Value[packetList].Count - 1 && tempPacket.moreFragments)) {
packetReady = false;
} else {
packetLength += tempPacket.data.Length;
}
}
if (packetReady) {
byte[] data = new byte[packetLength];
int dataIndex = 0;
for (int packet = 0; packet < keyValuePair.Value[packetList].Count; packet++) {
Array.Copy(keyValuePair.Value[packetList][packet].data, 0, data, dataIndex, keyValuePair.Value[packetList][packet].data.Length);
dataIndex += keyValuePair.Value[packetList][packet].data.Length;
}
OnReceiveData(data, keyValuePair.Key.productUserId, keyValuePair.Key.channel);
//keyValuePair.Value[packetList].Clear();
emptyPacketLists.Add(keyValuePair.Value[packetList]);
}
}
for (int i = 0; i < emptyPacketLists.Count; i++) {
keyValuePair.Value.Remove(emptyPacketLists[i]);
}
emptyPacketLists.Clear();
}
} catch (Exception e) {
Debug.LogException(e);
}
}
protected abstract void OnReceiveInternalData(InternalMessages type, ProductUserId clientUserID, SocketId socketId);
protected abstract void OnReceiveData(byte[] data, ProductUserId clientUserID, int channel);
protected abstract void OnConnectionFailed(ProductUserId remoteId);
}
}