public void StreamDataExchange_OneWayStream() { bool success = false; bool completed = false; string message = GenerateMessage(); var stream = new MemoryStream(); var reading = new Task(async delegate() { try { byte[] data = null; while (data == null) { data = await StreamHandler.StreamReaderAsync(stream); Thread.Sleep(5); } string receivedMessage = BinaryHandler.FromByteArray <string>(data); success = receivedMessage.Equals(message); completed = true; } catch (Exception ex) { Assert.Fail(ex.Message); return; } }); var writing = new Task(async delegate() { await StreamHandler.StreamWriterAsync( stream, StreamChanelMode.Oneway, BinaryHandler.ToByteArray(message)); }); reading.Start(); writing.Start(); while (!completed) { Thread.Sleep(5); } stream.Dispose(); Assert.IsTrue(success); }
public void StreamDataExchange_Pipes() { bool success = false; bool completed = false; int size = 200000000; string message = GenerateMessage(size); var client = new System.IO.Pipes.NamedPipeClientStream("TESTPIPE"); var server = new System.IO.Pipes.NamedPipeServerStream("TESTPIPE"); var banchmarkTimer = new System.Diagnostics.Stopwatch(); banchmarkTimer.Start(); var reading = new Task(async delegate() { await client.ConnectAsync(); try { // Data to message format. string receivedMessage = await StreamHandler.StreamReaderAsync <string>(client); // Stoping banchmark. banchmarkTimer.Stop(); // Validate data. success = receivedMessage.Equals(message); completed = true; } catch (Exception ex) { Assert.Fail(ex.Message); return; } }); var writing = new Task(async delegate() { // Wait client connection. await server.WaitForConnectionAsync(); try { // Sending message to stream. await StreamHandler.StreamWriterAsync(server, message); } catch (Exception ex) { Assert.Fail(ex.Message); return; } }); reading.Start(); writing.Start(); while (!completed) { Thread.Sleep(5); } float secondsFromStart = banchmarkTimer.ElapsedMilliseconds / (1000.0f); float sharedMBSize = size / 1000000.0f; float speedMBpS = sharedMBSize / secondsFromStart; Console.WriteLine("Transmission time: " + secondsFromStart + " seconds"); Console.WriteLine("Transmisted: " + sharedMBSize + " MB"); Console.WriteLine("Speed: " + speedMBpS + " MB/s | " + (speedMBpS * 8) + "Mb/s"); client.Dispose(); server.Dispose(); Assert.IsTrue(success); }
/// <summary> /// Code that will work on server loop when connection will be established. /// Recoomended to using as default DNS Handler for queries reciving. /// </summary> public static async void ClientToServerAsync(BaseServerTransmissionController controller) { DateTime sessionTime = DateTime.Now.AddSeconds(5000); // Read until trasmition exits not finished. while (controller.PipeServer.IsConnected) { // Open stream reader. byte[] binaryQuery; #region Reciving message // Read data from stream. // Avoid an error caused to disconection of client. try { binaryQuery = await StreamHandler.StreamReaderAsync(controller.PipeServer); } // Catch the Exception that is raised if the pipe is broken or disconnected. catch (Exception e) { Console.WriteLine("DNS HANDLER ERROR: {0}", e.Message); return; } if (DateTime.Compare(sessionTime, DateTime.Now) < 0) { Console.WriteLine("Connection terminated cause allowed time has expired."); // Avoid disconnectin error. try { controller.PipeServer.Disconnect(); } catch { // Exception caused by disconecction on client side. } return; } #endregion #region Finalizing connection // Disconnect user if query recived. if (controller.PipeServer.IsConnected) { try { controller.PipeServer.Disconnect(); } catch { // Exception caused by disconecction on client side. } } // Remove temporal data. controller.PipeServer.Dispose(); #endregion #region Query processing // Drop if stream is over. if (binaryQuery == null || binaryQuery.Length == 0) { //Console.WriteLine("NULL REQUEST AVOIDED. CONNECTION TERMINATED."); break; } // Decode query from binary data. Query query; try { query = UniformDataOperator.Binary.BinaryHandler.FromByteArray <Query>(binaryQuery); } catch (Exception ex) { Console.WriteLine("DNS HANDLER ERROR (DNS50): DAMAGED DATA : {0}", ex.Message); return; } // Try to decrypt. In case of fail decryptor returns an entry message. await EnctyptionOperatorsHandler.TryToDecryptAsync( query, EnctyptionOperatorsHandler.AsymmetricEO); // Log query. Console.WriteLine(@"RECIVED QUERY (DNS0): {0}", query); // Try to get correct transmisssion controller. if (controller is ClientToServerTransmissionController CToSTS) { // Redirect handler. CToSTS.queryHandlerCallback?.Invoke(controller, query); } else { // Log error. Console.WriteLine(@"DNS HANDLER ERROR (DNS40):\nQuery processing not posssible.\nTransmission controller not {0}", typeof(ClientToServerTransmissionController).FullName); } #endregion } // Log about transmission finish. Console.WriteLine("{0} : TRANSMISSION FINISHED AT {1}", controller.PipeName, DateTime.Now.ToString("HH:mm:ss.fff")); }