Exemplo n.º 1
0
        private void handleLobby()
        {
            RegisterDistrict initPacket = new RegisterDistrict(Program.Type, Program.ID, Program.Language, Program.Password, Program.IP, Program.Port, Program.Token);

            stream.Write(initPacket.ToArray(), 0, initPacket.ToArray().Length);
            stream.Flush();
            byte[] message = new byte[4096];
            int    bytesRead;

            while (true)
            {
                bytesRead = 0;
                try
                {
                    bytesRead = stream.Read(message, 0, 4096);
                }
                catch
                {
                    break;
                }
                if (bytesRead == 0)
                {
                    break;
                }

                IPacket packet = null;
                switch (message[0])
                {
                case (byte)OpCodes.WD_MSG_INFO:
                    packet = new MessageInfo();
                    break;
                }
                packet.Write(message, 1, bytesRead - 1);
                packet.Handle();
            }
            Log.Error("World.Client", "World server disconnected! Reconnecting...");
            client.Close();
            stream.Dispose();
            try
            {
                client = new TcpClient();
                connect(address);
                stream = client.GetStream();
                Log.Succes("World.Client", "Reconnected!");
                handleLobby();
            }
            catch (Exception e)
            {
                Log.Error("World.Client", "Failed to reconnect due to following exception:\n\n");
                Console.WriteLine(e.ToString());
                return;
            }
        }