Ejemplo n.º 1
0
 public DuplicateByteBuf(IByteBuf source)
     : base(source.MaxCapacity)
 {
     var dupe = source as DuplicateByteBuf;
     _buffer = dupe != null ? dupe._buffer : source;
     SetIndex(source.ReaderIndex, source.WriterIndex);
 }
Ejemplo n.º 2
0
        public SlicedByteBuffer(IByteBuf buffer, int index, int length)
            : base(length)
        {
            if (index < 0 || index > buffer.Capacity - length)
            {
                throw new ArgumentOutOfRangeException(nameof(index), buffer + ".slice(" + index + ", " + length + ')');
            }

            var slicedByteBuf = buffer as SlicedByteBuffer;
            if (slicedByteBuf != null)
            {
                this._buffer = slicedByteBuf._buffer;
                _adjustment = slicedByteBuf._adjustment + index;
            }
            else if (buffer is DuplicateByteBuf)
            {
                this._buffer = buffer.Unwrap();
                _adjustment = index;
            }
            else
            {
                this._buffer = buffer;
                _adjustment = index;
            }
            this._length = length;

            SetWriterIndex(length);
        }
Ejemplo n.º 3
0
 /// <summary>
 ///     Expand the existing cumulative <see cref="IByteBuf" />.
 /// </summary>
 private static IByteBuf ExpandCumulation(IByteBufAllocator alloc, IByteBuf cumulation, int readable)
 {
     var old = cumulation;
     cumulation = alloc.Buffer(old.ReadableBytes + readable);
     cumulation.WriteBytes(old);
     old.Release();
     return cumulation;
 }
Ejemplo n.º 4
0
        public Property ChannelOutboundBuffer_must_always_correctly_report_writability_and_PendingBytes(
            IByteBuf[] writes)
        {
            var writeable = true;
            var buffer = new ChannelOutboundBuffer(TestChannel.Instance,
                () => { writeable = !writeable; // toggle writeability
                });

            foreach (var msg in writes)
            {
                var initialSize = buffer.TotalPendingWriteBytes;
                var nextSize = initialSize + msg.ReadableBytes;
                var currentBytesUntilUnwriteable = buffer.BytesBeforeUnwritable;
                var currentBytesUntilWriteable = buffer.BytesBeforeWritable;
                var currentWriteability = writeable;
                long nextBytesUntilWriteable, nextBytesUntilUnwriteable;
                bool nextWritability;
                if (writeable)
                {
                    if (msg.ReadableBytes < currentBytesUntilUnwriteable) // should stay writable
                    {
                        nextWritability = writeable;
                        nextBytesUntilUnwriteable = currentBytesUntilUnwriteable - msg.ReadableBytes;
                        nextBytesUntilWriteable = 0; // already writable
                    }
                    else // should become unwriteable
                    {
                        nextWritability = !writeable;
                        nextBytesUntilWriteable = nextSize - WriteLowWaterMark;
                        nextBytesUntilUnwriteable = 0;
                    }
                }
                else //already unwriteable
                {
                    nextWritability = writeable;
                    nextBytesUntilWriteable = nextSize - WriteLowWaterMark;
                    nextBytesUntilUnwriteable = 0;
                }

                buffer.AddMessage(msg, msg.ReadableBytes, TaskCompletionSource.Void);

                var satisfiesGrowthPrediction = nextWritability == writeable &&
                                                nextBytesUntilUnwriteable == buffer.BytesBeforeUnwritable &&
                                                nextBytesUntilWriteable == buffer.BytesBeforeWritable &&
                                                nextSize == buffer.TotalPendingWriteBytes;
                if (!satisfiesGrowthPrediction)
                    return
                        false.Label(
                            $"Buffer failed to meet growth prediction for initial buffer size {initialSize} with next write of size {msg.ReadableBytes}." +
                            $"TotalPendingWriteBytes(ex={nextSize}, " +
                            $"ac={buffer.TotalPendingWriteBytes}), " +
                            $"Writability(ex={nextWritability}, ac={writeable}), " +
                            $"BytesUntilUnwriteable(ex={nextBytesUntilUnwriteable}, ac={buffer.BytesBeforeUnwritable}), " +
                            $"BytesUntilWriteable(ex={nextBytesUntilWriteable}, ac={buffer.BytesBeforeWritable})");
            }

            return true.ToProperty();
        }
 protected override void Encode(IChannelHandlerContext context, IByteBuf message, List<object> output)
 {
     base.Encode(context, message, _temporaryOutput);
     var lengthFrame = (IByteBuf) _temporaryOutput[0];
     var combined = lengthFrame.WriteBytes(message);
     ReferenceCountUtil.SafeRelease(message, 1); // ready to release it - bytes have been copied
     output.Add(combined.Retain());
     _temporaryOutput.Clear();
 }
Ejemplo n.º 6
0
 public static NetworkData Create(INode node, IByteBuf buf)
 {
     var readableBytes = buf.ReadableBytes;
     return new NetworkData()
     {
         Buffer = buf.ToArray(),
         Length = readableBytes,
         RemoteHost = node
     };
 }
 public override void Decode(IConnection connection, IByteBuf buffer, out List<IByteBuf> decoded)
 {
     decoded = new List<IByteBuf>();
     var obj = Decode(connection, buffer);
     while (obj != null)
     {
         decoded.Add(obj);
         HeliosTrace.Instance.DecodeSucccess(1);
         obj = Decode(connection, buffer);
     } 
 }
Ejemplo n.º 8
0
 public SwappedByteBuffer(IByteBuf buf)
 {
     _buf = buf;
     if (buf.Order == ByteOrder.BigEndian)
     {
         Order = ByteOrder.LittleEndian;
     }
     else
     {
         Order = ByteOrder.BigEndian;
     }
 }
Ejemplo n.º 9
0
        public override void Encode(IConnection connection, IByteBuf buffer, out List <IByteBuf> encoded)
        {
            var length = buffer.ReadableBytes + _lengthAdjustment;

            if (_lengthIncludesLenghtFieldLength)
            {
                length += _lengthFieldLength;
            }

            encoded = new List <IByteBuf>();
            var sourceByteBuf = connection.Allocator.Buffer(_lengthFieldLength + length);

            if (length < 0)
            {
                throw new ArgumentException(string.Format("Adjusted frame length ({0}) is less than zero", length));
            }

            switch (_lengthFieldLength)
            {
            case 1:
                if (length >= 256)
                {
                    throw new ArgumentException("length of object does not fit into one byte: " + length);
                }
                sourceByteBuf.WriteByte(length);
                break;

            case 2:
                if (length >= 65536)
                {
                    throw new ArgumentException("length of object does not fit into a short integer: " + length);
                }
                sourceByteBuf.WriteShort((ushort)length);
                break;

            case 4:
                unchecked
                {
                    sourceByteBuf.WriteUnsignedInt((uint)length);
                }
                break;

            case 8:
                sourceByteBuf.WriteLong(length);
                break;

            default:
                throw new Exception("Unknown length field length");
            }
            sourceByteBuf.WriteBytes(buffer);
            encoded.Add(sourceByteBuf);
            HeliosTrace.Instance.EncodeSuccess();
        }
Ejemplo n.º 10
0
 public virtual IByteBuf ReadBytes(IByteBuf destination, int length)
 {
     if (length > destination.WritableBytes)
     {
         throw new IndexOutOfRangeException(
                   string.Format("length({0}) exceeds destination.WritableBytes({1}) where destination is: {2}",
                                 length, destination.WritableBytes, destination));
     }
     ReadBytes(destination, destination.WriterIndex, length);
     destination.SetWriterIndex(destination.WriterIndex + length);
     return(this);
 }
Ejemplo n.º 11
0
 public virtual IByteBuf WriteBytes(IByteBuf src, int length)
 {
     if (length > src.ReadableBytes)
     {
         throw new IndexOutOfRangeException(
                   string.Format("length({0}) exceeds src.readableBytes({1}) where src is: {2}", length,
                                 src.ReadableBytes, src));
     }
     WriteBytes(src, src.ReaderIndex, length);
     src.SetReaderIndex(src.ReaderIndex + length);
     return(this);
 }
Ejemplo n.º 12
0
        public static NetworkData Create(INode node, IByteBuf buf)
        {
            var readableBytes = buf.ReadableBytes;

            return(new NetworkData()
            {
                Buffer = buf.ToArray(),
                Length = readableBytes,
                RemoteHost = node,
                Recieved = DateTime.Now
            });
        }
Ejemplo n.º 13
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            if (message is IByteBuf)
            {
                var output = RecyclableArrayList.Take();
                try
                {
                    var data = (IByteBuf)message;
                    _first = _cumulation == null;
                    if (_first)
                    {
                        _cumulation = data;
                    }
                    else
                    {
                        _cumulation = _cumulator(context.Allocator, _cumulation, data);
                    }
                    CallDecode(context, _cumulation, output);
                }
                catch (DecoderException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new DecoderException(ex);
                }
                finally
                {
                    if (_cumulation != null && !_cumulation.IsReadable())
                    {
                        _numReads = 0;
                        _cumulation.Release();
                        _cumulation = null;
                    }
                    else if (++_numReads >= _discardAfterReads)
                    {
                        _numReads = 0;
                        DiscardSomeReadBytes();
                    }

                    var size = output.Count;
                    _decodeWasNull = size == 0;
                    FireChannelRead(context, output, size);
                    output.Return();
                }
            }
            else
            {
                // not a byte buffer? then we can't handle it. Forward it along
                context.FireChannelRead(message);
            }
        }
Ejemplo n.º 14
0
        protected SocketChannelAsyncOperation PrepareWriteOperation(IByteBuf buffer)
        {
            var operation = _writeOperation ?? (_writeOperation = new SocketChannelAsyncOperation(this, false));

            if (!buffer.HasArray)
            {
                throw new NotImplementedException(
                          "IByteBuffer implementations not backed by array are currently not supported.");
            }
            operation.SetBuffer(buffer.Array, buffer.ArrayOffset + buffer.WriterIndex, buffer.WritableBytes);
            return(operation);
        }
Ejemplo n.º 15
0
        public override void Decode(IConnection connection, IByteBuf buffer, out List <IByteBuf> decoded)
        {
            decoded = new List <IByteBuf>();
            var obj = Decode(connection, buffer);

            while (obj != null)
            {
                decoded.Add(obj);
                HeliosTrace.Instance.DecodeSucccess(1);
                obj = Decode(connection, buffer);
            }
        }
Ejemplo n.º 16
0
 public SwappedByteBuffer(IByteBuf buf)
 {
     _buf = buf;
     if (buf.Order == ByteOrder.BigEndian)
     {
         Order = ByteOrder.LittleEndian;
     }
     else
     {
         Order = ByteOrder.BigEndian;
     }
 }
Ejemplo n.º 17
0
        protected override void Decode(IChannelHandlerContext context, IByteBuf input, List<object> output)
        {
            _logger.Debug("Decoding {0} into Protobuf", input);

            var readable = input.ReadableBytes;
            var buf = new byte[readable];
            input.ReadBytes(buf);
            var bs = ByteString.CopyFrom(buf);
            var result = _extensions == null
                ? _prototype.WeakCreateBuilderForType().WeakMergeFrom(bs).WeakBuild()
                : _prototype.WeakCreateBuilderForType().WeakMergeFrom(bs, _extensions).WeakBuild();
            output.Add(result);
        }
Ejemplo n.º 18
0
 public override IByteBuf GetBytes(int index, IByteBuf destination, int dstIndex, int length)
 {
     CheckDstIndex(index, length, dstIndex, destination.WritableBytes);
     if (destination.HasArray)
     {
         GetBytes(index, destination.Array, destination.ArrayOffset + dstIndex, length);
     }
     else
     {
         destination.SetBytes(dstIndex, Array, index, length);
     }
     return(this);
 }
Ejemplo n.º 19
0
 public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length)
 {
     CheckSrcIndex(index, length, srcIndex, src.Capacity);
     if (_buffer.HasArray)
     {
         src.GetBytes(srcIndex, _buffer.InternalArray(), index, length);
     }
     else
     {
         src.GetBytes(srcIndex, this, index, length);
     }
     return(this);
 }
Ejemplo n.º 20
0
 public override IByteBuf GetBytes(int index, IByteBuf destination, int dstIndex, int length)
 {
     CheckDstIndex(index, length, dstIndex, destination.Capacity);
     if (destination.HasArray)
     {
         GetBytes(index, destination.InternalArray(), dstIndex, length);
     }
     else
     {
         destination.SetBytes(dstIndex, this, index, length);
     }
     return(this);
 }
Ejemplo n.º 21
0
 public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length)
 {
     CheckSrcIndex(index, length, srcIndex, src.ReadableBytes);
     if (src.HasArray)
     {
         Buffer.SetRange(index, src.InternalArray().Slice(srcIndex, length));
     }
     else
     {
         src.ReadBytes(Buffer, srcIndex, length);
     }
     return(this);
 }
Ejemplo n.º 22
0
 public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length)
 {
     CheckSrcIndex(index, length, srcIndex, src.Capacity);
     if (src.HasArray)
     {
         SetBytes(index, src.Array, src.ArrayOffset + srcIndex, length);
     }
     else
     {
         src.GetBytes(srcIndex, Array, index, length);
     }
     return(this);
 }
Ejemplo n.º 23
0
        public override void Encode(IConnection connection, IByteBuf buffer, out List <IByteBuf> encoded)
        {
            encoded = new List <IByteBuf>();

            var sourceByteBuf = connection.Allocator.Buffer(buffer.ReadableBytes + 3);

            sourceByteBuf.WriteByte(_mllpStartCharacter);
            sourceByteBuf.WriteBytes(buffer);
            sourceByteBuf.WriteByte(_mllpFirstEndCharacter);
            sourceByteBuf.WriteByte(_mllpLastEndCharacter);
            encoded.Add(sourceByteBuf);

            HeliosTrace.Instance.EncodeSuccess();
        }
Ejemplo n.º 24
0
        /// <summary>
        /// bytebuf分片,会更新当前的bytebuf的readindex
        /// </summary>
        /// <param name="length"></param>
        /// <param name="slice"></param>
        /// <returns></returns>
        public IByteBuf Slice(int length, IByteBuf slice = null)
        {
            var slice0 = slice != null ? slice : new SliceByteBuf(this, length);

            //这里需要注意顺序 capacity -> writeindex -> readindex
            slice0.SetCapacity(this.ReadIndex + length);
            slice0.SetWriteIndex(this.ReadIndex + length);
            slice0.SetReadIndex(this.ReadIndex);
            slice0.SetBytes(this.bytes);

            this.ReadSkip(length);

            return(slice0);
        }
Ejemplo n.º 25
0
        protected override void Decode(IChannelHandlerContext context, IByteBuf input, List <object> output)
        {
            _logger.Debug("Decoding {0} into Protobuf", input);

            var readable = input.ReadableBytes;
            var buf      = new byte[readable];

            input.ReadBytes(buf);
            var bs     = ByteString.CopyFrom(buf);
            var result = _extensions == null
                ? _prototype.WeakCreateBuilderForType().WeakMergeFrom(bs).WeakBuild()
                : _prototype.WeakCreateBuilderForType().WeakMergeFrom(bs, _extensions).WeakBuild();

            output.Add(result);
        }
Ejemplo n.º 26
0
 public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length)
 {
     CheckSrcIndex(index, length, srcIndex, src.ReadableBytes);
     if (src.HasArray)
     {
         InternalBuffer.SetRange(index, src.InternalArray().Slice(srcIndex, length));
     }
     else
     {
         var tempBuffer = new byte[length];
         src.ReadBytes(tempBuffer, srcIndex, length);
         InternalBuffer.SetRange(index, tempBuffer);
     }
     return(this);
 }
Ejemplo n.º 27
0
 public virtual IByteBuf SetBytes(int index, IByteBuf src, int length)
 {
     CheckIndex(index, length);
     if (src == null)
     {
         throw new NullReferenceException("src cannot be null");
     }
     if (length > src.ReadableBytes)
     {
         throw new IndexOutOfRangeException(string.Format(
                                                "length({0}) exceeds src.readableBytes({1}) where src is: {2}", length, src.ReadableBytes, src));
     }
     SetBytes(index, src, src.ReaderIndex, length);
     src.SetReaderIndex(src.ReaderIndex + length);
     return(this);
 }
Ejemplo n.º 28
0
 public override IByteBuf WriteBytes(IByteBuf src, int srcIndex, int length)
 {
     EnsureWritable(length);
     if (src.HasArray)
     {
         InternalBuffer.DirectBufferWrite(src.InternalArray().Slice(srcIndex, length));
         src.SetReaderIndex(src.ReaderIndex + length);
     }
     else
     {
         var tempBuffer = new byte[length];
         src.ReadBytes(tempBuffer, srcIndex, length);
         InternalBuffer.DirectBufferWrite(tempBuffer);
     }
     return(this);
 }
Ejemplo n.º 29
0
        protected override int DoReadBytes(IByteBuf buf)
        {
            if (!buf.HasArray)
            {
                throw new NotImplementedException("Only IByteBuffer implementations backed by array are supported.");
            }

            SocketError errorCode;
            int         received = 0;

            try
            {
                received = Socket.Receive(buf.Array, buf.ArrayOffset + buf.WriterIndex, buf.WritableBytes,
                                          SocketFlags.None, out errorCode);
            }
            catch (ObjectDisposedException)
            {
                errorCode = SocketError.Shutdown;
            }

            switch (errorCode)
            {
            case SocketError.Success:
                if (received == 0)
                {
                    return(-1);    // indicate that socket was closed
                }
                break;

            case SocketError.WouldBlock:
                if (received == 0)
                {
                    return(0);
                }
                break;

            case SocketError.Shutdown:
                return(-1);    // socket was closed

            default:
                throw new SocketException((int)errorCode);
            }

            buf.SetWriterIndex(buf.WriterIndex + received);

            return(received);
        }
Ejemplo n.º 30
0
        public string GetId(int idType, int count = 1)
        {
            if (echoHandler.channel == null)
            {
                InitSocket();
            }

            echoHandler.sb.Clear();
            IByteBuf buf = Unpooled.Buffer().WriteByte(idType).WriteInt(count);

            echoHandler.channel.WriteAndFlushAsync(buf);
            echoHandler.slim.WaitOne(msgTimeout);
            string data = echoHandler.sb.ToString();

            echoHandler.sb.Clear();
            return(data);
        }
Ejemplo n.º 31
0
 private void send(IChannelHandlerContext context, IByteBuf buf)
 {
     Task.Factory.StartNew(() =>
     {
         if (context.Channel.OutBoundBuffer.IsWritable)
         {
             context.WriteAsync(buf);
         }
         else
         {
             context.Channel.Invoker.NextTimeExecute(() =>
             {
                 send(context, buf);
             });
         }
     });
 }
Ejemplo n.º 32
0
        /// <summary>
        /// 异步写
        /// </summary>
        /// <param name="buf"></param>
        /// /// <param name="isblocking"></param>
        /// <returns></returns>
        public override Task WriteAsync(IByteBuf buf)
        {
            Ensure.IsNotNull(buf != null);

            var promise = new TaskCompletionSource();

            outBoundBuffer.Add(new PendingMessage(buf, promise));

            Interlocked.Increment(ref incompleteWriteMsgCounter);

            //建议不要`new WaitingMsg()`而是使用对象池
            //sendingQueue.Enqueue(new TrackingMessage(promise, buf));

            Flush();

            return(promise.Task);
        }
Ejemplo n.º 33
0
 /// <summary>
 /// 接收完成并处理数据
 /// </summary>
 /// <param name="args"></param>
 private void ProcessReceive(ChannelSocketAsyncEventArgs args)
 {
     try
     {
         if (args.BytesTransferred > 0)
         {
             IByteBuf buf = args.ByteBuf;
             buf.SetWriteIndex(args.BytesTransferred);
             invoker.fireOnChannelRead(buf, ExitAndTryReceiving);
         }
     }
     catch (Exception ex)
     {
         Trace.WriteLine("processReceive.Unpacking:" + ex.Message);
         Shutdown();
     }
 }
Ejemplo n.º 34
0
        protected void IncompleteWrite(bool scheduleAsync, IByteBuf buffer)
        {
            // Did not write completely.
            if (scheduleAsync)
            {
                var operation = PrepareWriteOperation(buffer);

                SetState(StateFlags.WriteScheduled);
                var pending = Socket.SendAsync(operation);
                if (!pending)
                {
                    ((ISocketChannelUnsafe)Unsafe).FinishWrite(operation);
                }
            }
            else
            {
                // Schedule flush again later so other tasks can be picked up input the meantime
                EventLoop.Execute(FlushAction, this);
            }
        }
Ejemplo n.º 35
0
        public void poolarena_alloc_all()
        {
            var chunk = new PoolChunk();
            var arena = new PoolArena(1);

            IByteBuf buf        = null;
            int      allocBytes = 0;

            do
            {
                buf = arena.Alloc(8192);
                if (buf.Handle != 0)
                {
                    allocBytes += 8192;
                    Assert.AreEqual(allocBytes, arena.Useables());
                }
            } while (buf != null && buf.Handle != 0);

            Assert.AreEqual(allocBytes, chunk.Capacity);
        }
Ejemplo n.º 36
0
        public override void HandlerRemoved(IChannelHandlerContext context)
        {
            var buf      = InternalBuffer;
            var readable = buf.ReadableBytes;

            if (readable > 0)
            {
                var bytes = buf.ReadBytes(readable);
                buf.Release();
                context.FireChannelRead(bytes);
            }
            else
            {
                buf.Release();
            }
            _cumulation = null;
            _numReads   = 0;
            context.FireChannelReadComplete();
            HandlerRemovedInternal(context);
        }
Ejemplo n.º 37
0
        private void AllocPage(IByteBuf buf, int newSize, int size)
        {
            int          idx;
            PoolPageList pageList;

            if (IsTiny(newSize))
            {
                idx      = (newSize >> 4) - 1;
                pageList = tinyPoolPages[idx];
            }
            else
            {
                idx      = IntEx.Log2(newSize >> 10);
                pageList = smallPoolPages[idx];
            }

            var page = pageList.GetNextAvailPage();

            if (page != null)
            {
                long handle = page.Alloc(newSize);
                if (handle > -1)
                {
                    page.Chunk.BytebufInit(buf, handle, newSize, size);
                    useables += newSize;
                }
                return;
            }

            //1,如果缓存中没有elemsize=size的page
            //2,如果从缓冲中分配失败
            //1和2都满足,那么会重新分配一个新的page
            page = AllocNewPage(buf, newSize, size);

            if (page == null)
            {
                return;
            }

            pageList.AddLast(page);
        }
Ejemplo n.º 38
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            //context.FireChannelRead(message);
            IByteBuf byteBuf = message as IByteBuf;
            var      _byte   = byteBuf.ReadBytes(byteBuf.ReadableBytes).Array;
            var      _pack   = ProtobufSerializer.DeserializeProtobuf <RatelMessagePack>(_byte);

            if (!YamlConfig.ServerConfSetting.Conf_Key_Verify(_pack.conf_key))
            {
                return;
            }

            var _obj = new List <object>();

            _obj.Add(context);
            if (_pack.Data != null)
            {
                _obj.Add(_pack.Data);
            }
            RatelProxy.ProxyFactory.CreateMethodProxy <ICommand>(_pack.command, _obj.ToArray());
        }
Ejemplo n.º 39
0
 private void HandleReadException(IChannelPipeline pipeline, IByteBuf byteBuf, Exception cause, bool close)
 {
     if (byteBuf != null)
     {
         if (byteBuf.IsReadable())
         {
             Channel.ReadPending = false;
             pipeline.FireChannelRead(byteBuf);
         }
         else
         {
             byteBuf.Release();
         }
     }
     pipeline.FireChannelReadComplete();
     pipeline.FireExceptionCaught(cause);
     if (close || cause is SocketException)
     {
         CloseOnRead();
     }
 }
Ejemplo n.º 40
0
        public override void Encode(IConnection connection, IByteBuf buffer, out List<IByteBuf> encoded)
        {
            var length = buffer.ReadableBytes + _lengthAdjustment;
            if (_lengthIncludesLenghtFieldLength)
            {
                length += _lengthFieldLength;
            }

            encoded = new List<IByteBuf>();
            var sourceByteBuf = connection.Allocator.Buffer(_lengthFieldLength + length);
            if (length < 0) throw new ArgumentException(string.Format("Adjusted frame length ({0}) is less than zero", length));

            switch (_lengthFieldLength)
            {

                case 1:
                    if (length >= 256) throw new ArgumentException("length of object does not fit into one byte: " + length);
                    sourceByteBuf.WriteByte(length);
                    break;
                case 2:
                    if (length >= 65536) throw new ArgumentException("length of object does not fit into a short integer: " + length);
                    sourceByteBuf.WriteShort((ushort)length);
                    break;
                case 4:
                    unchecked
                    {
                        sourceByteBuf.WriteUnsignedInt((uint)length);
                    }
                    break;
                case 8:
                    sourceByteBuf.WriteLong(length);
                    break;
                default:
                    throw new Exception("Unknown length field length");
            }
            sourceByteBuf.WriteBytes(buffer);
            encoded.Add(sourceByteBuf);
            HeliosTrace.Instance.EncodeSuccess();
        }
Ejemplo n.º 41
0
 public ReadCountdown2(CountdownEvent latch, IByteBuf expectedData1, IByteBuf expectedData2)
 {
     _latch = latch;
     _expectedData1 = expectedData1;
     _expectedData2 = expectedData2;
 }
Ejemplo n.º 42
0
        public static string DecodeString(IByteBuf src, int readerIndex, int len, Encoding encoding)
        {
            if (len == 0)
            {
                return string.Empty;
            }

            if (src.HasArray)
            {
                return encoding.GetString(src.Array, src.ArrayOffset + readerIndex, len);
            }
            var buffer = src.Allocator.Buffer(len);
            try
            {
                buffer.WriteBytes(src, readerIndex, len);
                return encoding.GetString(buffer.ToArray(), 0, len);
            }
            finally
            {
                // Release the temporary buffer again.
                buffer.Release();
            }
        }
Ejemplo n.º 43
0
 protected NetworkState CreateNetworkState(Socket socket, INode remotehost, IByteBuf buffer, int bufferSize)
 {
     return new NetworkState(socket, remotehost, buffer, bufferSize);
 }
Ejemplo n.º 44
0
 public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length)
 {
     throw new IndexOutOfRangeException();
 }
Ejemplo n.º 45
0
 public static string HexDump(IByteBuf buffer, int bytesPerLine = 16)
 {
     return HexDump(buffer.ToArray(), bytesPerLine);
 }
Ejemplo n.º 46
0
 public virtual IByteBuf WriteBytes(IByteBuf src, int length)
 {
     if (length > src.ReadableBytes)
         throw new IndexOutOfRangeException(string.Format("length({0}) exceeds src.readableBytes({1}) where src is: {2}", length, src.ReadableBytes, src));
     WriteBytes(src, src.ReaderIndex, length);
     src.SetReaderIndex(src.ReaderIndex + length);
     return this;
 }
Ejemplo n.º 47
0
 public virtual IByteBuf WriteBytes(IByteBuf src, int srcIndex, int length)
 {
     EnsureWritable(length);
     SetBytes(WriterIndex, src, srcIndex, length);
     WriterIndex += length;
     return this;
 }
Ejemplo n.º 48
0
 public virtual IByteBuf GetBytes(int index, IByteBuf destination, int length)
 {
     GetBytes(index, destination, destination.WriterIndex, length);
     return this;
 }
Ejemplo n.º 49
0
 public ReadCountdown3(CountdownEvent latch, IByteBuf expectedData)
 {
     _latch = latch;
     _expectedData = expectedData;
 }
Ejemplo n.º 50
0
 public virtual IByteBuf ReadBytes(IByteBuf destination, int length)
 {
     if(length > destination.WritableBytes) 
         throw new IndexOutOfRangeException(string.Format("length({0}) exceeds destination.WritableBytes({1}) where destination is: {2}", 
             length, destination.WritableBytes, destination));
     ReadBytes(destination, destination.WriterIndex, length);
     destination.SetWriterIndex(destination.WriterIndex + length);
     return this;
 }
Ejemplo n.º 51
0
 public virtual IByteBuf ReadBytes(IByteBuf destination)
 {
     ReadBytes(destination, destination.WritableBytes);
     return this;
 }
Ejemplo n.º 52
0
 public abstract IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length);
Ejemplo n.º 53
0
 public virtual IByteBuf ReadBytes(IByteBuf destination, int dstIndex, int length)
 {
     CheckReadableBytes(length);
     GetBytes(ReaderIndex, destination, dstIndex, length);
     ReaderIndex += length;
     return this;
 }
Ejemplo n.º 54
0
 public virtual IByteBuf GetBytes(int index, IByteBuf destination)
 {
     GetBytes(index, destination, destination.WritableBytes);
     return this;
 }
Ejemplo n.º 55
0
 public abstract IByteBuf GetBytes(int index, IByteBuf destination, int dstIndex, int length);
Ejemplo n.º 56
0
 public virtual IByteBuf WriteBytes(IByteBuf src)
 {
     WriteBytes(src, src.ReadableBytes);
     return this;
 }
Ejemplo n.º 57
0
 public virtual IByteBuf SetBytes(int index, IByteBuf src)
 {
     SetBytes(index, src, src.ReadableBytes);
     return this;
 }
Ejemplo n.º 58
0
 public virtual IByteBuf SetBytes(int index, IByteBuf src, int length)
 {
     CheckIndex(index, length);
     if(src == null) throw new NullReferenceException("src cannot be null");
     if (length > src.ReadableBytes) throw new IndexOutOfRangeException(string.Format(
              "length({0}) exceeds src.readableBytes({1}) where src is: {2}", length, src.ReadableBytes, src));
     SetBytes(index, src, src.ReaderIndex, length);
     src.SetReaderIndex(src.ReaderIndex + length);
     return this;
 }
Ejemplo n.º 59
0
 public override IByteBuf GetBytes(int index, IByteBuf destination, int dstIndex, int length)
 {
     throw new IndexOutOfRangeException();
 }
Ejemplo n.º 60
0
 public NetworkState(Socket socket, INode remoteHost, IByteBuf buffer, int rawBufferLength)
 {
     Buffer = buffer;
     RemoteHost = remoteHost;
     Socket = socket;
     RawBuffer = new byte[rawBufferLength];
 }