示例#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));
            }
        }
示例#2
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);
        }
示例#3
0
        private static void DependencyTest()
        {
            Console.WriteLine("\nAsyncWriter Dependency Test");

            AsyncTextWriter tw  = new AsyncTextWriter();
            ActuationObject act = new ActuationObject(tw);

            act.Execute();

            WaitForKey();

            tw.Dispose();
        }
示例#4
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);
                }
            }
        }
        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);
        }
示例#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());
                }
            }
示例#7
0
        static MethodProfiler()
        {
            var    beginTime = Stopwatch.GetTimestamp();
            var    now       = LocalTime.Default.Now;
            string applicationName;
            var    assembly = Assembly.GetEntryAssembly();

            if (assembly != null)
            {
                var codeBase = assembly.CodeBase;
                var uri      = new Uri(codeBase);
                var fileName = uri.LocalPath;
                applicationName = fileName;
            }
            else
            {
                var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                applicationName = baseDirectory;
            }

            var path         = Path.GetTempFileName();
            var streamWriter = new StreamWriter(path, false, Encoding.UTF8, 65536);

            TextWriter = new AsyncTextWriter(streamWriter);

            var sb = new StringBuilder();

            sb.AppendFormat(@"declare @applicationId int

exec MethodProfilerApplication_Add {0},{1}",
                            applicationName.ToTSqlNVarChar(),
                            now.ToTSqlDateTime()
                            );
            sb.AppendFormat(",{0},{1}\r\n", beginTime, Stopwatch.Frequency);
            sb.Append("set @applicationId    = @@identity\r\n");
            TextWriter.Write(sb.ToString());
        }