Example #1
0
 public static void Run(int port, ITrace trace)
 {
     File.Delete("RemoteHost.txt");
     using (TestOutputFile =
                new StreamWriter(
                    File.Open("RemoteHost.txt", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)))
     {
         var serializer = new Serializer <ISerializableMessage>();
         serializer.AddType <TestData>();
         serializer.AddType <TestControl>();
         var serverConfiguration = new ServerConfiguration(port);
         var server = new Net.Server(serverConfiguration, trace);
         Console.WriteLine("Starting server");
         server.NewServerPeer += (server1, serverPeer) =>
         {
             serverPeer.NewConnection += (serverPeer1, connection) => new ServerConnection(serializer, trace).Server_NewConnection(server1, connection);
         };
         server.StartReceiving();
         Console.WriteLine("Setting signal that we are up and running");
         using (var startEvent = EventWaitHandle.OpenExisting("testRemoteServer"))
         {
             startEvent.Set();
         }
         Console.WriteLine("Waiting for stop events");
         StopEvent.WaitOne(600000);
         server.Dispose();
     }
 }
Example #2
0
        public TestSerializer()
        {
            Serializer.AddType(typeof(Class1));
            Serializer.AddType(typeof(Class2));

            Serializer ser = new Serializer(true);

            Class1 c1t = new Class1();

            c1t.a    = 3;
            c1t.b    = 4;
            c1t.c    = 5;
            c1t.d    = "6";
            c1t.e    = new byte[] { 1, 2, 3, 4 };
            c1t.g    = new Class2[2];
            c1t.g[1] = new Class2();
            c1t.g[0] = c1t.g[1];
            c1t.h    = Class1.mye.bbbb;
            c1t.i    = new int[] { 3, 2, 1 };

            c1t.f  = new Class2();
            c1t.ff = c1t.f;

            c1t.f.k = 22;

            byte[] data = ser.Serialize(c1t);

            Class1 c1 = (Class1)ser.Deserialize(data);

            c1.f.k++;

            if (c1.f.k != c1.ff.k)
            {
                throw new Exception("no funciona la reconstrucción de la serialización de objetos serializados mas de una vez :-(");
            }

            c1.g[0].k++;

            if (c1.g[0].k != c1.g[1].k)
            {
                throw new Exception("no funciona la reconstrucción de la serialización de objetos serializados mas de una vez :-(");
            }
        }
Example #3
0
        public void SendDiscoveryRequest_TwoServersRespond()
        {
            // This test should test with more than one server but that would mean to have more local ips
            // to bind to
            var ips             = GetAllIpv4Addresses().ToList();
            var closetEndpoints = FindMostPopulatedSubnet(ips);

            if (closetEndpoints.Count <= 1)
            {
                Assert.True(false, "System does not have more than 1 IP in the same 255.255.255.0 subnet to test with");
            }
            var received = new List <Tuple <ServerDiscoveryResponse, IpEndpoint> >();

            using (var serverDiscovery = new BroadcastClient(new IpEndpoint(closetEndpoints.First(), 4000), 4338, 3, new NullTrace()))
            {
                var serializer = new Serializer <ISerializableMessage>();
                serializer.AddType <ServerDiscoveryResponse>();
                serializer.AddType <ServerDiscoveryRequest>();
                var typedConnection = new TypedConnection <ISerializableMessage>(serverDiscovery.Connection, serializer);
                typedConnection.NewMessage += (p1, p2, p3) =>
                {
                    received.Add(Tuple.Create((ServerDiscoveryResponse)p2, serverDiscovery.CurrentEndpoint));
                };
                serverDiscovery.StartReceiving();
                var amountConnections = new int[1];
                var servers           = new List <Server>();

                ips = closetEndpoints;
                int amountOfServer = ips.Count;
                for (int i = 0; i < amountOfServer; i++)
                {
                    var copyOfi = i + 1;
                    var server  = new Server(new ServerConfiguration(new IpEndpoint(ips[i], 4338)), new NullTrace());
                    server.NewServerPeer += (server1, serverPeer) =>
                    {
                        serverPeer.NewConnection += (p1, p2) =>
                        {
                            amountConnections[0]++;
                            int runningId = 0;
                            var typed     = new TypedConnection <ISerializableMessage>(p2, serializer);
                            typed.NewMessage += (p3, p4, p5) =>
                            {
                                var request  = ((ServerDiscoveryRequest)p4);
                                var response = new ServerDiscoveryResponse {
                                    ServerVersion = request.ClientVersion * copyOfi, RunningId = runningId++
                                };
                                typed.Send(response);
                            };
                        };
                    };
                    servers.Add(server);
                    server.StartReceiving();
                }
                try
                {
                    for (int i = 0; i < 100; i++)
                    {
                        var receivedBefore = received.Count;
                        typedConnection.Send(new ServerDiscoveryRequest()
                        {
                            ClientVersion = i
                        });
                        System.Threading.Thread.Sleep(1);
                        Assert.True(Wait.UntilTrue(() => receivedBefore + amountOfServer == received.Count));
                    }
                    Assert.Equal(amountOfServer * 100, received.Count);
                    for (var index = 0; index < ips.Count; index++)
                    {
                        var ipAddress = ips[index];
                        var byIp      = received.Where(_ => _.Item2.IpAddress.Equals(ipAddress)).ToList();
                        Assert.Equal(100, byIp.Count);
                        byIp.Sort((p1, p2) => p1.Item1.ServerVersion.CompareTo(p2.Item1.ServerVersion));
                        Assert.Equal(Enumerable.Range(0, 100).Select(_ => _ * (1 + index)), byIp.Select(_ => _.Item1.ServerVersion));
                    }
                }
                finally
                {
                    foreach (var server in servers)
                    {
                        server.Dispose();
                    }
                }
            }
        }