Beispiel #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];
            Int32 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, UInt32> dtcp in DistrictsTcp)
                    {
                        lock (Districts)
                        {
                            foreach (KeyValuePair<UInt32, 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;
            }
        }
Beispiel #2
0
        private void handleWorld(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();
            District district = new District(0, 0);
            district.tcp = tcpClient;
            Byte[] message = new Byte[4096];
            Int32 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);
            }
            tcpClient.Close();
        }