Esempio n. 1
0
        void ConnectTogether(UdpNetworkDriver server, UdpNetworkDriver client, int maxIterations, out NetworkConnection serverConnection, out NetworkConnection clientConnection)
        {
            int servers = 0, clients = 0, iterations = 0;

            serverConnection = default(NetworkConnection);
            clientConnection = default(NetworkConnection);

            DataStreamReader reader;

            NetworkConnection poppedConnection = default(NetworkConnection);

            while (clients != 1 || servers != 1)
            {
                Assert.Less(iterations++, maxIterations);

                server.ScheduleUpdate().Complete();

                var newConnection = server.Accept();
                if (newConnection != default(NetworkConnection))
                {
                    clients++;
                    clientConnection = newConnection;
                }

                if (client.PopEvent(out poppedConnection, out reader) == ExperimentalEventType.Connect)
                {
                    serverConnection = poppedConnection;
                    servers++;
                }

                client.ScheduleUpdate().Complete();
                Assert.AreNotEqual(clientConnection, default(NetworkConnection));
            }
        }
Esempio n. 2
0
        ExperimentalEventType PollEvent(ExperimentalEventType ev, int maxIterations, UdpNetworkDriver socket, ref byte[] buffer, out int size, out NetworkConnection connection)
        {
            int iterator = 0;

            size       = 0;
            connection = default(NetworkConnection);

            while (iterator++ < maxIterations)
            {
                DataStreamReader reader;
                var context = default(DataStreamReader.Context);
                ExperimentalEventType e;
                if ((e = socket.PopEvent(out connection, out reader)) == ev)
                {
                    if (reader.IsCreated)
                    {
                        reader.ReadBytesIntoArray(ref context, ref buffer, reader.Length);
                        size = reader.Length;
                    }
                    return(e);
                }
                socket.ScheduleUpdate().Complete();
            }
            return(ExperimentalEventType.Empty);
        }
Esempio n. 3
0
        public void UdpC_ConnectSendTest_ShouldConnectAndReceiveData()
        {
            using (var server = new UdpNetworkDriver(new NetworkDataStreamParameter {
            }))
                using (var client = new UdpNetworkDriver(new NetworkDataStreamParameter {
                }))
                {
                    ushort serverPort = 50008;

                    server.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                    client.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));

                    server.Listen();

                    client.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));

                    NetworkConnection serverConnection, clientConnection;
                    int maxIterations = 100;

                    ConnectTogether(server, client, maxIterations, out serverConnection, out clientConnection);

                    var message = new byte[]
                    {
                        (byte)'m',
                        (byte)'e',
                        (byte)'s',
                        (byte)'s',
                        (byte)'a',
                        (byte)'g',
                        (byte)'e'
                    };

                    SendReceive(client, server, clientConnection, serverConnection, message, maxIterations);
                }
        }
Esempio n. 4
0
    private void Start()
    {
        Driver     = new UdpCNetworkDriver(new INetworkParameter[0]);
        Connection = default;

        NetworkEndPoint endpoint = NetworkEndPoint.Parse("127.0.0.1", 9000);

        Connection = Driver.Connect(endpoint);
    }
Esempio n. 5
0
        public void UdpC_BindToEndpoint_ReturnSocketHandle()
        {
            using (var socket = new UdpNetworkDriver(new NetworkDataStreamParameter {
            }))
            {
                var endpoint    = NetworkEndPoint.Parse(IPAddress.Any.ToString(), 0);
                var socketError = socket.Bind(endpoint);

                Assert.AreEqual(socketError, (int)SocketError.Success);
            }
        }
Esempio n. 6
0
        public UdpCClient(int port, int serverPort = -1)
        {
            m_Socket = new UdpNetworkDriver(new NetworkDataStreamParameter {
            });
            m_Socket.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), (ushort)port));
            if (serverPort == -1)
            {
                m_Socket.Listen();
            }

            this.serverPort = serverPort;
        }
Esempio n. 7
0
    private void Start()
    {
        Driver = new UdpCNetworkDriver(new INetworkParameter[0]);
        if (Driver.Bind(NetworkEndPoint.Parse("0.0.0.0", 9000)) != 0)
        {
            Debug.Log("Failed to bind to port 9000");
        }
        else
        {
            Driver.Listen();
        }

        Connections = new NativeList <NetworkConnection>(16, Allocator.Persistent);
    }
Esempio n. 8
0
        public void UdpC_ListenThenConnect_ShouldFail()
        {
            using (var socket = new UdpNetworkDriver(new NetworkDataStreamParameter {
            }))
            {
                var endpoint = NetworkEndPoint.Parse(IPAddress.Any.ToString(), 50007);
                socket.Bind(endpoint);

                socket.Listen();

                var error = Assert.Throws <SocketException>(() => { socket.Connect(endpoint); });
                Assert.AreEqual(error.SocketErrorCode, SocketError.AddressNotAvailable);
            }
        }
Esempio n. 9
0
        public void UdpC_BindMultipleToSameEndpoint_ReturnSocketError()
        {
            using (var first = new UdpNetworkDriver(new NetworkDataStreamParameter {
            }))
                using (var second = new UdpNetworkDriver(new NetworkDataStreamParameter {
                }))
                {
                    var endpoint = NetworkEndPoint.Parse(IPAddress.Any.ToString(), 50001);

                    var socketError = first.Bind(endpoint);
                    Assert.AreEqual(socketError, (int)SocketError.Success);

                    var error = second.Bind(endpoint);
                    Assert.AreEqual(error, (int)SocketError.AddressAlreadyInUse);
                }
        }
Esempio n. 10
0
        public void UdpC_ReconnectAndResend_ShouldReconnectAndResend()
        {
            using (var server = new UdpNetworkDriver(new NetworkDataStreamParameter {
            }))
                using (var client = new UdpNetworkDriver(new NetworkDataStreamParameter {
                }))
                {
                    ushort serverPort = 50007;

                    server.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                    client.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));

                    server.Listen();

                    NetworkConnection serverConnection, clientConnection;
                    int maxIterations = 100;

                    var id = client.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                    ConnectTogether(server, client, maxIterations, out serverConnection, out clientConnection);

                    client.Disconnect(id);

                    server.ScheduleUpdate().Complete();

                    var data = new byte[1472];
                    var size = 1472;
                    NetworkConnection from;

                    Assert.AreEqual(ExperimentalEventType.Disconnect, PollEvent(ExperimentalEventType.Disconnect, maxIterations, server, ref data, out size, out from));

                    id = client.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                    ConnectTogether(server, client, maxIterations, out serverConnection, out clientConnection);

                    var message = new byte[]
                    {
                        (byte)'m',
                        (byte)'e',
                        (byte)'s',
                        (byte)'s',
                        (byte)'a',
                        (byte)'g',
                        (byte)'e'
                    };

                    SendReceive(client, server, clientConnection, serverConnection, message, maxIterations);
                }
        }
Esempio n. 11
0
        public void UdpC_Timeout_ShouldDisconnect()
        {
            int customTimeout = 1000;

            using (var server = new UdpNetworkDriver(new NetworkConfigParameter {
                disconnectTimeoutMS = customTimeout
            }))
                using (var client = new UdpNetworkDriver(new NetworkConfigParameter {
                    disconnectTimeoutMS = customTimeout
                }))
                {
                    ushort serverPort = 50006;

                    server.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                    client.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));

                    server.Listen();

                    var id = client.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));

                    NetworkConnection serverConnection, clientConnection;
                    int maxIterations = 100;

                    ConnectTogether(server, client, maxIterations, out serverConnection, out clientConnection);
                    Assert.AreEqual(id, serverConnection);

                    // Force timeout
                    Thread.Sleep(customTimeout + 500);

                    var message = new DataStreamWriter(7, Allocator.Persistent);
                    message.Write((byte)'m');
                    message.Write((byte)'e');
                    message.Write((byte)'s');
                    message.Write((byte)'s');
                    message.Write((byte)'a');
                    message.Write((byte)'g');
                    message.Write((byte)'e');
                    server.Send(NetworkPipeline.Null, clientConnection, message);

                    var data = new byte[1472];
                    int size = -1;
                    NetworkConnection from;
                    Assert.AreEqual(ExperimentalEventType.Disconnect, PollEvent(ExperimentalEventType.Disconnect, maxIterations, server, ref data, out size, out from));
                    Assert.AreEqual(from, clientConnection);
                }
        }
Esempio n. 12
0
        public void UdpC_MultipleConnectTest_ShouldConnect()
        {
            using (var server = new UdpNetworkDriver(new NetworkDataStreamParameter {
            }))
                using (var client0 = new UdpNetworkDriver(new NetworkDataStreamParameter {
                }))
                    using (var client1 = new UdpNetworkDriver(new NetworkDataStreamParameter {
                    }))
                        using (var client2 = new UdpNetworkDriver(new NetworkDataStreamParameter {
                        }))
                            using (var client3 = new UdpNetworkDriver(new NetworkDataStreamParameter {
                            }))
                            {
                                ushort serverPort = 50005;

                                server.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                client0.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));
                                client1.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));
                                client2.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));
                                client3.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));

                                server.Listen();

                                NetworkConnection serverConnection, clientConnection;
                                int maxIterations = 100;


                                var id = client0.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                ConnectTogether(server, client0, maxIterations, out serverConnection, out clientConnection);
                                Assert.AreEqual(id, serverConnection);

                                id = client1.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                ConnectTogether(server, client1, maxIterations, out serverConnection, out clientConnection);
                                Assert.AreEqual(id, serverConnection);

                                id = client2.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                ConnectTogether(server, client2, maxIterations, out serverConnection, out clientConnection);
                                Assert.AreEqual(id, serverConnection);

                                id = client3.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                ConnectTogether(server, client3, maxIterations, out serverConnection, out clientConnection);
                                Assert.AreEqual(id, serverConnection);
                            }
        }
Esempio n. 13
0
    public SocketTransport(int port = 0, int maxConnections = 16)
    {
        m_IdToConnection = new NativeArray <NetworkConnection>(maxConnections, Allocator.Persistent);

        var configParams = default(NetworkConfigParameter);

        configParams.disconnectTimeoutMS = ServerGameLoop.serverDisconnectTimeout.IntValue;
        configParams.connectTimeoutMS    = NetworkParameterConstants.ConnectTimeoutMS;
        configParams.maxConnectAttempts  = NetworkParameterConstants.MaxConnectAttempts;

        m_Socket = new UdpNetworkDriver(new NetworkDataStreamParameter {
            size = 10 * NetworkConfig.maxPackageSize
        }, configParams);
        m_Socket.Bind(NetworkEndPoint.Parse(IPAddress.Any.ToString(), (ushort)port));

        if (port != 0)
        {
            m_Socket.Listen();
        }
    }
Esempio n. 14
0
        void SendReceive(UdpNetworkDriver sender, UdpNetworkDriver receiver, NetworkConnection from, NetworkConnection to, byte[] data, int maxIterations)
        {
            using (var writer = new DataStreamWriter(data.Length, Allocator.Persistent))
            {
                writer.Write(data, data.Length);
                sender.Send(NetworkPipeline.Null, to, writer);

                sender.ScheduleUpdate().Complete();
                receiver.ScheduleUpdate().Complete();

                var buffer = new byte[1472];
                int size   = 0;
                NetworkConnection connection;
                PollEvent(ExperimentalEventType.Data, maxIterations, receiver, ref buffer, out size, out connection);

                Assert.AreEqual(from, connection);
                Assert.AreEqual(data.Length, size);

                for (int i = 0; i < data.Length; i++)
                {
                    Assert.AreEqual(data[i], buffer[i]);
                }
            }
        }