Esempio n. 1
0
        public void Connect_Missing_TCP_Port()
        {
            var tcp = new Tcp();

            ExceptionAssert.Throws <Exception>(() =>
            {
                var _ = tcp.ConnectAsync("/ip4/127.0.0.1/udp/32700").Result;
            });
            ExceptionAssert.Throws <Exception>(() =>
            {
                var _ = tcp.ConnectAsync("/ip4/127.0.0.1").Result;
            });
        }
Esempio n. 2
0
        public async Task Listen()
        {
            var          tcp             = new Tcp();
            var          cs              = new CancellationTokenSource(TimeSpan.FromSeconds(30));
            var          connected       = false;
            MultiAddress listenerAddress = null;

            void Handler(Stream stream, MultiAddress local, MultiAddress remote)
            {
                Assert.IsNotNull(stream);
                Assert.AreEqual(listenerAddress, local);
                Assert.IsNotNull(remote);
                Assert.AreNotEqual(local, remote);
                connected = true;
            }

            try
            {
                listenerAddress = tcp.Listen("/ip4/127.0.0.1", Handler, cs.Token);
                Assert.IsTrue(listenerAddress.Protocols.Any(p => p.Name == "tcp"));
                await using (var stream = await tcp.ConnectAsync(listenerAddress, cs.Token))
                {
                    await Task.Delay(50, cs.Token);

                    Assert.IsNotNull(stream);
                    Assert.IsTrue(connected);
                }
            }
            finally
            {
                cs.Cancel();
            }
        }
Esempio n. 3
0
        static void ClientConnect()
        {
            var tcp  = new Tcp();
            var peer = tcp.ConnectAsync("/ip4/127.0.0.1/tcp/4002").Result;

            Message.WriteAsync("/multistream/1.0.0", peer).Wait();
            var got = Message.ReadStringAsync(peer).Result;

            Message.WriteAsync("/plaintext/1.0.0", peer).Wait();
            got = Message.ReadStringAsync(peer).Result;

            Message.WriteAsync("/multistream/1.0.0", peer).Wait();
            got = Message.ReadStringAsync(peer).Result;

            //Message.WriteAsync("/ipfs/id/1.0.0", peer).Wait();
            //got = Message.ReadStringAsync(peer).Result;
            //new Identify1().ProcessMessagesAsync(peer).Wait();
            while (true)
            {
                var b = peer.ReadByte();
                if (b == -1)
                {
                    break;
                }
                Console.WriteLine("got {0:x2} '{1}'", b, (char)b);
            }
        }
Esempio n. 4
0
        public async Task Listen_Handler_Throws()
        {
            var tcp    = new Tcp();
            var cs     = new CancellationTokenSource(TimeSpan.FromSeconds(30));
            var called = false;

            void Handler(Stream stream, MultiAddress local, MultiAddress remote)
            {
                called = true;
                throw new Exception("foobar");
            }

            try
            {
                var addr = tcp.Listen("/ip4/127.0.0.1", Handler, cs.Token);
                Assert.IsTrue(addr.Protocols.Any(p => p.Name == "tcp"));
                await using (var stream = await tcp.ConnectAsync(addr, cs.Token))
                {
                    await Task.Delay(50, cs.Token);

                    Assert.IsNotNull(stream);
                    Assert.IsTrue(called);
                }
            }
            finally
            {
                cs.Cancel();
            }
        }
Esempio n. 5
0
        public async Task ConnectAsync(string hostname, int port, CancellationToken token)
        {
            token.Register(() => Tcp.Close());
            await Tcp.ConnectAsync(hostname, port);

            Transceiver.Start(Tcp);
        }
Esempio n. 6
0
        // LibuvSharp Tcp Client
        public static async Task LoopClient()
        {
            using (var client = new Tcp()) {
                await client.ConnectAsync(Default.IPEndPoint);

                await client.WriteAsync("Hello World from LibuvSharp!");
            }
        }
Esempio n. 7
0
        public void Connect_Unknown_Address()
        {
            var tcp = new Tcp();

            ExceptionAssert.Throws <SocketException>(() =>
            {
                var _ = tcp.ConnectAsync("/ip4/127.0.10.10/tcp/32700").Result;
            });
        }
Esempio n. 8
0
        public void Connect_Missing_IP_Address()
        {
            var tcp = new Tcp();

            ExceptionAssert.Throws <Exception>(() =>
            {
                var _ = tcp.ConnectAsync("/tcp/32700").Result;
            });
        }
Esempio n. 9
0
        public void CanSendHandles()
        {
            int count = 0;

            Loop.Default.Run(async() => {
                var handles      = new Stack <Handle>();
                string name      = "test";
                var pipelistener = new IPCPipeListener();
                pipelistener.Bind(name);
                pipelistener.Connection += () => {
                    var client = pipelistener.Accept();
                    client.Resume();
                    client.HandleData += (handle, data) => {
                        handles.Push(handle);
                        count++;
                        if (count == 3)
                        {
                            foreach (var h in handles)
                            {
                                h.Close();
                            }
                            pipelistener.Close();
                        }
                    };
                };
                pipelistener.Listen();

                var pipe = new IPCPipe();
                await pipe.ConnectAsync(name);


                var ipep        = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7000);
                var tcplistener = new TcpListener();
                tcplistener.Bind(ipep);
                tcplistener.Connection += () => {
                    var client = tcplistener.Accept();
                    pipe.Write(client, new byte[1], (ex) => {
                        client.Close();
                        tcplistener.Close();
                    });
                };
                tcplistener.Listen();

                var tcp = new Tcp();
                await tcp.ConnectAsync(ipep);
                tcp.Write("HELLO WORLD");

                var udp = new Udp();
                udp.Bind(ipep);
                pipe.Write(udp, Encoding.Default.GetBytes("UDP"), (ex) => udp.Close());
                pipe.Write(pipe, Encoding.Default.GetBytes("pipe"), (ex) => pipe.Close());
            });

            Assert.Equal(3, count);
        }
Esempio n. 10
0
        public void Connect_Cancelled()
        {
            var tcp = new Tcp();
            var cs  = new CancellationTokenSource();

            cs.Cancel();
            ExceptionAssert.Throws <OperationCanceledException>(() =>
            {
                var _ = tcp.ConnectAsync("/ip4/127.0.10.10/tcp/32700", cs.Token).Result;
            });
        }
Esempio n. 11
0
		void TestCanSendHandles(string pipename, IPEndPoint ipep)
		{
			int count = 0;

			Loop.Default.Run(async () => {
				var handles = new Stack<Handle>();
				var pipelistener = new IPCPipeListener();
				pipelistener.Bind(pipename);
				pipelistener.Connection += () => {
					var client = pipelistener.Accept();
					client.Resume();
					client.HandleData += (handle, data) => {
						handles.Push(handle);
						count++;
						if (count == 3) {
							foreach (var h in handles) {
								h.Close();
							}
							pipelistener.Close();
						}
					};
				};
				pipelistener.Listen();

				var pipe = new IPCPipe();
				await pipe.ConnectAsync(pipename);

				var tcplistener = new TcpListener();
				tcplistener.Bind(ipep);
				tcplistener.Connection += () => {
					var client = tcplistener.Accept();
					pipe.Write(client, new byte[1], (ex) => {
						client.Close();
						tcplistener.Close();
					});
				};
				tcplistener.Listen();

				var tcp = new Tcp();
				await tcp.ConnectAsync(ipep);
				tcp.Write("HELLO WORLD");

				var udp = new Udp();
				udp.Bind(ipep);
				pipe.Write(udp, Encoding.Default.GetBytes("UDP"), (ex) => udp.Close());
				pipe.Write(pipe, Encoding.Default.GetBytes("pipe"), (ex) => pipe.Close());
			});

			Assert.Equal(3, count);
		}
Esempio n. 12
0
 public static void Main(string[] args)
 {
     Loop.Default.Run(async() => {
         var tcp = new Tcp();
         await tcp.ConnectAsync(Dns.GetHostAddressesAsync, "ip.bentkus.eu", 80);
         tcp.TryWrite("GET / HTTP/1.1\r\nHost: ip.bentkus.eu\r\n\r\n");
         var data = await tcp.ReadStructAsync();
         if (data.HasValue)
         {
             Console.WriteLine(Encoding.ASCII.GetString(data.Value.Array, data.Value.Offset, data.Value.Count));
         }
         Console.WriteLine("Connected!");
     });
 }
Esempio n. 13
0
        public async Task TimeProtocol()
        {
            var cs     = new CancellationTokenSource(TimeSpan.FromSeconds(30));
            var server = await new MultiAddress("/dns4/time.nist.gov/tcp/37").ResolveAsync(cs.Token);
            var data   = new byte[4];

            var tcp = new Tcp();

            await using (var time = await tcp.ConnectAsync(server[0], cs.Token))
            {
                var n = await time.ReadAsync(data, 0, data.Length, cs.Token);

                Assert.AreEqual(4, n); // sometimes zero!
            }
        }
Esempio n. 14
0
        public async Task SendReceive()
        {
            var cs  = new CancellationTokenSource(TimeSpan.FromSeconds(30));
            var tcp = new Tcp();

            using (var server = new HelloServer())
                using (var stream = await tcp.ConnectAsync(server.Address, cs.Token))
                {
                    var bytes = new byte[5];
                    await stream.ReadAsync(bytes, 0, bytes.Length);

                    Console.WriteLine("got " + Encoding.UTF8.GetString(bytes));
                    //Assert.AreEqual("hello", Encoding.UTF8.GetString(bytes));
                }
        }
Esempio n. 15
0
        public static async Task Client()
        {
            try {
                using (var client = new Tcp()) {
                    await client.ConnectAsync(Default.IPEndPoint);

                    client.Write("Labas Pasauli!");
                    var str = await client.ReadStringAsync();

                    Console.WriteLine("From Server: {0}", str);
                }
            } catch (Exception e) {
                Console.WriteLine("Client Exception:");
                Console.WriteLine(e);
            }
        }
Esempio n. 16
0
        public static async Task Client()
        {
            try {
                var client = new Tcp();
                await client.ConnectAsync(ep);

                client.Write(Encoding.ASCII, "Labas Pasauli!");
                var str = await client.ReadStringAsync();

                Console.WriteLine("From Server: {0}", str);

                client.Shutdown();
            } catch (Exception e) {
                Console.WriteLine("Client Exception:");
                Console.WriteLine(e);
            }
        }
Esempio n. 17
0
        public async static Task ConnectAsync(this Tcp tcp, IEnumerable <IPEndPoint> endPoints)
        {
            Exception lastException = null;
            bool      connected     = false;

            foreach (var endPoint in endPoints)
            {
                try {
                    await tcp.ConnectAsync(endPoint);

                    connected = true;
                    break;
                } catch (Exception exception) {
                    lastException = exception;
                    continue;
                }
            }

            if (!connected)
            {
                throw lastException;
            }
        }
Esempio n. 18
0
 public async static Task ConnectAsync(this Tcp tcp, Func <string, Task <IEnumerable <IPAddress> > > resolveFunctionAsync, string hostNameOrAddress, int port)
 {
     await tcp.ConnectAsync(resolveFunctionAsync(hostNameOrAddress), port);
 }
Esempio n. 19
0
 public async static Task ConnectAsync(this Tcp tcp, Func <string, IPAddress[]> resolveFunction, DnsEndPoint dnsEndPoint)
 {
     await tcp.ConnectAsync(resolveFunction, dnsEndPoint.Host, dnsEndPoint.Port);
 }
Esempio n. 20
0
 public async static Task ConnectAsync(this Tcp tcp, Func <string, IPAddress[]> resolveFunction, string hostNameOrAddress, int port)
 {
     await tcp.ConnectAsync(tcp.Loop.QueueUserWorkItemAsync(() => resolveFunction(hostNameOrAddress)), port);
 }
Esempio n. 21
0
 public async static Task ConnectAsync(this Tcp tcp, Func <string, Task <IEnumerable <IPAddress> > > resolveFunctionAsync, DnsEndPoint dnsEndPoint)
 {
     await tcp.ConnectAsync(resolveFunctionAsync, dnsEndPoint.Host, dnsEndPoint.Port);
 }
Esempio n. 22
0
 public async static Task ConnectAsync(this Tcp tcp, Task <IPAddress[]> resolver, int port)
 {
     await tcp.ConnectAsync(resolver.AsIEnumerable(), port);
 }
Esempio n. 23
0
 public async static Task ConnectAsync(this Tcp tcp, Task <IEnumerable <IPAddress> > resolver, int port)
 {
     await tcp.ConnectAsync((await resolver).Select(ip => new IPEndPoint(ip, port)));
 }