コード例 #1
0
        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));
                }
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        /// <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
                }
            }
        }
コード例 #4
0
        /// <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);
                }
            }
        }
コード例 #5
0
 static void WGenericInt32()
 {
     using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
     {
         for (int i = 0; i < 1000; i++)
         {
             writer.Write <int>(i * 1000);
         }
     }
 }
コード例 #6
0
    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();
    }
コード例 #7
0
        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));
                }
            }
        }