Esempio n. 1
0
        public async Task Handle(TcpClient client, CancellationToken cancellationToken = default)
        {
            using var _           = client;
            client.ReceiveTimeout = (int)ReceiveTimeout.TotalMilliseconds;

            using var stream = client.GetStream();
            // TODO: SSL 지원하기
            // using var stream = new SslStream(client.GetStream(), false);

            using var reader       = new StreamReader(stream, Encoding.ASCII, leaveOpen: true);
            await using var writer = new StreamWriter(stream, Encoding.ASCII, leaveOpen: true)
                        {
                            AutoFlush = true,
                            NewLine   = ControlChars.CrLf,
                        };

            using var scope = _scopeFactory.CreateScope();
            var session = new Pop3Session(client, reader, writer,
                                          scope.ServiceProvider.GetRequiredService <IMediator>(),
                                          scope.ServiceProvider.GetService <ILogger <Pop3Session> >());
            var sessionHandler = scope.ServiceProvider.GetRequiredService <Pop3SessionHandler>();

            try
            {
                await sessionHandler.Handle(session, cancellationToken);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "{EndPoint} - Error handling a session", session.EndPoint);
            }
        }
Esempio n. 2
0
        public async Task Handle(Pop3Session session, CancellationToken cancellationToken = default)
        {
            await session.Start();

            var buffer = new char[256];

            while
            (
                !cancellationToken.IsCancellationRequested &&
                session.Client.Connected
            )
            {
                // Socket.ReceiveTimeout 은 ReadAsync 에서는 효과 없으니까 Read 사용하자
                // https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receivetimeout?view=net-5.0#remarks
                var received = await Task.Run(() => session.Reader.Read(buffer), cancellationToken);

                _logger.LogInformation("{EndPoint} - Read {received} bytes", session.EndPoint, received);
                if (received == 0)
                {
                    return;
                }

                var(command, arguments) = GetMessage(buffer[..received]);