Пример #1
0
        public void BasicRoundTrip()
        {
            var serializer = new JsonCommonSerializer();

            var port = new Random().Next(6000, 60000);

            var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram);

            listener.Bind(IPAddress.Loopback, port);
            listener.Listen(100);
            var serverTransport = listener.GenerateTransportSource(true);
            var serverRouter    = new DefaultMessageRouter(serverTransport, serializer);

            serverRouter.AddService <IMyService>(new MyService());

            var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram);

            client.Connect(IPAddress.Loopback, port);
            var clientTransport = client.GenerateTransportSource(false);
            var clientRouter    = new DefaultMessageRouter(clientTransport, serializer);
            var proxy           = clientRouter.AddInterface <IMyService>();

            var result = proxy.Add(3, 4).Result;

            Assert.Equal(7, result);

            clientRouter.Dispose();
            clientTransport.Dispose();
            client.Dispose();

            serverRouter.Dispose();
            serverTransport.Dispose();
            listener.Dispose();
        }
Пример #2
0
        public void Wait_for_accept()
        {
            using (Udt.SocketPoller poller = new Udt.SocketPoller())
                using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream))
                {
                    socket.Bind(IPAddress.Loopback, 0);
                    socket.Listen(100);
                    ManualResetEvent doneEvent = new ManualResetEvent(false);

                    poller.AddSocket(socket);

                    Task.Factory.StartNew(() =>
                    {
                        using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream))
                        {
                            client.Connect(IPAddress.Loopback, socket.LocalEndPoint.Port);
                            doneEvent.WaitOne(1000);
                        }
                    });

                    Assert.IsTrue(poller.Wait(TimeSpan.FromSeconds(1)));
                    CollectionAssert.AreEqual(new[] { socket }, poller.WriteSockets);
                    CollectionAssert.AreEqual(new[] { socket }, poller.ReadSockets);

                    Udt.Socket acceptedSocket = socket.Accept();
                    acceptedSocket.Dispose();
                    doneEvent.Set();

                    Assert.IsTrue(poller.Wait(TimeSpan.Zero));
                    CollectionAssert.AreEqual(new[] { socket }, poller.WriteSockets);
                    CollectionAssert.IsEmpty(poller.ReadSockets);
                }
        }
Пример #3
0
		public void BasicRoundTrip()
		{
			var serializer = new JsonCommonSerializer();

			var port = new Random().Next(6000, 60000);

			var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram);
			listener.Bind(IPAddress.Loopback, port);
			listener.Listen(100);
			var serverTransport = listener.GenerateTransportSource(true);
			var serverRouter = new DefaultMessageRouter(serverTransport, serializer);
			serverRouter.AddService<IMyService>(new MyService());

			var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram);
			client.Connect(IPAddress.Loopback, port);
			var clientTransport = client.GenerateTransportSource(false);
			var clientRouter = new DefaultMessageRouter(clientTransport, serializer);
			var proxy = clientRouter.AddInterface<IMyService>();

			var result = proxy.Add(3, 4).Result;
			Assert.Equal(7, result);

			clientRouter.Dispose();
			clientTransport.Dispose();
			client.Dispose();

			serverRouter.Dispose();
			serverTransport.Dispose();
			listener.Dispose();
		}
Пример #4
0
 public void Add_socket_to_disposed_poller()
 {
     Udt.SocketPoller poller = new Udt.SocketPoller();
     poller.Dispose();
     Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
     Assert.Throws <ObjectDisposedException>(() => poller.AddSocket(socket));
     socket.Dispose();
 }
Пример #5
0
 public void Add_socket_to_disposed_poller()
 {
     Udt.SocketPoller poller = new Udt.SocketPoller();
     poller.Dispose();
     Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
     Assert.Throws<ObjectDisposedException>(() => poller.AddSocket(socket));
     socket.Dispose();
 }
Пример #6
0
 public void Add_closed_socket()
 {
     using (Udt.SocketPoller poller = new Udt.SocketPoller())
     {
         Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
         socket.Dispose();
         Assert.Throws <Udt.SocketException>(() => poller.AddSocket(socket));
     }
 }
Пример #7
0
 public void Add_closed_socket()
 {
     using (Udt.SocketPoller poller = new Udt.SocketPoller())
     {
         Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
         socket.Dispose();
         Assert.Throws<Udt.SocketException>(() => poller.AddSocket(socket));
     }
 }
Пример #8
0
 public void Remove_closed_socket()
 {
     using (Udt.SocketPoller poller = new Udt.SocketPoller())
     {
         Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
         poller.AddSocket(socket);
         socket.Dispose();
         poller.RemoveSocket(socket);
     }
 }
Пример #9
0
 public void Remove_closed_socket()
 {
     using (Udt.SocketPoller poller = new Udt.SocketPoller())
     {
         Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
         poller.AddSocket(socket);
         socket.Dispose();
         poller.RemoveSocket(socket);
     }
 }
Пример #10
0
        public void Bind_IPAddress_int__AfterClosed()
        {
            int port = _portNum++;

            Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
            socket.Dispose();

            Assert.Throws <ObjectDisposedException>(() =>
            {
                socket.Bind(IPAddress.Any, port);
            });
        }
Пример #11
0
 private void ShutdownSocket()
 {
     try
     {
         socket.Close();
         socket.Dispose();
     }
     catch (Exception ex)
     {
         LOGGER.Error("Socket close exception", ex);
     }
 }
Пример #12
0
        public void Benchmark()
        {
            //var serializerSource = new Newtonsoft.Json.JsonSerializer();
            var serializer = new ProtobufCommonSerializer();            //new JsonCommonSerializer(serializerSource); //

            var port = new Random().Next(6000, 60000);

            var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram);

            listener.Bind(IPAddress.Loopback, port);
            listener.Listen(100);
            var serverTransport = listener.GenerateTransportSource(true);
            var serverRouter    = new DefaultMessageRouter(serverTransport, serializer);

            serverRouter.AddService <ISumService>(new SumService());

            var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram);

            client.Connect(IPAddress.Loopback, port);
            var clientTransport = client.GenerateTransportSource(false);
            var clientRouter    = new DefaultMessageRouter(clientTransport, serializer);
            var proxy           = clientRouter.AddInterface <ISumService>();

            const int randCnt = 100;
            var       rand    = new Random(42);
            var       randoms = new int[randCnt];

            for (int i = 0; i < randCnt; i++)
            {
                randoms[i] = rand.Next(10000000, 20000000);
            }

            var       sw = new Stopwatch();
            long      timeFromClient = 0, timeToClient = 0;
            const int cnt = 1000;

            for (int j = 0; j < cnt; j++)
            {
                sw.Start();
                var sum = proxy.Sum(randoms).Result;
                sw.Stop();
                Assert.Equal(randoms.Sum(), sum);
                for (int i = 0; i < randCnt; i++)
                {
                    randoms[i] = rand.Next(10000000, 20000000);
                }
                var times = proxy.TimeDiff(Stopwatch.GetTimestamp()).Result;
                timeFromClient += times.Item1;
                timeToClient   += Stopwatch.GetTimestamp() - times.Item2;
            }

            _testOutputHelper.WriteLine("Completed {0} sum passes in {1}ms", cnt, sw.ElapsedMilliseconds);
            _testOutputHelper.WriteLine("Client to server latency: {0}us", timeFromClient / cnt / 10);
            _testOutputHelper.WriteLine("Server to client latency: {0}us", timeToClient / cnt / 10);

            sw.Reset();
            var tree = new SumServiceTree();

            SumServiceTree.FillTree(tree, rand, 2);
            _testOutputHelper.WriteLine("Starting large message transfer.");
            sw.Start();
            var result = proxy.Increment(tree).Result;

            sw.Stop();
            Assert.Equal(tree.Leaf + 1, result.Leaf);
            _testOutputHelper.WriteLine("Completed large transfer in {0}ms", sw.Elapsed.TotalMilliseconds);

            clientRouter.Dispose();
            clientTransport.Dispose();
            client.Dispose();

            serverRouter.Dispose();
            serverTransport.Dispose();
            listener.Dispose();
        }
Пример #13
0
		public void Benchmark()
		{
			//var serializerSource = new Newtonsoft.Json.JsonSerializer();
			var serializer = new ProtobufCommonSerializer();//new JsonCommonSerializer(serializerSource); // 

			var port = new Random().Next(6000, 60000);

			var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram);
			listener.Bind(IPAddress.Loopback, port);
			listener.Listen(100);
			var serverTransport = listener.GenerateTransportSource(true);
			var serverRouter = new DefaultMessageRouter(serverTransport, serializer);
			serverRouter.AddService<ISumService>(new SumService());

			var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram);
			client.Connect(IPAddress.Loopback, port);
			var clientTransport = client.GenerateTransportSource(false);
			var clientRouter = new DefaultMessageRouter(clientTransport, serializer);
			var proxy = clientRouter.AddInterface<ISumService>();

			const int randCnt = 100;
			var rand = new Random(42);
			var randoms = new int[randCnt];
			for (int i = 0; i < randCnt; i++) randoms[i] = rand.Next(10000000, 20000000);

			var sw = new Stopwatch();
			long timeFromClient = 0, timeToClient = 0;
			const int cnt = 1000;
			for (int j = 0; j < cnt; j++)
			{
				sw.Start();
				var sum = proxy.Sum(randoms).Result;
				sw.Stop();
				Assert.Equal(randoms.Sum(), sum);
				for (int i = 0; i < randCnt; i++) randoms[i] = rand.Next(10000000, 20000000);
				var times = proxy.TimeDiff(Stopwatch.GetTimestamp()).Result;
				timeFromClient += times.Item1;
				timeToClient += Stopwatch.GetTimestamp() - times.Item2;
			}

			_testOutputHelper.WriteLine("Completed {0} sum passes in {1}ms", cnt, sw.ElapsedMilliseconds);
			_testOutputHelper.WriteLine("Client to server latency: {0}us", timeFromClient / cnt / 10);
			_testOutputHelper.WriteLine("Server to client latency: {0}us", timeToClient / cnt / 10);

			sw.Reset();
			var tree = new SumServiceTree();
			SumServiceTree.FillTree(tree, rand, 2);
			_testOutputHelper.WriteLine("Starting large message transfer.");
			sw.Start();
			var result = proxy.Increment(tree).Result;
			sw.Stop();
			Assert.Equal(tree.Leaf + 1, result.Leaf);
			_testOutputHelper.WriteLine("Completed large transfer in {0}ms", sw.Elapsed.TotalMilliseconds);

			clientRouter.Dispose();
			clientTransport.Dispose();
			client.Dispose();

			serverRouter.Dispose();
			serverTransport.Dispose();
			listener.Dispose();
		}
Пример #14
0
        public void Bind_IPAddress_int__AfterClosed()
        {
            int port = _portNum++;

            Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
            socket.Dispose();

            Assert.Throws<ObjectDisposedException>(() =>
            {
                socket.Bind(IPAddress.Any, port);
            });
        }