Пример #1
0
        /// <summary>
        /// Sends IoP protocol message over the network stream.
        /// </summary>
        /// <param name="Data">Message to send.</param>
        public async Task SendMessageAsync(Message Data)
        {
            string dataStr = Data.ToString();

            log.Trace("()\n{0}", dataStr.Substring(0, Math.Min(dataStr.Length, 512)));

            byte[] rawData = ProtocolHelper.GetMessageBytes(Data);
            await stream.WriteAsync(rawData, 0, rawData.Length);

            log.Trace("(-)");
        }
Пример #2
0
        /// <summary>
        /// Sends a message to the client over the open network stream.
        /// </summary>
        /// <param name="Client">TCP client.</param>
        /// <param name="Message">Message to send.</param>
        /// <returns>true if the message was sent successfully to the target recipient.</returns>
        private async Task <bool> SendMessageInternalAsync(TcpClient Client, Message Message)
        {
            log.Trace("()");

            bool res = false;

            string msgStr = Message.ToString();

            log.Trace("Sending message:\n{0}", msgStr);
            byte[] responseBytes = ProtocolHelper.GetMessageBytes(Message);

            await StreamWriteLock.WaitAsync();

            try
            {
                NetworkStream stream = Client.GetStream();
                if (stream != null)
                {
                    await stream.WriteAsync(responseBytes, 0, responseBytes.Length);

                    res = true;
                }
                else
                {
                    log.Info("Connection to the client has been terminated.");
                }
            }
            catch (IOException)
            {
                log.Info("Connection to the client has been terminated.");
            }
            finally
            {
                StreamWriteLock.Release();
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
Пример #3
0
        /// <summary>
        /// Sends a message to the client over the open network stream.
        /// </summary>
        /// <param name="Message">Message to send.</param>
        /// <returns>true if the message was sent successfully to the target recipient.</returns>
        private async Task <bool> SendMessageInternalAsync(Message Message)
        {
            log.Trace("()");

            bool res = false;

            string msgStr = Message.ToString();

            log.Trace("Sending response to client:\n{0}", msgStr.Substring(0, Math.Min(msgStr.Length, 512)));
            byte[] responseBytes = ProtocolHelper.GetMessageBytes(Message);

            await StreamWriteLock.WaitAsync();

            try
            {
                if (Stream != null)
                {
                    await Stream.WriteAsync(responseBytes, 0, responseBytes.Length);

                    res = true;
                }
                else
                {
                    log.Info("Connection to the client has been terminated.");
                }
            }
            catch (IOException)
            {
                log.Info("Connection to the client has been terminated.");
            }
            finally
            {
                StreamWriteLock.Release();
            }

            log.Trace("(-):{0}", res);
            return(res);
        }
Пример #4
0
        /// <summary>
        /// Implementation of the test itself.
        /// </summary>
        /// <returns>true if the test passes, false otherwise.</returns>
        public override async Task <bool> RunAsync()
        {
            IPAddress ServerIp    = (IPAddress)ArgumentValues["Server IP"];
            int       PrimaryPort = (int)ArgumentValues["primary Port"];

            log.Trace("(ServerIp:'{0}',PrimaryPort:{1})", ServerIp, PrimaryPort);

            bool res = false;

            Passed = false;

            ProtocolClient client = new ProtocolClient();

            try
            {
                MessageBuilder mb = client.MessageBuilder;

                // Step 1
                await client.ConnectAsync(ServerIp, PrimaryPort, false);

                byte[]  payload        = Encoding.UTF8.GetBytes("test");
                Message requestMessage = mb.CreatePingRequest(payload);

                byte[] messageData = ProtocolHelper.GetMessageBytes(requestMessage);
                byte[] part1       = new byte[6];
                byte[] part2       = new byte[messageData.Length - part1.Length];
                Array.Copy(messageData, 0, part1, 0, part1.Length);
                Array.Copy(messageData, part1.Length, part2, 0, part2.Length);
                await client.SendRawAsync(part1);


                log.Trace("Entering 500 seconds wait...");
                await Task.Delay(500 * 1000);

                log.Trace("Wait completed.");

                // We should be disconnected by now, so sending or receiving should throw.
                bool disconnectedOk = false;
                try
                {
                    await client.SendRawAsync(part2);

                    await client.ReceiveMessageAsync();
                }
                catch
                {
                    log.Trace("Expected exception occurred.");
                    disconnectedOk = true;
                }

                // Step 1 Acceptance
                Passed = disconnectedOk;

                res = true;
            }
            catch (Exception e)
            {
                log.Error("Exception occurred: {0}", e.ToString());
            }
            client.Dispose();

            log.Trace("(-):{0}", res);
            return(res);
        }