Exemplo n.º 1
0
        void ReceiveData(byte[] data, IPEndPoint ip)
        {
            if (DebugInfo.downData && data.Length > 1)
            {
                string dataString = "";
                foreach (byte b in data)
                {
                    dataString += b + " ";
                }
                Debug("Received " + data.Length + "[" + dataString + "]");
            }

            Client c = GetClient(ip);

            //Pinged
            if (data.Length == 1 && data[0] == pingByte)
            {
                if (c != null && c.Pinging)
                {
                    c.Ping();
                }
                else
                {
                    udpSocket.Send(data, 1, ip);
                }

                return;
            }

            else if (c != null)
            {
                inMessages.Add(new MessageInfo(new MessageBuffer(data), ip, this));
            }
            else if (data.Length == 4)
            {
                int id = BitConverter.ToInt32(data, 0);
                c = GetClient(id);

                if (c != null)
                {
                    if (DebugInfo.acceptData)
                    {
                        Debug("Received udp ip for ID " + id);
                    }

                    c.udpAdress = ip;
                    connectedList.Add(c);
                }
            }
            else
            {
                inMessagesExternal.Add(new MessageInfo(new MessageBuffer(data), ip, this));
            }

            downByteBuffer += data.Length;
            downByteTotal  += data.Length;
        }