static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MudClient form = new MudClient(); Application.Run(form); }
/** * Thread responsbale for openeing and mainiting the connection to the server * @param o form object. */ private void ClientProcess(Object o) { MudClient form = (MudClient)o; Thread receiveThread; //Wait for the server to form to load fully. Thread.Sleep(1500); while ((form.bConnected == false) && (form.bQuit == false)) { try { if (bConnected == false) { form.clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); form.clientSocket.Connect(new IPEndPoint(IPAddress.Parse(IP[ipIndex]), 8500)); form.bConnected = true; //send message to login screen that we're connected form.loginScreen.Connected("Connected"); //start recieve thread receiveThread = new Thread(ClientReceive); receiveThread.IsBackground = true; receiveThread.Start(o); } while ((form.bQuit == false) && (form.bConnected == true)) { if (form.IsDisposed == true) { form.bQuit = true; form.clientSocket.Close(); } } } catch (System.Exception) { } Thread.Sleep(500); } //If Sever is randomly lost, restart. Application.Restart(); Environment.Exit(0); }
/** * Thread responsible for receiving all incoming data from the server * @param o form object reference */ private void ClientReceive(Object o) { MudClient form = (MudClient)o; while (form.bConnected == true) { try { byte[] buffer = new byte[4096]; int result; //listen for buffer result = form.clientSocket.Receive(buffer); if (result > 0) { //decode stream Msg m = Msg.DecodeStream(buffer, Salt, ShouldDecrypt); //Get message type and do approriate actiobn if (m != null) { Console.Write("Got a message: "); switch (m.mID) { case DungeonResponse.ID: { DungeonResponse dr = (DungeonResponse)m; form.AddDungeonText(dr.response, true); } break; case MapLayout.ID: { MapLayout ml = (MapLayout)m; DrawQueue.Enqueue(() => ParseMap(ml.mapInfo)); break; } case LoginResponse.ID: { LoginResponse lm = (LoginResponse)m; if (lm.loggedIn == "1") { //Login was successful start decrypting and start main game loginScreen.LoginResponse(lm.message, true); ShouldDecrypt = true; } else { //login failed, show failed message loginScreen.LoginResponse(lm.message, false); ShouldDecrypt = false; } } break; case PlayerLocations.ID: { PlayerLocations pl = (PlayerLocations)m; DrawQueue.Enqueue(() => UpdatePlayerLocations(pl.LocationString)); } break; case UpdateChat.ID: { UpdateChat uc = (UpdateChat)m; form.AddDungeonText(uc.message, false); } break; case SaltSend.ID: { SaltSend ss = (SaltSend)m; Salt = ss.salt; SendLoginMessage(); break; } case SaltRequest.ID: { SaltRequest sr = (SaltRequest)m; SendCreateUserMessage(); break; } default: break; } } } } catch (Exception ex) { //If a discconect happens restart the service form.bConnected = false; Console.WriteLine(U.NL("Lost server!") + ex); Application.Restart(); Environment.Exit(0); } } }
/** * Defualt constructor * @param frm reference to the mudclient */ public LoginScreen(MudClient frm) { InitializeComponent(); form = frm; }