75 lines
2.2 KiB
C#
75 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Timers;
|
|
using PSO2SERVER.LuaScriptEngine;
|
|
using PSO2SERVER.Network;
|
|
using PSO2SERVER.Protocol.Packets;
|
|
|
|
namespace PSO2SERVER
|
|
{
|
|
public class Server
|
|
{
|
|
public static Server Instance { get; private set; }
|
|
public string _name { get; private set; }
|
|
private readonly SocketServer _server;
|
|
public DateTime StartTime { get; private set; }
|
|
|
|
public LuaEngine LuaEngine { get; private set; }
|
|
|
|
public List<Client> Clients { get; private set; }
|
|
public Timer PingTimer;
|
|
|
|
public Server()
|
|
{
|
|
Instance = this;
|
|
_name = ServerApp.ServerBlockBalanceName;
|
|
_server = new SocketServer(ServerApp.ServerBlockBalanceProt);
|
|
StartTime = DateTime.Now;
|
|
LuaEngine = new LuaEngine();
|
|
|
|
_server.NewClient += HandleNewClient;
|
|
Clients = new List<Client>();
|
|
|
|
PingTimer = new Timer(1000 * ServerApp.Config.PingTime); // 1 Minute default
|
|
PingTimer.Elapsed += PingClients;
|
|
PingTimer.Start();
|
|
}
|
|
|
|
public void Run()
|
|
{
|
|
while (true)
|
|
{
|
|
// Run the underlying SocketServer
|
|
_server.Run();
|
|
|
|
// Check Clients to make sure they still exist
|
|
foreach (var client in Clients)
|
|
if (client.IsClosed)
|
|
{
|
|
Clients.Remove(client);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HandleNewClient(SocketClient client)
|
|
{
|
|
var newClient = new Client(this, client);
|
|
Clients.Add(newClient);
|
|
}
|
|
|
|
private void PingClients(object sender, ElapsedEventArgs e)
|
|
{
|
|
// Ping!
|
|
// TODO: Disconnect a client if we don't get a response in a certain amount of time
|
|
foreach (var client in Clients)
|
|
{
|
|
if (client != null && client._account != null)
|
|
{
|
|
Logger.Write("[HEY] Pinging " + client._account.Username);
|
|
client.SendPacket(new ServerPingPacket());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |