Skip to content

Commit

Permalink
refactor in code and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
MUN1Z committed Aug 25, 2020
1 parent 4b21b4e commit 6745861
Show file tree
Hide file tree
Showing 31 changed files with 689 additions and 631 deletions.
9 changes: 5 additions & 4 deletions examples/Benchmarks/KingNetwork.Benchmarks.Load/RunClients.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using KingNetwork.Client;
using KingNetwork.Shared;
using KingNetwork.Shared.Interfaces;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -158,14 +159,14 @@ public static void StartClients(string ip, int clientConnections)
/// <summary>
/// Method responsible for execute the callback of message received from server in client.
/// </summary>
/// <param name="kingBuffer">The king buffer from received message.</param>
private static void OnMessageReceived(KingBufferReader kingBuffer)
/// <param name="reader">The king buffer reader from received message.</param>
private static void OnMessageReceived(IKingBufferReader reader)
{
try
{
_messagesReceived++;
_dataReceived += kingBuffer.Length;
_bufferLength = kingBuffer.Length;
_dataReceived += reader.Length;
_bufferLength = reader.Length;

//if (_networkListenerType == NetworkListenerType.WS)
//{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using KingNetwork.Client;
using KingNetwork.Shared;
using KingNetwork.Shared.Interfaces;
using KingNetwork.SimpleExample.Shared;
using System;
using System.Threading;
Expand Down Expand Up @@ -53,15 +54,15 @@ static void Main(string[] args)
/// <summary>
/// Method responsible for execute the callback of message received from server in client.
/// </summary>
/// <param name="kingBuffer">The king buffer from received message.</param>
private static void OnMessageReceived(KingBufferReader kingBuffer)
/// <param name="reader">The king buffer reader from received message.</param>
private static void OnMessageReceived(IKingBufferReader reader)
{
try
{
if (_networkListenerType == NetworkListenerType.WSText)
Console.WriteLine($"OnMessageReceived: {kingBuffer.ReadString()}");
Console.WriteLine($"OnMessageReceived: {reader.ReadString()}");
else
switch (kingBuffer.ReadMessagePacket<MyPackets>())
switch (reader.ReadMessagePacket<MyPackets>())
{
case MyPackets.PacketOne:
Console.WriteLine("OnMessageReceived for PacketOne");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using KingNetwork.Server.Interfaces;
using KingNetwork.Shared;
using KingNetwork.SimpleExample.Shared;
using KingNetwork.Shared.Interfaces;

namespace KingNetwork.SimpleExample.Server
{
Expand Down Expand Up @@ -39,16 +40,16 @@ static void Main(string[] args)
/// Method responsible for execute the callback of message received from client in server.
/// </summary>
/// <param name="client">The client instance.</param>
/// <param name="kingBuffer">The king buffer from received message.</param>
private static void OnMessageReceived(IClient client, KingBufferReader kingBuffer)
/// <param name="reader">The king buffer reader from received message.</param>
private static void OnMessageReceived(IClient client, IKingBufferReader reader)
{
try
{
if (_networkListenerType == NetworkListenerType.WSText)
{
Console.WriteLine($"OnMessageReceived from {client.Id}");

string text = kingBuffer.ReadString();
string text = reader.ReadString();
Console.WriteLine($"Message: {text}");

var writer = KingBufferWriter.Create();
Expand All @@ -59,11 +60,11 @@ private static void OnMessageReceived(IClient client, KingBufferReader kingBuffe
}
else
{
switch (kingBuffer.ReadMessagePacket<MyPackets>())
switch (reader.ReadMessagePacket<MyPackets>())
{
case MyPackets.PacketOne:
Console.WriteLine($"OnMessageReceived PacketOne from {client.Id}");
Console.WriteLine($"Message: {kingBuffer.ReadString()}");
Console.WriteLine($"Message: {reader.ReadString()}");

var writer = KingBufferWriter.Create();

Expand Down
40 changes: 40 additions & 0 deletions src/KingNetwork.Client/Interfaces/INetworkListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using KingNetwork.Shared;
using KingNetwork.Shared.Interfaces;

namespace KingNetwork.Client.Interfaces
{
/// <summary>
/// This class is responsible for representation of network listener.
/// </summary>
internal interface INetworkListener
{
/// <summary>
/// Method responsible for start the client network tcp listener.
/// </summary>
/// <param name="ip">The ip address of server.</param>
/// <param name="port">The port of server.</param>
/// <param name="maxMessageBuffer">The max length of message buffer.</param>
void StartClient(string ip, int port, ushort maxMessageBuffer);

/// <summary>
/// Method responsible for send message to connected server.
/// </summary>
/// <param name="writer">The king buffer writer of received message.</param>
void SendMessage(IKingBufferWriter writer);

/// <summary>
/// This method is responsible for call the dispose implementation method.
/// </summary>
void Dispose();

/// <summary>
/// This method is responsible for verify if listener has connected.
/// </summary>
bool Connected();

/// <summary>
/// Method responsible for stop the tcp network listener.
/// </summary>
void Stop();
}
}
5 changes: 3 additions & 2 deletions src/KingNetwork.Client/Interfaces/IPacketHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using KingNetwork.Shared;
using KingNetwork.Shared.Interfaces;

namespace KingNetwork.Client.Interfaces
{
Expand All @@ -10,7 +11,7 @@ internal interface IPacketHandler
/// <summary>
/// This method is responsible for receive the message from client packet handler.
/// </summary>
/// <param name="kingBuffer">The king buffer of received message.</param>
void HandleMessageData(KingBufferReader kingBuffer);
/// <param name="reader">The king buffer reader of received message.</param>
void HandleMessageData(IKingBufferReader reader);
}
}
34 changes: 21 additions & 13 deletions src/KingNetwork.Client/KingClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading;
using KingNetwork.client;
using KingNetwork.Client.Interfaces;
using KingNetwork.Client.Listeners;
using KingNetwork.Shared;
using KingNetwork.Shared.Interfaces;

namespace KingNetwork.Client
{
Expand All @@ -21,7 +23,7 @@ public class KingClient
/// <summary>
/// The network listener instance.
/// </summary>
private NetworkListener _networkListener;
private INetworkListener _networkListener;

/// <summary>
/// The thread for start the network listener.
Expand Down Expand Up @@ -49,8 +51,8 @@ public class KingClient
/// <summary>
/// The client packet handler delegate.
/// </summary>
/// <param name="kingBuffer">The king buffer of received message.</param>
public delegate void ClientPacketHandler(KingBufferReader kingBuffer);
/// <param name="reader">The king buffer reader of received message.</param>
public delegate void ClientPacketHandler(IKingBufferReader reader);

#endregion

Expand Down Expand Up @@ -128,7 +130,13 @@ public void Connect(string ip, ushort port = 7171, NetworkListenerType listenerT
{
_clientThread = new Thread(() =>
{
_networkListener = NetworkListenerFactory.CreateForType(listenerType, OnMessageReceived, OnClientDisconnected);
if (listenerType == NetworkListenerType.TCP)
_networkListener = new TcpNetworkListener(OnMessageReceived, OnClientDisconnected);
else if (listenerType == NetworkListenerType.UDP)
_networkListener = new UdpNetworkListener(OnMessageReceived, OnClientDisconnected);
else if (listenerType == NetworkListenerType.WSBinary || listenerType == NetworkListenerType.WSText)
_networkListener = new WSNetworkListener(listenerType, OnMessageReceived, OnClientDisconnected);
_networkListener.StartClient(ip, port, maxMessageBuffer);
});

Expand Down Expand Up @@ -159,12 +167,12 @@ public void Disconnect()
/// <summary>
/// Method responsible for send message to connected server.
/// </summary>
/// <param name="kingBuffer">The king buffer to send message.</param>
public void SendMessage(KingBufferWriter kingBuffer)
/// <param name="writer">The king buffer writer to send message.</param>
public void SendMessage(IKingBufferWriter writer)
{
try
{
_networkListener.SendMessage(kingBuffer);
_networkListener.SendMessage(writer);
}
catch (Exception ex)
{
Expand All @@ -179,15 +187,15 @@ public void SendMessage(KingBufferWriter kingBuffer)
/// <summary>
/// Method responsible for execute the callback of message received from client in server.
/// </summary>
/// <param name="kingBuffer">The king buffer of received message.</param>
private void OnMessageReceived(KingBufferReader kingBuffer)
/// <param name="reader">The king buffer reader of received message.</param>
private void OnMessageReceived(IKingBufferReader reader)
{
try
{
if (kingBuffer.Length > 0 && _clientPacketHandlers.Count > 0 && _clientPacketHandlers.TryGetValue(kingBuffer.ReadByte(), out var clientPacketHandler))
clientPacketHandler(kingBuffer);
if (reader.Length > 0 && _clientPacketHandlers.Count > 0 && _clientPacketHandlers.TryGetValue(reader.ReadByte(), out var clientPacketHandler))
clientPacketHandler(reader);
else
MessageReceivedHandler(kingBuffer);
MessageReceivedHandler(reader);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using KingNetwork.Shared;
using KingNetwork.Client.Interfaces;
using KingNetwork.Shared;
using KingNetwork.Shared.Interfaces;
using System;
using System.Net;
using System.Net.Sockets;

namespace KingNetwork.Client
namespace KingNetwork.Client.Listeners
{
/// <summary>
/// This class is responsible for representation of abstract network listener.
/// </summary>
public abstract class NetworkListener : IDisposable
public abstract class NetworkListener : INetworkListener, IDisposable
{
#region private members

Expand Down Expand Up @@ -45,7 +47,7 @@ public abstract class NetworkListener : IDisposable
/// <summary>
/// The value for remote end point.
/// </summary>
public EndPoint _remoteEndPoint;
protected EndPoint _remoteEndPoint;

#endregion

Expand All @@ -54,8 +56,8 @@ public abstract class NetworkListener : IDisposable
/// <summary>
/// The delegate of message received handler from server connection.
/// </summary>
/// <param name="kingBuffer">The king buffer of received message.</param>
public delegate void MessageReceivedHandler(KingBufferReader kingBuffer);
/// <param name="reader">The king buffer reader of received message.</param>
public delegate void MessageReceivedHandler(IKingBufferReader reader);

/// <summary>
/// The delegate of client disconnected handler connection.
Expand Down Expand Up @@ -88,33 +90,19 @@ public NetworkListener(MessageReceivedHandler messageReceivedHandler, ClientDisc

#region public methods implementation

/// <summary>
/// Method responsible for start the client network tcp listener.
/// </summary>
/// <param name="ip">The ip address of server.</param>
/// <param name="port">The port of server.</param>
/// <param name="maxMessageBuffer">The max length of message buffer.</param>
/// <inheritdoc/>
public virtual void StartClient(string ip, int port, ushort maxMessageBuffer) { }

/// <summary>
/// Method responsible for send message to connected server.
/// </summary>
/// <param name="kingBuffer">The king buffer of received message.</param>
public virtual void SendMessage(KingBufferWriter kingBuffer) { }
/// <inheritdoc/>
public virtual void SendMessage(IKingBufferWriter writer) { }

/// <summary>
/// This method is responsible for call the dispose implementation method.
/// </summary>
/// <inheritdoc/>
public void Dispose() => Dispose(true);

/// <summary>
/// This method is responsible for verify if listener has connected.
/// </summary>
/// <inheritdoc/>
public bool Connected() => _listener.Connected;

/// <summary>
/// Method responsible for stop the tcp network listener.
/// </summary>
/// <inheritdoc/>
public void Stop()
{
try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using KingNetwork.Shared;
using KingNetwork.Shared.Interfaces;
using System;
using System.Net;
using System.Net.Sockets;

namespace KingNetwork.Client
namespace KingNetwork.Client.Listeners
{
/// <summary>
/// This class is responsible for managing the network tcp listener.
Expand All @@ -24,12 +25,7 @@ public TcpNetworkListener(MessageReceivedHandler messageReceivedHandler, ClientD

#region public methods implementation

/// <summary>
/// Method responsible for start the client network tcp listener.
/// </summary>
/// <param name="ip">The ip address of server.</param>
/// <param name="port">The port of server.</param>
/// <param name="maxMessageBuffer">The max length of message buffer.</param>
/// <inheritdoc/>
public override void StartClient(string ip, int port, ushort maxMessageBuffer)
{
try
Expand All @@ -53,15 +49,12 @@ public override void StartClient(string ip, int port, ushort maxMessageBuffer)
}
}

/// <summary>
/// Method responsible for send message to connected server.
/// </summary>
/// <param name="kingBuffer">The king buffer of received message.</param>
public override void SendMessage(KingBufferWriter kingBuffer)
/// <inheritdoc/>
public override void SendMessage(IKingBufferWriter writer)
{
try
{
_stream.BeginWrite(kingBuffer.BufferData, 0, kingBuffer.Length, null, null);
_stream.BeginWrite(writer.BufferData, 0, writer.Length, null, null);
}
catch (Exception ex)
{
Expand Down
Loading

0 comments on commit 6745861

Please sign in to comment.