public int AuthorizeUser(BattleHandler bh, out string resMassage, out string userAuth) { userAuth = ""; string tokenAuth; if (bh.RC.values.TryGetValue("Authorization", out tokenAuth)) { string[] split = tokenAuth.Split('-'); User user; if (ma.Users.TryGetValue(split[0], out user)) { if (user.online == 0) { resMassage = "You need to login first!"; return(-1); } if (user.GetToken().Equals(tokenAuth)) { userAuth = user.name; resMassage = "Authorized!"; return(0); } } } resMassage = "Authoriziaton Failed!"; return(-1); }
/* * Rests the BattleConnection */ public void Reset() { try { client1.Close(); client2.Close(); } catch { } client1 = null; client2 = null; BH = null; }
/* * Waits for the Battling Users to send the next request * * @params: * - BH: The BattleHandler */ public void Rewait(BattleHandler BH) { client1 = null; client2 = null; while (client1 == null || client2 == null) { if (client1 == null) { client1 = Server.AcceptSocket(); string username; if (AuthorizeAgain(client1, ref BH, out username) == 0) { SendHead(200, "text/plain", 300, ref client1); SendBody("Authorized for Battle!\n", ref client1); BH.RC = RC; BH.DoRequest(ref client1, username); } else { SendResponse(200, "text/plain", "Not Authorized for Battle!", ref client1); client1.Close(); } } if (client2 == null) { client2 = Server.AcceptSocket(); string username; if (AuthorizeAgain(client2, ref BH, out username) == 0) { //BuildContext(ReceiveRequest(ref client)); SendHead(200, "text/plain", 300, ref client2); SendBody("Authorized for Battle!\n", ref client2); BH.RC = RC; BH.DoRequest(ref client2, username); } else { SendResponse(200, "text/plain", "Not Authorized for Battle!", ref client2); client2.Close(); } } } }
/* * Authorizes a User again who is in this Battle * * @params: * - client: Socket to which the User connected * - BH: The BattleHandler * - username: Username of the Connected User */ public int AuthorizeAgain(Socket client, ref BattleHandler BH, out string username) { username = ""; BuildContext(ReceiveRequest(ref client)); string tokenAuth; if (RC.values.TryGetValue("Authorization", out tokenAuth)) { string[] split = tokenAuth.Split('-'); User user; if (ma.Users.TryGetValue(split[0], out user)) { if (user.online == 0) { return(-1); } if (user.GetToken().Equals(tokenAuth)) { if (user.name.Equals(username1)) { BH.client1 = client; username = username1; } else if (user.name.Equals(username2)) { BH.client2 = client; username = username2; } return(0); } } } return(-1); }
/* * Initializes the socket and waits for connections * * @returns: * - 0: On success * - -1: On failure */ public int InitListener() { int err = 0; try { Server = new TcpListener(Addr, Port); Server.Start(); while (Running) { Console.WriteLine("Waiting for Battle Connections..."); if (client1 == null) { client1 = Server.AcceptSocket(); Console.WriteLine("User 1 Connected!"); if (Authorize(ref client1, ref username1) == 0) { SendHead(200, "text/plain", 250, ref client1); SendBody("Authorized for Battle!\n", ref client1); } else { SendResponse(200, "text/plain", "Not Authorized for Battle!", ref client1); client1.Close(); } } if (client2 == null) { client2 = Server.AcceptSocket(); Console.WriteLine("User 2 Connected!"); if (Authorize(ref client2, ref username2) == 0) { SendHead(200, "text/plain", 250, ref client2); SendBody("Authorized for Battle!\n", ref client2); } else { SendResponse(200, "text/plain", "Not Authorized for Battle!", ref client2); client2.Close(); } } if (client1 != null && client2 != null) { if (client1.Connected && client2.Connected) { BH = new BattleHandler(this, RC, client1, client2, username1, username2); } } Running = false; } } catch (SocketException e) { Console.WriteLine(e); } finally { Server.Stop(); err = -1; } return(err); }