/// <summary> /// Creates a new instance of <see cref="GPCMClient"/> /// </summary> public GPCMServer(string serverName, DatabaseDriver driver, IPEndPoint bindTo, int maxConnections) : base(serverName, bindTo, maxConnections) { //GPCMHandler.DBQuery = new GPCMDBQuery(driver); DB = new DBQueryBase(driver); GPCMClient.OnDisconnect += ClientDisconnected; GPCMClient.OnSuccessfulLogin += ClientSuccessfulLogin; // Setup timer. Every 15 seconds should be sufficient if (PollTimer == null || !PollTimer.Enabled) { PollTimer = new System.Timers.Timer(15000); PollTimer.Elapsed += (s, e) => { // Send keep alive to all connected clients if (Clients.Count > 0) { Parallel.ForEach(Clients.Values, client => KAHandler.SendKeepAlive(client)); } // DisconnectByReason hanging connections if (Processing.Count > 0) { Parallel.ForEach(Processing.Values, client => CheckTimeout(client)); } }; PollTimer.Start(); } // Setup timer. Every 5 seconds should be sufficient if (StatusTimer == null || !StatusTimer.Enabled) { StatusTimer = new System.Timers.Timer(5000); StatusTimer.Elapsed += (s, e) => { // Return if we are empty if (PlayerStatusQueue.IsEmpty) { return; } //var transaction =DB.BeginTransaction(); try { long timestamp = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds(); GPCMClient result; while (PlayerStatusQueue.TryDequeue(out result)) { // Skip if this player never finished logging in if (result == null) { continue; } if (!result.CompletedLoginProcess) { continue; } LoginQuery.UpdateStatus(timestamp, result.RemoteEndPoint.Address, result.PlayerInfo.PlayerId, (uint)result.PlayerInfo.PlayerStatus); } //transaction.Commit(); } catch (Exception ex) { LogWriter.Log.WriteException(ex); // transaction.Rollback(); } }; StatusTimer.Start(); } // Set connection handling ConnectionEnforceMode = EnforceMode.DuringPrepare; // TODO: Change this //FullErrorMessage = Config.GetValue("Settings", "LoginServerFullMessage").Replace("\"", ""); FullErrorMessage = ""; StartAcceptAsync(); }
public static void Switch(GPCMClient client, Dictionary <string, string> recv, GPCMConnectionUpdate OnSuccessfulLogin, GPCMStatusChanged OnStatusChanged) { string command = recv.Keys.First(); try { switch (command) { case "inviteto": InviteToHandler.AddFriends(client, recv); break; case "login": LoginHandler.ProcessLogin(client, recv, OnSuccessfulLogin, OnStatusChanged); break; case "getprofile": GetProfileHandler.SendProfile(client, recv); break; case "addbuddy": AddBuddyHandler.Addfriends(client, recv); break; case "delbuddy": DelBuddyHandler.Handle(client, recv); break; case "updateui": UpdateUiHandler.UpdateUi(client, recv); break; case "updatepro": UpdateProHandler.UpdateUser(client, recv); break; case "registernick": RegisterNickHandler.RegisterNick(client, recv); break; case "logout": client.DisconnectByReason(DisconnectReason.NormalLogout); break; case "status": StatusHandler.UpdateStatus(client, recv, OnStatusChanged); break; case "newuser": NewUserHandler.NewUser(client, recv); break; case "ka": KAHandler.SendKeepAlive(client); break; default: LogWriter.Log.Write("[GPCM] received unknown data " + command, LogLevel.Debug); GameSpyUtils.SendGPError(client, GPErrorCode.General, "An invalid request was sended."); break; } } catch (Exception e) { LogWriter.Log.WriteException(e); } }