Ejemplo n.º 1
0
 public async Task HandleInConnection(InConnection inc, IConnectionHandler outAdapter)
 {
     try {
         if (outAdapter == null)
         {
             warning($"'{inc.InAdapter.Name}' {inc} -> (no out adapter)");
             return;
         }
         onConnectionBegin(inc, outAdapter);
         int redirectCount = 0;
         while (true)
         {
             inc.RunningHandler = outAdapter;
             if (outAdapter is IConnectionHandler2 ich2)
             {
                 await ich2.HandleConnection(inc).CAF();
             }
             else if (inc is InConnectionTcp tcp)
             {
                 await outAdapter.HandleTcpConnection(tcp).CAF();
             }
             else
             {
                 throw new Exception($"'{outAdapter.Name}' cannot handle this type of connection.");
             }
             if (inc.IsHandled || !inc.IsRedirected)
             {
                 break;
             }
             if (++redirectCount >= 10)
             {
                 error($"'{inc.InAdapter.Name}' {inc} too many redirects, last redirect: {outAdapter.Name}");
                 return;
             }
             var nextAdapter = inc.Redirected?.Adapter as IConnectionHandler;
             if (nextAdapter == null)
             {
                 warning($"'{inc.InAdapter.Name}' {inc} was redirected by '{outAdapter.Name}'" +
                         $" to '{inc.Redirected}' which can not be found.");
                 return;
             }
             outAdapter = nextAdapter;
             if (LoggingLevel <= Logging.Level.None)
             {
                 debug($"'{inc.InAdapter.Name}' {inc} was redirected to '{inc.Redirected}'");
             }
             inc.Redirected = null;
         }
     } catch (Exception e) {
         await onConnectionException(inc, e).CAF();
     } finally {
         await onConnectionEnd(inc).CAF();
     }
 }
Ejemplo n.º 2
0
        public static Task <ConnectResult> ConnectWrapper(IConnectionHandler handler, ConnectArgument arg)
        {
            var tcs    = new TaskCompletionSource <ConnectResult>();
            var newinc = InConnectionTcp.Create(arg.InAdapter, arg.Dest, async(r) => {
                if (r.Ok)
                {
                    var stream = new LoopbackStream();
                    r.Stream   = stream;
                    tcs.SetResult(r);
                    return(stream.Another);
                }
                else
                {
                    tcs.SetResult(r);
                    return(null);
                }
            });

            newinc.Url = arg.Url;
            newinc.DestOriginalName = arg.DestOriginalName;
            NaiveUtils.RunAsyncTask(async() => {
                try {
                    await handler.HandleTcpConnection(newinc).CAF();
                } catch (Exception e) {
                    tcs.SetException(e);
                    return;
                }
                if (newinc.IsRedirected && tcs.Task.IsCompleted == false)
                {
                    tcs.SetResult(ConnectResult.RedirectTo(handler, newinc.Redirected));
                }
                else
                {
                    tcs.SetException(new Exception("handleConnection() did nothing."));
                }
            });
            return(tcs.Task);
        }