Skip to content

Commit

Permalink
Merge pull request #23 from exomia/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
baetz-daniel authored Jul 30, 2019
2 parents 2ba697f + b49128a commit 663a89b
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 120 deletions.
4 changes: 2 additions & 2 deletions Exomia.Network/ClientActionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace Exomia.Network
/// <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<T, TServerClient> server,
TServerClient client)
public delegate void ClientActionHandler<out T, TServerClient>(IServer<TServerClient> server,
TServerClient client)
where T : class
where TServerClient : ServerClientBase<T>;
}
18 changes: 9 additions & 9 deletions Exomia.Network/ClientDataReceivedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace Exomia.Network
/// <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<out T, TServerClient>(IServer<T, TServerClient> server,
TServerClient client,
uint commandID,
object data,
uint responseID)
public delegate void ClientCommandDataReceivedHandler<T, TServerClient>(IServer<TServerClient> server,
TServerClient client,
uint commandID,
object data,
uint responseID)
where T : class
where TServerClient : ServerClientBase<T>;

Expand All @@ -40,10 +40,10 @@ public delegate void ClientCommandDataReceivedHandler<out T, TServerClient>(ISer
/// <returns>
/// <b>true</b> if you want to handle more data; <b>false</b> otherwise.
/// </returns>
public delegate bool ClientDataReceivedHandler<out T, TServerClient>(IServer<T, TServerClient> server,
TServerClient client,
object data,
uint responseID)
public delegate bool ClientDataReceivedHandler<out T, TServerClient>(IServer<TServerClient> server,
TServerClient client,
object data,
uint responseID)
where T : class
where TServerClient : ServerClientBase<T>;
}
6 changes: 3 additions & 3 deletions Exomia.Network/ClientDisconnectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ namespace Exomia.Network
/// <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<T, TServerClient> server,
TServerClient client,
DisconnectReason reason)
public delegate void ClientDisconnectHandler<out T, TServerClient>(IServer<TServerClient> server,
TServerClient client,
DisconnectReason reason)
where T : class
where TServerClient : ServerClientBase<T>;
}
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.1.5</Version>
<Version>1.4.2.0</Version>
<PackageLicenseUrl>https://raw.githubusercontent.com/exomia/network/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/exomia/network</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand Down
14 changes: 2 additions & 12 deletions Exomia.Network/IServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,9 @@ namespace Exomia.Network
/// </summary>
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <typeparam name="TServerClient"> Type of the server client. </typeparam>
public interface IServer<in T, in TServerClient> : IDisposable
where T : class
where TServerClient : ServerClientBase<T>
public interface IServer<in TServerClient> : IDisposable
where TServerClient : IServerClient
{
/// <summary>
/// Runs.
/// </summary>
/// <param name="port"> The port. </param>
/// <returns>
/// True if it succeeds, false if it fails.
/// </returns>
bool Run(int port);

/// <summary>
/// Sends data to the client.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Exomia.Network/Lib/ServerClientEventEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Remove(ClientDataReceivedHandler<T, TServerClient> callback)
/// <param name="client"> The client. </param>
/// <param name="data"> The data. </param>
/// <param name="responseID"> The responseID. </param>
public void Raise(IServer<T, TServerClient> server, TServerClient client, object data, uint responseID)
public void Raise(IServer<TServerClient> server, TServerClient client, object data, uint responseID)
{
for (int i = _dataReceived.Count - 1; i >= 0; --i)
{
Expand Down
116 changes: 66 additions & 50 deletions Exomia.Network/ServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Exomia.Network
/// </summary>
/// <typeparam name="T"> Socket|Endpoint. </typeparam>
/// <typeparam name="TServerClient"> Type of the server client. </typeparam>
public abstract class ServerBase<T, TServerClient> : IServer<T, TServerClient>
public abstract class ServerBase<T, TServerClient> : IServer<TServerClient>
where T : class
where TServerClient : ServerClientBase<T>
{
Expand Down Expand Up @@ -151,7 +151,13 @@ private protected ServerBase()
Dispose(false);
}

/// <inheritdoc />
/// <summary>
/// Runs.
/// </summary>
/// <param name="port"> Port. </param>
/// <returns>
/// True if it succeeds, false if it fails.
/// </returns>
public bool Run(int port)
{
if (_isRunning) { return true; }
Expand Down Expand Up @@ -264,13 +270,12 @@ protected virtual void OnClientConnected(TServerClient client) { }
/// <summary>
/// Create a new ServerClient than a client connects.
/// </summary>
/// <param name="arg0"> Socket|EndPoint. </param>
/// <param name="serverClient"> [out] out new ServerClient. </param>
/// <returns>
/// <c>true</c> if the new ServerClient should be added to the clients list; <c>false</c>
/// otherwise.
/// </returns>
protected abstract bool CreateServerClient(T arg0, out TServerClient serverClient);
protected abstract bool CreateServerClient(out TServerClient serverClient);

/// <summary>
/// Executes the client disconnect on a different thread, and waits for the result.
Expand Down Expand Up @@ -332,8 +337,9 @@ private protected virtual void OnAfterClientDisconnect(TServerClient client) { }
/// <param name="arg0"> Socket|Endpoint. </param>
private void InvokeClientConnected(T arg0)
{
if (CreateServerClient(arg0, out TServerClient serverClient))
if (CreateServerClient(out TServerClient serverClient))
{
serverClient.Arg0 = arg0;
bool lockTaken = false;
try
{
Expand All @@ -353,10 +359,10 @@ private void InvokeClientConnected(T arg0)
#region Add & Remove

/// <summary>
/// add a command.
/// add commands deserializers.
/// </summary>
/// <param name="commandID"> Identifier for the command. </param>
/// <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.
Expand All @@ -365,24 +371,29 @@ private void InvokeClientConnected(T arg0)
/// Thrown when one or more required arguments
/// are null.
/// </exception>
public void AddCommand(uint commandID, DeserializePacketHandler<object> deserialize)
public void AddCommand(DeserializePacketHandler<object> deserialize, params uint[] commandIDs)
{
if (commandID > Constants.USER_COMMAND_LIMIT)
{
throw new ArgumentOutOfRangeException(
$"{nameof(commandID)} is restricted to 0 - {Constants.USER_COMMAND_LIMIT}");
}

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

bool lockTaken = false;
try
{
_dataReceivedCallbacksLock.Enter(ref lockTaken);
if (!_dataReceivedCallbacks.TryGetValue(commandID, out ServerClientEventEntry<T, TServerClient> buffer))

foreach (uint commandID in commandIDs)
{
buffer = new ServerClientEventEntry<T, TServerClient>(deserialize);
_dataReceivedCallbacks.Add(commandID, buffer);
if (commandID > Constants.USER_COMMAND_LIMIT)
{
throw new ArgumentOutOfRangeException(
$"{nameof(commandID)} is restricted to 0 - {Constants.USER_COMMAND_LIMIT}");
}
if (!_dataReceivedCallbacks.TryGetValue(
commandID, out ServerClientEventEntry<T, TServerClient> buffer))
{
buffer = new ServerClientEventEntry<T, TServerClient>(deserialize);
_dataReceivedCallbacks.Add(commandID, buffer);
}
}
}
finally
Expand All @@ -392,34 +403,38 @@ public void AddCommand(uint commandID, DeserializePacketHandler<object> deserial
}

/// <summary>
/// remove a command.
/// Removes the commands described by commandIDs.
/// </summary>
/// <param name="commandID"> Identifier for the command. </param>
/// <param name="commandIDs"> A variable-length parameters list containing command ids. </param>
/// <returns>
/// True if it succeeds, false if it fails.
/// 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>
public bool RemoveCommand(uint commandID)
public bool RemoveCommands(params uint[] commandIDs)
{
if (commandID > Constants.USER_COMMAND_LIMIT)
{
throw new ArgumentOutOfRangeException(
$"{nameof(commandID)} is restricted to 0 - {Constants.USER_COMMAND_LIMIT}");
}

bool removed = false;
bool lockTaken = false;
try
{
_dataReceivedCallbacksLock.Enter(ref lockTaken);
return _dataReceivedCallbacks.Remove(commandID);
foreach (uint commandID in commandIDs)
{
if (commandID > Constants.USER_COMMAND_LIMIT)
{
throw new ArgumentOutOfRangeException(
$"{nameof(commandID)} is restricted to 0 - {Constants.USER_COMMAND_LIMIT}");
}
removed |= _dataReceivedCallbacks.Remove(commandID);
}
}
finally
{
if (lockTaken) { _dataReceivedCallbacksLock.Exit(false); }
}
return removed;
}

/// <summary>
Expand Down Expand Up @@ -449,23 +464,22 @@ public void AddDataReceivedCallback(uint commandID, ClientDataReceivedHandler<T,

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

ServerClientEventEntry<T, TServerClient> buffer;
bool lockTaken = false;
bool lockTaken = false;
try
{
_dataReceivedCallbacksLock.Enter(ref lockTaken);
if (!_dataReceivedCallbacks.TryGetValue(commandID, out buffer))
if (!_dataReceivedCallbacks.TryGetValue(commandID, out ServerClientEventEntry<T, TServerClient> buffer))
{
throw new Exception(
$"Invalid parameter '{nameof(commandID)}'! Use 'AddCommand(uint, DeserializeData)' first.");
$"Invalid parameter '{nameof(commandID)}'! Use 'AddCommand(DeserializeData, params uint[])' first.");
}

buffer.Add(callback);
}
finally
{
if (lockTaken) { _dataReceivedCallbacksLock.Exit(false); }
}

buffer.Add(callback);
}

/// <summary>
Expand Down Expand Up @@ -509,31 +523,32 @@ private protected abstract SendError SendTo(T arg0,
uint responseID);

/// <inheritdoc />
SendError IServer<T, TServerClient>.SendTo(TServerClient client,
uint commandID,
byte[] data,
int offset,
int length,
uint responseID)
public SendError SendTo(TServerClient client,
uint commandID,
byte[] data,
int offset,
int length,
uint responseID)
{
return SendTo(client.Arg0, commandID, data, offset, length, responseID);
}

/// <inheritdoc />
SendError IServer<T, TServerClient>.SendTo(TServerClient client,
uint commandID,
ISerializable serializable,
uint responseID)
public SendError SendTo(TServerClient client,
uint commandID,
ISerializable serializable,
uint responseID)
{
byte[] dataB = serializable.Serialize(out int length);
return SendTo(client.Arg0, commandID, dataB, 0, length, responseID);
}

/// <inheritdoc />
SendError IServer<T, TServerClient>.SendTo<T1>(TServerClient client,
uint commandID,
in T1 data,
uint responseID)
public SendError SendTo<T1>(TServerClient client,
uint commandID,
in T1 data,
uint responseID)
where T1 : unmanaged
{
byte[] dataB = data.ToBytesUnsafe2(out int length);
return SendTo(client.Arg0, commandID, dataB, 0, length, responseID);
Expand Down Expand Up @@ -564,14 +579,15 @@ public void SendToAll(uint commandID, byte[] data, int offset, int length)
}

/// <inheritdoc />
void IServer<T, TServerClient>.SendToAll<T1>(uint commandID, in T1 data)
public void SendToAll<T1>(uint commandID, in T1 data)
where T1 : unmanaged
{
byte[] buffer = data.ToBytesUnsafe2(out int length);
SendToAll(commandID, buffer, 0, length);
}

/// <inheritdoc />
void IServer<T, TServerClient>.SendToAll(uint commandID, ISerializable serializable)
public void SendToAll(uint commandID, ISerializable serializable)
{
byte[] buffer = serializable.Serialize(out int length);
SendToAll(commandID, buffer, 0, length);
Expand Down
Loading

0 comments on commit 663a89b

Please sign in to comment.