private ByteBuffer SerializeString(string value) { if (String.IsNullOrEmpty(value)) { return(ByteBuffer.Empty); } var buffer = allocator.Take(Utf8.GetMaxByteCount(value.Length)); var count = Utf8.GetBytes(value, 0, value.Length, buffer, 0); return(new ByteBuffer(allocator, buffer, count)); }
private int EnsureCapacity(int capacity) { if (currentIndex == chunks.Count - 1) { var nextSize = currentChunk.Length * ChunkGrowthFactor; // get a buffer at least the size of MinSize or the size of the remaining data if (nextSize < capacity) { nextSize = capacity; } if (nextSize < MinimumChunkSize) { nextSize = MinimumChunkSize; } currentChunk = allocator.Take(nextSize); chunks.Add(currentChunk); lengths.Add(lengths.LastOrDefault() + currentChunk.Length); currentIndex++; } else { currentChunk = chunks[++currentIndex]; } chunkPos = 0; return(currentChunk.Length); }
/// <summary> /// Returns true if further IO is pending. (ie. body must be read) /// </summary> /// <param name="header"></param> /// <param name="bodyLength"></param> /// <param name="extraLength"></param> /// <returns></returns> private bool ProcessHeader(byte[] header, out int bodyLength) { #if DEBUG if (header[Protocol.HEADER_INDEX_MAGIC] != Protocol.ResponseMagic) { throw new InvalidOperationException("Expected magic value " + Protocol.ResponseMagic + ", received: " + header[Protocol.HEADER_INDEX_MAGIC]); } #endif // TODO test if unsafe array gives a perf boost OpCode = header[Protocol.HEADER_INDEX_OPCODE]; KeyLength = NetworkOrderConverter.DecodeUInt16(header, Protocol.HEADER_INDEX_KEY); DataType = header[Protocol.HEADER_INDEX_DATATYPE]; StatusCode = NetworkOrderConverter.DecodeUInt16(header, Protocol.HEADER_INDEX_STATUS); CorrelationId = unchecked ((uint)NetworkOrderConverter.DecodeInt32(header, Protocol.HEADER_INDEX_OPAQUE)); CAS = NetworkOrderConverter.DecodeUInt64(header, Protocol.HEADER_INDEX_CAS); bodyLength = NetworkOrderConverter.DecodeInt32(header, Protocol.HEADER_INDEX_BODY); if (bodyLength > 0) { var extraLength = header[Protocol.HEADER_INDEX_EXTRA]; data = allocator.Take(bodyLength); Extra = new ArraySegment <byte>(data, 0, extraLength); Data = new ArraySegment <byte>(data, extraLength, bodyLength - extraLength); return(true); } return(false); }
internal BinaryResponse(IBufferAllocator allocator) { this.allocator = allocator; StatusCode = -1; header = allocator.Take(Protocol.HeaderLength); remainingHeader = Protocol.HeaderLength; }
public static ByteBuffer GetBytes(IBufferAllocator allocator, bool value) { var retval = new ByteBuffer(allocator, allocator.Take(1), 1); retval.Array[0] = value ? TRUE : FALSE; return(retval); }
public virtual Key Transform(string key) { var max = Encoding.UTF8.GetMaxByteCount(key.Length); var buffer = allocator.Take(max); var count = Encoding.UTF8.GetBytes(key, 0, key.Length, buffer, 0); return(new Key(allocator, buffer, count)); }
public PooledSegment(IBufferAllocator allocator, int count) { Require.NotNull(allocator, "allocator"); Require.That(count >= 0, "count must be >= 0"); this.allocator = allocator; this.array = allocator.Take(count); this.count = count; }
public Key(IBufferAllocator allocator, int length) : this() { Require.NotNull(allocator, "allocator"); Require.That(length >= 0, "length must be >= 0"); this.allocator = allocator; this.length = length; array = allocator.Take(length); }
public static Key EncodeKey(IBufferAllocator allocator, string key) { if (String.IsNullOrEmpty(key)) return Key.Empty; var max = Encoding.UTF8.GetMaxByteCount(key.Length); var buffer = allocator.Take(max); var count = Encoding.UTF8.GetBytes(key, 0, key.Length, buffer, 0); return new Key(allocator, buffer, count); }
public override Key Transform(string key) { using (var byteKey = base.Transform(key)) { var hash = Murmur32.ComputeHash(byteKey.Array, 0, byteKey.Length); var array = allocator.Take(4); NetworkOrderConverter.EncodeUInt32(hash, array, 0); return(new Key(allocator, array, 4)); } }
public static ByteBuffer GetBytes(IBufferAllocator allocator, short value) { var retval = new ByteBuffer(allocator, allocator.Take(2), 2); var ptr = retval.Array; { ptr[0] = (byte)value; ptr[1] = (byte)(value >> 8); } return(retval); }
private PooledSegment SerializeString(string value) { if (String.IsNullOrEmpty(value)) { return(PooledSegment.Empty); } var utf8 = Encoding.UTF8; var buffer = allocator.Take(utf8.GetMaxByteCount(value.Length)); var count = utf8.GetBytes(value, 0, value.Length, buffer, 0); return(new PooledSegment(allocator, buffer, count)); }
public static ByteBuffer AsByteBuffer(this ArraySegment <byte> segment, IBufferAllocator allocator = null) { if (segment.Offset == 0) { return(new ByteBuffer(null, segment.Array, segment.Count)); } var target = allocator == null ? new byte[segment.Count] : allocator.Take(segment.Count); Buffer.BlockCopy(segment.Array, segment.Offset, target, 0, segment.Count); return(new ByteBuffer(allocator, target, segment.Count)); }
public static Key EncodeKey(IBufferAllocator allocator, string key) { if (String.IsNullOrEmpty(key)) { return(Key.Empty); } var max = Encoding.UTF8.GetMaxByteCount(key.Length); var buffer = allocator.Take(max); var count = Encoding.UTF8.GetBytes(key, 0, key.Length, buffer, 0); return(new Key(allocator, buffer, count)); }
protected BinaryRequest(IBufferAllocator allocator, byte commandCode, byte extraLength) { this.allocator = allocator; Operation = commandCode; CorrelationId = unchecked((uint)Interlocked.Increment(ref InstanceCounter)); // request id // prealloc header so that the extra data can be placed into the same buffer headerLength = Protocol.HeaderLength + extraLength; header = allocator.Take(headerLength); if (extraLength > 0) Extra = new ArraySegment<byte>(header, Protocol.HeaderLength, extraLength); }
public static ByteBuffer GetBytes(IBufferAllocator allocator, int value) { var retval = new ByteBuffer(allocator, allocator.Take(4), 4); var ptr = retval.Array; { ptr[0] = (byte)value; ptr[1] = (byte)(value >> 8); ptr[2] = (byte)(value >> 16); ptr[3] = (byte)(value >> 24); } return(retval); }
protected BinaryRequest(IBufferAllocator allocator, byte commandCode, byte extraLength) { this.allocator = allocator; Operation = commandCode; CorrelationId = unchecked ((uint)Interlocked.Increment(ref InstanceCounter)); // request id // prealloc header so that the extra data can be placed into the same buffer headerLength = Protocol.HeaderLength + extraLength; header = allocator.Take(headerLength); if (extraLength > 0) { Extra = new ArraySegment <byte>(header, Protocol.HeaderLength, extraLength); } }
protected BinaryRequest(IBufferAllocator allocator, byte operation, byte extraLength) { this.allocator = allocator; Operation = operation; CorrelationId = (uint)Interlocked.Increment(ref InstanceCounter); // request id headerLength = Protocol.HeaderLength + extraLength; if (extraLength > 0) { // prealloc header so that the extra data can be placed into the same buffer // (extra is placed after header in the output buffer) // only alloc header here if Extra > 0, otherwise do it in WriteTo // to reduce the time we own the buffer for header = allocator.Take(headerLength); Extra = new ArraySegment <byte>(header, Protocol.HeaderLength, extraLength); } }
public static ByteBuffer GetBytes(IBufferAllocator allocator, long value) { var retval = new ByteBuffer(allocator, allocator.Take(8), 8); var ptr = retval.Array; { ptr[0] = (byte)value; ptr[1] = (byte)(value >> 8); ptr[2] = (byte)(value >> 16); ptr[3] = (byte)(value >> 24); ptr[4] = (byte)(value >> 32); ptr[5] = (byte)(value >> 40); ptr[6] = (byte)(value >> 48); ptr[7] = (byte)(value >> 56); } return(retval); }
public static ByteBuffer GetBytes(IBufferAllocator allocator, decimal value) { // should use 'internal static void GetBytes(decimal d, byte[] buffer)' int v; var tmp = Decimal.GetBits(value); var retval = new ByteBuffer(allocator, allocator.Take(16), 16); const int I_0 = 0; const int I_1 = 4; const int I_2 = 8; const int I_3 = 12; var ptr = retval.Array; { v = tmp[0]; ptr[0 + I_0] = (byte)v; ptr[1 + I_0] = (byte)(v >> 8); ptr[2 + I_0] = (byte)(v >> 16); ptr[3 + I_0] = (byte)(v >> 24); v = tmp[1]; ptr[0 + I_1] = (byte)v; ptr[1 + I_1] = (byte)(v >> 8); ptr[2 + I_1] = (byte)(v >> 16); ptr[3 + I_1] = (byte)(v >> 24); v = tmp[2]; ptr[0 + I_2] = (byte)v; ptr[1 + I_2] = (byte)(v >> 8); ptr[2 + I_2] = (byte)(v >> 16); ptr[3 + I_2] = (byte)(v >> 24); v = tmp[3]; ptr[0 + I_3] = (byte)v; ptr[1 + I_3] = (byte)(v >> 8); ptr[2 + I_3] = (byte)(v >> 16); ptr[3 + I_3] = (byte)(v >> 24); } return(retval); }
public static ByteBuffer Allocate(IBufferAllocator allocator, int length) { return(new ByteBuffer(allocator, allocator.Take(length), length)); }
public bool WriteTo(WriteBuffer buffer) { if (header == null) { Debug.Assert(headerLength == Protocol.HeaderLength); header = allocator.Take(headerLength); } // 0. init header // 1. loop on header // 2. loop on Key, if any // 3. loop on Data, if any // 4. done switch (state) { case STATE_INITIAL: PrepareHeader(); state = STATE_WRITE_HEADER; goto case STATE_WRITE_HEADER; case STATE_WRITE_HEADER: writeOffset += buffer.Append(header, writeOffset, headerLength - writeOffset); if (writeOffset < headerLength) { return(true); } if (Key.Length > 0) { writeOffset = 0; state = STATE_WRITE_KEY; goto case STATE_WRITE_KEY; } goto case STATE_PREPARE_BODY; case STATE_WRITE_KEY: writeOffset += buffer.Append(Key.Array, writeOffset, Key.Length - writeOffset); if (writeOffset < Key.Length) { return(true); } goto case STATE_PREPARE_BODY; case STATE_PREPARE_BODY: if (Data.Count > 0) { writeOffset = 0; state = STATE_WRITE_BODY; goto case STATE_WRITE_BODY; } break; case STATE_WRITE_BODY: writeOffset += buffer.Append(Data.Array, Data.Offset + writeOffset, Data.Count - writeOffset); if (writeOffset < Data.Count) { return(true); } break; } state = STATE_DONE; return(false); }