Пример #1
0
 // helper function
 protected bool UnpackAndInvoke(NetworkReader reader, int channelId)
 {
     if (MessagePacker.Unpack(reader, out int msgType))
     {
         // try to invoke the handler for that message
         if (messageHandlers.TryGetValue(msgType, out NetworkMessageDelegate msgDelegate))
         {
             msgDelegate.Invoke(this, reader, channelId);
             lastMessageTime = Time.GameTime;
             return(true);
         }
         else
         {
             if (logger.LogEnabled())
             {
                 logger.Log("Unknown message ID " + msgType + " " + this + ". May be due to no existing RegisterHandler for this message.");
             }
             return(false);
         }
     }
     else
     {
         logger.LogError("Closed connection: " + this + ". Invalid message header.");
         Disconnect();
         return(false);
     }
 }
Пример #2
0
        // note: original HLAPI HandleBytes function handled >1 message in a while loop, but this wasn't necessary
        //       anymore because NetworkServer/NetworkClient Update both use while loops to handle >1 data events per
        //       frame already.
        //       -> in other words, we always receive 1 message per Receive call, never two.
        //       -> can be tested easily with a 1000ms send delay and then logging amount received in while loops here
        //          and in NetworkServer/Client Update. HandleBytes already takes exactly one.
        /// <summary>
        /// This function allows custom network connection classes to process data from the network before it is passed to the application.
        /// </summary>
        /// <param name="buffer">The data received.</param>
        internal void TransportReceive(ArraySegment <byte> buffer, int channelId)
        {
            if (buffer.Count == 0)
            {
                logger.LogError($"ConnectionRecv {this} Message was empty");
                return;
            }

            // unpack message
            using (PooledNetworkReader reader = NetworkReaderPool.GetReader(buffer))
            {
                if (MessagePacker.Unpack(reader, out int msgType))
                {
                    // try to invoke the handler for that message
                    if (messageHandlers.TryGetValue(msgType, out NetworkMessageDelegate msgDelegate))
                    {
                        msgDelegate.Invoke(this, reader, channelId);
                        lastMessageTime = Time.time;
                    }
                    else if (logger.LogEnabled())
                    {
                        logger.Log("Unknown message ID " + msgType + " " + this + ". May be due to no existing RegisterHandler for this message.");
                    }
                }
                else
                {
                    logger.LogError("Closed connection: " + this + ". Invalid message header.");
                    Disconnect();
                }
            }
        }
Пример #3
0
        // note: original HLAPI HandleBytes function handled >1 message in a while loop, but this wasn't necessary
        //       anymore because NetworkServer/NetworkClient Update both use while loops to handle >1 data events per
        //       frame already.
        //       -> in other words, we always receive 1 message per Receive call, never two.
        //       -> can be tested easily with a 1000ms send delay and then logging amount received in while loops here
        //          and in NetworkServer/Client Update. HandleBytes already takes exactly one.
        /// <summary>
        /// This function allows custom network connection classes to process data from the network before it is passed to the application.
        /// </summary>
        /// <param name="buffer">The data received.</param>
        internal void TransportReceive(ArraySegment <byte> buffer, int channelId)
        {
            if (buffer.Count == 0)
            {
                logger.LogError($"ConnectionRecv {this} Message was empty");
                return;
            }

            // unpack message
            using (PooledNetworkReader reader = NetworkReaderPool.GetReader(buffer))
            {
                if (MessagePacker.Unpack(reader, out int msgType))
                {
                    // try to invoke the handler for that message
                    if (InvokeHandler(msgType, reader, channelId))
                    {
                        lastMessageTime = Time.time;
                    }
                }
                else
                {
                    logger.LogError("Closed connection: " + this + ". Invalid message header.");
                    Disconnect();
                }
            }
        }
Пример #4
0
        public void TestPacking()
        {
            SceneMessage message = new SceneMessage()
            {
                value = "Hello world"
            };

            byte[] data = MessagePacker.Pack(message);

            SceneMessage unpacked = MessagePacker.Unpack <SceneMessage>(data);

            Assert.That(unpacked.value, Is.EqualTo("Hello world"));
        }