public void MessageIsBitPacked([ValueSource(nameof(cases))] TestCase TestCase) { long value = TestCase.value; int expectedBitCount = TestCase.expectedBits; var inStruct = new BitPackStruct { myValue = value, }; using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter()) { // generic write, uses generated function that should include bitPacking writer.Write(inStruct); Assert.That(writer.BitPosition, Is.EqualTo(expectedBitCount)); using (PooledNetworkReader reader = NetworkReaderPool.GetReader(writer.ToArraySegment(), null)) { var outStruct = reader.Read <BitPackStruct>(); Assert.That(reader.BitPosition, Is.EqualTo(expectedBitCount)); Assert.That(outStruct, Is.EqualTo(inStruct)); } } }
internal static void RegisterRequestDelegate <T>(Type invokeClass, string cmdName, RequestDelegate <T> func, bool cmdRequireAuthority = true) { async UniTaskVoid Wrapper(NetworkBehaviour obj, NetworkReader reader, INetworkConnection senderConnection, int replyId) { /// invoke the serverRpc and send a reply message T result = await func(obj, reader, senderConnection, replyId); using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter()) { writer.Write(result); var serverRpcReply = new ServerRpcReply { replyId = replyId, payload = writer.ToArraySegment() }; senderConnection.Send(serverRpcReply); } } void CmdWrapper(NetworkBehaviour obj, NetworkReader reader, INetworkConnection senderConnection, int replyId) { Wrapper(obj, reader, senderConnection, replyId).Forget(); } RegisterDelegate(invokeClass, cmdName, MirrorInvokeType.ServerRpc, CmdWrapper, cmdRequireAuthority); }
/// <summary> /// Sends discovery request from client /// </summary> public void BroadcastDiscoveryRequest() { if (clientUdpClient == null) { return; } IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, serverBroadcastListenPort); using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter()) { writer.WriteInt64(secretHandshake); try { Request request = GetRequest(); writer.Write(request); ArraySegment <byte> data = writer.ToArraySegment(); clientUdpClient.SendAsync(data.Array, data.Count, endPoint); } catch (Exception) { // It is ok if we can't broadcast to one of the addresses } } }
/// <summary> /// Reply to the client to inform it of this server /// </summary> /// <remarks> /// Override if you wish to ignore server requests based on /// custom criteria such as language, full server game mode or difficulty /// </remarks> /// <param name="request">Request coming from client</param> /// <param name="endpoint">Address of the client that sent the request</param> protected virtual void ProcessClientRequest(Request request, IPEndPoint endpoint) { Response info = ProcessRequest(request, endpoint); if (info == null) { return; } using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter()) { try { writer.WriteInt64(secretHandshake); writer.Write(info); ArraySegment <byte> data = writer.ToArraySegment(); // signature matches // send response serverUdpClient.Send(data.Array, data.Count, endpoint); } catch (Exception ex) { Debug.LogException(ex, this); } } }
static void WGenericInt32() { using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter()) { for (int i = 0; i < 1000; i++) { writer.Write <int>(i * 1000); } } }
public void SumOnServer(byte[] serverReceivedData) { PooledNetworkReader pooledNetworkReader = NetworkReaderPool.GetReader(serverReceivedData); int a = pooledNetworkReader.Read <int>(); int b = pooledNetworkReader.Read <int>(); pooledNetworkReader.Dispose(); PooledNetworkWriter pooledNetworkWriter = NetworkWriterPool.GetWriter(); int result = a + b; pooledNetworkWriter.Write(result); pooledNetworkWriter.ToArray(); byte[] dataToSendClient = pooledNetworkWriter.ToArray(); Debug.LogWarning("Server " + result); RpcReceive(DataCommand.TEST_SumOnServer, dataToSendClient); pooledNetworkWriter.Dispose(); }
public void MessageIsBitPacked() { var inStruct = new BitPackStruct { myValue = value, }; using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter()) { // generic write, uses generated function that should include bitPacking writer.Write(inStruct); Assert.That(writer.BitPosition, Is.EqualTo(32)); using (PooledNetworkReader reader = NetworkReaderPool.GetReader(writer.ToArraySegment(), null)) { var outStruct = reader.Read <BitPackStruct>(); Assert.That(reader.BitPosition, Is.EqualTo(32)); Assert.That(outStruct, Is.EqualTo(inStruct)); } } }