Пример #1
0
        /// <summary>
        /// Creates a new instance of <see cref="GPCMServer"/>
        /// </summary>
        public GPCMServer(DatabaseDriver databaseDriver) : base(databaseDriver)
        {
            GPCMClient.OnDisconnect      += GpcmClient_OnDisconnect;
            GPCMClient.OnSuccessfulLogin += GpcmClient_OnSuccessfulLogin;

            // 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 => client.SendKeepAlive());
                    }

                    // Disconnect 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;
                    }

                    // Open database connection
                    using (var transaction = databaseDriver.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;
                                }

                                // Only update record under these two status'
                                {
                                    // Update player record
                                    databaseDriver.Execute(
                                        "UPDATE profiles SET status=@P3, lastip=@P0, lastonline=@P1 WHERE profileid=@P2",
                                        result.RemoteEndPoint.Address,
                                        timestamp,
                                        result.PlayerId,
                                        (uint)result.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 = "";
        }