Commit c416b6df authored by Spirited's avatar Spirited

Modified sockets to be inheritance based

parent 124314ce
......@@ -15,7 +15,7 @@ namespace Comet.Account
/// listening functionality and event handling. This class defines how the server
/// listener and invoked events are customized for the account server.
/// </summary>
internal sealed class Server : TcpServerListener
internal sealed class Server : TcpServerListener<Client>
{
// Fields and Properties
private readonly PacketProcessor<Client> Processor;
......@@ -39,7 +39,7 @@ namespace Comet.Account
/// <param name="socket">Accepted client socket from the server socket</param>
/// <param name="buffer">Preallocated buffer from the server listener</param>
/// <returns>A new instance of a ServerActor around the client socket</returns>
protected override TcpServerActor Accepted(Socket socket, Memory<byte> buffer)
protected override Client Accepted(Socket socket, Memory<byte> buffer)
{
return new Client(socket, buffer);
}
......@@ -51,9 +51,9 @@ namespace Comet.Account
/// </summary>
/// <param name="actor">Server actor that represents the remote client</param>
/// <param name="packet">Packet bytes to be processed</param>
protected override void Received(TcpServerActor actor, ReadOnlySpan<byte> packet)
protected override void Received(Client actor, ReadOnlySpan<byte> packet)
{
this.Processor.Queue(actor as Client, packet.ToArray());
this.Processor.Queue(actor, packet.ToArray());
}
/// <summary>
......@@ -63,7 +63,7 @@ namespace Comet.Account
/// </summary>
/// <param name="actor">Actor requesting packet processing</param>
/// <param name="packet">An individual data packet to be processed</param>
private void Process(TcpServerActor actor, byte[] packet)
private void Process(Client actor, byte[] packet)
{
// Validate connection
if (!actor.Socket.Connected)
......@@ -90,7 +90,7 @@ namespace Comet.Account
{
// Decode packet bytes into the structure and process
msg.Decode(packet);
msg.Process(actor as Client);
msg.Process(actor);
}
catch (Exception e)
{
......
......@@ -13,7 +13,7 @@ namespace Comet.Game
/// listening functionality and event handling. This class defines how the server
/// listener and invoked events are customized for the game server.
/// </summary>
internal sealed class Server : TcpServerListener
internal sealed class Server : TcpServerListener<Client>
{
// Fields and Properties
private readonly PacketProcessor<Client> Processor;
......@@ -37,7 +37,7 @@ namespace Comet.Game
/// <param name="socket">Accepted client socket from the server socket</param>
/// <param name="buffer">Preallocated buffer from the server listener</param>
/// <returns>A new instance of a ServerActor around the client socket</returns>
protected override TcpServerActor Accepted(Socket socket, Memory<byte> buffer)
protected override Client Accepted(Socket socket, Memory<byte> buffer)
{
return new Client(socket, buffer);
}
......@@ -49,9 +49,9 @@ namespace Comet.Game
/// </summary>
/// <param name="actor">Server actor that represents the remote client</param>
/// <param name="packet">Packet bytes to be processed</param>
protected override void Received(TcpServerActor actor, ReadOnlySpan<byte> packet)
protected override void Received(Client actor, ReadOnlySpan<byte> packet)
{
this.Processor.Queue(actor as Client, packet.ToArray());
this.Processor.Queue(actor, packet.ToArray());
}
/// <summary>
......@@ -61,7 +61,7 @@ namespace Comet.Game
/// </summary>
/// <param name="actor">Actor requesting packet processing</param>
/// <param name="packet">An individual data packet to be processed</param>
private void Process(TcpServerActor actor, byte[] packet)
private void Process(Client actor, byte[] packet)
{
// Validate connection
if (!actor.Socket.Connected)
......@@ -91,7 +91,7 @@ namespace Comet.Game
{
// Decode packet bytes into the structure and process
msg.Decode(packet);
msg.Process(actor as Client);
msg.Process(actor);
}
catch (Exception e) { Console.WriteLine(e); }
}
......@@ -102,13 +102,11 @@ namespace Comet.Game
/// from other actors and server collections.
/// </summary>
/// <param name="actor">Server actor that represents the remote client</param>
protected override void Disconnected(TcpServerActor actor)
protected override void Disconnected(Client actor)
{
var client = actor as Client;
if (client == null) return;
if (client.Creation != null)
Kernel.Registration.Remove(client.Creation.Token);
if (actor == null) return;
if (actor.Creation != null)
Kernel.Registration.Remove(actor.Creation.Token);
}
}
}
......@@ -10,7 +10,9 @@ namespace Comet.Network.Sockets
/// actors. This allows a server listener to define unique socket events across
/// multiple projects via inheritance.
/// </summary>
public abstract class TcpServerEvents
/// <typeparam name="TActor">Type of actor passed by the parent project</typeparam>
public abstract class TcpServerEvents<TActor>
where TActor : TcpServerActor
{
/// <summary>
/// Invoked by the server listener's Accepting method to create a new server actor
......@@ -19,8 +21,8 @@ namespace Comet.Network.Sockets
/// </summary>
/// <param name="socket">Accepted client socket from the server socket</param>
/// <param name="buffer">Preallocated buffer from the server listener</param>
/// <returns>A new instance of a ServerActor around the client socket</returns>
protected abstract TcpServerActor Accepted(Socket socket, Memory<byte> buffer);
/// <returns>A new instance of a TActor around the client socket</returns>
protected abstract TActor Accepted(Socket socket, Memory<byte> buffer);
/// <summary>
/// Invoked by the server listener's Receiving method to process a completed packet
......@@ -30,7 +32,7 @@ namespace Comet.Network.Sockets
/// </summary>
/// <param name="actor">Server actor that represents the remote client</param>
/// <param name="packet">Packet bytes to be processed</param>
protected virtual void Received(TcpServerActor actor, ReadOnlySpan<byte> packet)
protected virtual void Received(TActor actor, ReadOnlySpan<byte> packet)
{
Console.WriteLine("Received {0} bytes", packet.Length);
Console.WriteLine(PacketDump.Hex(packet));
......@@ -42,7 +44,7 @@ namespace Comet.Network.Sockets
/// from other actors and server collections.
/// </summary>
/// <param name="actor">Server actor that represents the remote client</param>
protected virtual void Disconnected(TcpServerActor actor)
protected virtual void Disconnected(TActor actor)
{
}
}
......
......@@ -16,7 +16,9 @@ namespace Comet.Network.Sockets
/// Socket Task Extensions. Inherits from a base class for providing socket operation
/// event handling to the non-abstract derived class of TcpServerListener.
/// </summary>
public abstract class TcpServerListener : TcpServerEvents
/// <typeparam name="TActor">Type of actor passed by the parent project</typeparam>
public abstract class TcpServerListener<TActor> : TcpServerEvents<TActor>
where TActor : TcpServerActor
{
// Fields and properties
private readonly Semaphore AcceptanceSemaphore;
......@@ -106,7 +108,7 @@ namespace Comet.Network.Sockets
private async Task Receiving(object state)
{
// Initialize multiple receive variables
var actor = state as TcpServerActor;
var actor = state as TActor;
int consumed = 0, examined = 0;
while (actor.Socket.Connected && !this.ShutdownToken.IsCancellationRequested)
{
......@@ -140,7 +142,7 @@ namespace Comet.Network.Sockets
/// <param name="buffer">Actor for consuming bytes from the buffer</param>
/// <param name="examined">Number of examined bytes from the receive</param>
/// <param name="consumed">Number of consumed bytes by the split reader</param>
protected virtual void Splitting(TcpServerActor actor, int examined, ref int consumed)
protected virtual void Splitting(TActor actor, int examined, ref int consumed)
{
// Consume packets from the socket buffer
consumed = 0;
......@@ -160,7 +162,7 @@ namespace Comet.Network.Sockets
/// leased to the client on accept.
/// </summary>
/// <param name="actor">Actor being disconnected</param>
private void Disconnecting(TcpServerActor actor)
private void Disconnecting(TActor actor)
{
// Reclaim resources and release back to server pools
actor.Buffer.Span.Clear();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment