Exemple #1
0
        private void handleWorld(object client)
        {
            TcpClient     tcpClient    = (TcpClient)client;
            District      district     = new District(tcpClient);
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int    bytesRead;

            while (true)
            {
                bytesRead = 0;
                try
                {
                    bytesRead = clientStream.Read(message, 0, message.Length);
                }
                catch
                {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }

                IPacket packet = null;
                switch (message[0])
                {
                case (byte)OpCodes.DW_REGISTER_DISTRICT:
                    packet = new RegisterDistrict();
                    break;
                }
                packet.Write(message, 1, bytesRead - 1);
                packet.Handle(district);
            }
            try
            {
                lock (DistrictsTcp)
                {
                    foreach (KeyValuePair <TcpClient, uint> dtcp in DistrictsTcp)
                    {
                        lock (Districts)
                        {
                            foreach (KeyValuePair <uint, District> dis in Districts)
                            {
                                if (dis.Key == dtcp.Value)
                                {
                                    Log.Error("Listener", dis.Value.ToString() + " disconnected!");
                                    break;
                                }
                            }
                            Districts.Remove(dtcp.Value);
                            break;
                        }
                    }
                    DistrictsTcp.Remove(tcpClient);
                }
                tcpClient.Close();
            }
            catch (Exception e)
            {
                Log.Error("Districts.Listener", "Following exception was thrown when trying to remove district server:\n\n");
                Console.WriteLine(e.ToString());
                return;
            }
        }
Exemple #2
0
        private void handleDistrict(object client)
        {
            TcpClient     tcpClient    = (TcpClient)client;
            District      district     = new District(tcpClient);
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int    bytesRead;

            while (true)
            {
                bytesRead = 0;
                try
                {
                    bytesRead = clientStream.Read(message, 0, message.Length);
                }
                catch
                {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }
                else
                {
                    byte[] data = new byte[bytesRead];
                    Buffer.BlockCopy(message, 0, data, 0, bytesRead);

                    if (GetValue(data[0]) == 0)
                    {
                        int           type     = GetValue(data[1]);
                        byte          ID       = (byte)GetValue(data[2]);
                        LanguageCodes language = (LanguageCodes)GetValue(data[3]);

                        int iplen = 0;
                        int a     = GetValue(data[4]);
                        if (a == 1)
                        {
                            iplen = GetValue(data[5]);
                        }
                        else if (a == 2)
                        {
                            iplen = GetValue(data[5]) * 10 + GetValue(data[6]);
                        }

                        int    position = 5 + a;
                        string ip       = null;
                        for (int i = position; i < position + iplen; i++)
                        {
                            ip += Convert.ToChar(data[i]);
                        }
                        position += iplen;

                        int portlen = GetValue(data[position]) * 10 + GetValue(data[position + 1]);
                        position += 2;
                        string port = null;
                        for (int i = position; i < position + portlen; i++)
                        {
                            port += Convert.ToChar(data[i]);
                        }
                        position += portlen;

                        int toklen = GetValue(data[position]) * 10 + GetValue(data[position + 1]);
                        position += 2;
                        string token = null;
                        for (int i = position; i < position + toklen; i++)
                        {
                            token += Convert.ToChar(data[i]);
                        }

                        RegisterDistrict.Register(district, tcpClient, type, ID, language, ip, port, token);
                    }
                }
            }
            try
            {
                lock (DistrictsTcp)
                {
                    foreach (KeyValuePair <TcpClient, uint> dtcp in DistrictsTcp)
                    {
                        lock (Districts)
                        {
                            foreach (KeyValuePair <uint, District> dis in Districts)
                            {
                                if (dis.Key == dtcp.Value)
                                {
                                    Log.Error("Listener", dis.Value.ToString() + " disconnected!");
                                    break;
                                }
                            }
                            Districts.Remove(dtcp.Value);
                            break;
                        }
                    }
                    DistrictsTcp.Remove(tcpClient);
                }
                tcpClient.Close();
            }
            catch (Exception e)
            {
                Log.Error("Districts.Listener", "Following exception was thrown when trying to remove district server:\n\n");
                Console.WriteLine(e.ToString());
                return;
            }
        }