public void SendMessages(int messageSize, int messageCount)
        {
            var data = new byte[messageSize];
            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);
            var segment = _framer.FrameData(new ArraySegment<byte>(data, 0, data.Length));

            for (var i = 0; i < messageCount; i++)
            {
                _connection.EnqueueSend(segment);
            }
        }
Beispiel #2
0
        public void QueueMessage(byte[] message)
        {
            if (message.Length == 0)
            {
                return;
            }

            var segments = _framer.FrameData(new ArraySegment <byte>(message, 0, message.Length));

            _sendingQueue.Enqueue(segments);
            Interlocked.Increment(ref _pendingMessageCount);

            TrySend();
        }
        private async ValueTask TrySendAsync()
        {
            if (_closing == 1)
            {
                return;
            }
            if (!EnterSending())
            {
                return;
            }
            while (_sendingQueue.TryDequeue(out ReadOnlyMemory <byte> data))
            {
                Interlocked.Decrement(ref _pendingMessageCount);
                _messageFramer.FrameData(_sendBuufferPipeline.Writer, data);
                if (_sendBuufferPipeline.Writer.Length >= Setting.SendMaxPacketSize)
                {
                    break;
                }
            }
            if (_sendBuufferPipeline.Writer.Length > 0)
            {
                _sendBuufferPipeline.Writer.Flush();
            }

            if (_sendBuufferPipeline.Reader.Length == 0)
            {
                ExitSending();
                if (_sendingQueue.Count > 0)
                {
                    await TrySendAsync();
                }
                return;
            }

            try
            {
                var readResult = _sendBuufferPipeline.Reader.ReadResult();
                await Socket.SendAsync(readResult.Buffer, SocketFlags.None);
            }
            catch (Exception ex)
            {
                CloseInternal(SocketError.Shutdown, "Socket send error, errorMessage:" + ex.Message, ex);
            }
            ExitSending();
            await TrySendAsync();
        }
Beispiel #4
0
        public void Send(byte[] bytes, bool checkQueueSize = true)
        {
            if (IsClosed)
            {
                return;
            }

            int queueSize;

            if (checkQueueSize && (queueSize = _tcpConnection.SendQueueSize) > ConnectionQueueSizeThreshold)
            {
                CloseConnectionOnError(string.Format("Connection queue size is too large: {0}.", queueSize));
                return;
            }

            var data   = new ArraySegment <byte>(bytes);
            var framed = _framer.FrameData(data);

            _tcpConnection.EnqueueSend(framed);
        }
        public void EnqueueSend(T message)
        {
            var data = _formatter.ToArraySegment(message);

            _connection.EnqueueSend(_framer.FrameData(data));
        }
Beispiel #6
0
 private void OnMessageArrived(ArraySegment <byte> message)
 {
     byte[] data = new byte[message.Count];
     Array.Copy(message.Array, message.Offset, data, 0, message.Count);
     _messageHandler(_connection, data, reply => _connection.SendAsync(_messageFramer.FrameData(new ArraySegment <byte>(reply, 0, reply.Length))));
 }
Beispiel #7
0
 public void SendMessage(byte[] message)
 {
     _connection.EnqueueSend(_framer.FrameData(new ArraySegment <byte>(message, 0, message.Length)));
 }
Beispiel #8
0
 public void SendAsync(byte[] message)
 {
     _connection.SendAsync(_framer.FrameData(new ArraySegment <byte>(message, 0, message.Length)));
 }