Esempio n. 1
0
        /// <summary>
        /// Parses the first 8 or 9 bytes from multiple buffers and returns a FrameHeader
        /// </summary>
        public static FrameHeader ParseResponseHeader(ProtocolVersion version, byte[] buffer1, byte[] buffer2)
        {
            var headerBuffer = new byte[!version.Uses2BytesStreamIds() ? 8 : 9];

            Buffer.BlockCopy(buffer1, 0, headerBuffer, 0, buffer1.Length);
            Buffer.BlockCopy(buffer2, 0, headerBuffer, buffer1.Length, headerBuffer.Length - buffer1.Length);
            return(ParseResponseHeader(version, headerBuffer, 0));
        }
Esempio n. 2
0
 /// <summary>
 /// Gets the size of the protocol header, depending on the version of the protocol
 /// </summary>
 /// <param name="version">Version of the protocol used</param>
 public static byte GetSize(ProtocolVersion version)
 {
     if (version.Uses2BytesStreamIds())
     {
         return(9);
     }
     return(8);
 }
Esempio n. 3
0
        public static int GetHeaderSize(this ProtocolVersion version)
        {
            if (version.Uses2BytesStreamIds())
            {
                return(9);
            }

            return(8);
        }
Esempio n. 4
0
        /// <summary>
        /// Writes the frame header, leaving body length to 0
        /// </summary>
        public void WriteFrameHeader(byte flags, short streamId, byte opCode)
        {
            byte[] header;
            if (!_version.Uses2BytesStreamIds())
            {
                //8 bytes for the header, dedicating 1 for the streamId
                if (streamId > 127)
                {
                    throw new ArgumentException("StreamId must be smaller than 128 under protocol version " + _version);
                }
                header = new byte[]
                {
                    (byte)_version,
                    flags,
                    (byte)streamId,
                    opCode,
                    //Reserved for the body length
                    0, 0, 0, 0
                };
                Write(header);
                return;
            }
            //9 bytes for the header, dedicating 2 for the streamId
            var streamIdBytes = BeConverter.GetBytes(streamId);

            header = new byte[]
            {
                (byte)_version,
                flags,
                streamIdBytes[0],
                streamIdBytes[1],
                opCode,
                //Reserved for the body length
                0, 0, 0, 0
            };
            Write(header);
        }
Esempio n. 5
0
 /// <summary>
 /// Creates a new instance of <see cref="PoolingOptions"/> using the default amount of connections
 /// and settings based on the protocol version.
 /// <para>
 /// For modern server versions (Apache Cassandra 2.1+) the amount of core connections are set to 1,
 /// setting 2 for max local connections.
 /// </para>
 /// </summary>
 /// <returns>A new instance of <see cref="PoolingOptions"/></returns>
 /// <seealso cref="ProtocolVersion"/>
 public static PoolingOptions Create(ProtocolVersion protocolVersion = ProtocolVersion.MaxSupported)
 {
     if (!protocolVersion.Uses2BytesStreamIds())
     {
         //New instance of pooling options with default values
         return(new PoolingOptions());
     }
     //New instance of pooling options with default values for high number of concurrent requests
     return(new PoolingOptions()
            .SetCoreConnectionsPerHost(HostDistance.Local, 1)
            .SetMaxConnectionsPerHost(HostDistance.Local, 2)
            .SetCoreConnectionsPerHost(HostDistance.Remote, 1)
            .SetMaxConnectionsPerHost(HostDistance.Remote, 1)
            .SetMaxSimultaneousRequestsPerConnectionTreshold(HostDistance.Local, 1500)
            .SetMaxSimultaneousRequestsPerConnectionTreshold(HostDistance.Remote, 1500));
 }
Esempio n. 6
0
        /// <summary>
        /// Parses the first 8 or 9 bytes and returns a FrameHeader
        /// </summary>
        public static FrameHeader ParseResponseHeader(ProtocolVersion version, byte[] buffer, int offset)
        {
            var header = new FrameHeader()
            {
                _versionByte = buffer[offset++],
                Flags        = (HeaderFlag)buffer[offset++]
            };

            if (!version.Uses2BytesStreamIds())
            {
                //Stream id is a signed byte in v1 and v2 of the protocol
                header.StreamId = (sbyte)buffer[offset++];
            }
            else
            {
                header.StreamId = BeConverter.ToInt16(buffer, offset);
                offset         += 2;
            }
            header.Opcode     = buffer[offset++];
            header.BodyLength = BeConverter.ToInt32(Utils.SliceBuffer(buffer, offset, 4));
            return(header);
        }
Esempio n. 7
0
        /// <summary>
        /// Gets a buffer containing 8 bytes for header and 4 bytes for the body.
        /// For result + void response message  (protocol v2)
        /// </summary>
        private static byte[] GetResultBuffer(short streamId, ProtocolVersion version = ProtocolVersion.V2)
        {
            var header = (byte)((int)version | 0x80);

            if (version.Uses2BytesStreamIds())
            {
                var bytes = BeConverter.GetBytes(streamId);
                return(new byte[]
                {
                    //header
                    header, 0, bytes[0], bytes[1], ResultResponse.OpCode, 0, 0, 0, 4,
                    //body
                    0, 0, 0, 1
                });
            }

            return(new byte[]
            {
                //header
                header, 0, (byte)streamId, ResultResponse.OpCode, 0, 0, 0, 4,
                //body
                0, 0, 0, 1
            });
        }