示例#1
0
        private void NewLobbyPlayer(NetTunnel tunnel)
        {
            string username   = tunnel.WaitMessage();
            string hashedPass = tunnel.WaitMessage();

            if (string.IsNullOrEmpty(username))
            {
                Logger.Log(LogType.Error, "Blank username");
                tunnel.Destroy();
                return;
            }

            if (string.IsNullOrEmpty(hashedPass))
            {
                Logger.Log(LogType.Error, "Blank password");
                tunnel.Destroy();
                return;
            }

            LobbyPlayer lobbyPlayer = Database.Login(username, hashedPass, tunnel, this);

            if (lobbyPlayer == null)
            {
                tunnel.SendMessage("fail");
                tunnel.Destroy();
                Logger.Log(LogType.Event, "Bad login attempt (bad credentials) from " + username);
                return;
            }
            else if (lobbyList.ContainsKey(lobbyPlayer.Name))
            {
                if (lobbyList[lobbyPlayer.Name].Tunnel.SendMessage("alreadyin"))
                {
                    tunnel.SendMessage("fail");
                    tunnel.Destroy();
                    Logger.Log(LogType.Event, "Bad login attempt (existing player) from " + username);
                    return;
                }
                else
                {
                    tunnel.SendMessage("ok");
                }
            }
            else
            {
                tunnel.SendMessage("ok");
            }

            lobbyList.Add(lobbyPlayer.Name, lobbyPlayer);

            Logger.Log(LogType.Event, "Added player to lobby " + lobbyPlayer.Name);
        }
示例#2
0
        private void RegisterNewPlayer(NetTunnel tunnel)
        {
            string username = tunnel.WaitMessage();
            string email    = tunnel.WaitMessage();

            RegistrationResult res = Database.RegisterNewPlayer(username, email);

            if (res == RegistrationResult.Successful)
            {
                tunnel.SendMessage("ok");
            }
            else
            {
                tunnel.SendMessage("fail:" + Enum.GetName(typeof(RegistrationResult), res));
            }
        }
示例#3
0
        private void ResetPassword(NetTunnel tunnel)
        {
            string username = tunnel.WaitMessage();
            string email    = tunnel.WaitMessage();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(email))
            {
                return;
            }

            if (Database.ResetPassword(username, email))
            {
                tunnel.SendMessage("ok");
            }
            else
            {
                tunnel.SendMessage("fail");
            }
        }
示例#4
0
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                Socket listener = (Socket)ar.AsyncState;
                Socket client   = listener.EndAccept(ar);

                Logger.Log(LogType.Event, "New connection to server");

                NetTunnel tunnel = new NetTunnel(client);

                string conType = tunnel.WaitMessage();

                if (string.IsNullOrEmpty(conType))
                {
                    Logger.Log(LogType.Error, "Did not specify connection type");
                    return;
                }
                else if (conType == "login")
                {
                    NewLobbyPlayer(tunnel);
                }
                else if (conType.StartsWith("joingame:"))
                {
                    NewGamePlayer(tunnel, conType.Substring("joingame:".Length));
                }
                else if (conType == "testgame")
                {
                    NewTestGame(tunnel);
                }
                else if (conType == "register")
                {
                    RegisterNewPlayer(tunnel);
                }
                else if (conType == "forgot")
                {
                    ResetPassword(tunnel);
                }
            }
            catch (Exception ex)
            {
                Logger.Log("From Server's AcceptCallback while handling new connection", ex);
            }

            try
            {
                listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
            }
            catch (Exception ex)
            {
                Logger.Log("From Server's AcceptCallback while restarting the listener", ex);
            }
        }
        private void ResetPassword(NetTunnel tunnel)
        {
            string username = tunnel.WaitMessage();
            string email = tunnel.WaitMessage();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(email))
                return;

            if (Database.ResetPassword(username, email))
            {
                tunnel.SendMessage("ok");
            }
            else
            {
                tunnel.SendMessage("fail");
            }
        }
        private void RegisterNewPlayer(NetTunnel tunnel)
        {
            string username = tunnel.WaitMessage();
            string email = tunnel.WaitMessage();

            RegistrationResult res = Database.RegisterNewPlayer(username, email);
            if (res == RegistrationResult.Successful)
                tunnel.SendMessage("ok");
            else
                tunnel.SendMessage("fail:" + Enum.GetName(typeof(RegistrationResult), res));
        }
        private void NewLobbyPlayer(NetTunnel tunnel)
        {
            string username = tunnel.WaitMessage();
            string hashedPass = tunnel.WaitMessage();

            if (string.IsNullOrEmpty(username))
            {
                Logger.Log(LogType.Error, "Blank username");
                tunnel.Destroy();
                return;
            }

            if (string.IsNullOrEmpty(hashedPass))
            {
                Logger.Log(LogType.Error, "Blank password");
                tunnel.Destroy();
                return;
            }

            LobbyPlayer lobbyPlayer = Database.Login(username, hashedPass, tunnel, this);

            if (lobbyPlayer == null)
            {
                tunnel.SendMessage("fail");
                tunnel.Destroy();
                Logger.Log(LogType.Event, "Bad login attempt (bad credentials) from " + username);
                return;
            }
            else if (lobbyList.ContainsKey(lobbyPlayer.Name))
            {
                if (lobbyList[lobbyPlayer.Name].Tunnel.SendMessage("alreadyin"))
                {
                    tunnel.SendMessage("fail");
                    tunnel.Destroy();
                    Logger.Log(LogType.Event, "Bad login attempt (existing player) from " + username);
                    return;
                }
                else
                {
                    tunnel.SendMessage("ok");
                }
            }
            else
            {
                tunnel.SendMessage("ok");
            }

            lobbyList.Add(lobbyPlayer.Name, lobbyPlayer);

            Logger.Log(LogType.Event, "Added player to lobby " + lobbyPlayer.Name);
        }
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                Socket listener = (Socket)ar.AsyncState;
                Socket client = listener.EndAccept(ar);

                Logger.Log(LogType.Event, "New connection to server");

                NetTunnel tunnel = new NetTunnel(client);

                string conType = tunnel.WaitMessage();

                if (string.IsNullOrEmpty(conType))
                {
                    Logger.Log(LogType.Error, "Did not specify connection type");
                    return;
                }
                else if (conType == "login")
                {
                    NewLobbyPlayer(tunnel);
                }
                else if (conType.StartsWith("joingame:"))
                {
                    NewGamePlayer(tunnel, conType.Substring("joingame:".Length));
                }
                else if (conType == "testgame")
                {
                    NewTestGame(tunnel);
                }
                else if (conType == "register")
                {
                    RegisterNewPlayer(tunnel);
                }
                else if (conType == "forgot")
                {
                    ResetPassword(tunnel);
                }
            }
            catch (Exception ex)
            {
                Logger.Log("From Server's AcceptCallback while handling new connection", ex);
            }

            try
            {
                listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
            }
            catch (Exception ex)
            {
                Logger.Log("From Server's AcceptCallback while restarting the listener", ex);
            }
        }