Example #1
0
        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);
        }
        /// <summary>
        /// Code that will work on server loop when connection will be established.
        /// Recoomended to using as default DNS Handler for message sending.
        ///
        /// Provide broadcasting to client by useing GetMessage delegate of BroadcastingServerTC controller.
        /// </summary>
        public static async void BroadcastAsync(BaseServerTransmissionController controller)
        {
            // Try to get correct controller.
            if (controller is BroadcastTransmissionController broadcastController)
            {
                // Read until trasmition exits not finished.
                // Avoid an error caused to disconection of client.
                try
                {
                    // Get message
                    byte[] message = broadcastController.GetMessage(broadcastController);

                    // Write message to stream.
                    Console.WriteLine("{0}: Start transmission to client.", controller.PipeName);

                    // Send data to stream.
                    await StreamHandler.StreamWriterAsync(controller.PipeServer, message);
                }
                // Catch the Exception that is raised if the pipe is broken or disconnected.
                catch (Exception e)
                {
                    Console.WriteLine("DNS HANDLER ERROR (StC0): {0}", e.Message);
                    return;
                }
            }
            else
            {
                // Log about transmission finish.
                Console.WriteLine("TRANSMISSION ERROR: Try to user invalid controller for broadcasting.");
            }

            // Disconnect user if query recived.
            if (controller.PipeServer.IsConnected)
            {
                try
                {
                    controller.PipeServer.Disconnect();
                }
                catch
                {
                    // Exception caused by disconecction on client side.
                }
            }

            // Log about transmission finish.
            Console.WriteLine("{0} : TRANSMISSION FINISHED AT {1}",
                              controller.PipeName, DateTime.Now.ToString("HH:mm:ss.fff"));
        }
Example #3
0
        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);
        }