Exemplo n.º 1
0
        public override void Send(IOutgoingPacket packet)
        {
            var bytePacket = packet.GetFinalizedPacket();

            Console.WriteLine("Sent {0}", packet.PacketId.ToString());
            this.Send(bytePacket, 0, bytePacket.Length);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
     }
 }
Exemplo n.º 5
0
        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."),
            });
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
        public void SendMessage(IOutgoingPacket outgoingPacket)
        {
            var packetBytes = outgoingPacket.GetBytes();

            SendData(packetBytes);
        }
 public PacketWrapper(IOutgoingPacket packet) => _packet = packet;
Exemplo n.º 9
0
 public abstract void Send(IOutgoingPacket packet);