Example #1
0
        public Task SendAsync(int correlationId, ReusableMemoryStream data, bool acknowledge)
        {
            if (!_socket.Connected)
            {
                throw new TransportException(TransportError.ConnectError);
            }

            if (acknowledge)
            {
                _correlationIds.Enqueue(correlationId);
            }

            // Sending will use synchronous send first using non blocking mode on the socket.
            // If we cannot send all bytes in one call, we switch to an asynchronous send loop.
            var future = SuccessTask;
            var buffer = _bufferPool.Reserve();

            try
            {
                data.Position = 0;
                int read = data.Read(buffer, 0, buffer.Length);
                while (read != 0)
                {
                    SocketError error;
                    int         sent = _socket.Send(buffer, 0, read, SocketFlags.None, out error);
                    if (error == SocketError.WouldBlock || (error == SocketError.Success && sent < read))
                    {
                        // Start an async send loop
                        _sendContext.Data    = data;
                        _sendContext.Promise = new TaskCompletionSource <Void>();
                        _sendContext.Buffer  = buffer;
                        _sendArgs.UserToken  = this;
                        _sendArgs.SetBuffer(buffer, sent, read - sent);
                        future = _sendContext.Promise.Task;
                        if (!_socket.SendAsync(_sendArgs))
                        {
                            OnSendCompleted(_socket, _sendArgs);
                        }
                        break;
                    }
                    else if (error != SocketError.Success)
                    {
                        throw new SocketException((int)error);
                    }
                    read = data.Read(buffer, 0, buffer.Length);
                }
                if (read == 0)
                {
                    _bufferPool.Release(buffer);
                }
            }
            catch (Exception ex)
            {
                CleanSend();
                throw new TransportException(TransportError.WriteError, ex);
            }

            return(future);
        }
Example #2
0
        public static byte[] DeserializeByteArray(ReusableMemoryStream stream)
        {
            var len = BigEndianConverter.ReadInt32(stream);

            if (len == -1)
            {
                return(null);
            }
            var buff = new byte[len];

            stream.Read(buff, 0, len);
            return(buff);
        }
Example #3
0
        public static byte[] DeserializeBytesWithVarIntSize(ReusableMemoryStream stream)
        {
            var len = VarIntConverter.ReadAsInt32(stream);

            if (len == -1)
            {
                return(null);
            }

            var buffer = new byte[len];

            stream.Read(buffer, 0, len);
            return(buffer);
        }
        public static byte[] DeserializeBytes(ReusableMemoryStream stream)
        {
            var len = BigEndianConverter.ReadInt32(stream);

            // per contract, null string is represented with -1 len.
            if (len == -1)
            {
                return(null);
            }

            var buffer = new byte[len];

            stream.Read(buffer, 0, len);
            return(buffer);
        }