async Task ReceiveRequestAsync(UdpClient udpClient) { // only proceed if there is available data in network buffer, or otherwise Receive() will block // average time for UdpClient.Available : 10 us UdpReceiveResult udpReceiveResult = await udpClient.ReceiveAsync(); NetworkReader networkReader = NetworkReaderPool.GetReader(udpReceiveResult.Buffer); long handshake = networkReader.ReadInt64(); if (handshake != secretHandshake) { // message is not for us NetworkReaderPool.Recycle(networkReader); throw new ProtocolViolationException("Invalid handshake"); } Request request = new Request(); request.Deserialize(networkReader); ProcessClientRequest(request, udpReceiveResult.RemoteEndPoint); NetworkReaderPool.Recycle(networkReader); }
public void GrowCapacity() { NetworkReaderPool.Capacity = 1; // create and recycle one PooledNetworkReader a = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); NetworkReaderPool.Recycle(a); // grow capacity NetworkReaderPool.Capacity = 2; // get two PooledNetworkReader b = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); PooledNetworkReader c = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); Assert.That(b, !Is.Null); Assert.That(c, !Is.Null); // exactly one should be reused, one should be new bool bReused = b == a; bool cReused = c == a; Assert.That((bReused && !cReused) || (!bReused && cReused)); }
public static void Prefix(NetworkReader reader) { if (!DevTools.Instance.Config.LoggingNetworkMessages) { return; } var newreader = NetworkReaderPool.GetReader(reader.buffer); newreader.Position = reader.Position; if (!MessagePacking.Unpack(newreader, out var key)) { return; } if (NetworkServer.handlers.TryGetValue(key, out var networkMessageDelegate) && networkMessageDelegate.Method.DeclaringType.IsGenericType) { string methodName = networkMessageDelegate.Method.DeclaringType.GetGenericArguments()[0].Name; if (methodName == "CommandMessage") { return; } if (DevTools.Instance.Config.DisabledLoggingNetworkMessages.Contains(methodName)) { return; } Log.Debug($"[Receiving: {methodName}]"); } NetworkReaderPool.Recycle(newreader); }
public void PoolReUsesReadersUpToSizeLimit() { NetworkReaderPool.Capacity = 1; // get 2 Readers PooledNetworkReader a = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); PooledNetworkReader b = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); // recycle all NetworkReaderPool.Recycle(a); ExpectWarn("NetworkReaderPool.Recycle, Pool was full leaving extra reader for GC", () => { NetworkReaderPool.Recycle(b); }); // get 2 new ones PooledNetworkReader c = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); PooledNetworkReader d = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); // exactly one should be reused, one should be new bool cReused = c == a || c == b; bool dReused = d == a || d == b; Assert.That((cReused && !dReused) || (!cReused && dReused)); }
public void ShrinkCapacity() { NetworkReaderPool.Capacity = 2; // get Reader and recycle so we have 2 in there, hence 'next' is at limit PooledNetworkReader a = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); PooledNetworkReader b = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); NetworkReaderPool.Recycle(a); NetworkReaderPool.Recycle(b); // shrink NetworkReaderPool.Capacity = 1; // get one. should return the only one which is still in there. PooledNetworkReader c = NetworkReaderPool.GetReader(default(ArraySegment <byte>)); Assert.That(c, !Is.Null); Assert.That(c == a || c == b); }
async Task ReceiveGameBroadcastAsync(UdpClient udpClient) { // only proceed if there is available data in network buffer, or otherwise Receive() will block // average time for UdpClient.Available : 10 us UdpReceiveResult udpReceiveResult = await udpClient.ReceiveAsync(); NetworkReader networkReader = NetworkReaderPool.GetReader(udpReceiveResult.Buffer); if (networkReader.ReadInt64() != secretHandshake) { NetworkReaderPool.Recycle(networkReader); return; } Response response = new Response(); response.Deserialize(networkReader); ProcessResponse(response, udpReceiveResult.RemoteEndPoint); NetworkReaderPool.Recycle(networkReader); }