コード例 #1
0
        public static async Task Run(SessionContextPool pool, TcpClient client, ProxyServerConfig proxyConfig)
        {
            var    result = LastPipeState.GameStart;
            string contextId;

            using (var context = new SessionContext(client, proxyConfig))
            {
                contextId = context.Id;
                pool.ContextContainer.TryAdd(contextId, context);
                do
                {
                    try
                    {
                        var processor = Handlers[result];
                        //Console.WriteLine($"[{context.Id}] {result} → {processor.GetType()} ↓");
                        result = await processor.Run(proxyConfig.Handler, context);
                    }
                    catch (Exception)
                    {
                        result = LastPipeState.GameOver;
                    }
                } while (result != LastPipeState.GameOver);

                //Console.WriteLine($"[{context.Id}] {result} ⬆");
            }

            pool.ContextContainer.TryRemove(contextId, out _);
        }
コード例 #2
0
 public SessionContext(TcpClient client, ProxyServerConfig proxyConfig)
 {
     Id           = Guid.NewGuid().ToString("N");
     ProxyConfig  = proxyConfig;
     StageFunc    = proxyConfig.StateFunc;
     Client       = client;
     ClientStream = client.GetStream();
 }
コード例 #3
0
        public ProxyMediatorServer(ProxyServerConfig config)
        {
            async void HandleClient(SessionContextPool pool, TcpClient client, CancellationToken _) =>
            await Session.Run(pool, client, config);

            _proxyListener = new ProxyListener(config.Pool, config.EndPoint, HandleClient);
            // ReSharper disable once PossibleNullReferenceException
            var ipPortParts = _proxyListener.InternalTcpListener.LocalEndpoint.ToString().Split(':');

            config.EndPoint.Port = int.Parse(ipPortParts[1]);
        }
コード例 #4
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                ProxyMediatorServer proxyMediatorServer = null;
                try
                {
                    var proxyMediatorServerStop =
                        new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
                    var proxyServerConfig = new ProxyServerConfig(_handler)
                    {
                        StateFunc = _onStageFunc,
                    };
                    proxyMediatorServer = new ProxyMediatorServer(proxyServerConfig);
                    stoppingToken.Register(() =>
                    {
                        try
                        {
                            // ReSharper disable once AccessToDisposedClosure
                            proxyMediatorServer.Dispose();
                        }
                        catch (Exception e)
                        {
                            _logger.LogError(e, $"error when disposing {nameof(ProxyMediatorServer)}");
                        }

                        proxyMediatorServerStop.SetResult(true);
                    });
                    await proxyMediatorServerStop.Task;
                }
                catch (Exception e)
                {
                    _logger.LogError(e, $"error in {nameof(ProxyMediatorBackgroundService)}");
                }
                finally
                {
                    proxyMediatorServer?.Dispose();
                }
            }
        }