Example #1
0
        static async Task Test_Pipe_SslStream_Client(CancellationToken cancel)
        {
            string hostname = "news.goo.ne.jp";
            int    port     = 443;

            AsyncCleanuperLady lady = new AsyncCleanuperLady();

            try
            {
                var tcp = new FastPalTcpProtocolStub(lady, cancel: cancel);

                var sock = await tcp.ConnectAsync(hostname, port);

                FastSslProtocolStack ssl = new FastSslProtocolStack(lady, sock.UpperEnd, null, null, cancel);

                var sslClientOptions = new PalSslClientAuthenticationOptions()
                {
                    TargetHost = hostname,
                    ValidateRemoteCertificateProc = (cert) => { return(true); },
                };

                var ssl_sock = await ssl.SslStartClient(sslClientOptions, cancel);

                var app = ssl_sock.GetFastAppProtocolStub(cancel);

                app.AttachHandle.SetStreamReceiveTimeout(1000);

                var st = app.GetStream().GetPalNetworkStream();

                var tcp_info = ssl_sock.Info.GetValue <ILayerInfoTcpEndPoint>();
                var ssl_info = ssl_sock.Info.GetValue <ILayerInfoSsl>();

                WriteLine($"Connected. {tcp_info.LocalIPAddress}:{tcp_info.LocalPort} -> {tcp_info.RemoteIPAddress}:{tcp_info.RemotePort}");
                StreamWriter w = new StreamWriter(st);
                w.AutoFlush = true;

                await w.WriteAsync(
                    "GET / HTTP/1.0\r\n" +
                    $"HOST: {hostname}\r\n\r\n"
                    );

                StreamReader r = new StreamReader(st);
                while (true)
                {
                    string s = await r.ReadLineAsync();

                    if (s == null)
                    {
                        break;
                    }
                    WriteLine(s);
                }
            }
            finally
            {
                await lady;
            }
        }
Example #2
0
        static async Task Test_Pipe_TCP_Client(CancellationToken cancel)
        {
            AsyncCleanuperLady lady = new AsyncCleanuperLady();

            try
            {
                var tcp = new FastPalTcpProtocolStub(lady, cancel: cancel);

                var sock = await tcp.ConnectAsync("www.google.com", 80, null);

                var app = sock.GetFastAppProtocolStub(cancel);

                var st = app.GetStream().GetPalNetworkStream();

                app.AttachHandle.SetStreamTimeout(2000, -1);
                WriteLine("Connected.");
                StreamWriter w = new StreamWriter(st);
                w.AutoFlush = true;

                await w.WriteAsync(
                    "GET / HTTP/1.0\r\n" +
                    "HOST: www.google.com\r\n\r\n"
                    );

                StreamReader r = new StreamReader(st);
                while (true)
                {
                    string s = await r.ReadLineAsync();

                    if (s == null)
                    {
                        break;
                    }
                    WriteLine(s);
                }
            }
            finally
            {
                await lady;
            }
        }
Example #3
0
            async Task <Result> ClientSingleConnectionAsync(Direction dir, AsyncManualResetEvent fireMeWhenReady, CancellationToken cancel)
            {
                Result             ret  = new Result();
                AsyncCleanuperLady lady = new AsyncCleanuperLady();

                try
                {
                    var tcp = new FastPalTcpProtocolStub(lady, cancel: cancel);

                    var sock = await tcp.ConnectAsync(ServerIP, ServerPort, cancel, ConnectTimeout);

                    var app = sock.GetFastAppProtocolStub();

                    var attachHandle = app.AttachHandle;

                    var st = app.GetStream();

                    if (dir == Direction.Recv)
                    {
                        app.AttachHandle.SetStreamReceiveTimeout(RecvTimeout);
                    }

                    try
                    {
                        var hello = await st.ReceiveAllAsync(16);

                        Dbg.Where();
                        if (Encoding.ASCII.GetString(hello.Span).StartsWith("TrafficServer\r\n") == false)
                        {
                            throw new ApplicationException("Target server is not a Traffic Server.");
                        }
                        Dbg.Where();

                        //throw new ApplicationException("aaaa" + dir.ToString());

                        fireMeWhenReady.Set();

                        cancel.ThrowIfCancellationRequested();

                        await WebSocketHelper.WaitObjectsAsync(
                            manualEvents : ClientStartEvent.ToSingleArray(),
                            cancels : cancel.ToSingleArray()
                            );

                        long tickStart = FastTick64.Now;
                        long tickEnd   = tickStart + this.TimeSpan;

                        var sendData = new MemoryBuffer <byte>();
                        sendData.WriteBool8(dir == Direction.Recv);
                        sendData.WriteUInt64(SessionId);
                        sendData.WriteSInt64(TimeSpan);

                        await st.SendAsync(sendData);

                        if (dir == Direction.Recv)
                        {
                            RefInt totalRecvSize = new RefInt();
                            while (true)
                            {
                                long now = FastTick64.Now;

                                if (now >= tickEnd)
                                {
                                    break;
                                }

                                await WebSocketHelper.WaitObjectsAsync(
                                    tasks : st.FastReceiveAsync(totalRecvSize : totalRecvSize).ToSingleArray(),
                                    timeout : (int)(tickEnd - now),
                                    exceptions : ExceptionWhen.TaskException | ExceptionWhen.CancelException);

                                ret.NumBytesDownload += totalRecvSize;
                            }
                        }
                        else
                        {
                            attachHandle.SetStreamReceiveTimeout(Timeout.Infinite);

                            while (true)
                            {
                                long now = FastTick64.Now;

                                if (now >= tickEnd)
                                {
                                    break;
                                }

                                /*await WebSocketHelper.WaitObjectsAsync(
                                 *  tasks: st.FastSendAsync(SendData, flush: true).ToSingleArray(),
                                 *  timeout: (int)(tick_end - now),
                                 *  exceptions: ExceptionWhen.TaskException | ExceptionWhen.CancelException);*/

                                await st.FastSendAsync(SendData, flush : true);
                            }

                            Task recvResult = Task.Run(async() =>
                            {
                                var recvMemory = await st.ReceiveAllAsync(8);

                                MemoryBuffer <byte> recvMemoryBuf = recvMemory;
                                ret.NumBytesUpload = recvMemoryBuf.ReadSInt64();

                                st.Disconnect();
                            });

                            Task sendSurprise = Task.Run(async() =>
                            {
                                byte[] surprise = new byte[260];
                                surprise.AsSpan().Fill((byte)'!');
                                while (true)
                                {
                                    await st.SendAsync(surprise);

                                    await WebSocketHelper.WaitObjectsAsync(
                                        manualEvents: sock.Pipe.OnDisconnectedEvent.ToSingleArray(),
                                        timeout: 200);
                                }
                            });

                            await WhenAll.Await(false, recvResult, sendSurprise);

                            await recvResult;
                        }

                        st.Disconnect();

                        Dbg.Where();
                        return(ret);
                    }
                    catch (Exception ex)
                    {
                        Dbg.Where(ex.Message);
                        ExceptionQueue.Add(ex);
                        throw;
                    }
                }
                finally
                {
                    await lady;
                }
            }