Esempio n. 1
0
    void ProcessUdpMessage()
    {
        udpMessageReceived = false;

        //Perfect solution for thread sychronization
        byte[] udpBuffer = udpReceiveBuffer;
        Debug.Log("udpReceived a msg, Length is : " + udpBuffer.Length);

        if (udpBuffer[0] == 0x02)
        {
            //Login Confirm;
            int userNum = System.BitConverter.ToInt32(udpBuffer, 1);
            for (int i = 0; i < userNum; i++)
            {
                byte[] iptemp = new byte[4];
                for (int j = 0; j < 4; j++)
                {
                    //Little endian
                    //iptemp[j] = udpBuffer[j + 5 + 26 * i];

                    //Big endian
                    iptemp[3 - j] = udpBuffer[j + 5 + 26 * i];
                }
                IPAddress ip      = new IPAddress(iptemp);
                ushort    tcpPort = System.BitConverter.ToUInt16(udpBuffer, 5 + 26 * i + 4);
                string    name    = Encoding.Default.GetString(udpBuffer, 5 + 26 * i + 6, 20);
                users.Add(name.Trim(), new UserItem(ip, tcpPort, name, this));
                Debug.Log("User " + i + " : " + ip.ToString() + " tcpPort: " + tcpPort + " userName: "******"LoadChatRoom failed : " + e.ToString());
            }
        }
        else if (udpBuffer[0] == 0x05)
        {
            int userNum = System.BitConverter.ToInt32(udpBuffer, 1);
            if (userNum < users.Count)
            {
                foreach (var u in users)
                {
                    bool remove = true;
                    for (int i = 0; i < userNum; i++)
                    {
                        string name = Encoding.Default.GetString(udpBuffer, 5 + 26 * i + 6, 20);
                        if (name == u.Key)
                        {
                            remove = false;
                            break;
                        }
                    }
                    if (remove)
                    {
                        users.Remove(u.Key);
                        displayController.UndisplayUser(u.Value);
                        Debug.Log("Remove user " + " : " + u.Value.ip.ToString() + " tcpPort: " + u.Value.port + " userName: "******"Add user " + " : " + ip.ToString() + " tcpPort: " + tcpPort + " userName: " + name);
                        break;
                    }
                }
            }
        }
    }