Example #1
0
        // Start is called before the first frame update

        protected void Awake()
        {
            if (oneTime)
            {
                messages      = new SortedList();
                scriptEnabled = true;
                packageIndex  = 0;
                if (gameObjectSpecificPrefix)
                {
                    Vector3 position = gameObject.transform.position;
                    Vector3 rotation = gameObject.transform.rotation.eulerAngles;
                    Vector3 scale    = gameObject.transform.lossyScale;

                    Vector3 index = position + scale + rotation;

                    prefix = prefix + gameObject.name + (gameObject.transform.childCount + index.magnitude).ToString();
                }

                if (!UDPConnectionManager.CheckIfPrefixExist(prefix))
                {
                    UDPConnectionManager.AddPrefix(prefix);
                }
                UDPConnectionManager.AddToMessageHandler(prefix, this);
                oneTime = false;
            }
        }
Example #2
0
        public void ResetPacketHeader(int prefixBitLen)
        {
            packetType = Packet.PacketType.User;

            int byteCountMaxPlayer = UDPConnectionManager.GetMaxPlayerCount();

            byteCountMaxPlayer = (int)CountBits(byteCountMaxPlayer); // bit count for player index

            int bitLen = byteCountMaxPlayer + 5 + prefixBitLen;

            headerInBits = new bool[bitLen];

            int headerIndex = 0;

            senderPlayerId = UDPConnectionManager.singleton.GetPlayerId();

            bool[] bytes = ByteArray2BitArray(IntToBytes((int)senderPlayerId));

            AppendBitsFromBoolArrayToBoolComplete(ref headerIndex, byteCountMaxPlayer, bytes, ref headerInBits);

            bytes = ByteArray2BitArray(IntToBytes(prefixBitLen));

            AppendBitsFromBoolArrayToBoolComplete(ref headerIndex, 5, bytes, ref headerInBits);

            int prefixIndex = UDPConnectionManager.PrefixIndex(packetPrefix);

            bytes = ByteArray2BitArray(IntToBytes(prefixIndex));

            AppendBitsFromBoolArrayToBoolComplete(ref headerIndex, prefixBitLen, bytes, ref headerInBits);
        }
Example #3
0
 public static void AddMessage(string prefix, string playerId, PacketHeader receivedMessage)
 {
     if (playerById.ContainsKey(playerId) && playerById[playerId].ContainsKeyInMessageHandler(prefix))
     {
         //Debug.Log("sender id: " + playerId);
         Packet packet = new Packet(receivedMessage);
         if (playerId == UDPConnectionManager.singleton.GetPlayerId().ToString() && UDPConnectionManager.PlayerIsAlive())
         {
             playerById[playerId].AddMessage(prefix, packet);
             if (playerById[playerId].ConstainsKeyInBothHandler(prefix) && UDPConnectionManager.ContainsPrefix(prefix))
             {
                 Packet secPacket = new Packet(receivedMessage);
                 UDPConnectionManager.AddMessageToMainPlayer(prefix, secPacket);
             }
         }
         else if (playerId != UDPConnectionManager.singleton.GetPlayerId().ToString())
         {
             if (playerById[playerId].PlayerIsAlive())
             {
                 playerById[playerId].AddMessage(prefix, packet);
             }
         }
     }
     else
     {
         //Debug.Log("NonPlayerMessage");
         Packet packet = new Packet(receivedMessage);
         UDPConnectionManager.AddMssageToOther(prefix, packet);
     }
 }
Example #4
0
        public PacketHeader(string prefix)
        {
            base.Setup();
            packetPrefix       = prefix;
            packetReceiveIndex = 0;
            packetType         = Packet.PacketType.User;

            int byteCountMaxPlayer = UDPConnectionManager.GetMaxPlayerCount();

            byteCountMaxPlayer = (int)CountBits(byteCountMaxPlayer); // bit count for player index

            senderPlayerId = UDPConnectionManager.singleton.GetPlayerId();

            int bitLen = byteCountMaxPlayer + 5 + Packet.GetPrefixBitLen();

            headerInBits = new bool[bitLen];

            int headerIndex = 0;

            bool[] bytes = ByteArray2BitArray(IntToBytes((int)senderPlayerId));

            AppendBitsFromBoolArrayToBoolComplete(ref headerIndex, byteCountMaxPlayer, bytes, ref headerInBits);

            bytes = ByteArray2BitArray(IntToBytes((int)Packet.GetPrefixBitLen()));

            AppendBitsFromBoolArrayToBoolComplete(ref headerIndex, 5, bytes, ref headerInBits);

            int prefixIndex = UDPConnectionManager.PrefixIndex(prefix);

            bytes = ByteArray2BitArray(IntToBytes(prefixIndex));



            AppendBitsFromBoolArrayToBoolComplete(ref headerIndex, Packet.GetPrefixBitLen(), bytes, ref headerInBits);
        }
Example #5
0
 protected void CustomUpdate()
 {
     if (!sendManualy && UDPConnectionManager.ConectedToServer() && !firstTimeInConnection)
     {
         packet.SetManualyIncresedIndex(false);
         SendMessage();
     }
 }
Example #6
0
 // Start is called before the first frame update
 void Start()
 {
     if (connectionManager == null)
     {
         UDPConnectionManager[] connectionManagers = FindObjectsOfType <UDPConnectionManager>();
         foundManagers     = connectionManagers;
         connectionManager = connectionManagers[0].gameObject.GetComponent <UDPConnectionManager>();
     }
 }
Example #7
0
 public void SendMultipleMessages(bool value)
 {
     if (scriptEnabled && UDPConnectionManager.ConectedToServer())
     {
         this.value = value;
         Thread thread = new Thread(() => SendMessage());
         thread.Name = "Object State";
         thread.Start();
     }
 }
 private void OnEnable()
 {
     if (singleton == null)
     {
         Setup();
         singleton = this;
         if (connectOnStart)
         {
             UserConnectToServer();
         }
     }
 }
Example #9
0
        // Update is called once per frame
        protected void FixedUpdate()
        {
            if (UDPConnectionManager.ConectedToServer() && firstTimeInConnection)
            {
                packet = new Packet(prefix);
                firstTimeInConnection = false;
                if (senderId != -1)
                {
                    packet.SetSenderId(senderId);
                }
            }

            //if (!sendManualy && UDPConnectionManager.ConectedToServer() && !firstTimeInConnection)
            //{
            //    SendMessage();
            //}
        }
Example #10
0
 public void RestoreToState()
 {
     if (state != null && state.StateIsSaved())
     {
         List <object> variables = state.GetVariables();
         SortedList    coping    = (SortedList)variables[0];
         SortedList    copy      = new SortedList(coping);
         messages      = copy;
         scriptEnabled = (bool)variables[1];
         prefix        = (string)variables[2];
         packageIndex  = (int)variables[3];
         if (!UDPConnectionManager.CheckIfPrefixExist(prefix))
         {
             UDPConnectionManager.AddPrefix(prefix);
         }
         UDPConnectionManager.AddToMessageHandler(prefix, this);
     }
 }
Example #11
0
        public void SendBoolean(bool value)
        {
            if (scriptEnabled)
            {
                try
                {
                    this.value = value;
                    if (sendManualy && UDPConnectionManager.ConectedToServer())
                    {
                        SendMessage();
                    }

                    //Debug.Log(msg);
                }
                catch (System.Exception ex)
                {
                    Debug.Log(ex.Message);
                }
            }
        }
Example #12
0
        public PacketHeader(byte[] receive)
        {
            base.Setup();
            this.currentIndex  = 0;
            packetReceiveIndex = -1;

            received             = receive;
            this.receivedMessage = Byte2Bool(receive);

            int typeLen = (int)CountBits((int)Packet.PacketType.NumberOfValues);

            bool[] packetConsoleBit = BitsReverseLen(receivedMessage, ref this.currentIndex, typeLen);
            res = BitArrayToUInt(packetConsoleBit);

            bool[] packetLenBits = BitsReverseLen(receivedMessage, ref this.currentIndex, 7);

            res        = BitArrayToUInt(packetLenBits); // package lenght for data check
            packageLen = (int)res;

            bool[] packetIndexBits = BitsReverseLen(receivedMessage, ref this.currentIndex, 32);
            res = BitArrayToUInt(packetIndexBits); // package lenght for data check
            packetReceiveIndex = (int)res;


            int byteCountMaxPlayer = UDPConnectionManager.GetMaxPlayerCount();

            byteCountMaxPlayer = (int)CountBits(byteCountMaxPlayer);


            packetLenBits = BitsReverseLen(receivedMessage, ref currentIndex, byteCountMaxPlayer);
            playerId      = (int)BitArrayToUInt(packetLenBits);

            packetLenBits = BitsReverseLen(receivedMessage, ref currentIndex, 5);
            uint lenPrefix = BitArrayToUInt(packetLenBits);

            packetLenBits = BitsReverseLen(receivedMessage, ref currentIndex, (int)lenPrefix);
            prefixIndex   = (int)BitArrayToUInt(packetLenBits);


            //PrintValues(packetLenBits, 0);
        }
Example #13
0
        public void SendFloat(float value)
        {
            if (this.enabled)
            {
                try
                {
                    this.value = value;

                    if (sendManualy && UDPConnectionManager.ConectedToServer())
                    {
                        SendMessage();
                    }

                    //await connection.InvokeAsync("Send", connection.GetHashCode().ToString(), msg);
                    //Debug.Log(msg);
                }
                catch (System.Exception ex)
                {
                    Debug.Log(ex.Message);
                }
            }
        }
Example #14
0
        void OnEnable()
        {
            scriptEnabled = true;

            if (oneTime)
            {
                if (gameObjectSpecificPrefix)
                {
                    Vector3 position = gameObject.transform.position;
                    Vector3 rotation = gameObject.transform.rotation.eulerAngles;
                    Vector3 scale    = gameObject.transform.lossyScale;

                    Vector3 index = position + scale + rotation;

                    prefix = prefix + gameObject.name + (gameObject.transform.childCount + index.magnitude).ToString();
                }
                UDPConnectionManager.AddPrefix(prefix);
                scriptEnabled = true;
                Debug.Log("Handler added");
                oneTime = false;
            }
        }
Example #15
0
        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);
            }
        }
Example #16
0
        public override void SendMessage()
        {
            if (scriptEnabled)
            {
                //Debug.Log("transformSend");
                if (prevPosition != position && packet != null)
                {
                    packet.ResetPacket();


                    if (frame != null && bufferFrame != null && bufferFrame2 != null)
                    {
                        bufferFrame3 = bufferFrame2;
                        bufferFrame2 = bufferFrame;
                        bufferFrame  = frame;
                        frame        = position;
                        Vector3 positionTemp  = position;
                        Vector3 buffPosition  = (Vector3)bufferFrame;
                        Vector3 buffPosition2 = (Vector3)bufferFrame2;
                        Vector3 buffPosition3 = (Vector3)bufferFrame3;

                        packet.AddFloat(positionTemp.x);
                        packet.AddFloat(positionTemp.y);
                        packet.AddFloat(positionTemp.z);

                        packet.AddFloat(buffPosition.x);
                        packet.AddFloat(buffPosition.y);
                        packet.AddFloat(buffPosition.z);

                        packet.AddFloat(buffPosition2.x);
                        packet.AddFloat(buffPosition2.y);
                        packet.AddFloat(buffPosition2.z);

                        packet.AddFloat(buffPosition3.x);
                        packet.AddFloat(buffPosition3.y);
                        packet.AddFloat(buffPosition3.z);

                        message = packet.CreatePacket();
                        UDPConnectionManager.SendMessage(message);
                    }
                    else if (frame != null && bufferFrame != null)
                    {
                        bufferFrame2 = bufferFrame;
                        bufferFrame  = frame;
                        frame        = position;
                        Vector3 positionTemp  = position;
                        Vector3 buffPosition  = (Vector3)bufferFrame;
                        Vector3 buffPosition2 = (Vector3)bufferFrame2;

                        packet.AddFloat(positionTemp.x);
                        packet.AddFloat(positionTemp.y);
                        packet.AddFloat(positionTemp.z);

                        packet.AddFloat(buffPosition.x);
                        packet.AddFloat(buffPosition.y);
                        packet.AddFloat(buffPosition.z);

                        packet.AddFloat(buffPosition2.x);
                        packet.AddFloat(buffPosition2.y);
                        packet.AddFloat(buffPosition2.z);

                        message = packet.CreatePacket();
                        UDPConnectionManager.SendMessage(message);
                    }
                    else if (frame != null && bufferFrame == null)
                    {
                        bufferFrame = frame;
                        frame       = position;
                        Vector3 positionTemp = position;
                        Vector3 buffPosition = (Vector3)bufferFrame;

                        packet.AddFloat(positionTemp.x);
                        packet.AddFloat(positionTemp.y);
                        packet.AddFloat(positionTemp.z);

                        packet.AddFloat(buffPosition.x);
                        packet.AddFloat(buffPosition.y);
                        packet.AddFloat(buffPosition.z);

                        message = packet.CreatePacket();
                        UDPConnectionManager.SendMessage(message);
                    }
                    else if (frame == null)
                    {
                        frame = position;
                        Vector3 positionTemp = position;

                        packet.AddFloat(positionTemp.x);
                        packet.AddFloat(positionTemp.y);
                        packet.AddFloat(positionTemp.z);

                        message = packet.CreatePacket();
                        UDPConnectionManager.SendMessage(message);
                    }

                    prevPosition = position;
                }
            }
        }
Example #17
0
        public override void SendMessage()
        {
            packet.ResetPacket();

            if (frame != null && bufferFrame != null && bufferFrame2 != null && bufferFrame3 != null)
            {
                bufferFrame4 = bufferFrame3;
                bufferFrame3 = bufferFrame2;
                bufferFrame2 = bufferFrame;
                bufferFrame  = frame;
                frame        = this.value;
                float frameFloat = this.value;
                float buffFloat  = (float)bufferFrame;
                float buffFloat2 = (float)bufferFrame2;
                float buffFloat3 = (float)bufferFrame3;
                float buffFloat4 = (float)bufferFrame4;

                packet.AddFloat(frameFloat);

                packet.AddFloat(buffFloat);

                packet.AddFloat(buffFloat2);

                packet.AddFloat(buffFloat3);

                packet.AddFloat(buffFloat4);

                message = packet.CreatePacket();
                UDPConnectionManager.SendMessage(message);
            }
            else if (frame != null && bufferFrame != null && bufferFrame2 != null)
            {
                bufferFrame3 = bufferFrame2;
                bufferFrame2 = bufferFrame;
                bufferFrame  = frame;
                frame        = this.value;
                float frameFloat = this.value;
                float buffFloat  = (float)bufferFrame;
                float buffFloat2 = (float)bufferFrame2;
                float buffFloat3 = (float)bufferFrame3;

                packet.AddFloat(frameFloat);

                packet.AddFloat(buffFloat);

                packet.AddFloat(buffFloat2);

                packet.AddFloat(buffFloat3);

                message = packet.CreatePacket();
                UDPConnectionManager.SendMessage(message);
            }
            else if (frame != null && bufferFrame != null)
            {
                bufferFrame2 = bufferFrame;
                bufferFrame  = frame;
                frame        = this.value;
                float frameFloat = this.value;
                float buffFloat  = (float)bufferFrame;
                float buffFloat2 = (float)bufferFrame2;

                packet.AddFloat(frameFloat);

                packet.AddFloat(buffFloat);

                packet.AddFloat(buffFloat2);


                message = packet.CreatePacket();
                UDPConnectionManager.SendMessage(message);
            }
            else if (frame != null && bufferFrame == null)
            {
                bufferFrame = frame;
                frame       = this.value;
                float frameFloat = this.value;
                float buffFloat  = (float)bufferFrame;

                packet.AddFloat(frameFloat);

                packet.AddFloat(buffFloat);

                message = packet.CreatePacket();
                UDPConnectionManager.SendMessage(message);
            }
            else if (frame == null)
            {
                frame = this.value;
                float frameFloat = this.value;

                packet.AddFloat(frameFloat);


                message = packet.CreatePacket();
                UDPConnectionManager.SendMessage(message);
            }
        }
Example #18
0
        public override void SendMessage()
        {
            if (prevRotation != rotation && Mathf.Abs(rotation.magnitude - prevRotation.magnitude) > 0.001)
            {
                packet.ResetPacket();



                if (frame != null && bufferFrame != null && bufferFrame2 != null)
                {
                    bufferFrame3 = bufferFrame2;
                    bufferFrame2 = bufferFrame;
                    bufferFrame  = frame;
                    frame        = rotation;
                    Vector3 position      = rotation;
                    Vector3 buffPosition  = (Vector3)bufferFrame;
                    Vector3 buffPosition2 = (Vector3)bufferFrame2;
                    Vector3 buffPosition3 = (Vector3)bufferFrame3;

                    packet.AddFloat(position.x);
                    packet.AddFloat(position.y);
                    packet.AddFloat(position.z);

                    packet.AddFloat(buffPosition.x);
                    packet.AddFloat(buffPosition.y);
                    packet.AddFloat(buffPosition.z);

                    packet.AddFloat(buffPosition2.x);
                    packet.AddFloat(buffPosition2.y);
                    packet.AddFloat(buffPosition2.z);

                    packet.AddFloat(buffPosition3.x);
                    packet.AddFloat(buffPosition3.y);
                    packet.AddFloat(buffPosition3.z);

                    message = packet.CreatePacket();
                    UDPConnectionManager.SendMessage(message);
                }
                else if (frame != null && bufferFrame != null)
                {
                    bufferFrame2 = bufferFrame;
                    bufferFrame  = frame;
                    frame        = rotation;
                    Vector3 frameRotation = rotation;
                    Vector3 buffPosition  = (Vector3)bufferFrame;
                    Vector3 buffPosition2 = (Vector3)bufferFrame2;

                    packet.AddFloat(frameRotation.x);
                    packet.AddFloat(frameRotation.y);
                    packet.AddFloat(frameRotation.z);

                    packet.AddFloat(buffPosition.x);
                    packet.AddFloat(buffPosition.y);
                    packet.AddFloat(buffPosition.z);

                    packet.AddFloat(buffPosition2.x);
                    packet.AddFloat(buffPosition2.y);
                    packet.AddFloat(buffPosition2.z);

                    message = packet.CreatePacket();
                    UDPConnectionManager.SendMessage(message);
                }
                else if (frame != null && bufferFrame == null)
                {
                    bufferFrame = frame;
                    frame       = rotation;

                    Vector3 frameRotation = rotation;
                    Vector3 buffPosition  = (Vector3)bufferFrame;

                    packet.AddFloat(frameRotation.x);
                    packet.AddFloat(frameRotation.y);
                    packet.AddFloat(frameRotation.z);

                    packet.AddFloat(buffPosition.x);
                    packet.AddFloat(buffPosition.y);
                    packet.AddFloat(buffPosition.z);

                    message = packet.CreatePacket();
                    UDPConnectionManager.SendMessage(message);
                }
                else if (frame == null)
                {
                    frame = rotation;
                    Vector3 frameRotation = rotation;

                    packet.AddFloat(frameRotation.x);
                    packet.AddFloat(frameRotation.y);
                    packet.AddFloat(frameRotation.z);


                    message = packet.CreatePacket();
                    UDPConnectionManager.SendMessage(message);
                }
                prevRotation = rotation;
            }
        }
Example #19
0
        public void DisconnectPacket()
        {
            int maxPlayerBitCount = (int)StaicBitFunctions.CountBits(UDPConnectionManager.GetMaxPlayerCount());

            message = message + Convert.ToString(UDPConnectionManager.singleton.GetPlayerId(), toBase: 2).PadLeft(maxPlayerBitCount, '0');
        }
Example #20
0
 public string GetPrefix()
 {
     return(UDPConnectionManager.PrefixByIndex(prefixIndex));
 }