2015-01-13 18:29:11 +00:00
|
|
|
|
using System;
|
2014-07-08 12:58:53 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2015-01-13 18:29:11 +00:00
|
|
|
|
using xServer.Core.Packets;
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
2015-01-13 18:29:11 +00:00
|
|
|
|
namespace xServer.Core
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
|
|
|
|
public class Server
|
|
|
|
|
{
|
|
|
|
|
public long BytesReceived { get; set; }
|
|
|
|
|
public long BytesSent { get; set; }
|
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when the state of the server changes.
|
|
|
|
|
/// </summary>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public event ServerStateEventHandler ServerState;
|
2015-04-21 18:27:52 +00:00
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a method that will handle a change in the server's state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s">The server to update the state of.</param>
|
|
|
|
|
/// <param name="listening">True if the server is listening; False if the server
|
|
|
|
|
/// is not listening.</param>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public delegate void ServerStateEventHandler(Server s, bool listening);
|
|
|
|
|
|
|
|
|
|
private void OnServerState(bool listening)
|
|
|
|
|
{
|
|
|
|
|
if (ServerState != null)
|
|
|
|
|
{
|
|
|
|
|
ServerState(this, listening);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when the state of a client changes.
|
|
|
|
|
/// </summary>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public event ClientStateEventHandler ClientState;
|
2015-04-21 18:27:52 +00:00
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a method that will handle a change in a client's state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s"></param>
|
|
|
|
|
/// <param name="c"></param>
|
|
|
|
|
/// <param name="connected"></param>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public delegate void ClientStateEventHandler(Server s, Client c, bool connected);
|
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires an event that informs subscribers that the a packet has been
|
|
|
|
|
/// received from the client.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="packet"></param>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
private void OnClientState(Client c, bool connected)
|
|
|
|
|
{
|
|
|
|
|
if (ClientState != null)
|
|
|
|
|
{
|
|
|
|
|
ClientState(this, c, connected);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when a packet is received by a client.
|
|
|
|
|
/// </summary>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public event ClientReadEventHandler ClientRead;
|
2015-04-21 18:27:52 +00:00
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a method that will handle a packet received from a client.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s">The destination server of the packet; also where the client specified
|
|
|
|
|
/// should reside</param>
|
|
|
|
|
/// <param name="c">The client that has sent the packet.</param>
|
|
|
|
|
/// <param name="packet">The packet that was sent to the server.</param>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public delegate void ClientReadEventHandler(Server s, Client c, IPacket packet);
|
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires an event that informs subscribers that the a packet has been
|
|
|
|
|
/// received from the client.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="packet"></param>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
private void OnClientRead(Client c, IPacket packet)
|
|
|
|
|
{
|
|
|
|
|
if (ClientRead != null)
|
|
|
|
|
{
|
|
|
|
|
ClientRead(this, c, packet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event ClientWriteEventHandler ClientWrite;
|
2015-04-21 18:27:52 +00:00
|
|
|
|
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public delegate void ClientWriteEventHandler(Server s, Client c, IPacket packet, long length);
|
|
|
|
|
|
|
|
|
|
private void OnClientWrite(Client c, IPacket packet, long length, byte[] rawData)
|
|
|
|
|
{
|
|
|
|
|
if (ClientWrite != null)
|
|
|
|
|
{
|
|
|
|
|
ClientWrite(this, c, packet, length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Socket _handle;
|
|
|
|
|
private SocketAsyncEventArgs _item;
|
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets if the server is currently processing data that
|
|
|
|
|
/// should prevent disconnection.
|
|
|
|
|
/// </summary>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
private bool Processing { get; set; }
|
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the status of the server. True if the server is currently
|
|
|
|
|
/// listening; False if the server is not currently listening.
|
|
|
|
|
/// </summary>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public bool Listening { get; private set; }
|
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The internal list of the clients connected to the server.
|
|
|
|
|
/// </summary>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
private List<Client> _clients;
|
2015-04-21 18:27:52 +00:00
|
|
|
|
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the clients currently connected to the server, or an empty array of
|
|
|
|
|
/// clients if the server is currently not listening.
|
|
|
|
|
/// </summary>
|
2014-07-08 12:58:53 +00:00
|
|
|
|
public Client[] Clients
|
|
|
|
|
{
|
2015-04-21 18:27:52 +00:00
|
|
|
|
get { return Listening ? _clients.ToArray() : new Client[0]; }
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int ConnectedClients { get; set; }
|
2015-05-29 23:03:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A collection containing all of the clients that have connected to the server.
|
|
|
|
|
/// </summary>
|
2015-03-20 13:45:58 +00:00
|
|
|
|
public Dictionary<string, DateTime> AllTimeConnectedClients { get; set; }
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
|
|
|
|
private List<Type> PacketTypes { get; set; }
|
|
|
|
|
|
2015-03-31 16:15:48 +00:00
|
|
|
|
public Server()
|
2014-07-08 12:58:53 +00:00
|
|
|
|
{
|
|
|
|
|
PacketTypes = new List<Type>();
|
2015-03-20 13:45:58 +00:00
|
|
|
|
AllTimeConnectedClients = new Dictionary<string, DateTime>();
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Listen(ushort port)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!Listening)
|
|
|
|
|
{
|
|
|
|
|
_clients = new List<Client>();
|
|
|
|
|
|
|
|
|
|
_item = new SocketAsyncEventArgs();
|
|
|
|
|
_item.Completed += Process;
|
|
|
|
|
|
2015-04-07 16:27:56 +00:00
|
|
|
|
if (_handle != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_handle.Close();
|
|
|
|
|
}
|
|
|
|
|
catch
|
2015-04-21 18:27:52 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2015-04-07 16:27:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-08 12:58:53 +00:00
|
|
|
|
_handle = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
|
|
|
|
|
|
_handle.Bind(new IPEndPoint(IPAddress.Any, port));
|
|
|
|
|
_handle.Listen(1000);
|
|
|
|
|
|
|
|
|
|
Processing = false;
|
|
|
|
|
Listening = true;
|
|
|
|
|
|
|
|
|
|
OnServerState(true);
|
|
|
|
|
|
|
|
|
|
if (!_handle.AcceptAsync(_item))
|
|
|
|
|
Process(null, _item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
Disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a Type to the serializer so a message can be properly serialized.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parent">The parent type, i.e.: IPacket</param>
|
|
|
|
|
/// <param name="type">Type to be added</param>
|
|
|
|
|
public void AddTypeToSerializer(Type parent, Type type)
|
|
|
|
|
{
|
|
|
|
|
if (type == null || parent == null)
|
|
|
|
|
throw new ArgumentNullException();
|
|
|
|
|
|
|
|
|
|
PacketTypes.Add(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddTypesToSerializer(Type parent, params Type[] types)
|
|
|
|
|
{
|
|
|
|
|
foreach (Type type in types)
|
|
|
|
|
AddTypeToSerializer(parent, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Process(object s, SocketAsyncEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (e.SocketError == SocketError.Success)
|
|
|
|
|
{
|
2015-05-26 17:35:28 +00:00
|
|
|
|
Client client = new Client(this, e.AcceptSocket, PacketTypes.ToArray());
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
|
|
|
|
lock (_clients)
|
|
|
|
|
{
|
2015-05-26 17:35:28 +00:00
|
|
|
|
_clients.Add(client);
|
|
|
|
|
client.ClientState += OnClientState;
|
|
|
|
|
client.ClientRead += OnClientRead;
|
|
|
|
|
client.ClientWrite += OnClientWrite;
|
2014-07-08 12:58:53 +00:00
|
|
|
|
|
2015-05-26 17:35:28 +00:00
|
|
|
|
OnClientState(client, true);
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
e.AcceptSocket = null;
|
|
|
|
|
if (!_handle.AcceptAsync(e))
|
|
|
|
|
Process(null, e);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Disconnect();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
Disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Disconnect()
|
|
|
|
|
{
|
|
|
|
|
if (Processing)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Processing = true;
|
|
|
|
|
|
|
|
|
|
if (_handle != null)
|
|
|
|
|
_handle.Close();
|
|
|
|
|
|
|
|
|
|
lock (_clients)
|
|
|
|
|
{
|
|
|
|
|
while (_clients.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
_clients[0].Disconnect();
|
2015-03-31 16:45:31 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_clients.RemoveAt(0);
|
|
|
|
|
}
|
|
|
|
|
catch
|
2015-04-21 18:27:52 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2014-07-08 12:58:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Listening = false;
|
|
|
|
|
OnServerState(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-21 18:27:52 +00:00
|
|
|
|
}
|