Exemple #1
0
        private protected override void EndReceive(SocketAsyncEventArgs args)
        {
            ReadOnlyMemory <byte> datagram = args.MemoryBuffer.Slice(0, args.BytesTransferred);

            // dispatch datagram to appropriate exchange
            var correlationId = new CorrelationId(datagram.Span, out var consumedBytes);

            datagram = datagram.Slice(consumedBytes);

            var headers = new PacketHeaders(datagram, out consumedBytes);

            datagram = datagram.Slice(consumedBytes);

request_channel:
            if (!channels.TryGetValue(correlationId, out var channel))
            {
                // channel doesn't exist in the list of active channel but rented successfully
                if (exchanges.TryRent(out var exchange))
                {
                    channel = new Channel(exchange, exchanges, receiveTimeout, cancellationHandler, correlationId);
                    if (!channels.TryAdd(correlationId, channel))
                    {
                        channel.Dispose();
                        goto request_channel;
                    }
                }
                else
                {
                    logger.NotEnoughRequestHandlers();
                    return;
                }
            }

            ProcessDatagram(channels, channel, correlationId, headers, datagram, args);
        }
Exemple #2
0
            private protected ValueTask WritePacket(PacketHeaders headers, Memory <byte> buffer, int count, CancellationToken token)
            {
                // write headers
                headers.WriteTo(buffer);
                WriteInt32LittleEndian(buffer.Span.Slice(PacketHeaders.NaturalSize), count);

                // transmit packet to the remote endpoint
                return(networkStream.WriteAsync(AdjustPacket(buffer, count), token));
            }
Exemple #3
0
        private protected override void EndReceive(SocketAsyncEventArgs args)
        {
            ReadOnlyMemory <byte> datagram = args.MemoryBuffer.Slice(0, args.BytesTransferred);

            // dispatch datagram to appropriate exchange
            var correlationId = new CorrelationId(ref datagram);

            if (channels.TryGetValue(correlationId, out var channel))
            {
                var headers = new PacketHeaders(ref datagram);
                ProcessDatagram(channels, channel, correlationId, headers, datagram, args);
            }
            else
            {
                logger.PacketDropped(correlationId, args.RemoteEndPoint);
            }
        }
Exemple #4
0
        private void Route(Socket socket, String routerName, IList <IPacketMessageEnvelope> envelopes, Int64 currentJournalSequence, Int64 previousJournalSequence)
        {
            var router = _context.RouterFactory.GetRouter(routerName);
            var outbox = router.Route(envelopes);

            if (outbox.Count == 0)
            {
                return;
            }

            var headers = new PacketHeaders()
            {
                CurrentJournalSequence  = currentJournalSequence,
                PreviousJournalSequence = previousJournalSequence
            };

            var packet = _context.CreatePacket(outbox, headers);

            socket.SendPacket(packet);
        }
        private protected override void EndReceive(SocketAsyncEventArgs args)
        {
            ReadOnlyMemory <byte> datagram = args.MemoryBuffer.Slice(0, args.BytesTransferred);

            // dispatch datagram to appropriate exchange
            var correlationId = new CorrelationId(ref datagram);
            var headers       = new PacketHeaders(ref datagram);

request_channel:
            if (!channels.TryGetValue(correlationId, out var channel))
            {
                // channel doesn't exist in the list of active channel but rented successfully
                if (exchanges.TryRent(out var exchange))
                {
                    channel = new Channel(exchange, exchanges, receiveTimeout, cancellationHandler, correlationId);
                    if (!channels.TryAdd(correlationId, channel))
                    {
                        channel.Dispose();
                        goto request_channel;
                    }
                }
                else
                {
                    logger.NotEnoughRequestHandlers();
                    return;
                }
            }

            if (headers.Control == FlowControl.Cancel)
            {
                ProcessCancellation(cancellationInvoker, ref channel, false, args);   // channel will be removed from the dictionary automatically
            }
            else
            {
                ProcessDatagram(channels, channel, correlationId, headers, datagram, args);
            }
        }
Exemple #6
0
 private static void ReadPrologue(ReadOnlyMemory <byte> prologue, out PacketHeaders headers, out int count)
 {
     headers = new PacketHeaders(prologue, out var headersSize);
     count   = ReadInt32LittleEndian(prologue.Span.Slice(headersSize));
 }