Пример #1
0
        public void Handle(World world)
        {
            Position = 0;
            String name = ReadS();
            String password = ReadS();
            Byte ID = (Byte)ReadByte();
            world.Name = name;
            Log.Info("RegisterWorld", name + " tries to register.");
            IPEndPoint address = (IPEndPoint)world.tcp.Client.RemoteEndPoint;
            if (ID != 0)
            {
                if (Program.worlds.Contains(ID) == false)
                {
                    Log.Succes("RegisterWorld", name + " was registered! World ID: " + ID);
                    world.Id = ID;
                    world.Send(new LW.RegisterSuccess(ID));
                    Program.worlds.Add(ID);
                }
                else
                {
                    Log.Error("RegisterWorld", "World server with ID " + ID + " already exists!");
                    world.tcp.Close();
                }

            }
            else
            {
                Log.Error("RegisterWorld", name + ": wrong host or password - " + address.Address.ToString() + "@" + password);
                Log.Info("RegisterWorld", name + " will be disconnected in 5 seconds");
                Thread.Sleep(5 * 1000);
                world.tcp.Close();
            }
        }
Пример #2
0
        private void handleWorld(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            World world = new World(tcpClient);
            NetworkStream clientStream = tcpClient.GetStream();
            Byte[] message = new Byte[4096];
            Int32 bytesRead;
            while (true)
            {
                bytesRead = 0;
                try
                {
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    break;
                }
                if (bytesRead == 0)
                {
                    break;
                }

                IPacket packet = null;
                switch (message[0])
                {
                    case (Byte)OpCodes.WL_REGISTER_WORLD: packet = new RegisterWorld(); break;
                    case (Byte)OpCodes.WL_SET_DATA: packet = new SetData(); break;
                }
                packet.Write(message, 1, bytesRead - 1);
                packet.Handle(world);
            }
            Log.Error("World.Listener", world.Name + " disconnected!");
            try
            {
                Program.worlds.Remove((Byte)world.Id);
                lock (Worlds)
                {
                    Worlds.Remove(world.Id);
                }
                tcpClient.Close();
            }
            catch(Exception e)
            {
                Log.Error("World.Listener", "Failed to remove disconnected world, exception:\n\n");
                Console.WriteLine(e.ToString());
                return;
            }
        }