Esempio n. 1
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. 2
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. 3
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. 4
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. 5
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. 6
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. 7
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. 8
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. 9
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. 10
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();
        }
    }