예제 #1
0
        public override async Task HandleTcpConnection(InConnectionTcp connection)
        {
RETRY:
            CheckPool();
            bool isConnFromPool = true;
            var  conn           = TryGetConnection();

            if (conn == null)
            {
                isConnFromPool = false;
                conn           = await NewConnection();
            }
            var usedCount = conn.UsedCount;

            Connection.SessionStream s;
            try {
                conn.Open();
                s = conn.CurrentSession;
                await s.WriteHeader(connection.Dest);
            } catch (Exception e) {
                Logger.error($"{(usedCount > 0 ? $"reusing({usedCount}) " : "")}" +
                             $"connection error{(isConnFromPool ? " (will retry)" : "")}:" +
                             $" {e.Message} on {conn.ws.BaseStream}");
                conn.Close();
                if (isConnFromPool)
                {
                    goto RETRY;
                }
                throw;
            }
            await connection.HandleAndPutStream(this, s.AsMyStream);

            TryPut(conn).Forget();
        }
예제 #2
0
        public override async Task HandleTcpConnection(InConnectionTcp connection)
        {
            Exception     e             = null;
            ConnectResult connectResult = null;

            try {
                connectResult = await Connect(connection);
            } catch (Exception ex) when(if_failed != null)
            {
                Logging.exception(ex, Logging.Level.Error, $"{this}: {connection} failed ({connectResult.FailedReason}), redirecting to {if_failed}.");
                connection.RedirectTo(if_failed);
                return;
            }
            if (!connectResult.Ok && if_failed != null)
            {
                Logging.warning($": {connection} failed ({connectResult.FailedReason}), redirecting to {if_failed}.");
                connection.RedirectTo(if_failed);
                return;
            }
            try {
                if (connectResult.Ok)
                {
                    await connection.HandleAndPutStream(this, connectResult.Stream, connectResult.WhenCanRead);
                }
                else
                {
                    await connection.HandleAndGetStream(connectResult);
                }
            } finally {
                if (connectResult.Ok)
                {
                    MyStream.CloseWithTimeout(connectResult.Stream);
                }
            }
        }