示例#1
0
        private void OnSendCompleted(IAsyncResult ar)
        {
            try { _stream.EndWrite(ar); }
            catch (Exception) {
                // ignore errors, let the receiving end take care of that.
                return;
            }

            var isComplete = _encoder.OnSendCompleted(_writeBuffer.Count);

            if (!isComplete)
            {
                _encoder.Send(_writeBuffer);
                _stream.BeginWrite(_writeBuffer.Buffer, _writeBuffer.Offset, _writeBuffer.Count, OnSendCompleted, null);
                return;
            }

            var msg = _currentOutboundMessage;

            lock (_outboundMessages) {
                if (!_outboundMessages.TryDequeue(out _currentOutboundMessage))
                {
                    _currentOutboundMessage = null;
                    _sendCompleteAction(this, msg);
                    return;
                }
            }

            _sendCompleteAction(this, msg);
            SendCurrent();
        }
示例#2
0
        /// <summary>
        ///     Send a new message
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <remarks>
        ///     <para>
        ///         Outbound messages are enqueued and sent in order.
        ///     </para>
        ///     <para>
        ///         You may enqueue <c>byte[]</c> arrays or <see cref="Stream" />  objects. They will not be serialized but
        ///         MicroMessage framed directly.
        ///     </para>
        /// </remarks>
        public void Send(object message)
        {
            if (_socket == null || !_socket.Connected)
            {
                throw new SocketException((int)SocketError.NotConnected);
            }

            _sendLock.Wait();
            _messagePendingSendOperation = message;
            _encoder.Prepare(message);
            _encoder.Send(_writeArgsWrapper);
            var isPending = _socket.SendAsync(_writeArgs);

            if (!isPending)
            {
                OnSendCompleted(this, _writeArgs);
            }
        }
        private void OnSendCompleted(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success ||
                e.BytesTransferred == 0)
            {
                // ignore errors, let the receiving end take care of that.
                return;
            }

            var isComplete = _encoder.OnSendCompleted(e.BytesTransferred);

            if (!isComplete)
            {
                _encoder.Send(_writeArgsWrapper);
                var isPending = _socket.SendAsync(_writeArgs);
                if (!isPending)
                {
                    OnSendCompleted(this, _writeArgs);
                }
                return;
            }

            var msg = _currentOutboundMessage;

            lock (_outboundMessages) {
                if (!_outboundMessages.TryDequeue(out _currentOutboundMessage))
                {
                    _currentOutboundMessage = null;
                    _sendCompleteAction(this, msg);
                    return;
                }
            }

            _sendCompleteAction(this, msg);
            SendCurrent();
        }