public DuplicateByteBuf(IByteBuf source) : base(source.MaxCapacity) { var dupe = source as DuplicateByteBuf; _buffer = dupe != null ? dupe._buffer : source; SetIndex(source.ReaderIndex, source.WriterIndex); }
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); }
/// <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; }
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(); }
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); } }
public SwappedByteBuffer(IByteBuf buf) { _buf = buf; if (buf.Order == ByteOrder.BigEndian) { Order = ByteOrder.LittleEndian; } else { Order = ByteOrder.BigEndian; } }
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(); }
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); }
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); }
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 }); }
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); } }
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); }
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); } }
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); }
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); }
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); }
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); }
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); }
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); }
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(); }
/// <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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }); } }); }
/// <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); }
/// <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(); } }
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); } }
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); }
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); }
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); }
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()); }
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(); } }
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(); }
public ReadCountdown2(CountdownEvent latch, IByteBuf expectedData1, IByteBuf expectedData2) { _latch = latch; _expectedData1 = expectedData1; _expectedData2 = expectedData2; }
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(); } }
protected NetworkState CreateNetworkState(Socket socket, INode remotehost, IByteBuf buffer, int bufferSize) { return new NetworkState(socket, remotehost, buffer, bufferSize); }
public override IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length) { throw new IndexOutOfRangeException(); }
public static string HexDump(IByteBuf buffer, int bytesPerLine = 16) { return HexDump(buffer.ToArray(), bytesPerLine); }
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; }
public virtual IByteBuf WriteBytes(IByteBuf src, int srcIndex, int length) { EnsureWritable(length); SetBytes(WriterIndex, src, srcIndex, length); WriterIndex += length; return this; }
public virtual IByteBuf GetBytes(int index, IByteBuf destination, int length) { GetBytes(index, destination, destination.WriterIndex, length); return this; }
public ReadCountdown3(CountdownEvent latch, IByteBuf expectedData) { _latch = latch; _expectedData = expectedData; }
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; }
public virtual IByteBuf ReadBytes(IByteBuf destination) { ReadBytes(destination, destination.WritableBytes); return this; }
public abstract IByteBuf SetBytes(int index, IByteBuf src, int srcIndex, int length);
public virtual IByteBuf ReadBytes(IByteBuf destination, int dstIndex, int length) { CheckReadableBytes(length); GetBytes(ReaderIndex, destination, dstIndex, length); ReaderIndex += length; return this; }
public virtual IByteBuf GetBytes(int index, IByteBuf destination) { GetBytes(index, destination, destination.WritableBytes); return this; }
public abstract IByteBuf GetBytes(int index, IByteBuf destination, int dstIndex, int length);
public virtual IByteBuf WriteBytes(IByteBuf src) { WriteBytes(src, src.ReadableBytes); return this; }
public virtual IByteBuf SetBytes(int index, IByteBuf src) { SetBytes(index, src, src.ReadableBytes); return this; }
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; }
public override IByteBuf GetBytes(int index, IByteBuf destination, int dstIndex, int length) { throw new IndexOutOfRangeException(); }
public NetworkState(Socket socket, INode remoteHost, IByteBuf buffer, int rawBufferLength) { Buffer = buffer; RemoteHost = remoteHost; Socket = socket; RawBuffer = new byte[rawBufferLength]; }