public async Task RunAsync(X509Certificate2 certificate, int threadCount, CancellationToken cancellationToken) { Contract.Requires(certificate != null); Contract.Requires(threadCount > 0); try { BootstrapperEventSource.Log.Info("Starting", null); PerformanceCounters.ConnectionsEstablishedTotal.RawValue = 0; PerformanceCounters.ConnectionsCurrent.RawValue = 0; this.tlsCertificate = certificate; this.parentEventLoopGroup = new MultithreadEventLoopGroup(1); this.eventLoopGroup = new MultithreadEventLoopGroup(threadCount); this.bufferAllocator = new PooledByteBufferAllocator(16 * 1024, 300 * 1024 * 1024 / threadCount); // reserve up to 300 MB of 16 KB buffers ServerBootstrap bootstrap = this.SetupBootstrap(); BootstrapperEventSource.Log.Info(string.Format("Initializing TLS endpoint on port {0} with certificate {1}.", MqttsPort, this.tlsCertificate.Thumbprint), null); this.serverChannel = await bootstrap.BindAsync(IPAddress.Any, MqttsPort); cancellationToken.Register(this.CloseAsync); BootstrapperEventSource.Log.Info("Started", null); } catch (Exception ex) { BootstrapperEventSource.Log.Error("Failed to start", ex); this.CloseAsync(); } }
PooledByteBuffer(ThreadLocalPool.Handle returnHandle, IByteBufferAllocator allocator, byte[] pooledArray, int maxCapacity) : base(allocator, pooledArray, 0, 0, maxCapacity) { this.length = pooledArray.Length; this.returnHandle = returnHandle; this.pooledArray = pooledArray; }
EmptyByteBuffer(IByteBufferAllocator allocator, ByteOrder order) { Contract.Requires(allocator != null); this.allocator = allocator; this.order = order; this.str = this.GetType().Name + (order == ByteOrder.BigEndian ? "BE" : "LE"); }
IotHubClient(DeviceClient deviceClient, string deviceId, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter) { this.deviceClient = deviceClient; this.deviceId = deviceId; this.settings = settings; this.allocator = allocator; this.messageAddressConverter = messageAddressConverter; }
public CompositeByteBuffer(IByteBufferAllocator allocator, int maxNumComponents) : base(int.MaxValue) { Contract.Requires(allocator != null); Contract.Requires(maxNumComponents >= 2); this.allocator = allocator; this.maxNumComponents = maxNumComponents; this.leak = LeakDetector.Open(this); }
public UnpooledHeapByteBuffer( IByteBufferAllocator allocator, byte[] initialArray, int readerIndex, int writerIndex, int maxCapacity) : base(maxCapacity) { Contract.Requires(allocator != null); Contract.Requires(initialArray != null); Contract.Requires(initialArray.Length <= maxCapacity); this.allocator = allocator; this.SetArray(initialArray); this.SetIndex(readerIndex, writerIndex); }
public CompositeByteBuffer( IByteBufferAllocator allocator, int maxNumComponents, IEnumerable<IByteBuffer> buffers) : base(int.MaxValue) { Contract.Requires(allocator != null); Contract.Requires(maxNumComponents >= 2); this.allocator = allocator; this.maxNumComponents = maxNumComponents; this.AddComponents0(0, buffers); this.ConsolidateIfNeeded(); this.SetIndex(0, this.Capacity); this.leak = LeakDetector.Open(this); }
public static Func<IDeviceIdentity, Task<IMessagingServiceClient>> PreparePoolFactory(string baseConnectionString, int connectionPoolSize, TimeSpan? connectionIdleTimeout, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter) { IotHubConnectionStringBuilder csb = IotHubConnectionStringBuilder.Create(baseConnectionString); Func<IDeviceIdentity, Task<IMessagingServiceClient>> mqttCommunicatorFactory = deviceIdentity => { var identity = (IotHubDeviceIdentity)deviceIdentity; csb.AuthenticationMethod = DeriveAuthenticationMethod(csb.AuthenticationMethod, identity); csb.HostName = identity.IotHubHostName; string connectionString = csb.ToString(); return CreateFromConnectionStringAsync(identity.Id, connectionString, connectionPoolSize, connectionIdleTimeout, settings, allocator, messageAddressConverter); }; return mqttCommunicatorFactory; }
public void PooledHeapBuffer() { IByteBufferAllocator allocator = this.NewAllocator(); IByteBuffer heapBuffer = allocator.HeapBuffer(); try { Assert.IsAssignableFrom <PooledHeapByteBuffer>(heapBuffer); } finally { heapBuffer.Release(); } }
void WriteArrayMessage(IByteBufferAllocator allocator, IArrayRedisMessage msg, List <object> output) { if (msg.IsNull) { this.WriteArrayHeader(allocator, msg.IsNull, RedisConstants.NullValue, output); } else { this.WriteArrayHeader(allocator, msg.IsNull, msg.Children.Count, output); foreach (IRedisMessage child in msg.Children) { this.WriteRedisMessage(allocator, child, output); } } }
public static void DoEncode(IByteBufferAllocator bufferAllocator, Packet packet, List <object> output) { IByteBuffer buffer = bufferAllocator.Buffer(); try { packet.Encode(buffer); output.Add(buffer); buffer = null; } finally { buffer?.SafeRelease(); } }
// Allocate callback from libuv thread uv_buf_t INativeUnsafe.PrepareRead(ReadOperation readOperation) { Debug.Assert(readOperation is object); var ch = _channel; IChannelConfiguration config = ch.Configuration; IByteBufferAllocator allocator = config.Allocator; IRecvByteBufAllocatorHandle allocHandle = RecvBufAllocHandle; IByteBuffer buffer = allocHandle.Allocate(allocator); allocHandle.AttemptedBytesRead = buffer.WritableBytes; return(readOperation.GetBuffer(buffer)); }
static void EncodePublishMessage <TMessage>(IByteBufferAllocator bufferAllocator, MessagePacket <TMessage> packet, List <object> output) where TMessage : IMessage { byte[] SubjectNameBytes = EncodeStringInUtf8(packet.Subject); byte[] ReplyToBytes = EncodeStringInUtf8(packet.ReplyTo); int variablePartSize = SubjectNameBytes.Length + SPACES_BYTES.Length; variablePartSize += (ReplyToBytes.Length > 0 ? ReplyToBytes.Length + SPACES_BYTES.Length : 0); IByteBuffer Payload = Unpooled.WrappedBuffer(packet.Message.ToByteArray()); byte[] PayloadSize = EncodeStringInUtf8(Payload.ReadableBytes.ToString()); variablePartSize += PayloadSize.Length + CRLF_BYTES.Length; variablePartSize += Payload.ReadableBytes + CRLF_BYTES.Length; int fixedHeaderBufferSize = PUB_BYTES.Length + SPACES_BYTES.Length; IByteBuffer buf = null; try { buf = bufferAllocator.Buffer(fixedHeaderBufferSize + variablePartSize); buf.WriteBytes(PUB_BYTES); buf.WriteBytes(SPACES_BYTES); buf.WriteBytes(SubjectNameBytes); buf.WriteBytes(SPACES_BYTES); if (!string.IsNullOrEmpty(packet.ReplyTo)) { buf.WriteBytes(ReplyToBytes); buf.WriteBytes(SPACES_BYTES); } buf.WriteBytes(PayloadSize); buf.WriteBytes(CRLF_BYTES); buf.WriteBytes(Payload); buf.WriteBytes(CRLF_BYTES); output.Add(buf); buf = null; } finally { buf?.SafeRelease(); } }
public MessageFrame ToMessage(IByteBufferAllocator alloc) { MessageFrameBuilder bldr = new MessageFrameBuilder(alloc, 60, FrameType.VariableShort); Position basePos = _lastKnownRegion; bldr.Put(MessageType.Byte, _update.GetLocalY(basePos)); bldr.Put(MessageType.Byte, DataTransformation.Negate, _update.GetLocalX(basePos)); foreach (RegionUpdateMessage update in _messages) { var frame = update.ToMessage(alloc); bldr.Put(MessageType.Byte, frame.Id); bldr.PutBytes(frame.Payload); } return(bldr.ToMessageFrame()); }
protected internal UnpooledHeapByteBuffer(IByteBufferAllocator alloc, int initialCapacity, int maxCapacity) : base(maxCapacity) { if (alloc is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.alloc); } if (initialCapacity > maxCapacity) { ThrowHelper.ThrowArgumentException_InitialCapacityMaxCapacity(initialCapacity, maxCapacity); } _allocator = alloc; SetArray(NewArray(initialCapacity)); SetIndex0(0, 0); }
void Write(IByteBufferAllocator allocator, ArrayRedisMessage message, ICollection <object> output) { Contract.Requires(allocator != null); Contract.Requires(message != null); Contract.Requires(output != null); this.WriteArrayHeader(allocator, message.IsNull ? default(long?) : message.Children.Count, output); if (message.IsNull) { return; } foreach (IRedisMessage childMessage in message.Children) { this.Write(allocator, childMessage, output); } }
static void EncodeSubAckMessage(IByteBufferAllocator bufferAllocator, SubAckPacket message, List <object> output) { int payloadBufferSize = message.ReturnCodes.Count; int variablePartSize = PacketIdLength + payloadBufferSize; int fixedHeaderBufferSize = 1 + MaxVariableLength; IByteBuffer buf = bufferAllocator.Buffer(fixedHeaderBufferSize + variablePartSize); buf.WriteByte(CalculateFirstByteOfFixedHeader(message)); WriteVariableLengthInt(buf, variablePartSize); buf.WriteShort(message.PacketId); foreach (QualityOfService qos in message.ReturnCodes) { buf.WriteByte((byte)qos); } output.Add(buf); }
protected static void EncodePublishMessage(IByteBufferAllocator bufferAllocator, PublishPacket packet, List <object> output) { byte[] SubjectNameBytes = EncodeStringInUtf8(packet.Subject); byte[] ReplyToBytes = EncodeStringInUtf8(packet.ReplyTo); int variablePartSize = SubjectNameBytes.Length + SPACES_BYTES.Length; variablePartSize += (ReplyToBytes.Length > 0 ? ReplyToBytes.Length + SPACES_BYTES.Length : 0); byte[] PayloadSize = EncodeStringInUtf8(packet.PayloadLength.ToString()); variablePartSize += PayloadSize.Length + CRLF_BYTES.Length; variablePartSize += packet.PayloadLength + CRLF_BYTES.Length; int fixedHeaderBufferSize = PUB_BYTES.Length + SPACES_BYTES.Length; IByteBuffer buf = null; try { buf = bufferAllocator.Buffer(fixedHeaderBufferSize + variablePartSize); buf.WriteBytes(PUB_BYTES); buf.WriteBytes(SPACES_BYTES); buf.WriteBytes(SubjectNameBytes); buf.WriteBytes(SPACES_BYTES); if (!string.IsNullOrEmpty(packet.ReplyTo)) { buf.WriteBytes(ReplyToBytes); buf.WriteBytes(SPACES_BYTES); } buf.WriteBytes(PayloadSize); buf.WriteBytes(CRLF_BYTES); if (packet.Payload != null) { buf.WriteBytes(packet.Payload); } buf.WriteBytes(CRLF_BYTES); output.Add(buf); buf = null; } finally { buf?.SafeRelease(); } }
static void EncodeSubscribeMessage(IByteBufferAllocator bufferAllocator, SubscribePacket packet, List <object> output) { const int VariableHeaderSize = PacketIdLength; int payloadBufferSize = 0; ThreadLocalObjectList encodedTopicFilters = ThreadLocalObjectList.NewInstance(); IByteBuffer buf = null; try { foreach (SubscriptionRequest topic in packet.Requests) { byte[] topicFilterBytes = EncodeStringInUtf8(topic.TopicFilter); payloadBufferSize += StringSizeLength + topicFilterBytes.Length + 1; // length, value, QoS encodedTopicFilters.Add(topicFilterBytes); } int variablePartSize = VariableHeaderSize + payloadBufferSize; int fixedHeaderBufferSize = 1 + MaxVariableLength; buf = bufferAllocator.Buffer(fixedHeaderBufferSize + variablePartSize); buf.WriteByte(CalculateFirstByteOfFixedHeader(packet)); WriteVariableLengthInt(buf, variablePartSize); // Variable Header buf.WriteShort(packet.PacketId); // todo: review: validate? // Payload for (int i = 0; i < encodedTopicFilters.Count; i++) { var topicFilterBytes = (byte[])encodedTopicFilters[i]; buf.WriteShort(topicFilterBytes.Length); buf.WriteBytes(topicFilterBytes, 0, topicFilterBytes.Length); buf.WriteByte((int)packet.Requests[i].QualityOfService); } output.Add(buf); buf = null; } finally { buf?.SafeRelease(); encodedTopicFilters.Return(); } }
static void WriteFrameMessage(IByteBufferAllocator allocator, FrameThriftMessage msg, List <object> output) { IByteBuffer buf = null; try { var len = 4 + msg.Content.ReadableBytes; buf = allocator.Buffer(len); buf.WriteInt(msg.Content.ReadableBytes); buf.WriteBytes(msg.Content); output.Add(buf.Copy(0, len)); } finally { buf?.Release(); } }
void WriteBulkStringHeader(IByteBufferAllocator allocator, BulkStringHeaderRedisMessage msg, List <object> output) { IByteBuffer buf = allocator.Buffer(RedisConstants.TypeLength + (msg.IsNull ? RedisConstants.NullLength : RedisConstants.LongMaxLength + RedisConstants.EndOfLineLength)); RedisMessageType.BulkString.WriteTo(buf); if (msg.IsNull) { buf.WriteShort(RedisConstants.NullShort); } else { buf.WriteBytes(this.NumberToBytes(msg.BulkStringLength)); buf.WriteShort(RedisConstants.EndOfLineShort); } output.Add(buf); }
/// <summary> /// This is the main encoding method. /// It's only visible for testing. /// @param bufferAllocator Allocates ByteBuf /// @param packet MQTT packet to encode /// @return ByteBuf with encoded bytes /// </summary> internal static void DoEncode(IByteBufferAllocator bufferAllocator, Packet packet, List <object> output) { switch (packet.PacketType) { case PacketType.CONNECT: EncodeConnectMessage(bufferAllocator, (ConnectPacket)packet, output); break; case PacketType.CONNACK: EncodeConnAckMessage(bufferAllocator, (ConnAckPacket)packet, output); break; case PacketType.PUBLISH: EncodePublishMessage(bufferAllocator, (PublishPacket)packet, output); break; case PacketType.PUBACK: case PacketType.PUBREC: case PacketType.PUBREL: case PacketType.PUBCOMP: case PacketType.UNSUBACK: EncodePacketWithIdOnly(bufferAllocator, (PacketWithId)packet, output); break; case PacketType.SUBSCRIBE: EncodeSubscribeMessage(bufferAllocator, (SubscribePacket)packet, output); break; case PacketType.SUBACK: EncodeSubAckMessage(bufferAllocator, (SubAckPacket)packet, output); break; case PacketType.UNSUBSCRIBE: EncodeUnsubscribeMessage(bufferAllocator, (UnsubscribePacket)packet, output); break; case PacketType.PINGREQ: case PacketType.PINGRESP: case PacketType.DISCONNECT: EncodePacketWithFixedHeaderOnly(bufferAllocator, packet, output); break; default: throw new ArgumentException("Unknown packet type: " + packet.PacketType, nameof(packet)); } }
protected UnpooledUnsafeDirectByteBuffer(IByteBufferAllocator alloc, byte[] initialBuffer, int maxCapacity, bool doFree) : base(maxCapacity) { Contract.Requires(alloc != null); Contract.Requires(initialBuffer != null); int initialCapacity = initialBuffer.Length; if (initialCapacity > maxCapacity) { throw new ArgumentException($"initialCapacity({initialCapacity}) > maxCapacity({maxCapacity})"); } this.allocator = alloc; this.doNotFree = !doFree; this.SetByteBuffer(initialBuffer, false); }
static void EncodeUnsubscribeMessage(IByteBufferAllocator bufferAllocator, UnsubscribePacket packet, List <Object> output) { const Int32 VariableHeaderSize = 2; var payloadBufferSize = 0; ThreadLocalObjectList encodedTopicFilters = ThreadLocalObjectList.NewInstance(); Packet buf = null; try { foreach (var topic in packet.TopicFilters) { var topicFilterBytes = EncodeStringInUtf8(topic); payloadBufferSize += StringSizeLength + topicFilterBytes.Length; // length, value encodedTopicFilters.Add(topicFilterBytes); } var variablePartSize = VariableHeaderSize + payloadBufferSize; var fixedHeaderBufferSize = 1 + MaxVariableLength; buf = bufferAllocator.Buffer(fixedHeaderBufferSize + variablePartSize); buf.WriteByte(CalculateFirstByteOfFixedHeader(packet)); WriteVariableLengthInt(buf, variablePartSize); // Variable Header buf.WriteShort(packet.PacketId); // todo: review: validate? // Payload for (var i = 0; i < encodedTopicFilters.Count; i++) { var topicFilterBytes = (Byte[])encodedTopicFilters[i]; buf.WriteShort(topicFilterBytes.Length); buf.WriteBytes(topicFilterBytes, 0, topicFilterBytes.Length); } output.Add(buf); buf = null; } finally { buf?.SafeRelease(); encodedTopicFilters.Return(); } }
static void EncodeConnAckMessage(IByteBufferAllocator bufferAllocator, ConnAckPacket message, List <object> output) { IByteBuffer buffer = bufferAllocator.Buffer(4); buffer.WriteByte(CalculateFirstByteOfFixedHeader(message)); buffer.WriteByte(2); // remaining length if (message.SessionPresent) { buffer.WriteByte(1); // 7 reserved 0-bits and SP = 1 } else { buffer.WriteByte(0); // 7 reserved 0-bits and SP = 0 } buffer.WriteByte((byte)message.ReturnCode); output.Add(buffer); }
public JT808SimpleTcpClient(EndPoint remoteAddress, EndPoint localAddress) { clientBufferAllocator = new PooledByteBufferAllocator(); clientGroup = new MultithreadEventLoopGroup(1); cb = new Bootstrap() .Group(clientGroup) .Channel <TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Option(ChannelOption.Allocator, clientBufferAllocator) .Handler(new ActionChannelInitializer <TcpSocketChannel>(channel => { channel.Pipeline.AddLast("jt808Buffer", new DelimiterBasedFrameDecoder(int.MaxValue, Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); channel.Pipeline.AddLast("jt808Decode", new JT808ClientDecoder()); })); clientChannel = cb.ConnectAsync(remoteAddress, localAddress).Result; }
static void EncodePongMessage(IByteBufferAllocator bufferAllocator, PongPacket packet, List <object> output) { IByteBuffer buf = null; try { buf = bufferAllocator.Buffer(PONG_BYTES.Length + CRLF_BYTES.Length); buf.WriteBytes(PONG_BYTES); buf.WriteBytes(CRLF_BYTES); output.Add(buf); buf = null; } finally { buf?.SafeRelease(); } }
public IHttpContent ReadChunk(IByteBufferAllocator allocator) { if (this.input.IsEndOfInput) { if (this.sentLastChunk) { return(null); } // Send last chunk for this input this.sentLastChunk = true; return(this.lastHttpContent); } else { IByteBuffer buf = this.input.ReadChunk(allocator); return(buf == null ? null : new DefaultHttpContent(buf)); } }
static void EncodePacketWithFixedHeaderOnly(IByteBufferAllocator bufferAllocator, Packet packet, List <object> output) { IByteBuffer buffer = null; try { buffer = bufferAllocator.Buffer(2); buffer.WriteByte(CalculateFirstByteOfFixedHeader(packet)); buffer.WriteByte(0); output.Add(buffer); buffer = null; } finally { buffer?.SafeRelease(); } }
protected override IByteBuffer ComposeFirst(IByteBufferAllocator allocator, IByteBuffer first) { if (first is CompositeByteBuffer composite) { first = allocator.DirectBuffer(composite.ReadableBytes); try { first.WriteBytes(composite); } catch (Exception cause) { first.Release(); ExceptionDispatchInfo.Capture(cause).Throw(); } composite.Release(); } return(first); }
protected override IByteBuffer Compose(IByteBufferAllocator alloc, IByteBuffer cumulation, IByteBuffer next) { int wrapDataSize = _owner.v_wrapDataSize; if (cumulation is CompositeByteBuffer composite) { int numComponents = composite.NumComponents; if (0u >= (uint)numComponents || !AttemptCopyToCumulation(composite.InternalComponent(numComponents - 1), next, wrapDataSize)) { composite.AddComponent(true, next); } return(composite); } return(AttemptCopyToCumulation(cumulation, next, wrapDataSize) ? cumulation : CopyAndCompose(alloc, cumulation, next)); }
/// <summary> /// Compose <paramref name="cumulation"/> and <paramref name="next"/> into a new <see cref="IByteBufferAllocator.Buffer(int)"/>. /// </summary> /// <param name="alloc">The allocator to use to allocate the new buffer.</param> /// <param name="cumulation">The current cumulation.</param> /// <param name="next">The next buffer.</param> /// <returns>The result of <code>cumulation + next</code>.</returns> protected IByteBuffer CopyAndCompose(IByteBufferAllocator alloc, IByteBuffer cumulation, IByteBuffer next) { var newCumulation = alloc.Buffer(cumulation.ReadableBytes + next.ReadableBytes); try { _ = newCumulation.WriteBytes(cumulation).WriteBytes(next); } catch (Exception) { _ = newCumulation.Release(); ReferenceCountUtil.SafeRelease(next); throw; } _ = cumulation.Release(); _ = next.Release(); return(newCumulation); }
static void EncodeUnsubscribeMessage(IByteBufferAllocator bufferAllocator, UnsubscribePacket packet, List <object> output) { //const int VariableHeaderSize = 2; //int payloadBufferSize = 0; //ThreadLocalObjectList encodedTopicFilters = ThreadLocalObjectList.NewInstance(); //IByteBuffer buf = null; //try //{ // foreach (string topic in packet.TopicFilters) // { // byte[] topicFilterBytes = EncodeStringInUtf8(topic); // payloadBufferSize += StringSizeLength + topicFilterBytes.Length; // length, value // encodedTopicFilters.Add(topicFilterBytes); // } // int variablePartSize = VariableHeaderSize + payloadBufferSize; // int fixedHeaderBufferSize = 1 + MaxVariableLength; // buf = bufferAllocator.Buffer(fixedHeaderBufferSize + variablePartSize); // buf.WriteByte(CalculateFirstByteOfFixedHeader(packet)); // WriteVariableLengthInt(buf, variablePartSize); // // Variable Header // buf.WriteShort(packet.PacketId); // todo: review: validate? // // Payload // for (int i = 0; i < encodedTopicFilters.Count; i++) // { // var topicFilterBytes = (byte[])encodedTopicFilters[i]; // buf.WriteShort(topicFilterBytes.Length); // buf.WriteBytes(topicFilterBytes, 0, topicFilterBytes.Length); // } // output.Add(buf); // buf = null; //} //finally //{ // buf?.SafeRelease(); // encodedTopicFilters.Return(); //} }
public void Decrypt(IByteBufferAllocator allocator, EncryptMode mode, Stream src, Stream dst, bool reliable) { if (RC4 == null || AES == null) { return; } //throw new ObjectDisposedException(GetType().FullName); using (var data = new BufferWrapper(allocator.Buffer().WithOrder(ByteOrder.LittleEndian))) using (var decryptor = GetAlgorithm(mode).CreateDecryptor()) using (var cs = new CryptoStream(src, decryptor, CryptoStreamMode.Read)) { var padding = cs.ReadByte(); var checksum = cs.ReadByte() | (cs.ReadByte() << 8) | (cs.ReadByte() << 16) | (cs.ReadByte() << 24); using (var dataStream = new WriteOnlyByteBufferStream(data.Buffer, false)) { cs.CopyTo(dataStream); } if (reliable) { var counter = (ushort)(Interlocked.Increment(ref _decryptCounter) - 1); var messageCounter = data.Buffer.GetShort(data.Buffer.ReaderIndex); if (counter != messageCounter) { throw new ProudException($"Invalid decrypt counter! Remote: {messageCounter} Local: {counter}"); } } var slice = data.Buffer.ReadSlice(data.Buffer.ReadableBytes - padding); using (var dataStream = new ReadOnlyByteBufferStream(slice, false)) { if (Hash.GetUInt32 <CRC32>(dataStream) != (uint)checksum) { throw new ProudException("Invalid checksum"); } dataStream.Position = reliable ? 2 : 0; dataStream.CopyTo(dst); } } }
public MessageFrame ToMessage(IByteBufferAllocator alloc) { var bldr = new MessageFrameBuilder(alloc, 150, FrameType.VariableShort); bldr.SwitchToBitAccess(); var blockBuilder = new MessageFrameBuilder(alloc); PutMovementUpdate(Segment, bldr); PutBlocks(Segment, blockBuilder); bldr.PutBits(8, LocalPlayers); foreach (SynchronizationSegment segment in Segments) { SegmentType type = segment.Type; if (type == SegmentType.Remove_Mob) { PutRemovePlayerUpdate(bldr); } else if (type == SegmentType.Add_Mob) { PutAddPlayerUpdate((AddPlayerSegment)segment, bldr); PutBlocks(segment, blockBuilder); } else { PutMovementUpdate(segment, bldr); PutBlocks(segment, blockBuilder); } } if (blockBuilder.GetLength() > 0) { bldr.PutBits(11, 2047); bldr.SwitchToByteAccess(); bldr.PutRawBuilder(blockBuilder); } else { bldr.SwitchToByteAccess(); } return(bldr.ToMessageFrame()); }
public async Task TestSoLingerZeroCausesOnlyRstOnClose_LibuvClient(IByteBufferAllocator allocator) { var sb = DefaultServerBootstrapFactory.Instance.NewInstance(); var cb = LibuvClientBootstrapFactory.Instance.NewInstance(); Configure(sb, cb, allocator); try { await TestSoLingerZeroCausesOnlyRstOnClose0(sb, cb); } finally { Task.WaitAll( sb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), sb.ChildGroup().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), cb.Group().ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5))); } }
public IByteBuffer Cumulate(IByteBufferAllocator alloc, IByteBuffer cumulation, IByteBuffer input) { if (!cumulation.IsReadable()) { _ = cumulation.Release(); return(input); } CompositeByteBuffer composite = null; try { composite = cumulation as CompositeByteBuffer; if (composite is object && 0u >= (uint)(cumulation.ReferenceCount - 1)) { // Writer index must equal capacity if we are going to "write" // new components to the end if (composite.WriterIndex != composite.Capacity) { _ = composite.AdjustCapacity(composite.WriterIndex); } } else { composite = alloc.CompositeBuffer(int.MaxValue).AddFlattenedComponents(true, cumulation); } _ = composite.AddFlattenedComponents(true, input); input = null; return(composite); } finally { if (input is object) { // We must release if the ownership was not transferred as otherwise it may produce a leak _ = input.Release(); // Also release any new buffer allocated if we're not returning it if (composite is object && composite != cumulation) { _ = composite.Release(); } } } }
/// <summary> /// Compose <paramref name="cumulation"/> and <paramref name="next"/> into a new <see cref="CompositeByteBuffer"/>. /// </summary> /// <param name="alloc"></param> /// <param name="cumulation"></param> /// <param name="next"></param> /// <returns></returns> protected IByteBuffer ComposeIntoComposite(IByteBufferAllocator alloc, IByteBuffer cumulation, IByteBuffer next) { // Create a composite buffer to accumulate this pair and potentially all the buffers // in the queue. Using +2 as we have already dequeued current and next. var composite = alloc.CompositeBuffer(Size() + 2); try { _ = composite.AddComponent(true, cumulation); _ = composite.AddComponent(true, next); } catch (Exception) { _ = composite.Release(); ReferenceCountUtil.SafeRelease(next); throw; } return(composite); }
public void SetUp(BenchmarkContext context) { this.ServerGroup = new MultithreadEventLoopGroup(1); this.WorkerGroup = new MultithreadEventLoopGroup(); Encoding iso = Encoding.GetEncoding("ISO-8859-1"); this.message = Unpooled.Buffer().WriteInt(3).WriteBytes(iso.GetBytes("ABC")).ToArray(); this.inboundThroughputCounter = context.GetCounter(InboundThroughputCounterName); this.outboundThroughputCounter = context.GetCounter(OutboundThroughputCounterName); var counterHandler = new CounterHandlerInbound(this.inboundThroughputCounter); this.signal = new ManualResetEventSlimReadFinishedSignal(this.ResetEvent); // using default settings this.serverBufferAllocator = new PooledByteBufferAllocator(); ServerBootstrap sb = new ServerBootstrap() .Group(this.ServerGroup, this.WorkerGroup) .Channel<TcpServerSocketChannel>() .ChildOption(ChannelOption.Allocator, this.serverBufferAllocator) .ChildHandler(new ActionChannelInitializer<TcpSocketChannel>(channel => { channel.Pipeline .AddLast(this.GetEncoder()) .AddLast(this.GetDecoder()) .AddLast(counterHandler) .AddLast(new CounterHandlerOutbound(this.outboundThroughputCounter)) .AddLast(new ReadFinishedHandler(this.signal, WriteCount)); })); // start server this.serverChannel = sb.BindAsync(TEST_ADDRESS).Result; // connect to server var address = (IPEndPoint)this.serverChannel.LocalAddress; this.ClientSocket = new System.Net.Sockets.Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); this.ClientSocket.Connect(address.Address, address.Port); this.Stream = new NetworkStream(this.ClientSocket, true); }
public static async Task<IMessagingServiceClient> CreateFromConnectionStringAsync(string deviceId, string connectionString, int connectionPoolSize, TimeSpan? connectionIdleTimeout, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter) { int maxPendingOutboundMessages = settings.MaxPendingOutboundMessages; var tcpSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only); var webSocketSettings = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only); webSocketSettings.PrefetchCount = tcpSettings.PrefetchCount = (uint)maxPendingOutboundMessages; if (connectionPoolSize > 0) { var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings { MaxPoolSize = unchecked ((uint)connectionPoolSize), Pooling = connectionPoolSize > 0 }; if (connectionIdleTimeout.HasValue) { amqpConnectionPoolSettings.ConnectionIdleTimeout = connectionIdleTimeout.Value; } tcpSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings; webSocketSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings; } DeviceClient client = DeviceClient.CreateFromConnectionString(connectionString, new ITransportSettings[] { tcpSettings, webSocketSettings }); try { await client.OpenAsync(); } catch (IotHubException ex) { throw ComposeIotHubCommunicationException(ex); } return new IotHubClient(client, deviceId, settings, allocator, messageAddressConverter); }
/// <summary> /// Creates a new heap buffer with a newly allocated byte array. /// /// @param initialCapacity the initial capacity of the underlying byte array /// @param maxCapacity the max capacity of the underlying byte array /// </summary> public UnpooledHeapByteBuffer(IByteBufferAllocator allocator, int initialCapacity, int maxCapacity) : this(allocator, new byte[initialCapacity], 0, 0, maxCapacity) { }
/// <summary> /// Encode the given <see cref="string" /> using the given <see cref="Encoding" /> into a new /// <see cref="IByteBuffer" /> which /// is allocated via the <see cref="IByteBufferAllocator" />. /// </summary> /// <param name="alloc">The <see cref="IByteBufferAllocator" /> to allocate {@link IByteBuffer}.</param> /// <param name="src">src The <see cref="string" /> to encode.</param> /// <param name="encoding">charset The specified <see cref="Encoding" /></param> public static IByteBuffer EncodeString(IByteBufferAllocator alloc, string src, Encoding encoding) => EncodeString0(alloc, src, encoding, 0);
/// <summary> /// Encode the given <see cref="string" /> using the given <see cref="Encoding" /> into a new /// <see cref="IByteBuffer" /> which /// is allocated via the <see cref="IByteBufferAllocator" />. /// </summary> /// <param name="alloc">The <see cref="IByteBufferAllocator" /> to allocate {@link IByteBuffer}.</param> /// <param name="src">src The <see cref="string" /> to encode.</param> /// <param name="encoding">charset The specified <see cref="Encoding" /></param> /// <param name="extraCapacity">the extra capacity to alloc except the space for decoding.</param> public static IByteBuffer EncodeString(IByteBufferAllocator alloc, string src, Encoding encoding, int extraCapacity) => EncodeString0(alloc, src, encoding, extraCapacity);
public IByteBuffer Allocate(IByteBufferAllocator alloc) => alloc.Buffer(this.bufferSize);
static IByteBuffer EncodeString0(IByteBufferAllocator alloc, string src, Encoding encoding, int extraCapacity) { int length = encoding.GetMaxByteCount(src.Length) + extraCapacity; bool release = true; IByteBuffer dst = alloc.Buffer(length); Contract.Assert(dst.HasArray, "Operation expects allocator to operate array-based buffers."); try { int written = encoding.GetBytes(src, 0, src.Length, dst.Array, dst.ArrayOffset + dst.WriterIndex); dst.SetWriterIndex(dst.WriterIndex + written); release = false; return dst; } finally { if (release) { dst.Release(); } } }
public IByteBuffer Allocate(IByteBufferAllocator alloc) { return alloc.Buffer(this.bufferSize); }
public static PublishPacket ComposePublishPacket(IChannelHandlerContext context, IMessage message, QualityOfService qos, IByteBufferAllocator allocator) { bool duplicate = message.DeliveryCount > 0; var packet = new PublishPacket(qos, duplicate, false); packet.TopicName = message.Address; if (qos > QualityOfService.AtMostOnce) { int packetId = unchecked((int)message.SequenceNumber) & 0x3FFF; // clear bits #14 and #15 switch (qos) { case QualityOfService.AtLeastOnce: break; case QualityOfService.ExactlyOnce: packetId |= 0x8000; // set bit #15 break; default: throw new ArgumentOutOfRangeException(nameof(qos), qos, null); } packet.PacketId = packetId + 1; } message.Payload.Retain(); packet.Payload = message.Payload; return packet; }
public EmptyByteBuffer(IByteBufferAllocator allocator) : base(0) { this.allocator = allocator; }
public PooledByteBuffer(ThreadLocalPool.Handle returnHandle, IByteBufferAllocator allocator, int maxFixedCapacity, int maxCapacity) : this(returnHandle, allocator, new byte[maxFixedCapacity], maxCapacity) { }
// // Static Operations // /// <summary> /// Replaces the default allocator with your own implementation /// </summary> /// <param name="allocator">New allocator</param> public static void SetAllocator(IByteBufferAllocator allocator) { if ( allocator == null ) throw new ArgumentNullException("allocator"); _allocator = allocator; }
public EmptyByteBuffer(IByteBufferAllocator allocator) : this(allocator, ByteOrder.BigEndian) { }
public void SetUp(BenchmarkContext context) { TaskScheduler.UnobservedTaskException += (sender, args) => Console.WriteLine(args.Exception); this.ClientGroup = new MultithreadEventLoopGroup(1); this.ServerGroup = new MultithreadEventLoopGroup(1); this.WorkerGroup = new MultithreadEventLoopGroup(); Encoding iso = Encoding.GetEncoding("ISO-8859-1"); this.message = iso.GetBytes("ABC"); this.inboundThroughputCounter = context.GetCounter(InboundThroughputCounterName); this.outboundThroughputCounter = context.GetCounter(OutboundThroughputCounterName); var counterHandler = new CounterHandlerInbound(this.inboundThroughputCounter); this.signal = new ManualResetEventSlimReadFinishedSignal(this.ResetEvent); // reserve up to 10mb of 16kb buffers on both client and server; we're only sending about 700k worth of messages this.serverBufferAllocator = new PooledByteBufferAllocator(); this.clientBufferAllocator = new PooledByteBufferAllocator(); Assembly assembly = typeof(TcpChannelPerfSpecs).Assembly; byte[] certificateData; using (Stream sourceStream = assembly.GetManifestResourceStream(assembly.GetManifestResourceNames()[0])) using (var tempStream = new MemoryStream()) { sourceStream.CopyTo(tempStream); certificateData = tempStream.ToArray(); } var tlsCertificate = new X509Certificate2(certificateData, "password"); string targetHost = tlsCertificate.GetNameInfo(X509NameType.DnsName, false); ServerBootstrap sb = new ServerBootstrap() .Group(this.ServerGroup, this.WorkerGroup) .Channel<TcpServerSocketChannel>() .ChildOption(ChannelOption.Allocator, this.serverBufferAllocator) .ChildHandler(new ActionChannelInitializer<TcpSocketChannel>(channel => { channel.Pipeline //.AddLast(TlsHandler.Server(tlsCertificate)) .AddLast(this.GetEncoder()) .AddLast(this.GetDecoder()) .AddLast(counterHandler) .AddLast(new CounterHandlerOutbound(this.outboundThroughputCounter)) .AddLast(new ReadFinishedHandler(this.signal, WriteCount)); })); Bootstrap cb = new Bootstrap() .Group(this.ClientGroup) .Channel<TcpSocketChannel>() .Option(ChannelOption.Allocator, this.clientBufferAllocator) .Handler(new ActionChannelInitializer<TcpSocketChannel>( channel => { channel.Pipeline //.AddLast(TlsHandler.Client(targetHost, null, (sender, certificate, chain, errors) => true)) .AddLast(this.GetEncoder()) .AddLast(this.GetDecoder()) .AddLast(counterHandler) .AddLast(new CounterHandlerOutbound(this.outboundThroughputCounter)); })); // start server this.serverChannel = sb.BindAsync(TEST_ADDRESS).Result; // connect to server this.clientChannel = cb.ConnectAsync(this.serverChannel.LocalAddress).Result; }
public static async Task<PublishPacket> ComposePublishPacketAsync(IChannelHandlerContext context, IMessage message, QualityOfService qos, string topicName, IByteBufferAllocator allocator) { bool duplicate = message.DeliveryCount > 0; var packet = new PublishPacket(qos, duplicate, false); packet.TopicName = topicName; if (qos > QualityOfService.AtMostOnce) { int packetId = unchecked((int)message.SequenceNumber) & 0x3FFF; // clear bits #14 and #15 switch (qos) { case QualityOfService.AtLeastOnce: break; case QualityOfService.ExactlyOnce: packetId |= 0x8000; // set bit #15 break; default: throw new ArgumentOutOfRangeException(nameof(qos), qos, null); } packet.PacketId = packetId + 1; } using (Stream payloadStream = message.Payload) { long streamLength = payloadStream.Length; if (streamLength > int.MaxValue) { throw new InvalidOperationException($"Message size ({streamLength} bytes) is too big to process."); } int length = (int)streamLength; IByteBuffer buffer = allocator.Buffer(length, length); await buffer.WriteBytesAsync(payloadStream, length); Contract.Assert(buffer.ReadableBytes == length); packet.Payload = buffer; } return packet; }
static ByteBufferUtil() { char[] digits = "0123456789abcdef".ToCharArray(); for (int i = 0; i < 256; i++) { HexdumpTable[i << 1] = digits[(int)((uint)i >> 4 & 0x0F)]; HexdumpTable[(i << 1) + 1] = digits[i & 0x0F]; } // Generate the lookup table for byte-to-hex-dump conversion for (int i = 0; i < Byte2Hex.Length; i++) { Byte2Hex[i] = ' ' + StringUtil.ByteToHexStringPadded(i); } // Generate the lookup table for hex dump paddings for (int i = 0; i < HexPadding.Length; i++) { int padding = HexPadding.Length - i; var buf = new StringBuilder(padding * 3); for (int j = 0; j < padding; j++) { buf.Append(" "); } HexPadding[i] = buf.ToString(); } // Generate the lookup table for byte dump paddings for (int i = 0; i < BytePadding.Length; i++) { int padding = BytePadding.Length - i; var buf = new StringBuilder(padding); for (int j = 0; j < padding; j++) { buf.Append(' '); } BytePadding[i] = buf.ToString(); } // Generate the lookup table for byte-to-char conversion for (int i = 0; i < Byte2Char.Length; i++) { if (i <= 0x1f || i >= 0x7f) { Byte2Char[i] = '.'; } else { Byte2Char[i] = (char)i; } } // Generate the lookup table for the start-offset header in each row (up to 64KiB). for (int i = 0; i < HexDumpRowPrefixes.Length; i++) { var buf = new StringBuilder(12); buf.Append(Environment.NewLine); buf.Append((i << 4 & 0xFFFFFFFFL | 0x100000000L).ToString("X2")); buf.Insert(buf.Length - 9, '|'); buf.Append('|'); HexDumpRowPrefixes[i] = buf.ToString(); } string allocType = SystemPropertyUtil.Get( "io.netty.allocator.type", "pooled"); allocType = allocType.Trim(); IByteBufferAllocator alloc; if ("unpooled".Equals(allocType, StringComparison.OrdinalIgnoreCase)) { alloc = UnpooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: {}", allocType); } else if ("pooled".Equals(allocType, StringComparison.OrdinalIgnoreCase)) { alloc = PooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: {}", allocType); } else { alloc = PooledByteBufferAllocator.Default; Logger.Debug("-Dio.netty.allocator.type: pooled (unknown: {})", allocType); } DefaultAllocator = alloc; }
/// <summary> /// Creates a new heap buffer with an existing byte array. /// /// @param initialArray the initial underlying byte array /// @param maxCapacity the max capacity of the underlying byte array /// </summary> public UnpooledHeapByteBuffer(IByteBufferAllocator allocator, byte[] initialArray, int maxCapacity) : this(allocator, initialArray, 0, initialArray.Length, maxCapacity) { }
static IByteBuffer ExpandCumulation(IByteBufferAllocator allocator, IByteBuffer cumulation, int readable) { IByteBuffer oldCumulation = cumulation; cumulation = allocator.Buffer(oldCumulation.ReadableBytes + readable); cumulation.WriteBytes(oldCumulation); oldCumulation.Release(); return cumulation; }
/// <summary> /// Read the given amount of bytes into a new {@link ByteBuf} that is allocated from the {@link ByteBufAllocator}. /// </summary> public static IByteBuffer ReadBytes(IByteBufferAllocator alloc, IByteBuffer buffer, int length) { bool release = true; IByteBuffer dst = alloc.Buffer(length); try { buffer.ReadBytes(dst); release = false; return dst; } finally { if (release) { dst.Release(); } } }