-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathRPCQueueProcessing.cs
More file actions
257 lines (227 loc) · 10.3 KB
/
RPCQueueProcessing.cs
File metadata and controls
257 lines (227 loc) · 10.3 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
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
using System;
using System.Collections.Generic;
using UnityEngine;
using Unity.Profiling;
using MLAPI.Configuration;
using MLAPI.Profiling;
using MLAPI.Transports;
namespace MLAPI.Messaging
{
/// <summary>
/// RpcQueueProcessing
/// Handles processing of RPCQueues
/// Inbound to invocation
/// Outbound to send
/// </summary>
internal class RpcQueueProcessing
{
#if DEVELOPMENT_BUILD || UNITY_EDITOR
static ProfilerMarker s_MLAPIRPCQueueProcess = new ProfilerMarker("MLAPIRPCQueueProcess");
static ProfilerMarker s_MLAPIRPCQueueSend = new ProfilerMarker("MLAPIRPCQueueSend");
#endif
// Batcher object used to manage the RPC batching on the send side
private MessageBatcher m_batcher = new MessageBatcher();
private int m_BatchThreshold = 512;
//NSS-TODO: Need to determine how we want to handle all other MLAPI send types
//Temporary place to keep internal MLAPI messages
private readonly List<FrameQueueItem> m_InternalMLAPISendQueue = new List<FrameQueueItem>();
/// <summary>
/// ProcessReceiveQueue
/// Public facing interface method to start processing all RPCs in the current inbound frame
/// </summary>
public void ProcessReceiveQueue(NetworkUpdateManager.NetworkUpdateStages currentStage)
{
bool AdvanceFrameHistory = false;
var rpcQueueContainer = NetworkingManager.Singleton.rpcQueueContainer;
if (rpcQueueContainer != null)
{
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_MLAPIRPCQueueProcess.Begin();
#endif
var CurrentFrame = rpcQueueContainer.GetCurrentFrame(QueueHistoryFrame.QueueFrameType.Inbound,currentStage);
if (CurrentFrame != null)
{
var currentQueueItem = CurrentFrame.GetFirstQueueItem();
while (currentQueueItem.queueItemType != RpcQueueContainer.QueueItemType.None)
{
AdvanceFrameHistory = true;
if (rpcQueueContainer.IsLoopBack() && !rpcQueueContainer.IsUsingBatching())
{
currentQueueItem.itemStream.Position = 1;
}
if (rpcQueueContainer.IsTesting())
{
Debug.Log("RPC invoked during the " + currentStage.ToString() + " update stage.");
}
NetworkingManager.InvokeRpc(currentQueueItem);
ProfilerStatManager.rpcsQueueProc.Record();
currentQueueItem = CurrentFrame.GetNextQueueItem();
}
//We call this to dispose of the shared stream writer and stream
CurrentFrame.CloseQueue();
}
if (AdvanceFrameHistory)
{
rpcQueueContainer.AdvanceFrameHistory(QueueHistoryFrame.QueueFrameType.Inbound);
}
}
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_MLAPIRPCQueueProcess.End();
#endif
}
/// <summary>
/// ProcessSendQueue
/// Called to send both performance RPC and internal messages and then flush the outbound queue
/// </summary>
public void ProcessSendQueue()
{
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_MLAPIRPCQueueSend.Begin();
#endif
RPCQueueSendAndFlush();
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_MLAPIRPCQueueSend.End();
#endif
InternalMessagesSendAndFlush();
}
/// <summary>
/// QueueInternalMLAPICommand
/// Added this as an example of how to add internal messages to the outbound send queue
/// </summary>
/// <param name="queueItem">message queue item to add<</param>
public void QueueInternalMLAPICommand(FrameQueueItem queueItem)
{
m_InternalMLAPISendQueue.Add(queueItem);
}
/// <summary>
/// Generic Sending Method for Internal Messages
/// TODO: Will need to open this up for discussion, but we will want to determine if this is how we want internal MLAPI command
/// messages to be sent. We might want specific commands to occur during specific network update regions (see NetworkUpdate
/// </summary>
public void InternalMessagesSendAndFlush()
{
foreach (FrameQueueItem queueItem in m_InternalMLAPISendQueue)
{
var PoolStream = queueItem.itemStream;
switch (queueItem.queueItemType)
{
case RpcQueueContainer.QueueItemType.CreateObject:
{
foreach (ulong clientId in queueItem.clientIds)
{
InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_ADD_OBJECT, queueItem.channel, PoolStream, queueItem.sendFlags);
}
ProfilerStatManager.rpcsSent.Record(queueItem.clientIds.Length);
break;
}
case RpcQueueContainer.QueueItemType.DestroyObject:
{
foreach (ulong clientId in queueItem.clientIds)
{
InternalMessageSender.Send(clientId, MLAPIConstants.MLAPI_DESTROY_OBJECT, queueItem.channel, PoolStream, queueItem.sendFlags);
}
ProfilerStatManager.rpcsSent.Record(queueItem.clientIds.Length);
break;
}
}
PoolStream.Dispose();
}
m_InternalMLAPISendQueue.Clear();
}
/// <summary>
/// RPCQueueSendAndFlush
/// Sends all RPC queue items in the current outbound frame
/// </summary>
private void RPCQueueSendAndFlush()
{
bool AdvanceFrameHistory = false;
var rpcQueueContainer = NetworkingManager.Singleton.rpcQueueContainer;
if (rpcQueueContainer != null)
{
var CurrentFrame = rpcQueueContainer.GetCurrentFrame(QueueHistoryFrame.QueueFrameType.Outbound,NetworkUpdateManager.NetworkUpdateStages.LateUpdate);
//If loopback is enabled
if (rpcQueueContainer.IsLoopBack())
{
//Migrate the outbound buffer to the inbound buffer
rpcQueueContainer.LoopbackSendFrame();
AdvanceFrameHistory = true;
}
else
{
if (CurrentFrame != null)
{
var currentQueueItem = CurrentFrame.GetFirstQueueItem();
while (currentQueueItem.queueItemType != RpcQueueContainer.QueueItemType.None)
{
AdvanceFrameHistory = true;
if(rpcQueueContainer.IsUsingBatching())
{
m_batcher.QueueItem(currentQueueItem);
m_batcher.SendItems(m_BatchThreshold, SendCallback);
}
else
{
SendFrameQueueItem(currentQueueItem);
}
currentQueueItem = CurrentFrame.GetNextQueueItem();
}
//If the size is < m_BatchThreshold then just send the messages
if(AdvanceFrameHistory && rpcQueueContainer.IsUsingBatching())
{
m_batcher.SendItems(0, SendCallback);
}
}
}
if (AdvanceFrameHistory)
{
rpcQueueContainer.AdvanceFrameHistory(QueueHistoryFrame.QueueFrameType.Outbound);
}
}
}
/// <summary>
/// SendCallback
/// This is the callback from the batcher when it need to send a batch
///
/// </summary>
/// <param name="clientId"> clientId to send to</param>
/// <param name="sendStream"> the stream to send</param>
private static void SendCallback(ulong clientId, MLAPI.MessageBatcher.SendStream sendStream)
{
var length = (int)sendStream.Stream.Length;
var bytes = sendStream.Stream.GetBuffer();
ArraySegment<byte> sendBuffer = new ArraySegment<byte>(bytes, 0, length);
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(clientId, sendBuffer, sendStream.channel);
}
/// <summary>
/// SendFrameQueueItem
/// Sends the RPC Queue Item to the specified destination
/// </summary>
/// <param name="queueItem">Information on what to send</param>
private void SendFrameQueueItem(FrameQueueItem queueItem)
{
switch (queueItem.queueItemType)
{
case RpcQueueContainer.QueueItemType.ServerRpc:
{
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(queueItem.networkId, queueItem.messageData, queueItem.channel);
//For each packet sent, we want to record how much data we have sent
ProfilerStatManager.bytesSent.Record((int)queueItem.streamSize);
ProfilerStatManager.rpcsSent.Record();
break;
}
case RpcQueueContainer.QueueItemType.ClientRpc:
{
foreach (ulong clientid in queueItem.clientIds)
{
NetworkingManager.Singleton.NetworkConfig.NetworkTransport.Send(clientid, queueItem.messageData, queueItem.channel);
//For each packet sent, we want to record how much data we have sent
ProfilerStatManager.bytesSent.Record((int)queueItem.streamSize);
}
//For each client we send to, we want to record how many RPCs we have sent
ProfilerStatManager.rpcsSent.Record(queueItem.clientIds.Length);
break;
}
}
}
}
}