Skip to content

Commit

Permalink
Merge pull request #24 from exomia/development
Browse files Browse the repository at this point in the history
v1.4.3
  • Loading branch information
baetz-daniel authored Jul 30, 2019
2 parents 663a89b + 460bf3e commit 8a5b08c
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 100 deletions.
8 changes: 3 additions & 5 deletions Exomia.Network/ClientActionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ namespace Exomia.Network
/// <summary>
/// Called than a client action occurs.
/// </summary>
/// <typeparam name="T"> Socket|EndPoint. </typeparam>
/// <typeparam name="TServerClient"> Type of the server client. </typeparam>
/// <param name="server"> The server. </param>
/// <param name="client"> The client. </param>
public delegate void ClientActionHandler<out T, TServerClient>(IServer<TServerClient> server,
TServerClient client)
where T : class
where TServerClient : ServerClientBase<T>;
public delegate void ClientActionHandler<TServerClient>(IServer<TServerClient> server,
TServerClient client)
where TServerClient : IServerClient;
}
26 changes: 11 additions & 15 deletions Exomia.Network/ClientDataReceivedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,22 @@ namespace Exomia.Network
/// <summary>
/// Handler, called when the server received client data.
/// </summary>
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <typeparam name="TServerClient"> Type of the server client. </typeparam>
/// <param name="server"> The server. </param>
/// <param name="client"> The client. </param>
/// <param name="commandID"> Identifier for the command. </param>
/// <param name="data"> The data. </param>
/// <param name="responseID"> Identifier for the response. </param>
public delegate void ClientCommandDataReceivedHandler<T, TServerClient>(IServer<TServerClient> server,
TServerClient client,
uint commandID,
object data,
uint responseID)
where T : class
where TServerClient : ServerClientBase<T>;
public delegate void ClientCommandDataReceivedHandler<TServerClient>(IServer<TServerClient> server,
TServerClient client,
uint commandID,
object data,
uint responseID)
where TServerClient : IServerClient;

/// <summary>
/// Handler, called when the server received client data.
/// </summary>
/// <typeparam name="T"> Socket|EndPoint. </typeparam>
/// <typeparam name="TServerClient"> Type of the server client. </typeparam>
/// <param name="server"> The server. </param>
/// <param name="client"> The client. </param>
Expand All @@ -40,10 +37,9 @@ public delegate void ClientCommandDataReceivedHandler<T, TServerClient>(IServer<
/// <returns>
/// <b>true</b> if you want to handle more data; <b>false</b> otherwise.
/// </returns>
public delegate bool ClientDataReceivedHandler<out T, TServerClient>(IServer<TServerClient> server,
TServerClient client,
object data,
uint responseID)
where T : class
where TServerClient : ServerClientBase<T>;
public delegate bool ClientDataReceivedHandler<TServerClient>(IServer<TServerClient> server,
TServerClient client,
object data,
uint responseID)
where TServerClient : IServerClient;
}
10 changes: 4 additions & 6 deletions Exomia.Network/ClientDisconnectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ namespace Exomia.Network
/// <summary>
/// Called than a client disconnects from the server.
/// </summary>
/// <typeparam name="T"> Socket|EndPoint. </typeparam>
/// <typeparam name="TServerClient"> Type of the server client. </typeparam>
/// <param name="server"> The server. </param>
/// <param name="client"> The client. </param>
/// <param name="reason"> The reason. </param>
public delegate void ClientDisconnectHandler<out T, TServerClient>(IServer<TServerClient> server,
TServerClient client,
DisconnectReason reason)
where T : class
where TServerClient : ServerClientBase<T>;
public delegate void ClientDisconnectHandler<TServerClient>(IServer<TServerClient> server,
TServerClient client,
DisconnectReason reason)
where TServerClient : IServerClient;
}
2 changes: 1 addition & 1 deletion Exomia.Network/Exomia.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Authors>exomia</Authors>
<Description>tcp / udp client and server</Description>
<Copyright>Copyright © $([System.DateTime]::Now.Year) exomia</Copyright>
<Version>1.4.2.0</Version>
<Version>1.4.3.0</Version>
<PackageLicenseUrl>https://raw.githubusercontent.com/exomia/network/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/exomia/network</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand Down
1 change: 0 additions & 1 deletion Exomia.Network/IServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace Exomia.Network
/// <summary>
/// Interface for server.
/// </summary>
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <typeparam name="TServerClient"> Type of the server client. </typeparam>
public interface IServer<in TServerClient> : IDisposable
where TServerClient : IServerClient
Expand Down
37 changes: 37 additions & 0 deletions Exomia.Network/IServerClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#region License

// Copyright (c) 2018-2019, exomia
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#endregion

using System;
using System.Net;

namespace Exomia.Network
{
/// <summary>
/// Interface for server client.
/// </summary>
public interface IServerClient
{
/// <summary>
/// Gets the Date/Time of the last received packet time stamp.
/// </summary>
/// <value>
/// The last received packet time stamp.
/// </value>
DateTime LastReceivedPacketTimeStamp { get; }

/// <summary>
/// Gets the IP address.
/// </summary>
/// <value>
/// The IP address.
/// </value>
IPAddress IPAddress { get; }
}
}
16 changes: 7 additions & 9 deletions Exomia.Network/Lib/ServerClientEventEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ namespace Exomia.Network.Lib
/// <summary>
/// A server client event entry. This class cannot be inherited.
/// </summary>
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <typeparam name="TServerClient"> Type of the server client. </typeparam>
sealed class ServerClientEventEntry<T, TServerClient>
where T : class
where TServerClient : ServerClientBase<T>
sealed class ServerClientEventEntry<TServerClient>
where TServerClient : IServerClient
{
/// <summary>
/// The deserialize.
Expand All @@ -27,23 +25,23 @@ sealed class ServerClientEventEntry<T, TServerClient>
/// <summary>
/// The data received.
/// </summary>
private readonly Event<ClientDataReceivedHandler<T, TServerClient>> _dataReceived;
private readonly Event<ClientDataReceivedHandler<TServerClient>> _dataReceived;

/// <summary>
/// Initializes a new instance of the <see cref="ServerClientEventEntry{T,TServerClient}" /> class.
/// Initializes a new instance of the <see cref="ServerClientEventEntry{TServerClient}" /> class.
/// </summary>
/// <param name="deserialize"> The deserialize. </param>
public ServerClientEventEntry(DeserializePacketHandler<object> deserialize)
{
_dataReceived = new Event<ClientDataReceivedHandler<T, TServerClient>>();
_dataReceived = new Event<ClientDataReceivedHandler<TServerClient>>();
_deserialize = deserialize;
}

/// <summary>
/// Adds callback.
/// </summary>
/// <param name="callback"> The callback to remove. </param>
public void Add(ClientDataReceivedHandler<T, TServerClient> callback)
public void Add(ClientDataReceivedHandler<TServerClient> callback)
{
_dataReceived.Add(callback);
}
Expand All @@ -52,7 +50,7 @@ public void Add(ClientDataReceivedHandler<T, TServerClient> callback)
/// Removes the given callback.
/// </summary>
/// <param name="callback"> The callback to remove. </param>
public void Remove(ClientDataReceivedHandler<T, TServerClient> callback)
public void Remove(ClientDataReceivedHandler<TServerClient> callback)
{
_dataReceived.Remove(callback);
}
Expand Down
76 changes: 30 additions & 46 deletions Exomia.Network/ServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ public abstract class ServerBase<T, TServerClient> : IServer<TServerClient>
/// <summary>
/// Called than a client is connected.
/// </summary>
public event ClientActionHandler<T, TServerClient> ClientConnected;
public event ClientActionHandler<TServerClient> ClientConnected;

/// <summary>
/// Called than a client is disconnected.
/// </summary>
public event ClientDisconnectHandler<T, TServerClient> ClientDisconnected;
public event ClientDisconnectHandler<TServerClient> ClientDisconnected;

/// <summary>
/// Occurs when data from a client is received.
/// </summary>
public event ClientCommandDataReceivedHandler<T, TServerClient> ClientDataReceived
public event ClientCommandDataReceivedHandler<TServerClient> ClientDataReceived
{
add { _clientDataReceived.Add(value); }
remove { _clientDataReceived.Remove(value); }
Expand Down Expand Up @@ -96,12 +96,12 @@ public event ClientCommandDataReceivedHandler<T, TServerClient> ClientDataReceiv
/// <summary>
/// The data received callbacks.
/// </summary>
private readonly Dictionary<uint, ServerClientEventEntry<T, TServerClient>> _dataReceivedCallbacks;
private readonly Dictionary<uint, ServerClientEventEntry<TServerClient>> _dataReceivedCallbacks;

/// <summary>
/// The client data received event handler.
/// </summary>
private readonly Event<ClientCommandDataReceivedHandler<T, TServerClient>> _clientDataReceived;
private readonly Event<ClientCommandDataReceivedHandler<TServerClient>> _clientDataReceived;

/// <summary>
/// The clients lock.
Expand Down Expand Up @@ -134,13 +134,13 @@ public int Port
/// </summary>
private protected ServerBase()
{
_dataReceivedCallbacks = new Dictionary<uint, ServerClientEventEntry<T, TServerClient>>(INITIAL_QUEUE_SIZE);
_dataReceivedCallbacks = new Dictionary<uint, ServerClientEventEntry<TServerClient>>(INITIAL_QUEUE_SIZE);
_clients = new Dictionary<T, TServerClient>(INITIAL_CLIENT_QUEUE_SIZE);

_clientsLock = new SpinLock(Debugger.IsAttached);
_dataReceivedCallbacksLock = new SpinLock(Debugger.IsAttached);

_clientDataReceived = new Event<ClientCommandDataReceivedHandler<T, TServerClient>>();
_clientDataReceived = new Event<ClientCommandDataReceivedHandler<TServerClient>>();
}

/// <summary>
Expand Down Expand Up @@ -231,7 +231,7 @@ private protected void DeserializeData(T arg0,
{
if (commandID <= Constants.USER_COMMAND_LIMIT &&
_dataReceivedCallbacks.TryGetValue(
commandID, out ServerClientEventEntry<T, TServerClient> scee))
commandID, out ServerClientEventEntry<TServerClient> scee))
{
sClient.SetLastReceivedPacketTimeStamp();

Expand Down Expand Up @@ -363,14 +363,10 @@ private void InvokeClientConnected(T arg0)
/// </summary>
/// <param name="deserialize"> The deserialize handler. </param>
/// <param name="commandIDs"> A variable-length parameters list containing command ids. </param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when one or more arguments are outside
/// the required range.
/// </exception>
/// <exception cref="ArgumentNullException">
/// Thrown when one or more required arguments
/// are null.
/// </exception>
/// <exception cref="ArgumentNullException"> Thrown when one or more required arguments
/// are null. </exception>
/// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside
/// the required range. </exception>
public void AddCommand(DeserializePacketHandler<object> deserialize, params uint[] commandIDs)
{
if (commandIDs == null) { throw new ArgumentNullException(nameof(commandIDs)); }
Expand All @@ -389,9 +385,9 @@ public void AddCommand(DeserializePacketHandler<object> deserialize, params uint
$"{nameof(commandID)} is restricted to 0 - {Constants.USER_COMMAND_LIMIT}");
}
if (!_dataReceivedCallbacks.TryGetValue(
commandID, out ServerClientEventEntry<T, TServerClient> buffer))
commandID, out ServerClientEventEntry<TServerClient> buffer))
{
buffer = new ServerClientEventEntry<T, TServerClient>(deserialize);
buffer = new ServerClientEventEntry<TServerClient>(deserialize);
_dataReceivedCallbacks.Add(commandID, buffer);
}
}
Expand All @@ -409,10 +405,8 @@ public void AddCommand(DeserializePacketHandler<object> deserialize, params uint
/// <returns>
/// True if at least one command is removed, false otherwise.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when one or more arguments are outside
/// the required range.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside
/// the required range. </exception>
public bool RemoveCommands(params uint[] commandIDs)
{
bool removed = false;
Expand Down Expand Up @@ -442,19 +436,13 @@ public bool RemoveCommands(params uint[] commandIDs)
/// </summary>
/// <param name="commandID"> Identifier for the command. </param>
/// <param name="callback"> ClientDataReceivedHandler{Socket|Endpoint} </param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when one or more arguments are outside
/// the required range.
/// </exception>
/// <exception cref="ArgumentNullException">
/// Thrown when one or more required arguments
/// are null.
/// </exception>
/// <exception cref="Exception">
/// Thrown when an exception error condition
/// occurs.
/// </exception>
public void AddDataReceivedCallback(uint commandID, ClientDataReceivedHandler<T, TServerClient> callback)
/// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside
/// the required range. </exception>
/// <exception cref="ArgumentNullException"> Thrown when one or more required arguments
/// are null. </exception>
/// <exception cref="Exception"> Thrown when an exception error condition
/// occurs. </exception>
public void AddDataReceivedCallback(uint commandID, ClientDataReceivedHandler<TServerClient> callback)
{
if (commandID > Constants.USER_COMMAND_LIMIT)
{
Expand All @@ -468,7 +456,7 @@ public void AddDataReceivedCallback(uint commandID, ClientDataReceivedHandler<T,
try
{
_dataReceivedCallbacksLock.Enter(ref lockTaken);
if (!_dataReceivedCallbacks.TryGetValue(commandID, out ServerClientEventEntry<T, TServerClient> buffer))
if (!_dataReceivedCallbacks.TryGetValue(commandID, out ServerClientEventEntry<TServerClient> buffer))
{
throw new Exception(
$"Invalid parameter '{nameof(commandID)}'! Use 'AddCommand(DeserializeData, params uint[])' first.");
Expand All @@ -487,15 +475,11 @@ public void AddDataReceivedCallback(uint commandID, ClientDataReceivedHandler<T,
/// </summary>
/// <param name="commandID"> Identifier for the command. </param>
/// <param name="callback"> ClientDataReceivedHandler{Socket|Endpoint} </param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when one or more arguments are outside
/// the required range.
/// </exception>
/// <exception cref="ArgumentNullException">
/// Thrown when one or more required arguments
/// are null.
/// </exception>
public void RemoveDataReceivedCallback(uint commandID, ClientDataReceivedHandler<T, TServerClient> callback)
/// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside
/// the required range. </exception>
/// <exception cref="ArgumentNullException"> Thrown when one or more required arguments
/// are null. </exception>
public void RemoveDataReceivedCallback(uint commandID, ClientDataReceivedHandler<TServerClient> callback)
{
if (commandID > Constants.USER_COMMAND_LIMIT)
{
Expand All @@ -505,7 +489,7 @@ public void RemoveDataReceivedCallback(uint commandID, ClientDataReceivedHandler

if (callback == null) { throw new ArgumentNullException(nameof(callback)); }

if (_dataReceivedCallbacks.TryGetValue(commandID, out ServerClientEventEntry<T, TServerClient> buffer))
if (_dataReceivedCallbacks.TryGetValue(commandID, out ServerClientEventEntry<TServerClient> buffer))
{
buffer.Remove(callback);
}
Expand Down
16 changes: 0 additions & 16 deletions Exomia.Network/ServerClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@

namespace Exomia.Network
{
/// <summary>
/// Interface for server client.
/// </summary>
public interface IServerClient
{
/// <summary>
/// Gets the Date/Time of the last received packet time stamp.
/// </summary>
/// <value>
/// The last received packet time stamp.
/// </value>
DateTime LastReceivedPacketTimeStamp { get; }

IPAddress IPAddress { get; }
}

/// <summary>
/// A server client base.
/// </summary>
Expand Down
Loading

0 comments on commit 8a5b08c

Please sign in to comment.