コード例 #1
0
        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);
            }
        }
コード例 #2
0
        void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                //State.receive_buffer = new byte[State.BufSize];

                socket.BeginReceiveFrom(State.receive_buffer, 0, State.BufSize, SocketFlags.None, ref epFrom, recv, state);


                //string receivedMessage = Encoding.ASCII.GetString(State.receive_buffer, 0, bytesLen);
                //Debug.Log(receivedMessage);

                int currentIndex = 0;

                bool[] receivedMessageBits = StaicBitFunctions.Byte2Bool(State.receive_buffer);

                int bitLenType = (int)StaicBitFunctions.CountBits((int)Packet.PacketType.NumberOfValues);

                bool[] packetLenBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, bitLenType);
                int    packetType    = (int)StaicBitFunctions.BitArrayToUInt(packetLenBits);

                //PrintValues(receivedMessage, 0);
                packetLenBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, 7);
                uint res = StaicBitFunctions.BitArrayToUInt(packetLenBits); // package lenght for data check

                bytesLen = socket.EndReceiveFrom(ar, ref epFrom);

                if (bytesLen > 0 && res == bytesLen)
                {
                    if (packetType == (int)Packet.PacketType.User)
                    {
                        PacketHeader receivedMessage = new PacketHeader(State.receive_buffer);
                        string       prefix          = receivedMessage.GetPrefix();
                        int          playerId        = receivedMessage.GetSenderId();

                        //Debug.Log(prefix);
                        if (messageHandler.ContainsKey(prefix))
                        {
                            //Thread thread;
                            //thread = new Thread(() => UDPPManager.AddMessage(prefix, playerId.ToString(), receivedMessage));
                            //thread.Start();
                            //Debug.LogError("it should work");
                            ThreadManager.AddOneTimeAction(delegate { UDPPlayerManager.AddMessage(prefix, playerId.ToString(), receivedMessage); });
                        }
                    }
                    else
                    {
                        int    commandLen   = (int)StaicBitFunctions.CountBits((int)ServerPacket.ServerCommand.ConnectedOther); // last element of enum
                        bool[] packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, commandLen);
                        res = StaicBitFunctions.BitArrayToUInt(packetInBits);                                                   // command num

                        if ((int)ServerPacket.ServerCommand.ConnectOther == (int)res)                                           // new command for server other connections
                        {
                            int bitLen = (int)StaicBitFunctions.CountBits(GetMaxPlayerCount());
                            packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, bitLen);
                            uint lenRes = StaicBitFunctions.BitArrayToUInt(packetInBits);
                            int  playerCountConnected = (int)lenRes;
                            Debug.Log("connected players: " + playerCountConnected);
                            if (playerCountConnected > 0)
                            {
                                int len = (int)StaicBitFunctions.CountBits(maxPlayers);
                                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;
                                    //Debug.Log("id :? " + custId);
                                    if (UDPPlayerManager.ContainsIdInConnected(custId.ToString()))
                                    {
                                        byte[] send_buffer = packetForServer.CommandPacket(ServerPacket.ServerCommand.ConnectedOther);
                                        socket.Send(send_buffer);
                                        break;
                                    }

                                    //UDPPManager.ActivatePlayer(custId.ToString());
                                    MainThreadManager.ExecuteOnMainThread(delegate
                                    {
                                        UDPPlayerManager.ActivatePlayer(custId.ToString());
                                    });

                                    Debug.Log("connection other id:" + custId);
                                }
                            }
                        }
                        if ((int)ServerPacket.ServerCommand.DisconnectedFromOther == (int)res)
                        {
                            int bitLen = (int)StaicBitFunctions.CountBits(GetMaxPlayerCount());
                            packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, bitLen);
                            res          = StaicBitFunctions.BitArrayToUInt(packetInBits);
                            int custId = (int)res;

                            Debug.Log("Disconnected id: " + custId);
                            byte[] send_buffer = packetForServer.CommandPacket(ServerPacket.ServerCommand.DisconnectedFromOther);
                            socket.Send(send_buffer);

                            MainThreadManager.ExecuteOnMainThread(delegate
                            {
                                UDPPlayerManager.DeactivatePlayer(custId.ToString());
                            });
                        }
                        if ((int)ServerPacket.ServerCommand.Start == (int)res && !gameStarted) // new command for server other connections
                        {
                            lock (objectLoadSceneLock)
                            {
                                packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, 1);
                                packetForServer.AddData(packetInBits[0]);
                                packetForServer.Command(ServerPacket.ServerCommand.Start);
                                gameStarted = packetInBits[0];
                            }
                        }
                        if ((int)ServerPacket.ServerCommand.Timer == (int)res) // new command for server other connections
                        {
                            packetInBits = StaicBitFunctions.BitsReverseLen(receivedMessageBits, ref currentIndex, 12);
                            res          = StaicBitFunctions.BitArrayToUInt(packetInBits);
                            packetForServer.AddData(res);
                            packetForServer.Command(ServerPacket.ServerCommand.Timer);
                        }
                    }
                }
                else
                {
                    //Debug.Log("result in packet len: " + res + " received packet: " + bytesLen);
                    //Debug.Log("received packet len: " + bytesLen);
                    //Debug.LogError("Packet Dropped"); // harmeless error
                    //Packet.PrintValues(receivedMessageBits, 0);
                }
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
            }
        }