Exemplo n.º 1
0
        static async Task Test000Client(string[] args)
        {
            try
            {
                using (var bd = new BatchDisposable())
                {
                    var client = new StreamServiceClient("StreamedNetTcpEndpoint", "net.tcp://*****:*****@"e:\1s.rar";
                    DownloadSteamInfo dsi            = await client.DownloadAsync(serverFileName, new DownloadFileInfo());

                    bd.Add(() => dsi?.ServerStream?.Dispose());

                    const string clientFileName = @"e:\2c.rar";
                    var          clientStream   = new FileStream(clientFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                    bd.Add(() => clientStream?.Dispose());


                    byte[] buffer = new byte[1024 * 1024];
                    await CommonHelpers.StreamCopyAsync(dsi.ServerStream, clientStream, buffer);

                    clientStream.Seek(0L, SeekOrigin.Begin);
                    const string serverFileNameUpload = @"e:\3s.rar";
                    await client.UploadAsync(serverFileNameUpload, clientStream);
                }
            }
            catch (System.Exception ex)
            {
                CommonHelpers.PrintException(ex, Console.Out, true);
            }
        }
Exemplo n.º 2
0
        static async Task Test001Client(string[] args)
        {
            try
            {
                using (var bd = new BatchDisposable())
                {
                    var client = new RpcClient("StreamedNetTcpRpcEndpoint", "net.tcp://localhost:8834/_000WcfStreamService.RPCInvoke.Rpc");
                    bd.Add(() => (client as IDisposable)?.Dispose());

                    RpcData param = new RpcData()
                    {
                        Data1      = "client",
                        StreamData = new MemoryStream()
                    };
                    bd.Add(() => param.StreamData?.Dispose());

                    RpcData ret = await client.InvokeAsync(param.Data1, param.Data2, param.DataLen, param.DataOffset, param.EnumId, param.Flags, param.StreamData);

                    bd.Add(() => ret.StreamData?.Dispose());

                    Console.WriteLine();
                }
            }
            catch (System.Exception ex)
            {
                CommonHelpers.PrintException(ex, Console.Out, true);
            }
        }
        public static async Task <string> ReceiveString(this WebSocket ws, byte[] helperBuffer = null, CancellationToken ct = default(CancellationToken))
        {
            const int BUFFER_SIZE = 16 * 1024;

            if (helperBuffer == null)
            {
                helperBuffer = new byte[BUFFER_SIZE];
            }

            using (var bd = new BatchDisposable())
            {
                bool closed = false;
                WebSocketReceiveResult rr = null;
                MemoryStream           ms = new MemoryStream();
                bd.Add(ms); //cleanup
                for (;;)
                {
                    ArraySegment <byte> seg = new ArraySegment <byte>(helperBuffer);
                    rr = await ws.ReceiveAsync(seg, ct);

                    await ms.WriteAsync(seg.Array, 0, rr.Count);

                    if (rr.MessageType == WebSocketMessageType.Close)
                    {
                        closed = true;
                        break;
                    }
                    if (rr.EndOfMessage)
                    {
                        break;
                    }
                }

                if (closed)
                {
                    return(string.Empty);
                }
                else
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    StreamReader reader = new StreamReader(ms, Encoding.UTF8, false, BUFFER_SIZE, true);
                    bd.Add(reader); //cleanup

                    return(await reader.ReadToEndAsync());
                }
            }
        }