183 lines
5.6 KiB
C#
183 lines
5.6 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using MySql.Data.MySqlClient;
|
|
|
|
namespace gServer
|
|
{
|
|
public class MainClass
|
|
{
|
|
public int SecondsPassed = 0;
|
|
public int SecondsPassedHalf = 0;
|
|
|
|
private String MySQL_Host = "localhost";
|
|
private String MySQL_Username = "gs";
|
|
private String MySQL_Password = "gs";
|
|
private String MySQL_Database = "gs";
|
|
private String MySQL_Connector = "";
|
|
private IDbConnection MySQL_Connection;
|
|
|
|
public MrAG.Serverlist.Server Listener;
|
|
public MrAG.Serverlist.Server ListenerAlternative;
|
|
|
|
public MrAG.Serverlist.Server ServerlistListener;
|
|
|
|
public List<User> Users = new List<User>();
|
|
public List<ServerlistServer> ServerlistServers = new List<ServerlistServer>();
|
|
public List<ServerData> Servers = new List<ServerData>();
|
|
|
|
public void Start()
|
|
{
|
|
this.MySQL_Connector = "Server=" + this.MySQL_Host + ";" +
|
|
"Database=" + this.MySQL_Database + ";" +
|
|
"User ID=" + this.MySQL_Username + ";" +
|
|
"Password=" + this.MySQL_Password + ";" + "Pooling=false";
|
|
|
|
this.MySQL_Connection = new MySqlConnection(this.MySQL_Connector);
|
|
this.MySQL_Connection.Open();
|
|
|
|
this.Query("UPDATE `users` SET `API_Sessionkey`=''");
|
|
|
|
this.Listener = MrAG.Serverlist.Host("__site", 17000, "__site", false);
|
|
this.ListenerAlternative = MrAG.Serverlist.Host("__site", 2222, "__site", false);
|
|
|
|
this.Listener.OnIncomingConnection = new Action<MrAG.Networking.IncomingConnection>(IncomingConnection);
|
|
this.Listener.OnFinishedConnection = new Action<MrAG.Networking.IncomingConnection>(FinishedConnection);
|
|
this.ListenerAlternative.OnIncomingConnection = new Action<MrAG.Networking.IncomingConnection>(IncomingConnection);
|
|
this.ListenerAlternative.OnFinishedConnection = new Action<MrAG.Networking.IncomingConnection>(FinishedConnection);
|
|
|
|
this.Listener.Timeout = 2000;
|
|
this.ListenerAlternative.Timeout = 2000;
|
|
|
|
this.Log("GamerServices server started on port 17000 (and 2222).");
|
|
|
|
this.ServerlistListener = MrAG.Serverlist.Host("__site", 8443, "__site", false);
|
|
this.ServerlistListener.OnIncomingConnection = new Action<MrAG.Networking.IncomingConnection>(Serverlist_IncomingConnection);
|
|
this.ServerlistListener.OnFinishedConnection = new Action<MrAG.Networking.IncomingConnection>(Serverlist_FinishedConnection);
|
|
this.ServerlistListener.Timeout = 2000;
|
|
|
|
double LastKeepAliveForSQL = DateTime.Now.TimeOfDay.TotalMinutes; // keepalive :3
|
|
while(true)
|
|
{
|
|
if (Math.Abs(LastKeepAliveForSQL - DateTime.Now.TimeOfDay.TotalMinutes) >= 30){ // 30 min interval
|
|
this.Query("SELECT 1"); // Completly useless query, just for keepalive :3
|
|
LastKeepAliveForSQL = DateTime.Now.TimeOfDay.TotalMinutes;
|
|
}
|
|
|
|
this.Listener.Update();
|
|
this.ListenerAlternative.Update();
|
|
this.ServerlistListener.Update();
|
|
|
|
for(int i=0; i<this.Users.Count; i++)
|
|
this.Users[i].Update();
|
|
|
|
for(int i=0; i<this.ServerlistServers.Count; i++)
|
|
this.ServerlistServers[i].Update();
|
|
|
|
Thread.Sleep(17);
|
|
}
|
|
}
|
|
|
|
public void Log(String Text)
|
|
{
|
|
Console.WriteLine(String.Format("[{0}] {1}", DateTime.Now.ToString(), Text));
|
|
}
|
|
|
|
public long Epoch()
|
|
{
|
|
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
|
|
}
|
|
|
|
public void IncomingConnection(MrAG.Networking.IncomingConnection c)
|
|
{
|
|
}
|
|
|
|
public void FinishedConnection(MrAG.Networking.IncomingConnection c)
|
|
{
|
|
this.Log("Connection accepted from " + c.IP);
|
|
|
|
this.Users.Add(new User(c, this));
|
|
}
|
|
|
|
public void Serverlist_IncomingConnection(MrAG.Networking.IncomingConnection c)
|
|
{
|
|
}
|
|
|
|
public void Serverlist_FinishedConnection(MrAG.Networking.IncomingConnection c)
|
|
{
|
|
this.Log("Serverlist connection accepted from " + c.IP);
|
|
|
|
this.ServerlistServers.Add(new ServerlistServer(c, this));
|
|
}
|
|
|
|
public Hashtable[] Query(String qry)
|
|
{
|
|
IDbCommand dbcmd = this.MySQL_Connection.CreateCommand();
|
|
dbcmd.CommandText = qry;
|
|
IDataReader dr = null;
|
|
try{
|
|
dr = dbcmd.ExecuteReader();
|
|
}catch(Exception ex){
|
|
this.Log("********************************************\nQUERY ERROR:\n" + ex.Message + "\n\nQUERY WAS:\n" + qry + "\n********************************************");
|
|
}
|
|
return Rows(dr);
|
|
}
|
|
|
|
public Hashtable[] Rows(IDataReader resource)
|
|
{
|
|
List<Hashtable> ret = new List<Hashtable>();
|
|
|
|
while(resource.Read())
|
|
{
|
|
Hashtable newRow = new Hashtable();
|
|
for (int i=0; i<resource.FieldCount; i++)
|
|
newRow[resource.GetName(i)] = resource.GetValue(i).ToString();
|
|
ret.Add(newRow);
|
|
}
|
|
|
|
resource.Close();
|
|
resource.Dispose();
|
|
resource = null;
|
|
|
|
return ret.ToArray();
|
|
}
|
|
|
|
public int Intval(String input)
|
|
{
|
|
int val = 0;
|
|
int.TryParse(input, out val);
|
|
return val;
|
|
}
|
|
|
|
public String Safe(String input)
|
|
{
|
|
String ret = input;
|
|
ret = ret.Replace("\\", "\\\\"); // \ -> \\
|
|
ret = ret.Replace("'", "\\'"); // ' -> \'
|
|
return ret;
|
|
}
|
|
|
|
public string MD5(string input)
|
|
{
|
|
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
|
|
System.Text.StringBuilder s = new System.Text.StringBuilder();
|
|
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
|
|
|
|
bs = x.ComputeHash(bs);
|
|
foreach (byte b in bs)
|
|
{
|
|
s.Append(b.ToString("x2"));
|
|
}
|
|
|
|
return s.ToString().ToLower();
|
|
}
|
|
|
|
public static void Main (string[] args)
|
|
{
|
|
new MainClass().Start();
|
|
}
|
|
}
|
|
}
|