Exemple #1
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 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();
            }
        }
Exemple #2
0
        /// <summary>
        /// This virtual function allows custom network connection classes to process data send by the application before it goes to the network transport layer.
        /// </summary>
        /// <param name="channelId">Channel to send data on.</param>
        /// <param name="bytes">Data to send.</param>
        /// <returns></returns>
        public virtual bool TransportSend(int channelId, byte[] bytes)
        {
#if MIRROR_PROFILING
            NetworkProfiler.RecordTraffic(NetworkDirection.Outgoing, channelId, bytes.Length);
#endif
            if (Transport.activeTransport.ClientConnected())
            {
                return(Transport.activeTransport.ClientSend(channelId, bytes));
            }
            else if (Transport.activeTransport.ServerActive())
            {
                return(Transport.activeTransport.ServerSend(connectionId, channelId, bytes));
            }
            return(false);
        }