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); } } }
public void AddConnection(SmtpConnector connection) { if (!_connectionReferences.TryAdd(connection.Id, new SmtpConnectionReference(connection))) { throw new ArgumentException(nameof(connection)); } }
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 { } } }
private void Walk(SmtpConnector connection) { connection.CheckTimeout(); }
public SmtpConnectionReference(SmtpConnector connection) { _weakReference = new WeakReference <SmtpConnector>(connection); ConnectionId = connection.Id; }
public bool TryGetConnection(out SmtpConnector connection) { return(_weakReference.TryGetTarget(out connection)); }