diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index c00d32f..3ef4061 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -13,5 +13,4 @@ jobs: with: dotnet-version: '3.1.100' # SDK Version to use. - run: dotnet build SimpleSockets --configuration Release - - run: dotnet test Tests/Test.Sockets.Basic --configuration Release - - run: dotnet test Tests/Test.Sockets.Parallel --configuration Release \ No newline at end of file + - run: dotnet test Tests/Test.Sockets.Basic --configuration Release \ No newline at end of file diff --git a/Examples/Console/NetCore.Console.Client/Client.cs b/Examples/Console/NetCore.Console.Client/Client.cs index b3063b2..feb1a9d 100644 --- a/Examples/Console/NetCore.Console.Client/Client.cs +++ b/Examples/Console/NetCore.Console.Client/Client.cs @@ -326,7 +326,7 @@ private static void ClientOnFileReceiver(SimpleSocketClient a, int currentPart, WriteLine("File received and stored at location: " + loc); } - private static void CustomHeader(SimpleSocket a, object msg, IDictionary dict) + private static void CustomHeader(SimpleSocket a, object msg, IDictionary dict, Type objectType) { WriteLine("Test"); // WriteLine("Bytes received from server with header = " + header + " and message = " + msg); diff --git a/Examples/Console/NetCore.Console.Server/Server.cs b/Examples/Console/NetCore.Console.Server/Server.cs index bbe5115..d1e98d5 100644 --- a/Examples/Console/NetCore.Console.Server/Server.cs +++ b/Examples/Console/NetCore.Console.Server/Server.cs @@ -358,7 +358,7 @@ private static void ListenerOnFileReceiver(IClientInfo client, int currentPart, WriteLine("File received and stored at location: " + loc); } - private static void CustomHeaderReceived(IClientInfo client, object msg, IDictionary metadata) + private static void CustomHeaderReceived(IClientInfo client, object msg, IDictionary metadata, Type objectType) { WriteLine("Test"); // WriteLine("The server received a message from the client with ID " + client.Id + " the header is : " + header + " and the message is : " + msg); diff --git a/SimpleSockets/Client/SimpleSocketClient.cs b/SimpleSockets/Client/SimpleSocketClient.cs index d99bbdf..bcb7137 100644 --- a/SimpleSockets/Client/SimpleSocketClient.cs +++ b/SimpleSockets/Client/SimpleSocketClient.cs @@ -19,7 +19,7 @@ namespace SimpleSockets.Client public delegate void MessageReceivedDelegate(SimpleSocketClient client, string msg); - public delegate void MessageWithMetadataReceivedDelegate(SimpleSocketClient client, object message, IDictionary metadata); + public delegate void MessageWithMetadataReceivedDelegate(SimpleSocketClient client, object message, IDictionary metadata, Type ObjectType); public delegate void BytesReceivedDelegate(SimpleSocketClient client, byte[] messageBytes); @@ -383,9 +383,9 @@ protected internal override void RaiseMessageContractReceived(IClientInfo client contract.RaiseOnMessageReceived(this, client, contract.DeserializeToObject(data), contract.MessageHeader); } - protected internal override void RaiseMessageWithMetaDataReceived(IClientInfo client, object message, IDictionary metadata) + protected internal override void RaiseMessageWithMetaDataReceived(IClientInfo client, object message, IDictionary metadata, Type objectType) { - MessageWithMetadataReceived?.Invoke(this, message, metadata); + MessageWithMetadataReceived?.Invoke(this, message, metadata, objectType); } protected internal override void RaiseBytesReceived(IClientInfo client, byte[] data) diff --git a/SimpleSockets/Client/SimpleSocketTcpClient.cs b/SimpleSockets/Client/SimpleSocketTcpClient.cs index b99f835..96b0447 100644 --- a/SimpleSockets/Client/SimpleSocketTcpClient.cs +++ b/SimpleSockets/Client/SimpleSocketTcpClient.cs @@ -43,6 +43,8 @@ public override void StartClient(string ipServer, int port, int reconnectInSecon if (EnableExtendedAuth) SendAuthMessage(); + else + SendBasicAuthMessage(); Endpoint = new IPEndPoint(GetIp(ipServer), port); @@ -217,7 +219,7 @@ protected internal override void Receive(IClientMetadata state, int offset = 0) } } - protected override async void ReceiveCallback(IAsyncResult result) + protected override void ReceiveCallback(IAsyncResult result) { var state = (ClientMetadata)result.AsyncState; @@ -247,11 +249,11 @@ protected override async void ReceiveCallback(IAsyncResult result) { if (state.SimpleMessage == null) state.SimpleMessage = new SimpleMessage(state, this, true); - await state.SimpleMessage.ReadBytesAndBuildMessage(receive); + state.SimpleMessage.ReadBytesAndBuildMessage(receive); } else if (receive > 0) { - await state.SimpleMessage.ReadBytesAndBuildMessage(receive); + state.SimpleMessage.ReadBytesAndBuildMessage(receive); } } diff --git a/SimpleSockets/Client/SimpleSocketTcpSslClient.cs b/SimpleSockets/Client/SimpleSocketTcpSslClient.cs index 834ab8e..aac6da6 100644 --- a/SimpleSockets/Client/SimpleSocketTcpSslClient.cs +++ b/SimpleSockets/Client/SimpleSocketTcpSslClient.cs @@ -83,13 +83,14 @@ public override void StartClient(string ipServer, int port, int reconnectInSecon if (EnableExtendedAuth) SendAuthMessage(); + else + SendBasicAuthMessage(); Endpoint = new IPEndPoint(GetIp(ipServer), port); TokenSource = new CancellationTokenSource(); Token = TokenSource.Token; - Task.Run(SendFromQueue, Token); Task.Run(() => @@ -339,7 +340,7 @@ protected internal override void Receive(IClientMetadata state, int offset = 0) } } - protected override async void ReceiveCallback(IAsyncResult result) + protected override void ReceiveCallback(IAsyncResult result) { var state = (ClientMetadata)result.AsyncState; try @@ -369,10 +370,10 @@ protected override async void ReceiveCallback(IAsyncResult result) { if (state.SimpleMessage == null) state.SimpleMessage = new SimpleMessage(state, this, Debug); - await state.SimpleMessage.ReadBytesAndBuildMessage(receive); + state.SimpleMessage.ReadBytesAndBuildMessage(receive); } else if (receive > 0) - await state.SimpleMessage.ReadBytesAndBuildMessage(receive); + state.SimpleMessage.ReadBytesAndBuildMessage(receive); } _mreRead.Set(); @@ -380,7 +381,8 @@ protected override async void ReceiveCallback(IAsyncResult result) } catch (SocketException se) { - RaiseLog("Server was forcibly closed."); + Log("Server was forcibly closed."); + Log(se); state.Reset(); DisposeSslStream(); _mreRead.Set(); diff --git a/SimpleSockets/Messaging/SimpleMessage.cs b/SimpleSockets/Messaging/SimpleMessage.cs index 01070c0..fd015b7 100644 --- a/SimpleSockets/Messaging/SimpleMessage.cs +++ b/SimpleSockets/Messaging/SimpleMessage.cs @@ -86,8 +86,11 @@ internal class SimpleMessage //Message processing helpers private byte[] _receivedBytes = new byte[0]; private byte[] _receivedMetadataBytes = new byte[0]; + private byte[] _receivedHeaderBytes = new byte[0]; + private int _readHeaderBytes = 0; private int _readBytes = 0; - private int _readMetadataBytes; + private int _readMetadataBytes = 0; + /// /// Constructor @@ -515,7 +518,7 @@ internal async Task CompressEncryptMessageAsync() /// Call from socket receiver and create a message from this. /// /// - internal async Task ReadBytesAndBuildMessage(int receive) { + internal bool ReadBytesAndBuildMessage(int receive) { var processing = true; @@ -556,12 +559,12 @@ internal async Task ReadBytesAndBuildMessage(int receive) { } if (_state.Flag == MessageFlag.ProcessingMetadata) { - processing = await ReadMetaData(receive); + processing = ReadMetaData(receive); receive = _state.Buffer.Length; } if (_state.Flag == MessageFlag.ProcessingData) { - processing = await ReadData(receive); + processing = ReadData(receive); receive = _state.Buffer.Length; } } @@ -667,7 +670,7 @@ private void DeconstructHeaderFields(byte headerFieldByte, byte[] buffer) #region Read message-data - private async Task ReadMetaData(int receive) { + private bool ReadMetaData(int receive) { if (receive <= 0) return false; @@ -726,7 +729,7 @@ private async Task ReadMetaData(int receive) { } //Returns true if there is more data to read. - private async Task ReadData(int receive) + private bool ReadData(int receive) { if (receive <= 0) return false; @@ -882,133 +885,145 @@ private void MessageHasBeenReceived() case MessageType.Auth: { - var message = Encoding.UTF8.GetString(_receivedBytes); - var arr = message.Split('|'); - _state.ClientName = arr[0]; - _state.Guid = arr[1]; - _state.UserDomainName = arr[2]; - _state.OsVersion = arr[3]; - break; + var message = Encoding.UTF8.GetString(_receivedBytes); + var arr = message.Split('|'); + _state.ClientName = arr[0]; + _state.Guid = arr[1]; + _state.UserDomainName = arr[2]; + _state.OsVersion = arr[3]; + break; } case MessageType.BasicAuth: { - var message = Encoding.UTF8.GetString(_receivedBytes); - var arr = message.Split('|'); - _state.Guid = arr[0]; - _state.OsVersion = arr[1]; - break; + var message = Encoding.UTF8.GetString(_receivedBytes); + var arr = message.Split('|'); + _state.Guid = arr[0]; + _state.OsVersion = arr[1]; + break; } case MessageType.Object: { - var type = Type.GetType(Encoding.UTF8.GetString(Header)); - var obj = _socket.ObjectSerializer.DeserializeBytesToObject(_receivedBytes, type); - _socket.RaiseObjectReceived(_state, obj, obj.GetType()); - break; + var type = Type.GetType(Encoding.UTF8.GetString(Header)); + var obj = _socket.ObjectSerializer.DeserializeBytesToObject(_receivedBytes, type); + _socket.RaiseObjectReceived(_state, obj, obj.GetType()); + break; } case MessageType.Message: - _socket.RaiseMessageReceived(_state, Encoding.UTF8.GetString(_receivedBytes)); - break; + _socket.RaiseMessageReceived(_state, Encoding.UTF8.GetString(_receivedBytes)); + break; case MessageType.Bytes: - _socket.RaiseBytesReceived(_state, _receivedBytes); - break; + _socket.RaiseBytesReceived(_state, _receivedBytes); + break; case MessageType.File: { - var file = Encoding.UTF8.GetString(Header); - var output = file; - - _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output, MessageState.ReceivingData); + var file = Encoding.UTF8.GetString(Header); + var output = file; - if (_totalParts == _partNumber) - { - file = GetTempPath(file); + _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output, MessageState.ReceivingData); - if (Encrypted) + if (_totalParts == _partNumber) { - _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output, MessageState.Decrypting); - Log("Decrypting file from path : " + file); - var tmp = file; - file = _socket.DecryptFile(file, _socket.TempPath + Path.GetRandomFileName()).FullName; - File.Delete(tmp); - Log("File has been decrypted from path: " + file); - _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output,MessageState.DecryptingDone); - } + file = GetTempPath(file); - if (Compressed) - { - _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output,MessageState.Decompressing); - Log("Decompressing file from path : " + file); - var tmp = file; - file = _socket.DecompressFile(new FileInfo(file), _socket.TempPath + Path.GetRandomFileName()).FullName; if (Encrypted) + { + _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output, MessageState.Decrypting); + Log("Decrypting file from path : " + file); + var tmp = file; + file = _socket.DecryptFile(file, _socket.TempPath + Path.GetRandomFileName()).FullName; File.Delete(tmp); - Log("File Decompressed from path : " + file); - _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output,MessageState.DecompressingDone); + Log("File has been decrypted from path: " + file); + _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output,MessageState.DecryptingDone); + } + + if (Compressed) + { + _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output,MessageState.Decompressing); + Log("Decompressing file from path : " + file); + var tmp = file; + file = _socket.DecompressFile(new FileInfo(file), _socket.TempPath + Path.GetRandomFileName()).FullName; + if (Encrypted) + File.Delete(tmp); + Log("File Decompressed from path : " + file); + _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output,MessageState.DecompressingDone); + } + + + if (Encrypted || Compressed) + { + if (File.Exists(output)) + File.Delete(output); + Log("Deleting file: " + output); + File.Move(file, output); + Log("Moving " + file + " to " + output); + File.Delete(file); // Delete encrypted/compressed file. + Log("Deleting temp file : " + file); + } + + _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output, MessageState.Completed); + Log("File has been received and saved to path : " + output); } + + break; + } + case MessageType.MessageWithMetadata: + { + var header = Encoding.UTF8.GetString(Header); + object obj = null; + Type objType = null; - - if (Encrypted || Compressed) + if (header != "ByteArray") { - if (File.Exists(output)) - File.Delete(output); - Log("Deleting file: " + output); - File.Move(file, output); - Log("Moving " + file + " to " + output); - File.Delete(file); // Delete encrypted/compressed file. - Log("Deleting temp file : " + file); + objType = Type.GetType(header); + obj = _socket.ObjectSerializer.DeserializeBytesToObject(_receivedBytes, objType); + } + else { + obj = _receivedBytes; + objType = obj.GetType(); } - _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, output, MessageState.Completed); - Log("File has been received and saved to path : " + output); - } - - break; - } - case MessageType.MessageWithMetadata: - { - var type = Type.GetType(Encoding.UTF8.GetString(Header)); - var metadata = _socket.ObjectSerializer.DeserializeJson>(_receivedMetadataBytes); - var obj = _socket.ObjectSerializer.DeserializeBytesToObject(_receivedBytes, type); - _socket.RaiseMessageWithMetaDataReceived(_state, obj, metadata); - break; + var metadata = _socket.ObjectSerializer.DeserializeJson>(_receivedMetadataBytes); + _socket.RaiseMessageWithMetaDataReceived(_state, obj, metadata, objType); + break; } case MessageType.MessageContract: { - var contractHeader = Encoding.UTF8.GetString(Header); - var contract = GetCorrespondingMessageContract(contractHeader); - if (contract != null) - _socket.RaiseMessageContractReceived(_state, contract, _receivedBytes); - else if (_debug) - Log("MessageContract with Header '" + contractHeader + "' does not exist."); - break; + var contractHeader = Encoding.UTF8.GetString(Header); + var contract = GetCorrespondingMessageContract(contractHeader); + if (contract != null) + _socket.RaiseMessageContractReceived(_state, contract, _receivedBytes); + else if (_debug) + Log("MessageContract with Header '" + contractHeader + "' does not exist."); + break; } case MessageType.Folder: { - var folder = Encoding.UTF8.GetString(Header); - var output = folder; - - _socket.RaiseFolderReceiver(_state, _partNumber, _totalParts, output, MessageState.ReceivingData); + var folder = Encoding.UTF8.GetString(Header); + var output = folder; - if (_totalParts == _partNumber) - { - folder = GetTempPath(folder); + _socket.RaiseFolderReceiver(_state, _partNumber, _totalParts, output, MessageState.ReceivingData); - if (Encrypted) + if (_totalParts == _partNumber) { - var tmp = _socket.DecryptFile(folder, _socket.TempPath + Path.GetRandomFileName()).FullName; - File.Delete(folder); - folder = tmp; - } + folder = GetTempPath(folder); + if (Encrypted) + { + var tmp = _socket.DecryptFile(folder, _socket.TempPath + Path.GetRandomFileName()).FullName; + File.Delete(folder); + folder = tmp; + } - _socket.ExtractToFolder(folder, output); - File.Delete(folder); //Delete extracted folder. - _socket.RaiseFolderReceiver(_state, _partNumber, _totalParts, output, MessageState.Completed); - } - break; + _socket.ExtractToFolder(folder, output); + File.Delete(folder); //Delete extracted folder. + + _socket.RaiseFolderReceiver(_state, _partNumber, _totalParts, output, MessageState.Completed); + } + break; } default: - throw new ArgumentOutOfRangeException(); + throw new ArgumentOutOfRangeException(); } ResetReadData(); @@ -1046,8 +1061,10 @@ public void ResetReadData() { Data = new byte[0]; _receivedBytes = new byte[0]; _receivedMetadataBytes = new byte[0]; + _receivedHeaderBytes = new byte[0]; _readBytes = 0; _readMetadataBytes = 0; + _readHeaderBytes = 0; } } diff --git a/SimpleSockets/Server/SimpleSocketListener.cs b/SimpleSockets/Server/SimpleSocketListener.cs index 7c0a95c..fd23c70 100644 --- a/SimpleSockets/Server/SimpleSocketListener.cs +++ b/SimpleSockets/Server/SimpleSocketListener.cs @@ -23,7 +23,7 @@ namespace SimpleSockets.Server public delegate void MessageReceivedDelegate(IClientInfo client, string message); - public delegate void MessageWithMetadataReceivedDelegate(IClientInfo client, object message, IDictionary metadata); + public delegate void MessageWithMetadataReceivedDelegate(IClientInfo client, object message, IDictionary metadata, Type objType); public delegate void BytesReceivedDelegate(IClientInfo client, byte[] messageData); @@ -423,7 +423,7 @@ public bool IsConnected(int id) { return !((socket.Poll(1000, SelectMode.SelectRead) && (socket.Available == 0)) || !socket.Connected); } - } catch (ObjectDisposedException de) + } catch (ObjectDisposedException) { return false; } @@ -574,9 +574,9 @@ protected internal override void RaiseMessageContractReceived(IClientInfo client contract.RaiseOnMessageReceived(this,client, contract.DeserializeToObject(data), contract.MessageHeader); } - protected internal override void RaiseMessageWithMetaDataReceived(IClientInfo client, object message, IDictionary metadata) + protected internal override void RaiseMessageWithMetaDataReceived(IClientInfo client, object message, IDictionary metadata, Type objType) { - MessageWithMetaDataReceived?.Invoke(client, message, metadata); + MessageWithMetaDataReceived?.Invoke(client, message, metadata, objType); } protected internal override void RaiseBytesReceived(IClientInfo client, byte[] data) @@ -749,6 +749,41 @@ public async Task SendMessageContractAsync(int id, IMessageContract contract, bo #region MessageWithMetadata + public void SendMessageWithMetadata(int id, byte[] data, IDictionary metadata, bool compress = false, bool encrypt = false, bool close = false) { + if (ObjectSerializer == null) + throw new ArgumentNullException(nameof(ObjectSerializer)); + + var client = GetClient(id); + var builder = new SimpleMessage(MessageType.MessageWithMetadata, this, Debug) + .CompressMessage(compress) + .EncryptMessage(encrypt) + .SetMetadata(metadata) + .SetBytes(data) + .SetHeaderString("ByteArray") + .SetSendClient(client); + + builder.Build(); + SendToSocket(builder.PayLoad, close, false, id); + } + + public async Task SendMessageWithMetadataAsync(int id, byte[] data, IDictionary metadata, bool compress = false, bool encrypt = false, bool close = false) + { + if (ObjectSerializer == null) + throw new ArgumentNullException(nameof(ObjectSerializer)); + + var client = GetClient(id); + var builder = new SimpleMessage(MessageType.MessageWithMetadata, this, Debug) + .CompressMessage(compress) + .EncryptMessage(encrypt) + .SetMetadata(metadata) + .SetBytes(data) + .SetHeaderString("ByteArray") + .SetSendClient(client); + + await builder.BuildAsync(); + SendToSocket(builder.PayLoad, close, false, id); + } + public void SendMessageWithMetadata(int id, object message, IDictionary metadata, bool compress = false, bool encrypt = false,bool close = false) { if (ObjectSerializer == null) diff --git a/SimpleSockets/Server/SimpleSocketTcpListener.cs b/SimpleSockets/Server/SimpleSocketTcpListener.cs index 1588909..3e76c52 100644 --- a/SimpleSockets/Server/SimpleSocketTcpListener.cs +++ b/SimpleSockets/Server/SimpleSocketTcpListener.cs @@ -176,7 +176,7 @@ protected internal override void Receive(IClientMetadata state, int offset = 0) } } - protected override async void ReceiveCallback(IAsyncResult result) + protected override void ReceiveCallback(IAsyncResult result) { var state = (ClientMetadata)result.AsyncState; state.MreTimeout.Set(); @@ -211,11 +211,11 @@ protected override async void ReceiveCallback(IAsyncResult result) { if (state.SimpleMessage == null) state.SimpleMessage = new SimpleMessage(state, this, Debug); - await ParallelQueue.Enqueue(() => state.SimpleMessage.ReadBytesAndBuildMessage(receive)); + state.SimpleMessage.ReadBytesAndBuildMessage(receive); } else if (receive > 0) { - await ParallelQueue.Enqueue(() => state.SimpleMessage.ReadBytesAndBuildMessage(receive)); + state.SimpleMessage.ReadBytesAndBuildMessage(receive); } } diff --git a/SimpleSockets/Server/SimpleSocketTcpSslListener.cs b/SimpleSockets/Server/SimpleSocketTcpSslListener.cs index bec1257..63861e0 100644 --- a/SimpleSockets/Server/SimpleSocketTcpSslListener.cs +++ b/SimpleSockets/Server/SimpleSocketTcpSslListener.cs @@ -373,7 +373,7 @@ protected internal override void Receive(IClientMetadata state, int offset = 0) } } - protected override async void ReceiveCallback(IAsyncResult result) + protected override void ReceiveCallback(IAsyncResult result) { var state = (ClientMetadata)result.AsyncState; state.MreTimeout.Set(); @@ -409,10 +409,10 @@ protected override async void ReceiveCallback(IAsyncResult result) { if (state.SimpleMessage == null) state.SimpleMessage = new SimpleMessage(state, this, Debug); - await ParallelQueue.Enqueue(() => state.SimpleMessage.ReadBytesAndBuildMessage(receive)); + state.SimpleMessage.ReadBytesAndBuildMessage(receive); } else if (receive > 0) - await ParallelQueue.Enqueue(() => state.SimpleMessage.ReadBytesAndBuildMessage(receive)); + state.SimpleMessage.ReadBytesAndBuildMessage(receive); } state.MreReceiving.Set(); diff --git a/SimpleSockets/SimpleSocket.cs b/SimpleSockets/SimpleSocket.cs index a07da9f..5e27d2b 100644 --- a/SimpleSockets/SimpleSocket.cs +++ b/SimpleSockets/SimpleSocket.cs @@ -551,7 +551,7 @@ protected internal async Task DecryptFileAsync(string input, string ou protected internal abstract void RaiseMessageContractReceived(IClientInfo clientInfo, IMessageContract contract, byte[] data); - protected internal abstract void RaiseMessageWithMetaDataReceived(IClientInfo clientInfo, object message, IDictionary metadata); + protected internal abstract void RaiseMessageWithMetaDataReceived(IClientInfo clientInfo, object message, IDictionary metadata, Type ObjectType); protected internal abstract void RaiseBytesReceived(IClientInfo clientInfo, byte[] data); diff --git a/Tests/Test.Sockets.Basic/TcpSslTests.cs b/Tests/Test.Sockets.Basic/TcpSslTests.cs index 465cc11..a38c4ed 100644 --- a/Tests/Test.Sockets.Basic/TcpSslTests.cs +++ b/Tests/Test.Sockets.Basic/TcpSslTests.cs @@ -81,14 +81,14 @@ public void Client_Message_Server() } [Test] - public void Cient_CustomMessage_Server() + public void Cient_MessageWithMetadata_Server() { string message = "This is a test custom header message."; var dictionary = new Dictionary(); dictionary.Add("Test", "This is a test"); - SimpleSockets.Server.MessageWithMetadataReceivedDelegate msgRec = (client, msg, head) => { + SimpleSockets.Server.MessageWithMetadataReceivedDelegate msgRec = (client, msg, head, type) => { Assert.AreEqual(message, msg); Assert.AreEqual(dictionary, head); }; @@ -145,7 +145,7 @@ public void Server_CustomMessage_Client() dictionary.Add("Test", "This is a test"); - SimpleSockets.Client.MessageWithMetadataReceivedDelegate msgRec = (client, msg, head) => { + SimpleSockets.Client.MessageWithMetadataReceivedDelegate msgRec = (client, msg, head, type) => { Assert.AreEqual(message, msg); Assert.AreEqual(dictionary, head); }; diff --git a/Tests/Test.Sockets.Basic/TcpTests.cs b/Tests/Test.Sockets.Basic/TcpTests.cs index 5d35b06..dc31c34 100644 --- a/Tests/Test.Sockets.Basic/TcpTests.cs +++ b/Tests/Test.Sockets.Basic/TcpTests.cs @@ -80,7 +80,7 @@ public void Client_CustomMessage_Server() dictionary.Add("Test", "This is a test"); - SimpleSockets.Server.MessageWithMetadataReceivedDelegate msgRec = (client, msg, head) => { + SimpleSockets.Server.MessageWithMetadataReceivedDelegate msgRec = (client, msg, head, type) => { Assert.AreEqual(message, msg); Assert.AreEqual(dictionary, head); }; @@ -165,7 +165,7 @@ public void Server_CustomMessage_Client() dictionary.Add("Test", "This is a test"); - SimpleSockets.Client.MessageWithMetadataReceivedDelegate msgRec = (client, msg, head) => { + SimpleSockets.Client.MessageWithMetadataReceivedDelegate msgRec = (client, msg, head, type) => { Assert.AreEqual(message, msg); Assert.AreEqual(dictionary, head); }; diff --git a/Tests/Test.Sockets.Parallel.Manual/Program.cs b/Tests/Test.Sockets.Parallel.Manual/Program.cs index 77faebf..c3f2319 100644 --- a/Tests/Test.Sockets.Parallel.Manual/Program.cs +++ b/Tests/Test.Sockets.Parallel.Manual/Program.cs @@ -31,7 +31,6 @@ static void Main(string[] args) Console.WriteLine("Starting Test..."); StartServer(); Thread.Sleep(1000); - Task task = null; for (var i = 0; i < _clientThreads; i++) { @@ -137,7 +136,7 @@ private static void ClientOnClientErrorThrown(SimpleSocketClient client, Excepti private static void ServerOnMessageReceived(IClientInfo client, string message) { _received.Count(); - // Console.WriteLine("Server has received a message from client " + client.Id + "|" + client.Guid + ", " + message); + Console.WriteLine("Server has received a message from client " + client.Id + "|" + client.Guid + ", " + message); if (string.IsNullOrEmpty(message)) _receivedEmpty.Count(); diff --git a/Tests/Test.Sockets.Parallel/TcpParallelTests.cs b/Tests/Test.Sockets.Parallel/TcpParallelTests.cs index cf746be..6c47763 100644 --- a/Tests/Test.Sockets.Parallel/TcpParallelTests.cs +++ b/Tests/Test.Sockets.Parallel/TcpParallelTests.cs @@ -17,7 +17,7 @@ public class TcpParallelTests private IList _clients = new List(); - private int _numClients = 15; + private int _numClients = 25; private int _numMessages = 1000; [OneTimeSetUp] @@ -100,6 +100,37 @@ public void Client_ParallelMessages_Server() { } + [Test] + public void Client_ParallelMessagesWithMetaData_Server() + { + + Counter counter = new Counter(); + + ManualResetEvent mre = new ManualResetEvent(false); + + + SimpleSockets.Server.MessageWithMetadataReceivedDelegate msgRec = (client, msg, metadata, type) => { + counter.Count(); + + if (counter.GetCount == _numClients * _numMessages) + mre.Set(); + }; + + _server.MessageWithMetaDataReceived += msgRec; + + foreach (var client in _clients) + { + new Thread(() => SendMessageWithMetadata(client)).Start(); + } + + // If it can't complete in 30 minutes fail + mre.WaitOne(new TimeSpan(0, 30, 0)); + + _server.MessageWithMetaDataReceived -= msgRec; + Assert.AreEqual((_numMessages * _numClients), counter.GetCount); // True if all messages have been received. + + } + [Test] public void Client_ParallelObjects_Server() { @@ -118,7 +149,7 @@ public void Client_ParallelObjects_Server() foreach (var client in _clients) { - new Thread(() => SendMessages(client, true)).Start(); + new Thread(() => SendMessages(client, true)); } // If it can't complete in 30 minutes fail @@ -128,6 +159,19 @@ public void Client_ParallelObjects_Server() Assert.AreEqual((_numMessages * _numClients), counter.GetCount); // True if all messages have been received. } + private void SendMessageWithMetadata(SimpleSocketClient client) { + string message = "This is test message nr "; + IDictionary dictionary = new Dictionary(); + dictionary.Add("Key1", new DataObject("Test", "This is a text", 10.56, new DateTime(2000, 1, 1))); + dictionary.Add("Key2", new DataObject("Test2", "This is a second test", 11, new DateTime(2000, 1, 1))); + + + for (var i = 0; i < _numMessages; i++) + { + client.SendMessageWithMetadata(message + (i + 1), dictionary); + } + } + private void SendMessages(SimpleSocketClient client, bool sendObjects) { string message = "This is test message nr "; diff --git a/Tests/Test.Sockets.Parallel/TcpSslParallelTests.cs b/Tests/Test.Sockets.Parallel/TcpSslParallelTests.cs index c0f8a74..45415ad 100644 --- a/Tests/Test.Sockets.Parallel/TcpSslParallelTests.cs +++ b/Tests/Test.Sockets.Parallel/TcpSslParallelTests.cs @@ -17,8 +17,8 @@ public class TcpSslParallelTests private IList _clients = new List(); - private int _numClients = 15; - private int _numMessages = 1000; + private int _numClients = 50; + private int _numMessages = 10000; private X509Certificate2 _cert; [OneTimeSetUp] @@ -139,6 +139,51 @@ public void Ssl_Client_ParallelObjects_Server() Assert.AreEqual((_numMessages * _numClients), counter.GetCount); // True if all messages have been received. } + [Test] + public void Client_ParallelMessagesWithMetaData_Server() + { + + Counter counter = new Counter(); + + ManualResetEvent mre = new ManualResetEvent(false); + + + SimpleSockets.Server.MessageWithMetadataReceivedDelegate msgRec = (client, msg, metadata, type) => { + counter.Count(); + + if (counter.GetCount == _numClients * _numMessages) + mre.Set(); + }; + + _server.MessageWithMetaDataReceived += msgRec; + + foreach (var client in _clients) + { + new Thread(() => SendMessageWithMetadata(client)).Start(); + } + + // If it can't complete in 30 minutes fail + mre.WaitOne(new TimeSpan(0, 30, 0)); + + _server.MessageWithMetaDataReceived -= msgRec; + Assert.AreEqual((_numMessages * _numClients), counter.GetCount); // True if all messages have been received. + + } + + private void SendMessageWithMetadata(SimpleSocketClient client) + { + string message = "This is test message nr "; + IDictionary dictionary = new Dictionary(); + dictionary.Add("Key1", new DataObject("Test", "This is a text", 10.56, new DateTime(2000, 1, 1))); + dictionary.Add("Key2", new DataObject("Test2", "This is a second test", 11, new DateTime(2000, 1, 1))); + + + for (var i = 0; i < _numMessages; i++) + { + client.SendMessageWithMetadata(message + (i + 1), dictionary); + } + } + private void SendMessages(SimpleSocketClient client, bool sendObjects) { string message = "This is test message nr ";