コード例 #1
0
        // this is like SendToReady - but it doesn't check the ready flag on the connection.
        // this is used for ObjectDestroy messages.
        static bool SendToObservers <T>(NetworkIdentity identity, T msg) where T : MessageBase
        {
            if (LogFilter.Debug)
            {
                Debug.Log("Server.SendToObservers id:" + typeof(T));
            }

            if (identity != null && identity.observers != null)
            {
                // pack message into byte[] once
                byte[] bytes = MessagePacker.Pack(msg);

                bool result = true;
                foreach (KeyValuePair <int, NetworkConnection> kvp in identity.observers)
                {
                    result &= kvp.Value.SendBytes(bytes);
                }
                return(result);
            }
            return(false);
        }
コード例 #2
0
ファイル: NetworkServer.cs プロジェクト: bejita968/Mirror
        public static bool SendToReady<T>(NetworkIdentity identity,T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase
        {
            if (LogFilter.Debug) Debug.Log("Server.SendToReady msgType:" + typeof(T));

            if (identity != null && identity.observers != null)
            {
                // pack message into byte[] once
                byte[] bytes = MessagePacker.Pack(msg);

                bool result = true;
                foreach (KeyValuePair<int, NetworkConnection> kvp in identity.observers)
                {
                    if (kvp.Value.isReady)
                    {
                        result &= kvp.Value.SendBytes(bytes, channelId);
                    }
                }
                return result;
            }
            return false;
        }
コード例 #3
0
        // connect host mode
        internal static void ConnectLocalServer()
        {
            if (LogFilter.Debug)
            {
                Debug.Log("Client Connect Local Server");
            }
            active = true;
            RegisterSystemHandlers(true);

            connectState = ConnectState.Connected;

            // create local connection to server
            connection = new ULocalConnectionToServer();
            SetHandlers(connection);

            // create server connection to local client
            ULocalConnectionToClient connectionToClient = new ULocalConnectionToClient();

            NetworkServer.SetLocalConnection(connectionToClient);

            localClientPacketQueue.Enqueue(MessagePacker.Pack(new ConnectMessage()));
        }
コード例 #4
0
        /// <summary>
        /// This function invokes the registered handler function for a message.
        /// <para>Network connections used by the NetworkClient and NetworkServer use this function for handling network messages.</para>
        /// </summary>
        /// <typeparam name="T">The message type to unregister.</typeparam>
        /// <param name="msg">The message object to process.</param>
        /// <returns></returns>
        public bool InvokeHandler <T>(T msg, int channelId) where T : IMessageBase
        {
            // get writer from pool
            NetworkWriter writer = NetworkWriterPool.GetWriter();

            // if it is a value type,  just use typeof(T) to avoid boxing
            // this works because value types cannot be derived
            // if it is a reference type (for example IMessageBase),
            // ask the message for the real type
            int msgType = MessagePacker.GetId(typeof(T).IsValueType ? typeof(T) : msg.GetType());

            MessagePacker.Pack(msg, writer);
            ArraySegment <byte> segment       = writer.ToArraySegment();
            NetworkReader       networkReader = NetworkReaderPool.GetReader(segment);
            bool result = InvokeHandler(msgType, networkReader, channelId);

            NetworkReaderPool.Recycle(networkReader);

            // recycle writer and return
            NetworkWriterPool.Recycle(writer);
            return(result);
        }
コード例 #5
0
 /// <summary>
 /// This sends a network message with a message ID on the connection. This message is sent on channel zero, which by default is the reliable channel.
 /// </summary>
 /// <typeparam name="T">The message type to unregister.</typeparam>
 /// <param name="msg">The message to send.</param>
 /// <param name="channelId">The transport layer channel to send on.</param>
 /// <returns></returns>
 public virtual bool Send <T>(T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase
 {
     // pack message and send
     byte[] message = MessagePacker.Pack(msg);
     return(SendBytes(message, channelId));
 }