/* Thor Server 2.0 Project - Codenamed "Stockholm" Copyright 2008 - 2009 Joe Hegarty This file is part of The Thor Server 2.0 Project - Codenamed "Stockholm". The Thor Server 2.0 Project - Codenamed "Stockholm" is free software: you can redistribute it and/or modify it under the terms of the Microsoft Public License The Thor Server Project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Microsoft Public License for more details. You should have received a copy of the Microsoft Public License along with The Thor Server Project. If not, see http://www.opensource.org/licenses/ms-pl.html */ using System; using System.Threading; using System.Collections.Generic; using System.Linq; using StockholmLibrary.Net; using StockholmLibrary.Game; using StockholmLibrary.Util; using StockholmLibrary.Data; using StockholmLibrary.Game.Sessions; using StockholmLibrary.Game.Packets.Client; using StockholmLibrary.Game.Packets.Server; namespace StockholmLibrary.Game.Handlers.Generic { //------------------------------------ public class GenericUserHandler { private Thread _userDataThread; private static object _loginRequestLock = new object(); private static Queue _loginRequestQueue = new Queue(); //------------------------------------ private class LoginRequest { public string Username; public string Password; public Peer Peer; //********************************************** public LoginRequest() { } //********************************************** public LoginRequest(string username, string password, Peer peer) { Username = username; Password = password; Peer = peer; } } //********************************************** public GenericUserHandler() { Stockholm.Get().PacketManager.RegisterGenericHandler(EClientPacket.LOGIN, new PacketRoutingInfo.ProcessPacketDelegate(HandleLogin)); Stockholm.Get().PacketManager.RegisterGenericHandler(EClientPacket.GENERATE_KEY, new PacketRoutingInfo.ProcessPacketDelegate(HandleGenerateKey)); _userDataThread = new Thread(new ThreadStart(UserDataThread)); _userDataThread.Name = "User Data Thread"; _userDataThread.Start(); } //********************************************** private void HandleGenerateKey(ClientPacket packet) { packet.Peer.EnqueuePacket(new ClientAllowedSetsPacket()); packet.Peer.EnqueuePacket(new ClientFormattingPacket()); } //********************************************** private void HandleLogin(ClientPacket packet) { LoginPacket loginPacket = new LoginPacket(); loginPacket.Deserialise(packet); lock (_loginRequestLock) { _loginRequestQueue.Enqueue(new LoginRequest(loginPacket.Username, loginPacket.Password, loginPacket.Peer)); } } //********************************************** private static void UserDataThread() { Logging.LogEvent("GenericUserHandler", "UserDataThread started succesfully", Logging.ELogLevel.INFO); while (Stockholm.Get().IsRunning) { HandleLoginRequests(); Thread.Sleep(5); } } //********************************************** private static void HandleLoginRequests() { lock (_loginRequestLock) { while (_loginRequestQueue.Count > 0) { var request = _loginRequestQueue.Dequeue(); StockholmDbDataContext db = Stockholm.Get().Database; var userQuery = from p in db.Users where p.Username == request.Username select p; if (userQuery.Count() > 0) { // User exists var user = userQuery.Single(); if (user.Password == request.Password) { // User details correct Session session = new Session(user.UserID, request.Peer); session.Peer = request.Peer; if (Stockholm.Get().GameManager.RoleManager.HasAccess(user.RoleID, "fuse_login")) { // User has the right to login, session is now active! Stockholm.Get().GameManager.SessionManager.AddSession(session); } else { // Bad permissions ErrorPacket packet = new ErrorPacket("login incorrect: Bad permissions"); request.Peer.EnqueuePacket(packet); } } else { // Bad password ErrorPacket packet = new ErrorPacket("login incorrect: Wrong password"); request.Peer.EnqueuePacket(packet); } } else { // Bad username ErrorPacket packet = new ErrorPacket("login incorrect: Wrong username"); request.Peer.EnqueuePacket(packet); } } } } } }