diff --git a/Exomia.Network/ClientActionHandler.cs b/Exomia.Network/ClientActionHandler.cs index b290c72..b7074ac 100644 --- a/Exomia.Network/ClientActionHandler.cs +++ b/Exomia.Network/ClientActionHandler.cs @@ -17,8 +17,8 @@ namespace Exomia.Network /// Type of the server client. /// The server. /// The client. - public delegate void ClientActionHandler(IServer server, - TServerClient client) + public delegate void ClientActionHandler(IServer server, + TServerClient client) where T : class where TServerClient : ServerClientBase; } \ No newline at end of file diff --git a/Exomia.Network/ClientDataReceivedHandler.cs b/Exomia.Network/ClientDataReceivedHandler.cs index e25662a..a76ba7e 100644 --- a/Exomia.Network/ClientDataReceivedHandler.cs +++ b/Exomia.Network/ClientDataReceivedHandler.cs @@ -20,11 +20,11 @@ namespace Exomia.Network /// Identifier for the command. /// The data. /// Identifier for the response. - public delegate void ClientCommandDataReceivedHandler(IServer server, - TServerClient client, - uint commandID, - object data, - uint responseID) + public delegate void ClientCommandDataReceivedHandler(IServer server, + TServerClient client, + uint commandID, + object data, + uint responseID) where T : class where TServerClient : ServerClientBase; @@ -40,10 +40,10 @@ public delegate void ClientCommandDataReceivedHandler(ISer /// /// true if you want to handle more data; false otherwise. /// - public delegate bool ClientDataReceivedHandler(IServer server, - TServerClient client, - object data, - uint responseID) + public delegate bool ClientDataReceivedHandler(IServer server, + TServerClient client, + object data, + uint responseID) where T : class where TServerClient : ServerClientBase; } \ No newline at end of file diff --git a/Exomia.Network/ClientDisconnectHandler.cs b/Exomia.Network/ClientDisconnectHandler.cs index dd3956c..de1ffa7 100644 --- a/Exomia.Network/ClientDisconnectHandler.cs +++ b/Exomia.Network/ClientDisconnectHandler.cs @@ -18,9 +18,9 @@ namespace Exomia.Network /// The server. /// The client. /// The reason. - public delegate void ClientDisconnectHandler(IServer server, - TServerClient client, - DisconnectReason reason) + public delegate void ClientDisconnectHandler(IServer server, + TServerClient client, + DisconnectReason reason) where T : class where TServerClient : ServerClientBase; } \ No newline at end of file diff --git a/Exomia.Network/Exomia.Network.csproj b/Exomia.Network/Exomia.Network.csproj index 5f51eba..a273678 100644 --- a/Exomia.Network/Exomia.Network.csproj +++ b/Exomia.Network/Exomia.Network.csproj @@ -4,7 +4,7 @@ exomia tcp / udp client and server Copyright © $([System.DateTime]::Now.Year) exomia - 1.4.1.5 + 1.4.2.0 https://raw.githubusercontent.com/exomia/network/master/LICENSE https://github.com/exomia/network true diff --git a/Exomia.Network/IServer.cs b/Exomia.Network/IServer.cs index 9462b2e..ca246c6 100644 --- a/Exomia.Network/IServer.cs +++ b/Exomia.Network/IServer.cs @@ -18,19 +18,9 @@ namespace Exomia.Network /// /// Generic type parameter. /// Type of the server client. - public interface IServer : IDisposable - where T : class - where TServerClient : ServerClientBase + public interface IServer : IDisposable + where TServerClient : IServerClient { - /// - /// Runs. - /// - /// The port. - /// - /// True if it succeeds, false if it fails. - /// - bool Run(int port); - /// /// Sends data to the client. /// diff --git a/Exomia.Network/Lib/ServerClientEventEntry.cs b/Exomia.Network/Lib/ServerClientEventEntry.cs index 75936b8..d66dfb7 100644 --- a/Exomia.Network/Lib/ServerClientEventEntry.cs +++ b/Exomia.Network/Lib/ServerClientEventEntry.cs @@ -64,7 +64,7 @@ public void Remove(ClientDataReceivedHandler callback) /// The client. /// The data. /// The responseID. - public void Raise(IServer server, TServerClient client, object data, uint responseID) + public void Raise(IServer server, TServerClient client, object data, uint responseID) { for (int i = _dataReceived.Count - 1; i >= 0; --i) { diff --git a/Exomia.Network/ServerBase.cs b/Exomia.Network/ServerBase.cs index 32efb46..8568031 100644 --- a/Exomia.Network/ServerBase.cs +++ b/Exomia.Network/ServerBase.cs @@ -25,7 +25,7 @@ namespace Exomia.Network /// /// Socket|Endpoint. /// Type of the server client. - public abstract class ServerBase : IServer + public abstract class ServerBase : IServer where T : class where TServerClient : ServerClientBase { @@ -151,7 +151,13 @@ private protected ServerBase() Dispose(false); } - /// + /// + /// Runs. + /// + /// Port. + /// + /// True if it succeeds, false if it fails. + /// public bool Run(int port) { if (_isRunning) { return true; } @@ -264,13 +270,12 @@ protected virtual void OnClientConnected(TServerClient client) { } /// /// Create a new ServerClient than a client connects. /// - /// Socket|EndPoint. /// [out] out new ServerClient. /// /// true if the new ServerClient should be added to the clients list; false /// otherwise. /// - protected abstract bool CreateServerClient(T arg0, out TServerClient serverClient); + protected abstract bool CreateServerClient(out TServerClient serverClient); /// /// Executes the client disconnect on a different thread, and waits for the result. @@ -332,8 +337,9 @@ private protected virtual void OnAfterClientDisconnect(TServerClient client) { } /// Socket|Endpoint. private void InvokeClientConnected(T arg0) { - if (CreateServerClient(arg0, out TServerClient serverClient)) + if (CreateServerClient(out TServerClient serverClient)) { + serverClient.Arg0 = arg0; bool lockTaken = false; try { @@ -353,10 +359,10 @@ private void InvokeClientConnected(T arg0) #region Add & Remove /// - /// add a command. + /// add commands deserializers. /// - /// Identifier for the command. /// The deserialize handler. + /// A variable-length parameters list containing command ids. /// /// Thrown when one or more arguments are outside /// the required range. @@ -365,24 +371,29 @@ private void InvokeClientConnected(T arg0) /// Thrown when one or more required arguments /// are null. /// - public void AddCommand(uint commandID, DeserializePacketHandler deserialize) + public void AddCommand(DeserializePacketHandler 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 buffer)) + + foreach (uint commandID in commandIDs) { - buffer = new ServerClientEventEntry(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 buffer)) + { + buffer = new ServerClientEventEntry(deserialize); + _dataReceivedCallbacks.Add(commandID, buffer); + } } } finally @@ -392,34 +403,38 @@ public void AddCommand(uint commandID, DeserializePacketHandler deserial } /// - /// remove a command. + /// Removes the commands described by commandIDs. /// - /// Identifier for the command. + /// A variable-length parameters list containing command ids. /// - /// True if it succeeds, false if it fails. + /// True if at least one command is removed, false otherwise. /// /// /// Thrown when one or more arguments are outside /// the required range. /// - 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; } /// @@ -449,23 +464,22 @@ public void AddDataReceivedCallback(uint commandID, ClientDataReceivedHandler buffer; - bool lockTaken = false; + bool lockTaken = false; try { _dataReceivedCallbacksLock.Enter(ref lockTaken); - if (!_dataReceivedCallbacks.TryGetValue(commandID, out buffer)) + if (!_dataReceivedCallbacks.TryGetValue(commandID, out ServerClientEventEntry 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); } /// @@ -509,31 +523,32 @@ private protected abstract SendError SendTo(T arg0, uint responseID); /// - SendError IServer.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); } /// - SendError IServer.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); } /// - SendError IServer.SendTo(TServerClient client, - uint commandID, - in T1 data, - uint responseID) + public SendError SendTo(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); @@ -564,14 +579,15 @@ public void SendToAll(uint commandID, byte[] data, int offset, int length) } /// - void IServer.SendToAll(uint commandID, in T1 data) + public void SendToAll(uint commandID, in T1 data) + where T1 : unmanaged { byte[] buffer = data.ToBytesUnsafe2(out int length); SendToAll(commandID, buffer, 0, length); } /// - void IServer.SendToAll(uint commandID, ISerializable serializable) + public void SendToAll(uint commandID, ISerializable serializable) { byte[] buffer = serializable.Serialize(out int length); SendToAll(commandID, buffer, 0, length); diff --git a/Exomia.Network/ServerClientBase.cs b/Exomia.Network/ServerClientBase.cs index 105fddf..32e90d6 100644 --- a/Exomia.Network/ServerClientBase.cs +++ b/Exomia.Network/ServerClientBase.cs @@ -13,11 +13,28 @@ namespace Exomia.Network { + /// + /// Interface for server client. + /// + public interface IServerClient + { + /// + /// Gets the Date/Time of the last received packet time stamp. + /// + /// + /// The last received packet time stamp. + /// + DateTime LastReceivedPacketTimeStamp { get; } + + IPAddress IPAddress { get; } + } + /// /// A server client base. /// /// Socket|EndPoint. - public abstract class ServerClientBase where T : class + public abstract class ServerClientBase : IServerClient + where T : class { /// /// Socket|Endpoint. @@ -29,25 +46,15 @@ public abstract class ServerClientBase where T : class /// private DateTime _lastReceivedPacketTimeStamp; - /// - /// Gets the Date/Time of the last received packet time stamp. - /// - /// - /// The last received packet time stamp. - /// + /// + public abstract IPAddress IPAddress { get; } + + /// public DateTime LastReceivedPacketTimeStamp { get { return _lastReceivedPacketTimeStamp; } } - /// - /// Gets the IP address. - /// - /// - /// The IP address. - /// - public abstract IPAddress IPAddress { get; } - /// /// Gets the argument 0. /// @@ -57,16 +64,13 @@ public DateTime LastReceivedPacketTimeStamp internal T Arg0 { get { return _arg0; } + set { _arg0 = value; } } /// - /// Initializes a new instance of the ; class. + /// Initializes a new instance of the class. /// - /// Socket|Endpoint. - private protected ServerClientBase(T arg0) - { - _arg0 = arg0; - } + private protected ServerClientBase() { } /// /// Sets last received packet time stamp. diff --git a/Exomia.Network/TCP/TcpServerClientBase.cs b/Exomia.Network/TCP/TcpServerClientBase.cs index 3039050..a05955e 100644 --- a/Exomia.Network/TCP/TcpServerClientBase.cs +++ b/Exomia.Network/TCP/TcpServerClientBase.cs @@ -23,12 +23,5 @@ public override IPAddress IPAddress { get { return (_arg0.RemoteEndPoint as IPEndPoint)?.Address; } } - - /// - /// Initializes a new instance of the class. - /// - /// The socket. - protected TcpServerClientBase(Socket socket) - : base(socket) { } } } \ No newline at end of file diff --git a/Exomia.Network/UDP/UdpServerClientBase.cs b/Exomia.Network/UDP/UdpServerClientBase.cs index d3cce2a..7e95329 100644 --- a/Exomia.Network/UDP/UdpServerClientBase.cs +++ b/Exomia.Network/UDP/UdpServerClientBase.cs @@ -22,12 +22,5 @@ public override IPAddress IPAddress { get { return (_arg0 as IPEndPoint)?.Address; } } - - /// - /// Initializes a new instance of the class. - /// - /// The end point. - protected UdpServerClientBase(EndPoint endPoint) - : base(endPoint) { } } } \ No newline at end of file diff --git a/README.md b/README.md index d39148b..54ab1b0 100644 --- a/README.md +++ b/README.md @@ -54,9 +54,9 @@ static async Task Main(string[] args) ```csharp class UdpServer : UdpServerEapBase { - protected override bool CreateServerClient(EndPoint endpoint, out UdpServerClient serverClient) + protected override bool CreateServerClient(out UdpServerClient serverClient) { - serverClient = new UdpServerClient(endpoint); + serverClient = new UdpServerClient(); return true; } @@ -67,7 +67,7 @@ class UdpServer : UdpServerEapBase class UdpServerClient : UdpServerClientBase { - public UdpServerClient(EndPoint endPoint) : base(endPoint) { } + public UdpServerClient() { } } ``` @@ -148,9 +148,9 @@ static async Task Main(string[] args) ```csharp class TcpServer : TcpServerEapBase { - protected override bool CreateServerClient(Socket socket, out TcpServerClient serverClient) + protected override bool CreateServerClient(out TcpServerClient serverClient) { - serverClient = new TcpServerClient(socket); + serverClient = new TcpServerClient(); return true; } @@ -160,7 +160,7 @@ class TcpServer : TcpServerEapBase class TcpServerClient : TcpServerClientBase { - public TcpServerClient(Socket socket) : base(socket) { } + public TcpServerClient() { } } ``` diff --git a/network.wiki b/network.wiki index 73a04b5..25e6177 160000 --- a/network.wiki +++ b/network.wiki @@ -1 +1 @@ -Subproject commit 73a04b55442b406f78f0cef84cab83fa5e5379c0 +Subproject commit 25e6177c0baefc5085397ecb773e5e93815cc988