private void ReceiveEvent()
        {
            //Log.Info("Receive Event: " + eventArgs.BytesTransferred + "," + dataOffset + "," + dataLength + "," + inHeader);
            if (usingSocketTimeout)
            {
                eventReceived = true;
            }

            if (eventArgs.BytesTransferred <= 0)
            {
                ConnectionFailed(new AerospikeException.Connection("Connection closed"));
                return;
            }

            dataOffset += eventArgs.BytesTransferred;

            if (dataOffset < dataLength)
            {
                eventArgs.SetBuffer(dataOffset, dataLength - dataOffset);
                Receive();
                return;
            }
            dataOffset = segment.offset;

            if (inHeader)
            {
                long proto  = ByteUtil.BytesToLong(dataBuffer, dataOffset);
                int  length = (int)(proto & 0xFFFFFFFFFFFFL);

                if (length <= 0)
                {
                    ReceiveBegin();
                    return;
                }

                compressed = ((proto >> 48) & 0xFF) == (long)Command.MSG_TYPE_COMPRESSED;
                inHeader   = false;

                if (length > segment.size)
                {
                    ResizeBuffer(length);
                    dataBuffer = segment.buffer;
                    dataOffset = segment.offset;
                }
                eventArgs.SetBuffer(dataBuffer, dataOffset, length);
                dataLength = dataOffset + length;
                Receive();
            }
            else
            {
                if (inAuthenticate)
                {
                    inAuthenticate = false;
                    inHeader       = true;

                    int resultCode = dataBuffer[dataOffset + 1];

                    if (resultCode != 0 && resultCode != ResultCode.SECURITY_NOT_ENABLED)
                    {
                        // Authentication failed. Session token probably expired.
                        // Signal tend thread to perform node login, so future
                        // transactions do not fail.
                        node.SignalLogin();

                        // This is a rare event because the client tracks session
                        // expiration and will relogin before session expiration.
                        // Do not try to login on same socket because login can take
                        // a long time and thousands of simultaneous logins could
                        // overwhelm server.
                        throw new AerospikeException(resultCode);
                    }
                    ConnectionReady();
                    return;
                }

                conn.UpdateLastUsed();

                if (compressed)
                {
                    int usize = (int)ByteUtil.BytesToLong(dataBuffer, dataOffset);
                    dataOffset += 8;
                    byte[] ubuf = new byte[usize];

                    ByteUtil.Decompress(dataBuffer, dataOffset, dataLength, ubuf, usize);
                    dataBuffer = ubuf;
                    dataOffset = 8;
                    dataLength = usize;
                }

                ParseCommand();
            }
        }
        protected internal override void ParseResult(Connection conn)
        {
            // Read header.
            conn.ReadFully(dataBuffer, 8);

            long sz          = ByteUtil.BytesToLong(dataBuffer, 0);
            int  receiveSize = (int)(sz & 0xFFFFFFFFFFFFL);

            if (receiveSize <= 0)
            {
                throw new AerospikeException("Invalid receive size: " + receiveSize);
            }

            SizeBuffer(receiveSize);
            conn.ReadFully(dataBuffer, receiveSize);
            conn.UpdateLastUsed();

            ulong type = (ulong)((sz >> 48) & 0xff);

            if (type == Command.AS_MSG_TYPE)
            {
                dataOffset = 5;
            }
            else if (type == Command.MSG_TYPE_COMPRESSED)
            {
                int    usize = (int)ByteUtil.BytesToLong(dataBuffer, 0);
                byte[] ubuf  = new byte[usize];

                ByteUtil.Decompress(dataBuffer, 8, receiveSize, ubuf, usize);
                dataBuffer = ubuf;
                dataOffset = 13;
            }
            else
            {
                throw new AerospikeException("Invalid proto type: " + type + " Expected: " + Command.AS_MSG_TYPE);
            }

            int resultCode = dataBuffer[dataOffset];

            dataOffset++;
            int generation = ByteUtil.BytesToInt(dataBuffer, dataOffset);

            dataOffset += 4;
            int expiration = ByteUtil.BytesToInt(dataBuffer, dataOffset);

            dataOffset += 8;
            int fieldCount = ByteUtil.BytesToShort(dataBuffer, dataOffset);

            dataOffset += 2;
            int opCount = ByteUtil.BytesToShort(dataBuffer, dataOffset);

            dataOffset += 2;

            if (resultCode == 0)
            {
                if (opCount == 0)
                {
                    // Bin data was not returned.
                    record = new Record(null, generation, expiration);
                    return;
                }
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                return;
            }

            if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR)
            {
                HandleNotFound(resultCode);
                return;
            }

            if (resultCode == ResultCode.FILTERED_OUT)
            {
                if (policy.failOnFilteredOut)
                {
                    throw new AerospikeException(resultCode);
                }
                return;
            }

            if (resultCode == ResultCode.UDF_BAD_RESPONSE)
            {
                record = ParseRecord(opCount, fieldCount, generation, expiration);
                HandleUdfError(resultCode);
                return;
            }

            throw new AerospikeException(resultCode);
        }
Example #3
0
        protected internal sealed override void ParseResult(Connection conn)
        {
            // Read blocks of records.  Do not use thread local receive buffer because each
            // block will likely be too big for a cache.  Also, scan callbacks can nest
            // further database commands which would contend with the thread local receive buffer.
            // Instead, use separate heap allocated buffers.
            byte[] protoBuf = new byte[8];
            byte[] buf      = null;
            byte[] ubuf     = null;
            int    receiveSize;

            while (true)
            {
                // Read header
                conn.ReadFully(protoBuf, 8);

                long proto = ByteUtil.BytesToLong(protoBuf, 0);
                int  size  = (int)(proto & 0xFFFFFFFFFFFFL);

                if (size <= 0)
                {
                    continue;
                }

                // Prepare buffer
                if (buf == null || size > buf.Length)
                {
                    // Corrupted data streams can result in a huge length.
                    // Do a sanity check here.
                    if (size > MAX_BUFFER_SIZE)
                    {
                        throw new AerospikeException("Invalid proto size: " + size);
                    }

                    int capacity = (size + 16383) & ~16383;                     // Round up in 16KB increments.
                    buf = new byte[capacity];
                }

                // Read remaining message bytes in group.
                conn.ReadFully(buf, size);
                conn.UpdateLastUsed();

                ulong type = (ulong)((proto >> 48) & 0xff);

                if (type == Command.AS_MSG_TYPE)
                {
                    dataBuffer  = buf;
                    dataOffset  = 0;
                    receiveSize = size;
                }
                else if (type == Command.MSG_TYPE_COMPRESSED)
                {
                    int usize = (int)ByteUtil.BytesToLong(buf, 0);

                    if (ubuf == null || usize > ubuf.Length)
                    {
                        if (usize > MAX_BUFFER_SIZE)
                        {
                            throw new AerospikeException("Invalid proto size: " + usize);
                        }

                        int capacity = (usize + 16383) & ~16383;                         // Round up in 16KB increments.
                        ubuf = new byte[capacity];
                    }

                    ByteUtil.Decompress(buf, 8, size, ubuf, usize);
                    dataBuffer  = ubuf;
                    dataOffset  = 8;
                    receiveSize = usize;
                }
                else
                {
                    throw new AerospikeException("Invalid proto type: " + type + " Expected: " + Command.AS_MSG_TYPE);
                }

                if (!ParseGroup(receiveSize))
                {
                    break;
                }
            }
        }