public static void ActivatePlayer(string id) { if (!playerCreated) { CreateMaxPlayer(UDPConnectionManager.GetMaxPlayerCount()); playerCreated = true; } if (freePlayerManagers.Count > 0 && !playersID.Contains(id)) { Debug.Log("Activated user: " + id); UDPPlayer player = freePlayerManagers[0]; int playerId = int.Parse(id); player.SetPlayerId(playerId); player.ActivatePlayerReceivers(); player.ActivateSender(); playerById.Add(id, player); freePlayerManagers.Remove(player); playersID.Add(id); player.gameObject.transform.position = UDPConnectionManager.singleton.transform.position; player.gameObject.SetActive(true); } }
void ReceiveCallbackConnection(IAsyncResult ar) { try { socket.BeginReceiveFrom(State.receive_buffer, 0, State.BufSize, SocketFlags.None, ref epFrom, recv, state); //Debug.LogError("Attemp Connect function"); //string receivedMessage = Encoding.ASCII.GetString(State.receive_buffer, 0, bytesLen); //Debug.Log(receivedMessage); bool[] receivedMessageBits = StaicBitFunctions.Byte2Bool(State.receive_buffer); //PrintValues(receivedMessage, 0); int currentIndex = 0; int bitLenType = (int)StaicBitFunctions.CountBits((int)Packet.PacketType.NumberOfValues); bool[] packetLenBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, bitLenType); int packetType = (int)StaicBitFunctions.BitArrayToUInt(packetLenBits); bool[] packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, 7); uint res = StaicBitFunctions.BitArrayToUInt(packetInBits); // package lenght for data check //Debug.Log(res); bytesLen = socket.EndReceiveFrom(ar, ref epFrom); if (bytesLen > 0 && res == bytesLen) { if (packetType == (int)Packet.PacketType.ServerCommand) { int commandLen = (int)StaicBitFunctions.CountBits((int)ServerPacket.ServerCommand.ConnectedOther); // last element of enum packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, commandLen); res = StaicBitFunctions.BitArrayToUInt(packetInBits); // command num //Debug.Log("result: " + res); if ((int)ServerPacket.ServerCommand.Connected == (int)res) { packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, 7); res = StaicBitFunctions.BitArrayToUInt(packetInBits); UDPConnectionManager.maxPlayers = (int)res; //Debug.Log("max players: " + res); int len = (int)StaicBitFunctions.CountBits((int)res); packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, len); res = StaicBitFunctions.BitArrayToUInt(packetInBits); int custId = (int)res; packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, 7); len = (int)StaicBitFunctions.BitArrayToUInt(packetInBits); packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, len); res = StaicBitFunctions.BitArrayToUInt(packetInBits); spawnId = (int)res; //UDPPManager.ActivatePlayer(custId.ToString()); id = custId; recv = new AsyncCallback(ReceiveCallback); serverConnectionState = ConnectionState.Connected; Debug.Log("connected"); MainThreadManager.ExecuteOnMainThread(delegate { mainPlayer.SetPlayerId(custId); mainPlayer.ActivatePlayerReceivers(); mainPlayer.ActivateSender(); UDPPlayerManager.ActivatePlayer(custId.ToString()); m_connectedToServer.Invoke(true); }); isDisconnected = false; //Debug.Log("Should print"); //Debug.Log("connection id:" + custId); } else if ((int)ServerPacket.ServerCommand.Full == (int)res) { Debug.Log("Server Is full"); MainThreadManager.ExecuteOnMainThread(delegate { m_connectedToServer.Invoke(false); }); serverConnectionState = ConnectionState.ServerIsFull; } else if ((int)ServerPacket.ServerCommand.ConnectOther == (int)res) // new command for server other connections { int len = (int)StaicBitFunctions.CountBits(UDPConnectionManager.maxPlayers); packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, len); uint lenRes = StaicBitFunctions.BitArrayToUInt(packetInBits); int playerCountConnected = (int)lenRes; //Debug.Log("connected players: " + playerCountConnected); if (playerCountConnected > 0) { for (int length = 0; length < playerCountConnected; length++) // 0 cant exist as player index { packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, len); res = StaicBitFunctions.BitArrayToUInt(packetInBits); int custId = (int)res; //UDPPManager.ActivatePlayer(custId.ToString()); MainThreadManager.ExecuteOnMainThread(delegate { UDPPlayerManager.ActivatePlayer(custId.ToString()); }); //Debug.Log("connect other id:" + custId); } } } } } else { //Debug.LogError("Packet Dropped"); // harmeless error } } catch (Exception e) { //string str = e.ToString(); MainThreadManager.ExecuteOnMainThread(delegate { m_connectedToServer.Invoke(false); }); serverConnectionState = ConnectionState.ServerTimeout; Debug.Log(e); } }