Implementation of a TCP/IP server
Exemplo n.º 1
0
        /// <summary>
        /// 指定ポートで Listening を開始する。
        /// Shutdown() を呼び出さずに2回目の Startup() を呼び出した場合、InvalidOperationException が発生する。
        /// </summary>
        /// <param name="listeningPort">Listeningするポート。</param>
        /// <param name="useIpV6">falseの場合、127.0.0.1で待ち受ける。trueの場合、::1で待ち受ける。既定false。</param>
        /// <param name="isSetProxyInProcess">trueの場合、プロセス内IEプロキシの設定を実施し、HTTP通信をNekoxyに向ける。既定true。</param>
        public static void Startup(int listeningPort, bool useIpV6 = false, bool isSetProxyInProcess = true)
        {
            if (server != null) throw new InvalidOperationException("Calling Startup() twice without calling Shutdown() is not permitted.");

            TransparentProxyLogic.AfterSessionComplete += InvokeAfterSessionComplete;
            TransparentProxyLogic.AfterReadRequestHeaders += InvokeAfterReadRequestHeaders;
            TransparentProxyLogic.AfterReadResponseHeaders += InvokeAfterReadResponseHeaders;
            ListeningPort = listeningPort;
            try
            {
                if (isSetProxyInProcess)
                    WinInetUtil.SetProxyInProcessForNekoxy(listeningPort);

                server = new TcpServer(listeningPort, useIpV6);
                server.Start(TransparentProxyLogic.CreateProxy);
                server.InitListenFinished.WaitOne();
                if (server.InitListenException != null) throw server.InitListenException;
            }
            catch (Exception)
            {
                Shutdown();
                throw;
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Listening しているスレッドを終了し、ソケットを閉じる。
 /// </summary>
 public static void Shutdown()
 {
     TransparentProxyLogic.AfterSessionComplete -= InvokeAfterSessionComplete;
     TransparentProxyLogic.AfterReadRequestHeaders -= InvokeAfterReadRequestHeaders;
     TransparentProxyLogic.AfterReadResponseHeaders -= InvokeAfterReadResponseHeaders;
     server?.Stop();
     server = null;
 }