Exemplo n.º 1
0
        // use this to implicitly become ready
        // -> extraMessage can contain character selection, etc.
        public static bool AddPlayer(NetworkConnection readyConn, MessageBase extraMessage)
        {
            // ensure valid ready connection
            if (readyConn != null)
            {
                ready = true;
                readyConnection = readyConn;
            }

            if (!ready)
            {
                Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                return false;
            }

            if (readyConnection.playerController != null)
            {
                Debug.LogError("ClientScene::AddPlayer: a PlayerController was already added. Did you call AddPlayer twice?");
                return false;
            }

            if (LogFilter.Debug) { Debug.Log("ClientScene::AddPlayer() called with connection [" + readyConnection + "]"); }

            AddPlayerMessage msg = new AddPlayerMessage();
            if (extraMessage != null)
            {
                NetworkWriter writer = new NetworkWriter();
                extraMessage.Serialize(writer);
                msg.value = writer.ToArray();
            }
            readyConnection.Send((short)MsgType.AddPlayer, msg);
            return true;
        }
Exemplo n.º 2
0
        public virtual bool Send(short msgType, MessageBase msg, int channelId = Channels.DefaultReliable)
        {
            NetworkWriter writer = new NetworkWriter();

            msg.Serialize(writer);

            // pack message and send
            byte[] message = Protocol.PackMessage((ushort)msgType, writer.ToArray());
            return(SendBytes(message, channelId));
        }
Exemplo n.º 3
0
        public virtual bool Send(short msgType, MessageBase msg)
        {
            NetworkWriter writer = new NetworkWriter();

            msg.Serialize(writer);

            // pack message and send
            byte[] message = Protocol.PackMessage((ushort)msgType, writer.ToArray());
            return(SendBytes(message));
        }
Exemplo n.º 4
0
        public static byte[] PackMessage(int msgType, MessageBase msg)
        {
            // reset cached writer length and position
            packWriter.SetLength(0);

            // write message type
            packWriter.Write((short)msgType);

            // serialize message into writer
            msg.Serialize(packWriter);

            // return byte[]
            return(packWriter.ToArray());
        }
Exemplo n.º 5
0
        public static byte[] PackMessage(int msgType, MessageBase msg)
        {
            NetworkWriter packWriter = NetworkWriterPool.GetPooledWriter();

            // write message type
            packWriter.Write((short)msgType);

            // serialize message into writer
            msg.Serialize(packWriter);

            // return byte[]
            byte[] data = packWriter.ToArray();
            NetworkWriterPool.Recycle(packWriter);
            return(data);
        }
Exemplo n.º 6
0
        public static byte[] PackMessage(int msgType, MessageBase msg)
        {
            using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
            {
                try
                {
                    // write message type
                    writer.WriteInt16((short)msgType);

                    // serialize message into writer
                    msg.Serialize(writer);

                    // return byte[]
                    return(writer.ToArray());
                }
                finally { }
            }
        }
Exemplo n.º 7
0
        public static byte[] PackMessage(int msgType, MessageBase msg)
        {
            NetworkWriter writer = NetworkWriterPool.GetWriter();

            try
            {
                // write message type
                writer.Write((short)msgType);

                // serialize message into writer
                msg.Serialize(writer);

                // return byte[]
                return(writer.ToArray());
            }
            finally
            {
                NetworkWriterPool.Recycle(writer);
            }
        }
Exemplo n.º 8
0
        // use this to implicitly become ready
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is too high, max is " + PlayerController.MaxPlayersPerClient);
                }
                return(false);
            }
            if (playerControllerId > PlayerController.MaxPlayersPerClient / 2)
            {
                if (LogFilter.logWarn)
                {
                    Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
                }
            }

            // fill out local players array
            while (playerControllerId >= s_LocalPlayers.Count)
            {
                s_LocalPlayers.Add(new PlayerController());
            }

            // ensure valid ready connection
            if (readyConn != null)
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }

            if (!s_IsReady)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                }
                return(false);
            }

            PlayerController existingPlayerController;

            if (s_ReadyConnection.GetPlayerController(playerControllerId, out existingPlayerController))
            {
                if (existingPlayerController.IsValid && existingPlayerController.gameObject != null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
                    }
                    return(false);
                }
            }

            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer() for ID " + playerControllerId + " called with connection [" + s_ReadyConnection + "]");
            }

            var msg = new AddPlayerMessage();

            msg.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                var writer = new NetworkWriter();
                extraMessage.Serialize(writer);
                msg.msgData = writer.ToArray();
            }
            s_ReadyConnection.Send((short)MsgType.AddPlayer, msg);
            return(true);
        }
Exemplo n.º 9
0
 public void Write(MessageBase msg)
 {
     msg.Serialize(this);
 }