Exemplo n.º 1
0
        /// <summary>
        /// Adds synchronization around the base method call.  Uses <code>this</code> as the lock.
        /// </summary>
        protected sealed override void SendMessage(IProtocolMessage message)
        {
            Logger.ReportDebuggerMessageSent(LoggingContext, message.Type, message.ToString());

            lock (this)
            {
                base.SendMessage(message);
            }
        }
Exemplo n.º 2
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>
        protected async Task <bool> SendMessageInternalAsync(IProtocolMessage Message)
        {
            log.Trace("()");

            bool res = false;

            string msgStr = Message.ToString();

            log.Trace("Sending message:\n{0}", msgStr.SubstrMax(512));
            byte[] messageBytes = MessageToByteArray(Message);

            await streamWriteLock.WaitAsync();

            try
            {
                if (Stream != null)
                {
                    await Stream.WriteAsync(messageBytes, 0, messageBytes.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);
        }