ToArray() public méthode

Returns a copy of internal array of bytes the writer is using, it copies only the bytes used.

public ToArray ( ) : byte[]
Résultat byte[]
Exemple #1
0
    public void TestNetworkWriterSize()
    {
        var writer = new UnityEngine.Networking.NetworkWriter();

        writer.Write((byte)42);
        byte[] bytes = writer.ToArray();
        Assert.AreEqual(1, bytes.Length);
    }
    public byte[] GetVoosInitBytes()
    {
        var voosBuffer = VOOS_INIT_BYTES_REUSED;

        Array.Clear(voosBuffer, 0, voosBuffer.Length);
        var writer = new UNET.NetworkWriter(voosBuffer);

        voosEngine.SerializePlayerInitPayloadV2(writer);
        return(Util.GZip(writer.ToArray()));
    }
    static void CommandPIStats(CommandArg[] args)
    {
        var engine = consoleInstance;

        System.Action doBinTest = () =>
        {
            byte[] buffer = new byte[10 * 1024 * 1024];
            var    writer = new UnityEngine.Networking.NetworkWriter(buffer);
            engine.SerializePlayerInitPayloadV2(writer);

            byte[] bytesUsed = writer.ToArray();
            byte[] zipped    = Util.GZip(bytesUsed);
            Util.Log($"{zipped.Length / 1024} kb zipped, {bytesUsed.Length / 1024} kb orig");
        };

        doBinTest();

        Util.Log($"Current stats:");
        FindObjectOfType <NetworkingController>().LogInitPlayerPayloadStats();
    }
Exemple #4
0
        void GenerateError(byte error)
        {
            NetworkMessageDelegate msgDelegate;

            if (m_MessageHandlers.TryGetValue((short)MsgType.Error, out msgDelegate))
            {
                ErrorMessage msg = new ErrorMessage();
                msg.errorCode = error;

                // write the message to a local buffer
                NetworkWriter writer = new NetworkWriter();
                msg.Serialize(writer);

                NetworkMessage netMsg = new NetworkMessage();
                netMsg.msgType   = (short)MsgType.Error;
                netMsg.reader    = new NetworkReader(writer.ToArray());
                netMsg.conn      = m_Connection;
                netMsg.channelId = 0;
                msgDelegate(netMsg);
            }
        }
Exemple #5
0
	public void WriteSetChunk(NetworkWriter nw)
	{
		if (updateMessageBytes) {

			NetworkWriter temp = new NetworkWriter();

			temp.Write(index.x);
			temp.Write(index.y);
			
			temp.Write(version);
			temp.Write(tileCount);
			
			for (int i = 0; i < VesselChunk.SIZE; i++) {
				
				for (int j = 0; j < VesselChunk.SIZE; j++) {
					
					VesselTile tile = TileAt(i, j);
					
					if (tile != null) {
						
						temp.Write(i);
						temp.Write(j);
						temp.Write((byte)tile.floor0);
						temp.Write((byte)tile.floor1);
						temp.Write((byte)tile.wallMask);
						temp.Write(tile.wallNode);
						
					}
				}
			}
			
			messageBytes = temp.ToArray();
			
			updateMessageBytes = false;
		}

		nw.WriteBytesFull(messageBytes);
	}
Exemple #6
0
        // invoked by unity runtime immediately after the regular "Update()" function.
        internal void UNetUpdate()
        {
            // go through each channel
            for (int channelId = 0; channelId < NetworkServer.numChannels; channelId++)
            {
                // serialize all the dirty components and send (if any were dirty)
                NetworkWriter writer = new NetworkWriter();
                if (OnSerializeAllSafely(m_NetworkBehaviours, writer, false, channelId))
                {
#if UNITY_EDITOR
                    UnityEditor.NetworkDetailStats.IncrementStat(
                        UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
                        (short)MsgType.UpdateVars, name, 1);
#endif
                    // construct message and send
                    UpdateVarsMessage message = new UpdateVarsMessage();
                    message.netId   = netId;
                    message.payload = writer.ToArray();

                    NetworkServer.SendByChannelToReady(gameObject, (short)MsgType.UpdateVars, message, channelId);
                }
            }
        }
        protected void SendCommandInternal(int cmdHash, NetworkWriter writer, int channelId, string cmdName)
        {
            // local players can always send commands, regardless of authority, other objects must have authority.
            if (!(isLocalPlayer || hasAuthority))
            {
                if (LogFilter.logWarn)
                {
                    Debug.LogWarning("Trying to send command for object without authority.");
                }
                return;
            }

            if (ClientScene.readyConnection == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Send command attempted with no client running [client=" + connectionToServer + "].");
                }
                return;
            }

            // construct the message
            CommandMessage message = new CommandMessage();

            message.netId   = netId;
            message.cmdHash = cmdHash;
            message.payload = writer.ToArray();

            ClientScene.readyConnection.SendByChannel((short)MsgType.Command, message, channelId);

#if UNITY_EDITOR
            UnityEditor.NetworkDetailStats.IncrementStat(
                UnityEditor.NetworkDetailStats.NetworkDirection.Outgoing,
                (short)MsgType.Command, cmdName, 1);
#endif
        }
 /// <summary>
 /// <para>This sends the contents of a NetworkWriter object on the connection.</para>
 /// </summary>
 /// <param name="writer">A writer object containing data to send.</param>
 /// <param name="channelId">The transport channel to send on.</param>
 /// <returns>
 /// <para>True if the data was sent.</para>
 /// </returns>
 public virtual bool SendWriter(NetworkWriter writer, int channelId)
 {
     if (this.logNetworkMessages)
     {
         this.LogSend(writer.ToArray());
     }
     return (this.CheckChannel(channelId) && this.m_Channels[channelId].SendWriter(writer));
 }
        // 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)
            {
                if (!s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }

            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 + "]");
            }

#if ENABLE_UNET_HOST_MIGRATION
            if (!hasMigrationPending())
            {
#endif
            var msg = new AddPlayerMessage();
            msg.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                var writer = new NetworkWriter();
                extraMessage.Serialize(writer);
                msg.msgData = writer.ToArray();
                msg.msgSize = writer.Position;
            }
            s_ReadyConnection.Send(MsgType.AddPlayer, msg);
#if ENABLE_UNET_HOST_MIGRATION
        }

        else
        {
            return(SendReconnectMessage(extraMessage));
        }
#endif
            return(true);
        }
 /// <summary>
 ///   <para>This sends the contents of a NetworkWriter object on the connection.</para>
 /// </summary>
 /// <param name="writer">A writer object containing data to send.</param>
 /// <param name="channelId">The transport channel to send on.</param>
 /// <returns>
 ///   <para>True if the data was sent.</para>
 /// </returns>
 public virtual bool SendWriter(NetworkWriter writer, int channelId)
 {
   if (this.logNetworkMessages)
     this.LogSend(writer.ToArray());
   if (this.CheckChannel(channelId))
     return this.m_Channels[channelId].SendWriter(writer);
   return false;
 }
Exemple #11
0
 public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
 {
     PlayerController controller;
     if (playerControllerId < 0)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
         }
         return false;
     }
     if (playerControllerId > 0x20)
     {
         if (LogFilter.logError)
         {
             Debug.LogError(string.Concat(new object[] { "ClientScene::AddPlayer: playerControllerId of ", playerControllerId, " is too high, max is ", 0x20 }));
         }
         return false;
     }
     if ((playerControllerId > 0x10) && LogFilter.logWarn)
     {
         Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
     }
     while (playerControllerId >= s_LocalPlayers.Count)
     {
         s_LocalPlayers.Add(new PlayerController());
     }
     if (readyConn == null)
     {
         if (!s_IsReady)
         {
             if (LogFilter.logError)
             {
                 Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
             }
             return false;
         }
     }
     else
     {
         s_IsReady = true;
         s_ReadyConnection = readyConn;
     }
     if ((s_ReadyConnection.GetPlayerController(playerControllerId, out controller) && controller.IsValid) && (controller.gameObject != null))
     {
         if (LogFilter.logError)
         {
             Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
         }
         return false;
     }
     if (LogFilter.logDebug)
     {
         Debug.Log(string.Concat(new object[] { "ClientScene::AddPlayer() for ID ", playerControllerId, " called with connection [", s_ReadyConnection, "]" }));
     }
     AddPlayerMessage msg = new AddPlayerMessage {
         playerControllerId = playerControllerId
     };
     if (extraMessage != null)
     {
         NetworkWriter writer = new NetworkWriter();
         extraMessage.Serialize(writer);
         msg.msgData = writer.ToArray();
         msg.msgSize = writer.Position;
     }
     s_ReadyConnection.Send(0x25, msg);
     return true;
 }
Exemple #12
0
        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 > 32)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is too high, max is " + 32);
                }
                return(false);
            }
            if (playerControllerId > 16 && LogFilter.logWarn)
            {
                Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
            }
            while (playerControllerId >= s_LocalPlayers.Count)
            {
                s_LocalPlayers.Add(new PlayerController());
            }
            if (readyConn == null)
            {
                if (!s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }
            PlayerController playerController;

            if (s_ReadyConnection.GetPlayerController(playerControllerId, out playerController) && playerController.IsValid && playerController.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 + "]");
            }
            AddPlayerMessage addPlayerMessage = new AddPlayerMessage();

            addPlayerMessage.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                NetworkWriter networkWriter = new NetworkWriter();
                extraMessage.Serialize(networkWriter);
                addPlayerMessage.msgData = networkWriter.ToArray();
                addPlayerMessage.msgSize = networkWriter.Position;
            }
            s_ReadyConnection.Send(37, addPlayerMessage);
            return(true);
        }
 public bool SendMessage(int connectionId, NetworkWriter writer, int channel, out NetworkError Error)
 {
     byte error;
     byte[] buffer = writer.ToArray();
     NetworkTransport.Send(SocketId, connectionId, channel, buffer, buffer.Length, out error);
     Error = (NetworkError)error;
     if (Error != NetworkError.Ok)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
 /// <summary>
 ///   <para>This adds a player object for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer will be called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message.</para>
 /// </summary>
 /// <param name="readyConn">The connection to become ready for this client.</param>
 /// <param name="playerControllerId">The local player ID number.</param>
 /// <param name="extraMessage">An extra message object that can be passed to the server for this player.</param>
 /// <returns>
 ///   <para>True if player was added.</para>
 /// </returns>
 public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
 {
   if ((int) playerControllerId < 0)
   {
     if (LogFilter.logError)
       Debug.LogError((object) ("ClientScene::AddPlayer: playerControllerId of " + (object) playerControllerId + " is negative"));
     return false;
   }
   if ((int) playerControllerId > 32)
   {
     if (LogFilter.logError)
       Debug.LogError((object) ("ClientScene::AddPlayer: playerControllerId of " + (object) playerControllerId + " is too high, max is " + (object) 32));
     return false;
   }
   if ((int) playerControllerId > 16 && LogFilter.logWarn)
     Debug.LogWarning((object) ("ClientScene::AddPlayer: playerControllerId of " + (object) playerControllerId + " is unusually high"));
   while ((int) playerControllerId >= ClientScene.s_LocalPlayers.Count)
     ClientScene.s_LocalPlayers.Add(new PlayerController());
   if (readyConn == null)
   {
     if (!ClientScene.s_IsReady)
     {
       if (LogFilter.logError)
         Debug.LogError((object) "Must call AddPlayer() with a connection the first time to become ready.");
       return false;
     }
   }
   else
   {
     ClientScene.s_IsReady = true;
     ClientScene.s_ReadyConnection = readyConn;
   }
   PlayerController playerController;
   if (ClientScene.s_ReadyConnection.GetPlayerController(playerControllerId, out playerController) && playerController.IsValid && (Object) playerController.gameObject != (Object) null)
   {
     if (LogFilter.logError)
       Debug.LogError((object) ("ClientScene::AddPlayer: playerControllerId of " + (object) playerControllerId + " already in use."));
     return false;
   }
   if (LogFilter.logDebug)
     Debug.Log((object) ("ClientScene::AddPlayer() for ID " + (object) playerControllerId + " called with connection [" + (object) ClientScene.s_ReadyConnection + "]"));
   if (ClientScene.s_ReconnectId == -1)
   {
     AddPlayerMessage addPlayerMessage = new AddPlayerMessage();
     addPlayerMessage.playerControllerId = playerControllerId;
     if (extraMessage != null)
     {
       NetworkWriter writer = new NetworkWriter();
       extraMessage.Serialize(writer);
       addPlayerMessage.msgData = writer.ToArray();
       addPlayerMessage.msgSize = (int) writer.Position;
     }
     ClientScene.s_ReadyConnection.Send((short) 37, (MessageBase) addPlayerMessage);
   }
   else
   {
     if (LogFilter.logDebug)
       Debug.Log((object) ("ClientScene::AddPlayer reconnect " + (object) ClientScene.s_ReconnectId));
     if (ClientScene.s_Peers == null)
     {
       ClientScene.SetReconnectId(-1, (PeerInfoMessage[]) null);
       if (LogFilter.logError)
         Debug.LogError((object) "ClientScene::AddPlayer: reconnecting, but no peers.");
       return false;
     }
     foreach (PeerInfoMessage peer in ClientScene.s_Peers)
     {
       if (peer.playerIds != null && peer.connectionId == ClientScene.s_ReconnectId)
       {
         foreach (PeerInfoPlayer playerId in peer.playerIds)
         {
           ReconnectMessage reconnectMessage = new ReconnectMessage();
           reconnectMessage.oldConnectionId = ClientScene.s_ReconnectId;
           reconnectMessage.netId = playerId.netId;
           reconnectMessage.playerControllerId = playerId.playerControllerId;
           if (extraMessage != null)
           {
             NetworkWriter writer = new NetworkWriter();
             extraMessage.Serialize(writer);
             reconnectMessage.msgData = writer.ToArray();
             reconnectMessage.msgSize = (int) writer.Position;
           }
           ClientScene.s_ReadyConnection.Send((short) 47, (MessageBase) reconnectMessage);
         }
       }
     }
     ClientScene.SetReconnectId(-1, (PeerInfoMessage[]) null);
   }
   return true;
 }
Exemple #15
0
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            bool result;

            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                result = false;
            }
            else if (playerControllerId > 32)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError(string.Concat(new object[]
                    {
                        "ClientScene::AddPlayer: playerControllerId of ",
                        playerControllerId,
                        " is too high, max is ",
                        32
                    }));
                }
                result = false;
            }
            else
            {
                if (playerControllerId > 16)
                {
                    if (LogFilter.logWarn)
                    {
                        Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
                    }
                }
                while ((int)playerControllerId >= ClientScene.s_LocalPlayers.Count)
                {
                    ClientScene.s_LocalPlayers.Add(new PlayerController());
                }
                if (readyConn == null)
                {
                    if (!ClientScene.s_IsReady)
                    {
                        if (LogFilter.logError)
                        {
                            Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                        }
                        return(false);
                    }
                }
                else
                {
                    ClientScene.s_IsReady         = true;
                    ClientScene.s_ReadyConnection = readyConn;
                }
                PlayerController playerController;
                if (ClientScene.s_ReadyConnection.GetPlayerController(playerControllerId, out playerController))
                {
                    if (playerController.IsValid && playerController.gameObject != null)
                    {
                        if (LogFilter.logError)
                        {
                            Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
                        }
                        return(false);
                    }
                }
                if (LogFilter.logDebug)
                {
                    Debug.Log(string.Concat(new object[]
                    {
                        "ClientScene::AddPlayer() for ID ",
                        playerControllerId,
                        " called with connection [",
                        ClientScene.s_ReadyConnection,
                        "]"
                    }));
                }
                if (!ClientScene.hasMigrationPending())
                {
                    AddPlayerMessage addPlayerMessage = new AddPlayerMessage();
                    addPlayerMessage.playerControllerId = playerControllerId;
                    if (extraMessage != null)
                    {
                        NetworkWriter networkWriter = new NetworkWriter();
                        extraMessage.Serialize(networkWriter);
                        addPlayerMessage.msgData = networkWriter.ToArray();
                        addPlayerMessage.msgSize = (int)networkWriter.Position;
                    }
                    ClientScene.s_ReadyConnection.Send(37, addPlayerMessage);
                    result = true;
                }
                else
                {
                    result = ClientScene.SendReconnectMessage(extraMessage);
                }
            }
            return(result);
        }
 internal void SendSpawnMessage(NetworkIdentity uv, NetworkConnection conn)
 {
     if (uv.serverOnly)
     return;
       if (uv.sceneId.IsEmpty())
       {
     ObjectSpawnMessage objectSpawnMessage = new ObjectSpawnMessage();
     objectSpawnMessage.netId = uv.netId;
     objectSpawnMessage.assetId = uv.assetId;
     objectSpawnMessage.position = uv.transform.position;
     NetworkWriter writer = new NetworkWriter();
     uv.UNetSerializeAllVars(writer);
     if ((int) writer.Position > 0)
       objectSpawnMessage.payload = writer.ToArray();
     if (conn != null)
       conn.Send((short) 3, (MessageBase) objectSpawnMessage);
     else
       NetworkServer.SendToReady(uv.gameObject, (short) 3, (MessageBase) objectSpawnMessage);
     NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, (short) 3, uv.assetId.ToString(), 1);
       }
       else
       {
     ObjectSpawnSceneMessage spawnSceneMessage = new ObjectSpawnSceneMessage();
     spawnSceneMessage.netId = uv.netId;
     spawnSceneMessage.sceneId = uv.sceneId;
     spawnSceneMessage.position = uv.transform.position;
     NetworkWriter writer = new NetworkWriter();
     uv.UNetSerializeAllVars(writer);
     if ((int) writer.Position > 0)
       spawnSceneMessage.payload = writer.ToArray();
     if (conn != null)
       conn.Send((short) 10, (MessageBase) spawnSceneMessage);
     else
       NetworkServer.SendToReady(uv.gameObject, (short) 3, (MessageBase) spawnSceneMessage);
     NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Outgoing, (short) 10, "sceneId", 1);
       }
 }
Exemple #17
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)
            {
                if (!s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }

            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 + "]");
            }

#if ENABLE_UNET_HOST_MIGRATION
            if (s_ReconnectId == ReconnectIdInvalid)
            {
#endif
            var msg = new AddPlayerMessage();
            msg.playerControllerId = playerControllerId;
            if (extraMessage != null)
            {
                var writer = new NetworkWriter();
                extraMessage.Serialize(writer);
                msg.msgData = writer.ToArray();
                msg.msgSize = writer.Position;
            }
            s_ReadyConnection.Send(MsgType.AddPlayer, msg);
#if ENABLE_UNET_HOST_MIGRATION
        }

        else
        {
            if (LogFilter.logDebug)
            {
                Debug.Log("ClientScene::AddPlayer reconnect " + s_ReconnectId);
            }

            if (s_Peers == null)
            {
                SetReconnectId(ReconnectIdInvalid, null);
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: reconnecting, but no peers.");
                }
                return(false);
            }

            // reconnect all the players
            for (int i = 0; i < s_Peers.Length; i++)
            {
                var peer = s_Peers[i];
                if (peer.playerIds == null)
                {
                    // this coluld be empty if this peer had no players
                    continue;
                }
                if (peer.connectionId == s_ReconnectId)
                {
                    for (int pid = 0; pid < peer.playerIds.Length; pid++)
                    {
                        var msg = new ReconnectMessage();
                        msg.oldConnectionId    = s_ReconnectId;
                        msg.netId              = peer.playerIds[pid].netId;
                        msg.playerControllerId = peer.playerIds[pid].playerControllerId;
                        if (extraMessage != null)
                        {
                            var writer = new NetworkWriter();
                            extraMessage.Serialize(writer);
                            msg.msgData = writer.ToArray();
                            msg.msgSize = writer.Position;
                        }

                        s_ReadyConnection.Send(MsgType.ReconnectPlayer, msg);
                    }
                }
            }
            // this should only be done once.
            SetReconnectId(ReconnectIdInvalid, null);
        }
#endif
            return(true);
        }
Exemple #18
0
    // line only
    private byte[] StrokeMessage(byte tile,
                                 Color color,
                                 int size,
                                 Vector2 start,
                                 Vector2 end)
    {
        var writer = new NetworkWriter();

        uint sx = (uint) Mathf.FloorToInt(start.x);
        uint sy = (uint) Mathf.FloorToInt(start.y);
        uint ex = (uint) Mathf.FloorToInt(end.x);
        uint ey = (uint) Mathf.FloorToInt(end.y);

        byte index = world.ColorToPalette(color, true);

        writer.Write((byte) Type.TileStroke);
        writer.Write(tile);
        PackBits(writer,
                 5, sx,
                 5, sy,
                 5, ex,
                 5, ey,
                 4, index,
                 4, (uint) size);

        return writer.ToArray();
    }
        /// <summary>
        ///   <para>This adds a player object for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer will be called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message.</para>
        /// </summary>
        /// <param name="readyConn">The connection to become ready for this client.</param>
        /// <param name="playerControllerId">The local player ID number.</param>
        /// <param name="extraMessage">An extra message object that can be passed to the server for this player.</param>
        /// <returns>
        ///   <para>True if player was added.</para>
        /// </returns>
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            if ((int)playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError((object)("ClientScene::AddPlayer: playerControllerId of " + (object)playerControllerId + " is negative"));
                }
                return(false);
            }
            if ((int)playerControllerId > 32)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError((object)("ClientScene::AddPlayer: playerControllerId of " + (object)playerControllerId + " is too high, max is " + (object)32));
                }
                return(false);
            }
            if ((int)playerControllerId > 16 && LogFilter.logWarn)
            {
                Debug.LogWarning((object)("ClientScene::AddPlayer: playerControllerId of " + (object)playerControllerId + " is unusually high"));
            }
            while ((int)playerControllerId >= ClientScene.s_LocalPlayers.Count)
            {
                ClientScene.s_LocalPlayers.Add(new PlayerController());
            }
            if (readyConn == null)
            {
                if (!ClientScene.s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError((object)"Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                ClientScene.s_IsReady         = true;
                ClientScene.s_ReadyConnection = readyConn;
            }
            PlayerController playerController;

            if (ClientScene.s_ReadyConnection.GetPlayerController(playerControllerId, out playerController) && playerController.IsValid && (Object)playerController.gameObject != (Object)null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError((object)("ClientScene::AddPlayer: playerControllerId of " + (object)playerControllerId + " already in use."));
                }
                return(false);
            }
            if (LogFilter.logDebug)
            {
                Debug.Log((object)("ClientScene::AddPlayer() for ID " + (object)playerControllerId + " called with connection [" + (object)ClientScene.s_ReadyConnection + "]"));
            }
            if (ClientScene.s_ReconnectId == -1)
            {
                AddPlayerMessage addPlayerMessage = new AddPlayerMessage();
                addPlayerMessage.playerControllerId = playerControllerId;
                if (extraMessage != null)
                {
                    NetworkWriter writer = new NetworkWriter();
                    extraMessage.Serialize(writer);
                    addPlayerMessage.msgData = writer.ToArray();
                    addPlayerMessage.msgSize = (int)writer.Position;
                }
                ClientScene.s_ReadyConnection.Send((short)37, (MessageBase)addPlayerMessage);
            }
            else
            {
                if (LogFilter.logDebug)
                {
                    Debug.Log((object)("ClientScene::AddPlayer reconnect " + (object)ClientScene.s_ReconnectId));
                }
                if (ClientScene.s_Peers == null)
                {
                    ClientScene.SetReconnectId(-1, (PeerInfoMessage[])null);
                    if (LogFilter.logError)
                    {
                        Debug.LogError((object)"ClientScene::AddPlayer: reconnecting, but no peers.");
                    }
                    return(false);
                }
                foreach (PeerInfoMessage peer in ClientScene.s_Peers)
                {
                    if (peer.playerIds != null && peer.connectionId == ClientScene.s_ReconnectId)
                    {
                        foreach (PeerInfoPlayer playerId in peer.playerIds)
                        {
                            ReconnectMessage reconnectMessage = new ReconnectMessage();
                            reconnectMessage.oldConnectionId    = ClientScene.s_ReconnectId;
                            reconnectMessage.netId              = playerId.netId;
                            reconnectMessage.playerControllerId = playerId.playerControllerId;
                            if (extraMessage != null)
                            {
                                NetworkWriter writer = new NetworkWriter();
                                extraMessage.Serialize(writer);
                                reconnectMessage.msgData = writer.ToArray();
                                reconnectMessage.msgSize = (int)writer.Position;
                            }
                            ClientScene.s_ReadyConnection.Send((short)47, (MessageBase)reconnectMessage);
                        }
                    }
                }
                ClientScene.SetReconnectId(-1, (PeerInfoMessage[])null);
            }
            return(true);
        }
        /// <summary>
        /// <para>This adds a player GameObject for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message.</para>
        /// </summary>
        /// <param name="readyConn">The connection to become ready for this client.</param>
        /// <param name="playerControllerId">The local player ID number.</param>
        /// <param name="extraMessage">An extra message object that can be passed to the server for this player.</param>
        /// <returns>
        /// <para>True if player was added.</para>
        /// </returns>
        public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
        {
            PlayerController controller;

            if (playerControllerId < 0)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
                }
                return(false);
            }
            if (playerControllerId > 0x20)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError(string.Concat(new object[] { "ClientScene::AddPlayer: playerControllerId of ", playerControllerId, " is too high, max is ", 0x20 }));
                }
                return(false);
            }
            if ((playerControllerId > 0x10) && LogFilter.logWarn)
            {
                Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
            }
            while (playerControllerId >= s_LocalPlayers.Count)
            {
                s_LocalPlayers.Add(new PlayerController());
            }
            if (readyConn == null)
            {
                if (!s_IsReady)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
                    }
                    return(false);
                }
            }
            else
            {
                s_IsReady         = true;
                s_ReadyConnection = readyConn;
            }
            if (s_ReadyConnection.GetPlayerController(playerControllerId, out controller) && (controller.IsValid && (controller.gameObject != null)))
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
                }
                return(false);
            }
            if (LogFilter.logDebug)
            {
                Debug.Log(string.Concat(new object[] { "ClientScene::AddPlayer() for ID ", playerControllerId, " called with connection [", s_ReadyConnection, "]" }));
            }
            if (!hasMigrationPending())
            {
                AddPlayerMessage msg = new AddPlayerMessage {
                    playerControllerId = playerControllerId
                };
                if (extraMessage != null)
                {
                    NetworkWriter writer = new NetworkWriter();
                    extraMessage.Serialize(writer);
                    msg.msgData = writer.ToArray();
                    msg.msgSize = writer.Position;
                }
                s_ReadyConnection.Send(0x25, msg);
            }
            else
            {
                return(SendReconnectMessage(extraMessage));
            }
            return(true);
        }
 private bool SendMessage(NetworkWriter writer, int channelId, out NetworkError error)
 {
     error = NetworkError.WrongConnection;
     if (!isConnected)
     {
         Debug.LogWarning("LLApiClient is not Connected");
         return false;
     }
     byte errorByte;
     byte[] buffer = writer.ToArray();
     NetworkTransport.Send(SocketId, ConnectionId, channelId, buffer, buffer.Length, out errorByte);
     error = (NetworkError)errorByte;
     if(error!= NetworkError.Ok)
     {
         Debug.LogError("LLApiClient Sock: " + SocketId + " ConId: " + ConnectionId + " Error Sending Message : " + error);
         return false;
     }
     return true;
 }
 /// <summary>
 /// <para>This adds a player GameObject for this client. This causes an AddPlayer message to be sent to the server, and NetworkManager.OnServerAddPlayer is called. If an extra message was passed to AddPlayer, then OnServerAddPlayer will be called with a NetworkReader that contains the contents of the message.</para>
 /// </summary>
 /// <param name="readyConn">The connection to become ready for this client.</param>
 /// <param name="playerControllerId">The local player ID number.</param>
 /// <param name="extraMessage">An extra message object that can be passed to the server for this player.</param>
 /// <returns>
 /// <para>True if player was added.</para>
 /// </returns>
 public static bool AddPlayer(NetworkConnection readyConn, short playerControllerId, MessageBase extraMessage)
 {
     PlayerController controller;
     if (playerControllerId < 0)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is negative");
         }
         return false;
     }
     if (playerControllerId > 0x20)
     {
         if (LogFilter.logError)
         {
             Debug.LogError(string.Concat(new object[] { "ClientScene::AddPlayer: playerControllerId of ", playerControllerId, " is too high, max is ", 0x20 }));
         }
         return false;
     }
     if ((playerControllerId > 0x10) && LogFilter.logWarn)
     {
         Debug.LogWarning("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " is unusually high");
     }
     while (playerControllerId >= s_LocalPlayers.Count)
     {
         s_LocalPlayers.Add(new PlayerController());
     }
     if (readyConn == null)
     {
         if (!s_IsReady)
         {
             if (LogFilter.logError)
             {
                 Debug.LogError("Must call AddPlayer() with a connection the first time to become ready.");
             }
             return false;
         }
     }
     else
     {
         s_IsReady = true;
         s_ReadyConnection = readyConn;
     }
     if (s_ReadyConnection.GetPlayerController(playerControllerId, out controller) && (controller.IsValid && (controller.gameObject != null)))
     {
         if (LogFilter.logError)
         {
             Debug.LogError("ClientScene::AddPlayer: playerControllerId of " + playerControllerId + " already in use.");
         }
         return false;
     }
     if (LogFilter.logDebug)
     {
         Debug.Log(string.Concat(new object[] { "ClientScene::AddPlayer() for ID ", playerControllerId, " called with connection [", s_ReadyConnection, "]" }));
     }
     if (s_ReconnectId == -1)
     {
         AddPlayerMessage msg = new AddPlayerMessage {
             playerControllerId = playerControllerId
         };
         if (extraMessage != null)
         {
             NetworkWriter writer = new NetworkWriter();
             extraMessage.Serialize(writer);
             msg.msgData = writer.ToArray();
             msg.msgSize = writer.Position;
         }
         s_ReadyConnection.Send(0x25, msg);
     }
     else
     {
         if (LogFilter.logDebug)
         {
             Debug.Log("ClientScene::AddPlayer reconnect " + s_ReconnectId);
         }
         if (s_Peers == null)
         {
             SetReconnectId(-1, null);
             if (LogFilter.logError)
             {
                 Debug.LogError("ClientScene::AddPlayer: reconnecting, but no peers.");
             }
             return false;
         }
         for (int i = 0; i < s_Peers.Length; i++)
         {
             PeerInfoMessage message2 = s_Peers[i];
             if ((message2.playerIds != null) && (message2.connectionId == s_ReconnectId))
             {
                 for (int j = 0; j < message2.playerIds.Length; j++)
                 {
                     ReconnectMessage message3 = new ReconnectMessage {
                         oldConnectionId = s_ReconnectId,
                         netId = message2.playerIds[j].netId,
                         playerControllerId = message2.playerIds[j].playerControllerId
                     };
                     if (extraMessage != null)
                     {
                         NetworkWriter writer2 = new NetworkWriter();
                         extraMessage.Serialize(writer2);
                         message3.msgData = writer2.ToArray();
                         message3.msgSize = writer2.Position;
                     }
                     s_ReadyConnection.Send(0x2f, message3);
                 }
             }
         }
         SetReconnectId(-1, null);
     }
     return true;
 }