using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Net.Sockets;
using System.Net;
using CSharpRSPServer.Server.Util;
using CSharpRSPServer.Server.Model.Players;
using CSharpRSPServer.Server.Model.NPCs;
using CSharpRSPServer.Server.Model.Items;
using CSharpRSPServer.Server.World;
namespace CSharpRSPServer.Server
{
public class Server
{
public static Server clientHandler = null;
public static Socket clientListener = null;
public static Boolean shutdownServer = false;
public static Boolean shutdownClientHandler;
public static int ServerlistenerPort = 43594;
public static ItemHandler itemHandler = new ItemHandler();
public static PlayerHandler playerHandler = new PlayerHandler();
public static NPCHandler npcHandler = new NPCHandler();
public static ShopHandler shopHandler = new ShopHandler();
public static ObjectHandler objectHandler = new ObjectHandler();
public Server()
{
}
public void KillServer()
{
try
{
shutdownClientHandler = true;
if (clientListener != null)
{
clientListener.Close();
}
clientListener = null;
}
catch (Exception __ex)
{
// error
}
}
public void OnAccept(IAsyncResult e)
{
try
{
while (!e.IsCompleted)
{
Thread.Sleep(500);
}
Socket socket = clientListener.EndAccept(e);
socket.NoDelay = false;
String connectingHost = socket.RemoteEndPoint.AddressFamily.ToString();
if (!ConnectionHandler.floodProtection(connectingHost) &&
!ConnectionHandler.containsIp(connectingHost) &&
!ConnectionHandler.isIpBanned(connectingHost))
{
ConnectionHandler.addIp(connectingHost);
//Misc.println("ClientHandler: Accepted from " + connectingHost + ":" + s.getPort());
playerHandler.newPlayerClient(socket, connectingHost);
}
else
{
//Misc.println("ClientHandler: Rejected " + connectingHost + ":" + s.getPort());
socket.Close();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
clientListener.BeginAccept(OnAccept, clientListener);
}
public void run()
{
try
{
shutdownClientHandler = false;
clientListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientListener.Bind(new IPEndPoint(IPAddress.None, ServerlistenerPort));
clientListener.Listen(Config.MAX_PLAYERS);
clientListener.BeginAccept(OnAccept, clientListener);
//Misc.println("Starting " + Config.SERVER_NAME + " on " + clientListener.getInetAddress().getHostAddress() + ":" + clientListener.getLocalPort());
/*while (true)
{
Socket s = clientListener.Accept();
s.NoDelay = false;
String connectingHost = s.RemoteEndPoint.AddressFamily.ToString();
if (!ConnectionHandler.floodProtection(connectingHost) &&
!ConnectionHandler.containsIp(connectingHost) &&
!ConnectionHandler.isIpBanned(connectingHost))
{
ConnectionHandler.addIp(connectingHost);
//Misc.println("ClientHandler: Accepted from " + connectingHost + ":" + s.getPort());
playerHandler.newPlayerClient(s, connectingHost);
}
else
{
//Misc.println("ClientHandler: Rejected " + connectingHost + ":" + s.getPort());
s.Close();
}
}*/
}
catch (Exception ioe)
{
throw new Exception(ioe.Message);
if (!shutdownClientHandler)
{
//Misc.println("Error: Unable to startup listener on " + ServerlistenerPort + " - port already in use?");
}
else
{
//Misc.println("ClientHandler was shut down.");
}
}
}
public void initialize()
{
ConnectionHandler.Initialize();
try
{
Thread t = new Thread(run);
t.Start();
}
catch
{
throw new Exception("t");
}
long lastTicks = Misc.CurrMillis();
long totalTimeSpentProcessing = 0;
int cycle = 0;
while (!shutdownServer)
{
try
{
itemHandler.process();
playerHandler.process();
npcHandler.process();
shopHandler.process();
objectHandler.process();
}
catch
{
throw new Exception("t2");
}
long timeSpent = Misc.CurrMillis() - lastTicks;
totalTimeSpentProcessing += timeSpent;
if (timeSpent >= Config.CYCLE_TIME)
{
timeSpent = Config.CYCLE_TIME;
}
try
{
Thread.Sleep((int)(Config.CYCLE_TIME - timeSpent));
}
catch (Exception ex)
{
}
lastTicks = Misc.CurrMillis();
cycle++;
if (cycle % 100 == 0)
{
float time = ((float)totalTimeSpentProcessing) / cycle;
}
}
playerHandler.destruct();
clientHandler.KillServer();
}
}
}