public override void Send(IOutgoingPacket packet) { var bytePacket = packet.GetFinalizedPacket(); Console.WriteLine("Sent {0}", packet.PacketId.ToString()); this.Send(bytePacket, 0, bytePacket.Length); }
public Context Write(IConnection connection, IOutgoingPacket packet) { if (disposed) { throw new ObjectDisposedException(nameof(Context)); } if (messages == null) { messages = new Dictionary <IConnection, Message>(); } Message message; if (!messages.TryGetValue(connection, out message)) { message = new Message(); messages.Add(connection, message); } message.Add(packet); return(this); }
public ValueTask <Task <T?> > SendPacket <T>(IActivityMonitor?m, IOutgoingPacket outgoingPacket) where T : class { ClientState?state = State; if (!IsConnected) { throw new InvalidOperationException("Client is Disconnected."); } if (state is null) { throw new NullReferenceException(); } return(SenderHelper.SendPacket <T>(m, state.Store, state.OutputPump, outgoingPacket)); }
protected async ValueTask ProcessOutgoingPacket(IOutputLogger?m, IOutgoingPacket outgoingPacket, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } using (m?.SendingMessage(ref outgoingPacket, OutputPump.PConfig.ProtocolLevel)) { if (outgoingPacket.Qos != QualityOfService.AtMostOnce) { // This must be done BEFORE writing the packet to avoid concurrency issues. _outgoingPacketStore.OnPacketSent(m, outgoingPacket.PacketId); // Explanation: // The receiver and input loop can run before the next line is executed. } await outgoingPacket.WriteAsync(OutputPump.PConfig.ProtocolLevel, _pipeWriter, cancellationToken); } }
public static ValueTask <Task <T?> > SendPacket <T>(IActivityMonitor?m, IOutgoingPacketStore store, OutputPump output, IOutgoingPacket packet) where T : class { IDisposableGroup?group = m?.OpenTrace($"Sending a packet '{packet}'in QoS {packet.Qos}"); return(packet.Qos switch { QualityOfService.AtMostOnce => PublishQoS0 <T>(m, group, output, packet), QualityOfService.AtLeastOnce => StoreAndSend <T>(m, group, output, store, packet, packet.Qos), QualityOfService.ExactlyOnce => StoreAndSend <T>(m, group, output, store, packet, packet.Qos), _ => throw new ArgumentException("Invalid QoS."), });
protected override async ValueTask <IOutgoingPacket> DoStorePacket(IActivityMonitor?m, IOutgoingPacket packet) { int packetSize = packet.GetSize(_protocolConfig.ProtocolLevel); m?.Trace($"Renting {packetSize} bytes to persist {packet}."); IMemoryOwner <byte> memOwner = MemoryPool <byte> .Shared.Rent(packetSize); PipeWriter pipe = PipeWriter.Create(memOwner.Memory.AsStream()); // And write their content to this memory. using (m?.OpenTrace($"Serializing {packet} into memory...")) { if (await packet.WriteAsync(_protocolConfig.ProtocolLevel, pipe, default) != WriteResult.Written) { throw new InvalidOperationException("Didn't wrote packet correctly."); } } Memory <byte> slicedMem = memOwner.Memory.Slice(0, packetSize); base[packet.PacketId].Content.Storage = new StoredPacket(slicedMem, memOwner); return(new FromMemoryOutgoingPacket(slicedMem, packet.Qos, packet.PacketId)); }
public void SendMessage(IOutgoingPacket outgoingPacket) { var packetBytes = outgoingPacket.GetBytes(); SendData(packetBytes); }
public PacketWrapper(IOutgoingPacket packet) => _packet = packet;
public abstract void Send(IOutgoingPacket packet);