152 lines
3.9 KiB
C#
152 lines
3.9 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using MySql.Data.MySqlClient;
|
|
|
|
namespace gServer
|
|
{
|
|
public class ServerData
|
|
{
|
|
public string Name = "null";
|
|
public string Meta = "null";
|
|
public string Game = "null";
|
|
public int CurrentUsers = 0;
|
|
public int MaxUsers = 0;
|
|
|
|
public string IP = "null";
|
|
public int Port = 0;
|
|
|
|
public long lastUpdate;
|
|
}
|
|
|
|
public class ServerlistServer
|
|
{
|
|
public MainClass main;
|
|
public MrAG.Networking.IncomingConnection Connection;
|
|
|
|
public ServerData serverData = new ServerData();
|
|
|
|
public ServerlistServer (MrAG.Networking.IncomingConnection c, MainClass main)
|
|
{
|
|
this.main = main;
|
|
|
|
c.Connection.Packets[0] = new Action<MrAG.Networking.Packet>(Packet_Add);
|
|
c.Connection.Packets[1] = new Action<MrAG.Networking.Packet>(Packet_Update);
|
|
c.Connection.Packets[2] = new Action<MrAG.Networking.Packet>(Packet_Offline);
|
|
c.Connection.Packets[3] = new Action<MrAG.Networking.Packet>(Packet_UpdatePlayerCount);
|
|
c.Connection.Packets[4] = new Action<MrAG.Networking.Packet>(Packet_UpdateMeta);
|
|
|
|
this.Connection = c;
|
|
}
|
|
|
|
public void Disconnected(bool Unexpected)
|
|
{
|
|
if(Unexpected)
|
|
this.main.Log(this.Connection.IP + " from Serverlist Server unexpectedly disconnected.");
|
|
|
|
this.main.Servers.Remove(this.serverData);
|
|
this.main.ServerlistServers.Remove(this);
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
try
|
|
{
|
|
this.Connection.Connection.Update();
|
|
}catch(Exception ex){
|
|
if(ex.Message == "The object was used after being disposed.")
|
|
{
|
|
this.Disconnected(true);
|
|
}
|
|
this.main.Log(this.Connection.IP + " from Serverlist Server errored: " + ex.Message);
|
|
this.Connection.Kick("Socket error");
|
|
}
|
|
}
|
|
|
|
public void InvalidPacket()
|
|
{
|
|
this.Connection.Kick("Invalid packet received!");
|
|
this.main.Log(this.Connection.IP + " from Serverlist Server kicked for an invalid packet.");
|
|
this.Disconnected(false);
|
|
}
|
|
|
|
public void Packet_Add(MrAG.Networking.Packet p)
|
|
{
|
|
bool shouldAdd = this.serverData.IP == "null";
|
|
this.serverData.IP = this.Connection.IP;
|
|
//this.serverData.IP = "127.0.0.1";
|
|
|
|
try
|
|
{
|
|
this.serverData.Name = p.ReadString();
|
|
this.serverData.Meta = p.ReadString();
|
|
this.serverData.Game = p.ReadString();
|
|
this.serverData.Port = p.ReadInt();
|
|
this.serverData.MaxUsers = p.ReadInt();
|
|
}catch{
|
|
this.InvalidPacket();
|
|
return;
|
|
}
|
|
|
|
if(shouldAdd)
|
|
{
|
|
this.main.Servers.Add(this.serverData);
|
|
this.main.Log("Added server " + this.serverData.Name + " to " + this.serverData.Game);
|
|
}else
|
|
this.main.Log("Tried adding " + this.serverData.Name + ", but shouldAdd = false");
|
|
|
|
p.Start_Send();
|
|
p.AddByte(0);
|
|
p.AddBool(shouldAdd);
|
|
if(!shouldAdd)
|
|
p.AddString("Already added!");
|
|
p.Finish_Send();
|
|
}
|
|
|
|
public void Packet_Update(MrAG.Networking.Packet p)
|
|
{
|
|
this.serverData.lastUpdate = this.main.Epoch();
|
|
|
|
if(this.serverData.IP == "null")
|
|
{
|
|
this.Connection.Connection.Connection.Start_Send();
|
|
this.Connection.Connection.Connection.AddByte(1);
|
|
this.Connection.Connection.Connection.AddBool(false);
|
|
this.Connection.Connection.Connection.Finish_Send();
|
|
}
|
|
}
|
|
|
|
public void Packet_Offline(MrAG.Networking.Packet p)
|
|
{
|
|
this.main.Servers.Remove(this.serverData);
|
|
|
|
this.serverData = new ServerData();
|
|
|
|
this.main.Log("Server " + this.Connection.IP + " stopped");
|
|
}
|
|
|
|
public void Packet_UpdatePlayerCount(MrAG.Networking.Packet p)
|
|
{
|
|
try
|
|
{
|
|
this.serverData.CurrentUsers = p.ReadInt();
|
|
}catch{
|
|
this.InvalidPacket();
|
|
return;
|
|
}
|
|
}
|
|
|
|
public void Packet_UpdateMeta(MrAG.Networking.Packet p)
|
|
{
|
|
try
|
|
{
|
|
this.serverData.Meta = p.ReadString();
|
|
}catch{
|
|
this.InvalidPacket();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|