|
| 1 | +using MLAPI.Serialization.Pooled; |
| 2 | +using MLAPI.Serialization; |
| 3 | +using MLAPI.Configuration; |
| 4 | +using MLAPI.Profiling; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System; |
| 8 | +using System.IO; |
| 9 | + |
| 10 | +namespace MLAPI |
| 11 | +{ |
| 12 | + class MessageBatcher |
| 13 | + { |
| 14 | + public class SendStream |
| 15 | + { |
| 16 | + public FrameQueueItem Item; |
| 17 | + public PooledBitStream Stream = PooledBitStream.Get(); |
| 18 | + public bool Empty = true; |
| 19 | + } |
| 20 | + |
| 21 | + // Stores the stream of batched RPC to send to each client, by ClientId |
| 22 | + private Dictionary<ulong, SendStream> SendDict = new Dictionary<ulong, SendStream>(); |
| 23 | + private PooledBitWriter Writer = new PooledBitWriter(PooledBitStream.Get()); |
| 24 | + |
| 25 | + // Used to store targets, internally |
| 26 | + private ulong[] TargetList = new ulong[0]; |
| 27 | + |
| 28 | + // Used to mark longer lengths. Works because we can't have zero-sized messages |
| 29 | + private const byte LongLenMarker = 0; |
| 30 | + |
| 31 | + private void PushLength(int length, ref PooledBitWriter writer) |
| 32 | + { |
| 33 | + // If length is single byte we write it |
| 34 | + if (length < 256) |
| 35 | + { |
| 36 | + writer.WriteByte((byte)length); // write the amounts of bytes that are coming up |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + // otherwise we write a two-byte length |
| 41 | + writer.WriteByte(LongLenMarker); // mark larger size |
| 42 | + writer.WriteByte((byte)(length % 256)); // write the length modulo 256 |
| 43 | + writer.WriteByte((byte)(length / 256)); // write the length divided by 256 |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private int PopLength(in BitStream messageStream) |
| 48 | + { |
| 49 | + int read = messageStream.ReadByte(); |
| 50 | + // if we read a non-zero value, we have a single byte length |
| 51 | + // or a -1 error we can return |
| 52 | + if (read != LongLenMarker) |
| 53 | + { |
| 54 | + return read; |
| 55 | + } |
| 56 | + // otherwise, a two-byte length follows. We'll read in len1, len2 |
| 57 | + int len1 = messageStream.ReadByte(); |
| 58 | + if (len1 < 0) |
| 59 | + { |
| 60 | + // pass errors back to caller |
| 61 | + return len1; |
| 62 | + } |
| 63 | + int len2 = messageStream.ReadByte(); |
| 64 | + if (len2 < 0) |
| 65 | + { |
| 66 | + // pass errors back to caller |
| 67 | + return len2; |
| 68 | + } |
| 69 | + |
| 70 | + return len1 + len2 * 256; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// FillTargetList |
| 75 | + /// Fills a list with the ClientId's an item is targeted to |
| 76 | + /// </summary> |
| 77 | + /// <param name="item">the FrameQueueItem we want targets for</param> |
| 78 | + /// <param name="list">the list to fill</param> |
| 79 | + private static void FillTargetList(in FrameQueueItem item, ref ulong[] list) |
| 80 | + { |
| 81 | + switch (item.QueueItemType) |
| 82 | + { |
| 83 | + case RPCQueueManager.QueueItemType.ServerRpc: |
| 84 | + Array.Resize(ref list, 1); |
| 85 | + list[0] = item.NetworkId; |
| 86 | + break; |
| 87 | + case RPCQueueManager.QueueItemType.ClientRpc: |
| 88 | + // copy the list |
| 89 | + list = item.ClientIds.ToArray(); |
| 90 | + break; |
| 91 | + default: |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// QueueItem |
| 98 | + /// Add a FrameQueueItem to be sent |
| 99 | + /// </summary>queueItem |
| 100 | + /// <param name="item">the threshold in bytes</param> |
| 101 | + public void QueueItem(in FrameQueueItem item) |
| 102 | + { |
| 103 | + FillTargetList(item, ref TargetList); |
| 104 | + |
| 105 | + foreach (ulong clientId in TargetList) |
| 106 | + { |
| 107 | + if (!SendDict.ContainsKey(clientId)) |
| 108 | + { |
| 109 | + // todo: consider what happens if many clients join and leave the game consecutively |
| 110 | + // we probably need a cleanup mechanism at some point |
| 111 | + SendDict[clientId] = new SendStream(); |
| 112 | + } |
| 113 | + |
| 114 | + if (SendDict[clientId].Empty) |
| 115 | + { |
| 116 | + SendDict[clientId].Empty = false; |
| 117 | + SendDict[clientId].Item = item; |
| 118 | + Writer.SetStream(SendDict[clientId].Stream); |
| 119 | + |
| 120 | + Writer.WriteBit(false); // Encrypted |
| 121 | + Writer.WriteBit(false); // Authenticated |
| 122 | + |
| 123 | + switch (item.QueueItemType) |
| 124 | + { |
| 125 | + // 6 bits are used for the message type, which is an MLAPIConstants |
| 126 | + case RPCQueueManager.QueueItemType.ServerRpc: |
| 127 | + Writer.WriteBits(MLAPIConstants.MLAPI_SERVER_RPC, 6); // MessageType |
| 128 | + break; |
| 129 | + case RPCQueueManager.QueueItemType.ClientRpc: |
| 130 | + Writer.WriteBits(MLAPIConstants.MLAPI_CLIENT_RPC, 6); // MessageType |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // write the amounts of bytes that are coming up |
| 136 | + PushLength(item.MessageData.Count, ref Writer); |
| 137 | + |
| 138 | + // write the message to send |
| 139 | + // todo: is there a faster alternative to .ToArray() |
| 140 | + Writer.WriteBytes(item.MessageData.ToArray(), item.MessageData.Count); |
| 141 | + |
| 142 | + ProfilerStatManager.bytesSent.Record((int)item.MessageData.Count); |
| 143 | + ProfilerStatManager.rpcsSent.Record(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public delegate void SendCallbackType(ulong clientId, SendStream messageStream); |
| 148 | + public delegate void ReceiveCallbackType(BitStream messageStream, MLAPI.RPCQueueManager.QueueItemType messageType, ulong clientId, float time); |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// SendItems |
| 152 | + /// Send any batch of RPC that are of length above threshold |
| 153 | + /// </summary> |
| 154 | + /// <param name="threshold"> the threshold in bytes</param> |
| 155 | + /// <param name="sendCallback"> the function to call for sending the batch</param> |
| 156 | + public void SendItems(int threshold, SendCallbackType sendCallback) |
| 157 | + { |
| 158 | + foreach (KeyValuePair<ulong, SendStream> entry in SendDict) |
| 159 | + { |
| 160 | + if (!entry.Value.Empty) |
| 161 | + { |
| 162 | + // read the queued message |
| 163 | + int length = (int)SendDict[entry.Key].Stream.Length; |
| 164 | + |
| 165 | + if (length >= threshold) |
| 166 | + { |
| 167 | + sendCallback(entry.Key, entry.Value); |
| 168 | + ProfilerStatManager.rpcBatchesSent.Record(); |
| 169 | + |
| 170 | + // clear the batch that was sent from the SendDict |
| 171 | + entry.Value.Stream.SetLength(0); |
| 172 | + entry.Value.Stream.Position = 0; |
| 173 | + entry.Value.Empty = true; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// ReceiveItems |
| 181 | + /// Process the messageStream and call the callback with individual RPC messages |
| 182 | + /// </summary> |
| 183 | + /// <param name="messageStream"> the messageStream containing the batched RPC</param> |
| 184 | + /// <param name="receiveCallback"> the callback to call has type int f(message, type, clientId, time) </param> |
| 185 | + /// <param name="messageType"> the message type to pass back to callback</param> |
| 186 | + /// <param name="clientId"> the clientId to pass back to callback</param> |
| 187 | + /// <param name="receiveTime"> the packet receive time to pass back to callback</param> |
| 188 | + public int ReceiveItems(in BitStream messageStream, ReceiveCallbackType receiveCallback, MLAPI.RPCQueueManager.QueueItemType messageType, ulong clientId, float receiveTime) |
| 189 | + { |
| 190 | + do |
| 191 | + { |
| 192 | + // read the length of the next RPC |
| 193 | + int rpcSize = PopLength(messageStream); |
| 194 | + |
| 195 | + if (rpcSize < 0) |
| 196 | + { |
| 197 | + // abort if there's an error reading lengths |
| 198 | + return 0; |
| 199 | + } |
| 200 | + |
| 201 | + // copy what comes after current stream position |
| 202 | + long pos = messageStream.Position; |
| 203 | + BitStream copy = PooledBitStream.Get(); |
| 204 | + copy.SetLength(rpcSize); |
| 205 | + copy.Position = 0; |
| 206 | + Buffer.BlockCopy(messageStream.GetBuffer(), (int)pos, copy.GetBuffer(), 0, rpcSize); |
| 207 | + |
| 208 | + receiveCallback(copy, messageType, clientId, receiveTime); |
| 209 | + |
| 210 | + // seek over the RPC |
| 211 | + // RPCReceiveQueueItem peeks at content, it doesn't advance |
| 212 | + messageStream.Seek(rpcSize, SeekOrigin.Current); |
| 213 | + } while (messageStream.Position < messageStream.Length); |
| 214 | + return 0; |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments