Example #1
0
        public void Run()
        {
            _server = new TcpipServer(new IPEndPoint(IPAddress.Any, 12345));
            _server.NewClient = ch =>
                {
                    _serverService = new Service(ch);
                    _serverService.RegisterLocalService<Func<Task>>(ServerAction);
                };
            _server.StartListening();

            _client = new TcpipClient(new IPEndPoint(IPAddress.Loopback, 12345));
            _clientService = new Service(_client);
            _clientService.OnNewRemoteService.Subscribe(s =>
                {
                    Console.WriteLine("NewRemoteService {0}", s);
                    _clientFunc = _clientService.QueryRemoteService<Func<Task>>();
                });
            _client.ConnectAsync();
            while (_clientFunc==null)
            {
                Thread.Sleep(1);
            }
            _clientFunc().Wait();
            _serverService.Dispose();
            try
            {
                _clientFunc().Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            _server.StopListening();
        }
 public void ConnectNothereFails()
 {
     var e = new AutoResetEvent(false);
     var client = new TcpipClient(_ipEndPoint);
     Exception connectException = null;
     client.OnConnect.Subscribe(
         u => Assert.True(false, "Should not connect"),
         ex => connectException = ex,
         () => Assert.True(false, "Connect should end with exception"));
     client.OnReceive.Subscribe(b => { }, () => e.Set());
     client.ConnectAsync();
     Assert.True(e.WaitOne(TimeSpan.FromSeconds(10)));
     Assert.NotNull(connectException);
 }
 public void ConnectClientToServerClientDisconnects()
 {
     var server = new TcpipServer(_ipEndPoint);
     var e = new AutoResetEvent(false);
     var e1 = new AutoResetEvent(false);
     var e2 = new AutoResetEvent(false);
     bool servercompleted = false;
     bool servercompleted2 = false;
     bool clientcompleted = false;
     bool clientcompleted2 = false;
     bool onServerConnected = false;
     bool onClientConnected = false;
     IPEndPoint clientEndPoint = null;
     IPEndPoint clientEndPoint2 = null;
     server.NewClient = ch =>
         {
             Assert.Equal(_ipEndPoint, (ch as ITcpIpChannel).LocalEndPoint);
             clientEndPoint = (ch as ITcpIpChannel).RemoteEndPoint;
             ch.OnConnect.Subscribe(u => onServerConnected = true,
                                    ex => Assert.True(false, ex.ToString()),
                                    () => servercompleted2 = true);
             ch.OnReceive.Subscribe(bb => Assert.True(false, "receive without send"), ex => Assert.True(false, ex.ToString()), () =>
                 {
                     servercompleted = true; e2.Set();
                 });
             e.Set();
         };
     server.StartListening();
     try
     {
         var client = new TcpipClient(_ipEndPoint);
         client.OnConnect.Subscribe(u =>
             {
                 onClientConnected = true;
                 clientEndPoint2 = client.LocalEndPoint;
                 Assert.Equal(_ipEndPoint, client.RemoteEndPoint);
                 e1.Set();
             },
             ex => Assert.True(false, ex.ToString()),
             () => clientcompleted2 = true);
         client.OnReceive.Subscribe(bb => Assert.True(false, "receive without send"),
                                    ex => Assert.True(false, ex.ToString()),
                                    () => clientcompleted = true);
         client.ConnectAsync();
         Assert.True(e.WaitOne(TimeSpan.FromSeconds(10)));
         Assert.True(e1.WaitOne(TimeSpan.FromSeconds(10)));
         client.Dispose();
         Assert.True(clientcompleted);
         Assert.True(clientcompleted2);
         Assert.True(e2.WaitOne(TimeSpan.FromSeconds(10)));
         Assert.True(servercompleted);
         Assert.True(servercompleted2);
         Assert.True(onClientConnected);
         Assert.True(onServerConnected);
         Assert.Equal(clientEndPoint, clientEndPoint2);
     }
     finally 
     {
         server.StopListening();
     }
 }