//===================================================================================================== //Client-server actions //----------------------------------------------------------------------------------------------------- public bool Register(string login, string password) { if (!ConnectToServer()) { return(false); } //send login request SendRegisterRequest(login, password); //await notification NetworkPacket netPacket = new NetworkPacket(ClientSocket, new RequestResponse().GetMaximumPacketSize()); while (ClientSocket.Available < netPacket.BufferSize) { // Debug.WriteLine("Waiting for register reponse"); Thread.Sleep(10); } ClientSocket.Receive(netPacket.Buffer, new RequestResponse().GetMaximumPacketSize(), SocketFlags.None); //netPacket.message.AddRange(netPacket.buffer); RequestResponse response = new RequestResponse(); response.Deserialize(netPacket.Buffer); bool result = (response.ResponseValue == 1 && response.ResponsePacketAction == RequestID.RegisterRequestAction); if (!result) { ShutdownConnection(); User = null; switch (response.ResponseValue) { case RequestID.ERROR.RegisterEmailTaken: { MessageBox.Show("Register failed: email taken", "Register error", MessageBoxButtons.OK); break; } case RequestID.ERROR.RegisterErrorTooShort: { MessageBox.Show("Register failed: email or password too short", "Register error", MessageBoxButtons.OK); break; } } } else { User = new User(response.UserID, login, ClientSocket); } //Debug.WriteLine("Register reponse " + result); return(result); }
public bool Login(string login, string password) { if (!ConnectToServer()) { return(false); } //send login request SendLoginRequest(login, password); //await notification NetworkPacket netPacket = new NetworkPacket(ClientSocket, new RequestResponse().GetMaximumPacketSize()); while (ClientSocket.Available < netPacket.BufferSize) { // Debug.WriteLine("Waiting for login reponse"); Thread.Sleep(10); } // MessageBox.Show("RESP MAX LEN: " + new RequestResponse().GetMaximumPacketSize(), "Login failed", MessageBoxButtons.OK); ClientSocket.Receive(netPacket.Buffer, new RequestResponse().GetMaximumPacketSize(), SocketFlags.None); RequestResponse response = new RequestResponse(); response.Deserialize(netPacket.Buffer); bool result = response.ResponseValue == RequestID.LOGIN_ACTION.LoginSuccess && response.ResponsePacketAction == RequestID.LoginRequestAction; if (result) { User = new User(response.UserID, login, ClientSocket); } else { ShutdownConnection(); User = null; MessageBox.Show("Login failed ", "Login failed", MessageBoxButtons.OK); } return(result); }