Inheritance: PendingOperationManager, IDisposable
Beispiel #1
0
        static IEnumerator<object> SendTask (SocketDataAdapter adapter) {
            var output = new AsyncTextWriter(adapter, Encoding.ASCII);
            output.AutoFlush = true;
            Writer = output;
            string nextMessageText = String.Format("ChatBot{0:00000}", Process.GetCurrentProcess().Id);
            Console.Title = nextMessageText;
            int i = 0;
            yield return new Sleep(new Random(Process.GetCurrentProcess().Id).NextDouble());
            while (true) {
                var f = output.WriteLine(nextMessageText);
                yield return f;

                if (f.Failed) {
                    Disconnected = true;
                    throw new DisconnectedException();
                }

                i += 1;

                if ((i % 1000) == 0)
                    Console.WriteLine("Sent: {0}", i);

                nextMessageText = String.Format("Message {0}", i);
                yield return new Sleep(SendRate);
            }
        }
Beispiel #2
0
        public override void SetUp()
        {
            base.SetUp();
            var adapter = new StreamDataAdapter(this.Stream);

            Writer = new AsyncTextWriter(adapter, Encoding.ASCII);
        }
Beispiel #3
0
 internal TelnetClient (TelnetServer server, TcpClient client) {
     Server = server;
     client.Client.NoDelay = true;
     client.Client.Blocking = false;
     Data = new SocketDataAdapter(client.Client, true);
     Data.ThrowOnDisconnect = false;
     Data.ThrowOnFullSendBuffer = false;
     Encoding encoding = Encoding.ASCII;
     Input = new AsyncTextReader(Data, encoding);
     Output = new AsyncTextWriter(Data, encoding);
     Output.AutoFlush = true;
     _SendFuture = server._Scheduler.Start(SendMessagesTask(), TaskExecutionPolicy.RunWhileFutureLives);
 }
Beispiel #4
0
 public static AsyncTextWriter GetResponseWriter(this HttpListenerContext context, Encoding encoding)
 {
     var adapter = new StreamDataAdapter(context.Response.OutputStream, true);
     var result = new AsyncTextWriter(adapter, encoding);
     result.AutoFlush = true;
     return result;
 }
Beispiel #5
0
 public override void SetUp()
 {
     base.SetUp();
     var adapter = new StreamDataAdapter(this.Stream);
     Writer = new AsyncTextWriter(adapter, Encoding.ASCII);
 }
Beispiel #6
0
            private IEnumerator<object> SendHeadersTask()
            {
                const int writeBufferSize = 1024;

                using (var atw = new AsyncTextWriter(Adapter, Encoding.ASCII, writeBufferSize, false)) {
                    var prologue = String.Format(
                        "HTTP/1.1 {0} {1}",
                        StatusCode, StatusText ?? (StatusCode == 200 ? "OK" : "Unknown")
                    );

                    yield return atw.WriteLine(prologue);

                    foreach (var header in Headers)
                        yield return atw.WriteLine(header.ToString());

                    yield return atw.WriteLine("");

                    yield return atw.Flush();
                }
            }
Beispiel #7
0
        static IEnumerator<object> PeerTask (TcpClient client, Peer peer) {
            var adapter = new SocketDataAdapter(client.Client, true);
            var input = new AsyncTextReader(adapter, Encoding.ASCII);
            var output = new AsyncTextWriter(adapter, Encoding.ASCII);

            adapter.ThrowOnDisconnect = false;
            adapter.ThrowOnFullSendBuffer = false;
            output.AutoFlush = true;

            peer.Input = input;
            peer.Output = output;

            yield return output.WriteLine("Welcome! Please enter your name.");

            string name = null;
            yield return input.ReadLine().Bind(() => name);

            if (name == null) {
                PeerDisconnected(peer);
                yield break;
            }

            peer.Name = name;

            PeerConnected(peer);

            yield return output.Write(VT100.EraseScreen);

            string nextLine = null;

            while (peer.Connected) {
                var f = input.ReadLine();
                yield return f;

                if (!f.GetResult(out nextLine) || nextLine == null) {
                    PeerDisconnected(peer);
                    yield break;
                }

                if (nextLine.Length > 0)
                    DispatchNewMessage(peer, nextLine);
            }
        }