示例#1
0
        public void CreatePipeServer()
        {
            var server = new W.IO.Pipes.PipeServer <W.IO.Pipes.PipeClient>(PIPENAME);

            server.Start();
            server.Started += s => { s.Stop(); };
            server.Dispose();
        }
示例#2
0
        public void TestNamedPipeLoggingWithHost()
        {
            Console.WriteLine("If this test fails, try it again.  It just might work.");
            var mreQuit = new System.Threading.ManualResetEvent(false);
            int count   = 0;

            using (var server = new W.IO.Pipes.PipeServer <W.IO.Pipes.PipeClient>(PIPENAME))
            {
                server.ClientConnected += (r) =>
                {
                    Console.WriteLine("Server accepted client");
                    r.MessageReceived += (o, m) =>
                    {
                        count += 1;
                        Console.WriteLine(m.AsString());
                        if (count == 10)
                        {
                            mreQuit.Set();
                        }
                    };
                };
                server.Started += s =>
                {
                    Console.WriteLine("Started");

                    using (var client = new W.IO.Pipes.PipeClient())
                    {
                        if (client.Connect(PIPENAME, System.IO.Pipes.PipeDirection.InOut))
                        {
                            for (int t = 0; t < 10; t++)
                            {
                                client.Write(string.Format("Test Message {0}", t).AsBytes());
                            }
                            Assert.IsTrue(mreQuit.WaitOne(20000));
                        }
                        else
                        {
                            Console.WriteLine("Failed to connect to the server");
                        }
                    }
                };
                server.Start();

                Assert.IsTrue(mreQuit.WaitOne(20000));
                Console.WriteLine("Complete");
            }
        }
示例#3
0
        public void PipeTransmit()
        {
            var mreMessageReceived = new System.Threading.ManualResetEvent(false);
            var mreServerReady     = new System.Threading.ManualResetEvent(false);
            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            using (var server = new W.IO.Pipes.PipeServer <W.IO.Pipes.PipeClient>(PIPENAME))
            {
                server.ClientConnected += c =>
                {
                    c.As <W.IO.Pipes.PipeClient>().MessageReceived += (o, message) =>
                    {
                        Assert.IsTrue(o == c);
                        Assert.IsTrue(message.AsString() == "Test");
                        Console.WriteLine("{0} Server Received Message", sw.Elapsed);
                        mreMessageReceived.Set();
                    };
                };
                server.Started += s =>
                {
                    mreServerReady.Set();
                };
                server.Start();
                Assert.IsTrue(mreServerReady.WaitOne(1000));

                using (var client = new W.IO.Pipes.PipeClient())
                {
                    Assert.IsTrue(client.Connect(PIPENAME));
                    client.Write("Test".AsBytes());
                    Assert.IsTrue(mreMessageReceived.WaitOne(10000));
                    Console.WriteLine("{0} Client Disposing", sw.Elapsed);
                }
                Console.WriteLine("{0} Client Disposed", sw.Elapsed);
            }
            Console.WriteLine("{0} Server Disposed", sw.Elapsed);
            sw.Stop();
        }
示例#4
0
        public static void Run()
        {
            var pipeName = "pipe-test";
            var mre      = new ManualResetEvent(false);
            var mreExit  = new ManualResetEvent(false);

            using (var server = new W.IO.Pipes.PipeServer <W.IO.Pipes.PipeClient>(pipeName))
            {
                server.ClientConnected += (client) =>
                {
                    client.MessageReceived += (o, m) =>
                    {
                        Console.WriteLine("Server Echo: " + m.AsString());
                        //echo the message
                        client.Write(m);
                    };
                };
                server.Started += (s) =>
                {
                    Console.WriteLine("Server Started");

                    using (var client = new W.IO.Pipes.PipeClient())
                    {
                        client.Disconnected += (o, e) =>
                        {
                            if (e != null)
                            {
                                Console.WriteLine("Client Disconnected: " + e.Message);
                            }
                            else
                            {
                                Console.WriteLine("Client Disconnected");
                            }
                        };
                        client.Connected += (o) =>
                        {
                            Console.WriteLine("Client Connected");
                        };
                        client.MessageReceived += (o, m) =>
                        {
                            Console.WriteLine("Client Received: " + m.AsString());
                            mre.Set();
                        };
                        Console.WriteLine("Press Any Key To Connect To the Server");
                        Console.ReadKey();
                        var connected = client.Connect(pipeName, System.IO.Pipes.PipeDirection.InOut);
                        if (connected)
                        {
                            while (true)
                            {
                                Console.Write("Send <Return to Exit>:");
                                var msg = Console.ReadLine();
                                if (string.IsNullOrEmpty(msg))
                                {
                                    break;
                                }
                                client.Write(msg.Trim().AsBytes());

                                mre.WaitOne(5000); //wait for the server's response
                                mre.Reset();
                            }
                            mreExit.Set();//release the server's loop
                        }
                        else
                        {
                            Console.WriteLine("Failed to connect to the pipe server");
                            Console.WriteLine("Press Any Key To Exit");
                            Console.ReadKey();
                            mreExit.Set();
                        }
                    }
                };
                server.Start();
                mreExit.WaitOne();
            }
            mre?.Dispose();
            mreExit?.Dispose();
        }