/// <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, LocProtocolMessage Message)
        {
            log.Trace("()");

            bool res = false;

            string msgStr = Message.ToString();

            log.Trace("Sending message:\n{0}", msgStr);
            byte[] responseBytes = LocMessageBuilder.MessageToByteArray(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);
        }
Example #2
0
 /// <summary>
 /// Converts an IoP Network protocol message to a binary format.
 /// </summary>
 /// <param name="Message">IoP Network protocol message.</param>
 /// <returns>Binary representation of the message to be sent over the network.</returns>
 public override byte[] MessageToByteArray(IProtocolMessage Message)
 {
     return(LocMessageBuilder.MessageToByteArray(Message));
 }