Exemplo n.º 1
0
        public void GetAvailableTcpPortTest()
        {
            int Port     = NetworkUtilities.GetAvailableTcpPort();
            var Listener = new TcpListener(IPAddress.Parse("127.0.0.1"), Port);

            Listener.Start();
            Listener.Stop();
        }
Exemplo n.º 2
0
        async protected Task TestSimpleConnectionAsync()
        {
            var FastcgiServerAsync = new TestFastcgiServerAsync();

            FastcgiServerAsync.Debug = true;
            var ServerPort = NetworkUtilities.GetAvailableTcpPort();

            FastcgiServerAsync.ListenAsync(ServerPort, "127.0.0.1");
            var Client = new TcpClient();
            await Client.ConnectAsync("127.0.0.1", ServerPort);

            var ClientStream = Client.GetStream();

            await new FastcgiPacket()
            {
                Version   = 1,
                Type      = CSharpUtils.Web._45.Fastcgi.Fastcgi.PacketType.FCGI_BEGIN_REQUEST,
                RequestId = 1,
                Content   = new ArraySegment <byte>(new byte[] { 0, 0, 0 }),
            }.WriteToAsync(ClientStream);

            await new FastcgiPacket()
            {
                Version   = 1,
                Type      = CSharpUtils.Web._45.Fastcgi.Fastcgi.PacketType.FCGI_PARAMS,
                RequestId = 1,
                //Content = new ArraySegment<byte>(new byte[] { 0, 0 }),
                Content = new ArraySegment <byte>(new byte[] { }),
            }.WriteToAsync(ClientStream);

            await new FastcgiPacket()
            {
                Version   = 1,
                Type      = CSharpUtils.Web._45.Fastcgi.Fastcgi.PacketType.FCGI_STDIN,
                RequestId = 1,
                //Content = new ArraySegment<byte>(new byte[] { 0, 0 }),
                Content = new ArraySegment <byte>(new byte[] { }),
            }.WriteToAsync(ClientStream);

            await ClientStream.FlushAsync();

            for (int n = 0; n < 10; n++)
            {
                if (!Client.Connected)
                {
                    return;
                }
                Thread.Sleep(TimeSpan.FromMilliseconds(10));
            }

            throw (new Exception("Socket not disconnected"));
        }
Exemplo n.º 3
0
        public void AcceptClientTest()
        {
            string    BindIp            = "127.0.0.1";
            ushort    BindPort          = NetworkUtilities.GetAvailableTcpPort();
            var       TestServerHandler = new ServerHandler(BindIp, BindPort);
            TcpClient TestClient;

            new Thread(() =>
            {
                TestServerHandler.IsAcceptingSocketEvent.WaitOne();
                TestClient = new TcpClient(BindIp, BindPort);
            }).Start();
            TestServerHandler.ListenStart();
            TestServerHandler.AcceptClient();
            TestServerHandler.ListenStop();
        }
Exemplo n.º 4
0
        public void ScgiServerConstructorTest()
        {
            string BindIp     = "127.0.0.1";
            int    BindPort   = NetworkUtilities.GetAvailableTcpPort();
            var    ScgiServer = new TestScgiServer(BindIp, BindPort);

            ScgiServer.Listen();
            new Thread(ScgiServer.AcceptLoop).Start();

            var TcpClient       = new TcpClient(BindIp, BindPort);
            var TcpClientStream = TcpClient.GetStream();
            var HeaderStream    = new MemoryStream().PreservePositionAndLock((Stream) =>
            {
                Stream.WriteStringzPair("CONTENT_LENGTH", "0");
                Stream.WriteStringzPair("SCGI", "1");
                Stream.WriteStringzPair("REQUEST_METHOD", "GET");
                Stream.WriteStringzPair("REQUEST_URI", "/test");
            });

            TcpClientStream.WriteString(HeaderStream.Length + ":");
            TcpClientStream.WriteBytes(HeaderStream.ToArray());
            TcpClientStream.WriteByte((byte)',');
            Assert.IsTrue(ScgiServer.HandleRequestEvent.WaitOne(1000));
        }