// 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 virtual 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 recieved.</param> public virtual void TransportReceive(ArraySegment <byte> buffer) { // unpack message NetworkReader reader = new NetworkReader(buffer); if (MessagePacker.UnpackMessage(reader, out int msgType)) { // logging if (logNetworkMessages) { Debug.Log("ConnectionRecv con:" + connectionId + " msgType:" + msgType + " content:" + BitConverter.ToString(buffer.Array, buffer.Offset, buffer.Count)); } // try to invoke the handler for that message if (InvokeHandler(msgType, reader)) { lastMessageTime = Time.time; } } else { Debug.LogError("Closed connection: " + connectionId + ". Invalid message header."); Disconnect(); } }
// 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) { // unpack message using (PooledNetworkReader networkReader = NetworkReaderPool.GetReader(buffer)) { if (MessagePacker.UnpackMessage(networkReader, out int msgType)) { // logging if (logger.LogEnabled()) { logger.Log("ConnectionRecv " + this + " msgType:" + msgType + " content:" + BitConverter.ToString(buffer.Array, buffer.Offset, buffer.Count)); } // try to invoke the handler for that message if (InvokeHandler(msgType, networkReader, channelId)) { lastMessageTime = Time.time; } } else { logger.LogError("Closed connection: " + this + ". Invalid message header."); Disconnect(); } } }
// 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 virtual 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 recieved.</param> public virtual void TransportReceive(ArraySegment <byte> buffer) { #if MIRROR_PROFILING // -1 for now since we don't know the incoming segment channel NetworkProfiler.RecordTraffic(NetworkDirection.Incoming, -1, buffer.Count); #endif // unpack message NetworkReader reader = new NetworkReader(buffer); if (MessagePacker.UnpackMessage(reader, out int msgType)) { // logging if (logNetworkMessages) { Debug.Log("ConnectionRecv con:" + connectionId + " msgType:" + msgType + " content:" + BitConverter.ToString(buffer.Array, buffer.Offset, buffer.Count)); } // try to invoke the handler for that message if (InvokeHandler(msgType, reader)) { lastMessageTime = Time.time; } } else { Debug.LogError("Closed connection: " + connectionId + ". Invalid message header."); Disconnect(); } }
// handle this message // 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. public virtual void TransportReceive(byte[] buffer) { // unpack message NetworkReader reader = new NetworkReader(buffer); if (MessagePacker.UnpackMessage(reader, out int msgType)) { if (logNetworkMessages) { Debug.Log("ConnectionRecv con:" + connectionId + " msgType:" + msgType + " content:" + BitConverter.ToString(buffer)); } // try to invoke the handler for that message if (InvokeHandler(msgType, reader)) { lastMessageTime = Time.time; } } else { Debug.LogError("HandleBytes UnpackMessage failed for: " + BitConverter.ToString(buffer)); } }