Exemplo n.º 1
0
        private async Task Loop()
        {
            _cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = _cancellationTokenSource.Token;

            while (!cancellationToken.IsCancellationRequested)
            {
                var needawait = false;
                try
                {
                    var cid = _nextConnectionId++;
                    _logger.LogInformation($"<ac {cid} {Thread.CurrentThread.ManagedThreadId}");
                    var tcpClient = await _listener.AcceptTcpClientAsync();

                    _logger.LogInformation($">ac {cid} {Thread.CurrentThread.ManagedThreadId} {tcpClient.Client.RemoteEndPoint}");

                    var session = new SmtpConnector(this, tcpClient, cid);
                    _ = session.Accept();
                }
                catch (Exception ex)
                {
                    Kooboo.Data.Log.Instance.Exception.Write(DateTime.Now.ToString() + ex.Message + "\r\n" + ex.StackTrace + "\r\n" + ex.Source);
                    needawait = true;
                }
                if (needawait)
                {
                    await Task.Delay(200);
                }
            }
        }
Exemplo n.º 2
0
 public void AddConnection(SmtpConnector connection)
 {
     if (!_connectionReferences.TryAdd(connection.Id, new SmtpConnectionReference(connection)))
     {
         throw new ArgumentException(nameof(connection));
     }
 }
Exemplo n.º 3
0
        public async void Start()
        {
            // 第一层端口占用保护
            if (Lib.Helper.NetworkHelper.IsPortInUse(Port))
            {
                return;
            }

            try
            {
                _listener = new TcpListener(new IPEndPoint(IPAddress.Any, Port));

                if (Timeout != 0)
                {
                    _listener.Server.ReceiveTimeout  =
                        _listener.Server.SendTimeout = Timeout;
                }

                _listener.Start();
            }
            catch (SocketException ex)
            {
                // 第二层端口占用保护
                if (ex.ErrorCode == 10048)
                {
                    _listener = null;
                    return;
                }
                else
                {
                    throw ex;
                }
            }

            _cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = _cancellationTokenSource.Token;

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var tcpClient = await _listener.AcceptTcpClientAsync();

                    var session = new SmtpConnector(this, tcpClient);
                    session.Accept();
                }
                catch
                {
                }
            }
        }
Exemplo n.º 4
0
 private void Walk(SmtpConnector connection)
 {
     connection.CheckTimeout();
 }
Exemplo n.º 5
0
 public SmtpConnectionReference(SmtpConnector connection)
 {
     _weakReference = new WeakReference <SmtpConnector>(connection);
     ConnectionId   = connection.Id;
 }
Exemplo n.º 6
0
 public bool TryGetConnection(out SmtpConnector connection)
 {
     return(_weakReference.TryGetTarget(out connection));
 }