Exemplo n.º 1
0
        public static async Task <List <IEnvelope> > ReceivePackets(EnvelopeReaderBuffer readerBuffer, Stream stream, CancellationToken ct)
        {
            var receivedPackets = new List <IEnvelope>();

            int         currentBytesRead;
            TLSEnvelope packet;

            do
            {
                currentBytesRead = await stream.ReadAsync(readerBuffer.Buffer, 0, readerBuffer.Buffer.Length, ct);

                TLSEnvelopeExtensions.UpdatePayload(currentBytesRead, readerBuffer);
                packet = TLSEnvelopeExtensions.TryTakeOnePacket(ref readerBuffer.Payload);
                if (packet != null)
                {
                    receivedPackets.Add(packet);
                }
            } while (currentBytesRead > 0 && (packet == null || readerBuffer.Payload != null));
            return(receivedPackets);
        }
Exemplo n.º 2
0
        public async Task <string> Receive(byte[] rawRequest, Transport transport)
        {
            Debug.Assert(transport == Transport.UDP);

            try
            {
                if (rawRequest == null || rawRequest.Length == 0)
                {
                    throw new Exception("TLSClient received null or empty packet");
                }

                var tlsEnvelope = new TLSEnvelope(rawRequest);

                if (!TLSEnvelopeExtensions.ValidateCrc32(rawRequest, out var actualCrc32))
                {
                    throw new Exception($"TLSEnvelope CRC32 Error: Expected: {tlsEnvelope.Crc32}, actual: {actualCrc32}");
                }

                var request = await this.Ratchet.DecryptRequest(tlsEnvelope);


                var command = request.ParseCommand();
                if (!command.CommandId.IsCommandDefined())
                {
                    throw new Exception($"TLSClient: The command {command.CommandId} is not defined.");
                }

                await ProcessCommand(command);
            }
            catch (Exception e)
            {
                var error = $"{nameof(TLSClient)} received bad request via {transport}: {e.Message}";
                this._log.LogError(error);
            }
            return(null); // this goes back to the IUdpConnection and is not used there.
        }