Beispiel #1
0
        public async Task <CancellationToken> Dispatch(string hostname, int port)
        {
            var cts = new CancellationTokenSource();

            await _tcpClient.ConnectAsync(hostname, port);

            var subject = SubjectEx.Create <Stream, byte[], byte[]>(_tcpClient.GetStream(), async x => await ReadFrameAsync(x), NetworkExtensions.IsSocketClosed, x => x == null, async(s, x) => await WriteFrameAsync(s, x), error => { }, () => { });

            subject.Subscribe(buf => Console.WriteLine(Encoding.UTF8.GetString(buf)));

            for (var line = Console.ReadLine(); !string.IsNullOrEmpty(line); line = Console.ReadLine())
            {
                subject.OnNext(Encoding.UTF8.GetBytes(line));
            }

            return(cts.Token);
        }
 public static ISubject <byte[], byte[]> ToSubject(this Stream stream, Func <Stream, Task <byte[]> > producer, Func <Stream, byte[], Task> consumer, Func <Exception, bool> isClosed)
 {
     return(SubjectEx.Create <Stream, byte[], byte[]>(stream, async x => await producer(x), isClosed, x => x == null, async(s, buf) => await consumer(s, buf), error => { }, () => { }));
 }