コード例 #1
0
        public void UDP_SocketOpenClose()
        {
            SL.C.Context ctx = default;
            Assert.True(API.Setup(&ctx));

            C.Socket sock = C.Socket.NewUDP(&ctx, C.Endpoint.NewV4(&ctx, _port));
            try
            {
                Assert.True(API.SocketOpen(&sock));
                Assert.AreEqual(C.SocketState.Bound, sock.state);
                Assert.AreEqual(C.SocketFlags.None, sock.flags);
                Assert.AreEqual(0, sock.error);
                Assert.False(C.Socket.HasFlag(&sock, C.SocketFlags.NonBlocking));

                Assert.True(API.SocketClose(&sock));
                Assert.AreEqual(C.SocketState.Closed, sock.state);
                Assert.AreEqual(0, sock.fd);

                Assert.True(API.Cleanup(&ctx));
                Assert.AreEqual(C.ContextState.Stopped, ctx.state);
            }
            finally
            {
                API.Cleanup(&ctx);
            }
        }
コード例 #2
0
        public void C_Socket_NewUDP()
        {
            Assert.True(IPAddress.TryParse("127.0.0.1", out IPAddress netip));

            SL.C.Context ctx = default;
            Assert.True(API.Setup(&ctx));

            try
            {
                C.Socket sock = C.Socket.NewUDP(&ctx, C.Endpoint.NewV4(&ctx, _port, C.IPv4.New(127, 0, 0, 1)));
                Assert.AreEqual(0, sock.fd);
                Assert.AreEqual(0, sock.dir);
                Assert.AreEqual(C.SocketState.New, sock.state);
                Assert.AreEqual(C.SockType.Dgram, (C.SockType)sock.type);
                Assert.AreEqual(C.SockProto.UDP, (C.SockProto)sock.proto);
                Assert.AreEqual(0, sock.error);
                Assert.AreEqual(C.SocketFlags.None, sock.flags);
                Assert.AreEqual(ctx.af_inet, sock.endpoint.af);
                Assert.AreEqual(_port, sock.endpoint.port);

                fixed(byte *pnetip = netip.GetAddressBytes())
                Assert.True(Util.MemCmp((byte *)&sock.endpoint.addr4, 0, pnetip, 0, sizeof(C.IPv4)));

                Assert.True(API.Cleanup(&ctx));
            }
            finally
            {
                API.Cleanup(&ctx);
            }
        }
コード例 #3
0
        public void C_Endpoint_NewV6()
        {
            if (!C.SL_IPV6_ENABLED)
            {
                return;
            }

            Assert.True(IPAddress.TryParse("fe80::300e:5130:704b:a647%21", out IPAddress netip));

            SL.C.Context ctx = default;
            Assert.True(API.Setup(&ctx));
            try
            {
                C.Endpoint endpoint = C.Endpoint.NewV6(&ctx, _port, C.IPv6.New(0xfe80, 0, 0, 0, 0x300e, 0x5130, 0x704b, 0xa647));
                Assert.AreEqual(ctx.af_inet6, endpoint.af);
                Assert.AreEqual(_port, endpoint.port);

                fixed(byte *pnetip = netip.GetAddressBytes())
                Assert.True(Util.MemCmp((byte *)&endpoint.addr6, 0, pnetip, 0, C.SL_IP6_SIZE));

                Assert.True(API.Cleanup(&ctx));
            }
            finally
            {
                API.Cleanup(&ctx);
            }
        }
コード例 #4
0
    public void SetupCleanup()
    {
        void *service = null;

        SL.C.Context ctx = default;
        Assert.True(SVL.API.Setup(&ctx, &service));
        Assert.True(SVL.API.Cleanup(&ctx, &service));
    }
コード例 #5
0
        public void UDP_DoubleCleanup()
        {
            SL.C.Context ctx = default;
            Assert.True(API.Cleanup(&ctx));
            Assert.AreEqual(C.ContextState.Stopped, ctx.state);

            Assert.True(API.Cleanup(&ctx));
            Assert.AreEqual(C.ContextState.Stopped, ctx.state);
        }
コード例 #6
0
    public void StartListenStop()
    {
        void *service = null;

        SL.C.Context  ctx            = default;
        SL.C.Endpoint local_endpoint = SL.C.Endpoint.NewV4(&ctx, _port, SL.C.IPv4.New(127, 0, 0, 1));
        Assert.True(SVL.API.Setup(&ctx, &service));
        Assert.True(SVL.API.Start(service, &local_endpoint));
        Assert.True(SVL.API.Stop(service));
        Assert.True(SVL.API.Cleanup(&ctx, &service));
    }
コード例 #7
0
    public void DoubleStartStop()
    {
        void *service = null;

        SL.C.Context ctx = default;
        Assert.True(SVL.API.Setup(&ctx, &service));
        Assert.True(SVL.API.Start(service));
        Assert.True(SVL.API.Start(service));
        Assert.True(SVL.API.Stop(service));
        Assert.True(SVL.API.Cleanup(&ctx, &service));
    }
コード例 #8
0
        public void UDP_Setup()
        {
            SL.C.Context ctx = default;
            Assert.True(API.Setup(&ctx));
            Assert.AreEqual(C.ContextState.Started, ctx.state);
            Assert.AreNotEqual(0, ctx.af_inet);
            Assert.AreNotEqual(0, ctx.af_inet6);

            Assert.True(API.Cleanup(&ctx));
            Assert.AreEqual(C.ContextState.Stopped, ctx.state);
        }
コード例 #9
0
        public void UDP_SocketSetBlocking()
        {
            SL.C.Context ctx = default;
            Assert.True(API.Setup(&ctx));

            C.Socket sock = C.Socket.NewUDP(&ctx, C.Endpoint.NewV4(&ctx, _port));
            try
            {
                Assert.True(API.SocketOpen(&sock));
                Assert.True(API.SocketNonBlocking(&sock, true));
                Assert.True(C.Socket.HasFlag(&sock, C.SocketFlags.NonBlocking));

                Assert.True(API.SocketClose(&sock));
                Assert.True(API.Cleanup(&ctx));
            }
            finally
            {
                API.SocketClose(&sock);
                API.Cleanup(&ctx);
            }
        }
コード例 #10
0
        public void C_Endpoint_NewV4()
        {
            Assert.True(IPAddress.TryParse("127.0.0.1", out IPAddress netip));

            SL.C.Context ctx = default;
            Assert.True(API.Setup(&ctx));

            try
            {
                C.Endpoint endpoint = C.Endpoint.NewV4(&ctx, _port, C.IPv4.New(127, 0, 0, 1));
                Assert.AreEqual(ctx.af_inet, endpoint.af);
                Assert.AreEqual(_port, endpoint.port);

                fixed(byte *pnetip = netip.GetAddressBytes())
                Assert.True(Util.MemCmp((byte *)&endpoint.addr4, 0, pnetip, 0, sizeof(C.IPv4)));

                Assert.True(API.Cleanup(&ctx));
            }
            finally
            {
                API.Cleanup(&ctx);
            }
        }
コード例 #11
0
    public void UDP_SocketSendRecv_Blocking()
    {
        Random rand = new Random();

        C.Socket sock_server;
        C.Socket sock_client;

        SL.C.Context ctx = default;
        Assert.True(API.Setup(&ctx));
        try
        {
            /* server side endpoints, socket, random payload, send/recv buffers */
            C.IPv4     loopback       = C.IPv4.New(127, 0, 0, 1);
            C.Endpoint ep_server      = C.Endpoint.NewV4(&ctx, _port, loopback);
            C.Endpoint ep_server_recv = default;
            sock_server = C.Socket.NewUDP(&ctx, ep_server);
            C.Buffer buf_server_recv;
            C.Buffer buf_server_send;
            byte[]   pl_server  = new byte[1235];
            byte[]   mem_server = new byte[1408];
            rand.NextBytes(pl_server);

            fixed(byte *bufptr = pl_server) buf_server_send = C.Buffer.New(bufptr, pl_server.Length);

            fixed(byte *bufptr = mem_server) buf_server_recv = C.Buffer.New(bufptr, mem_server.Length);

            /* client side endpoints, socket, random payload, send/recv buffers */
            C.Endpoint ep_client      = C.Endpoint.NewV4(&ctx, _port + 1, loopback);
            C.Endpoint ep_client_recv = default;
            sock_client = C.Socket.NewUDP(&ctx, ep_client);
            C.Buffer buf_client_recv;
            C.Buffer buf_client_send;
            byte[]   pl_client  = new byte[256];
            byte[]   mem_client = new byte[1408];
            rand.NextBytes(pl_client);

            fixed(byte *bufptr = pl_client) buf_client_send = C.Buffer.New(bufptr, pl_client.Length);

            fixed(byte *bufptr = mem_client) buf_client_recv = C.Buffer.New(bufptr, mem_client.Length);

            Assert.True(API.SocketOpen(&sock_server));
            Assert.True(API.SocketOpen(&sock_client));

            Assert.AreEqual(pl_client.Length, API.SocketSend(&sock_client, &buf_client_send, 1, &ep_server));
            Assert.AreEqual(pl_client.Length, API.SocketRecv(&sock_server, &buf_server_recv, 1, &ep_server_recv));

            fixed(byte *bufptr = pl_client) Assert.True(Util.MemCmp(bufptr, 0, buf_server_recv.buf, 0, pl_client.Length));

            Assert.True(Util.MemCmp((byte *)&ep_client, 2, (byte *)&ep_server_recv, 2, sizeof(C.Endpoint) - 2));

            Assert.AreEqual(pl_server.Length, API.SocketSend(&sock_server, &buf_server_send, 1, &ep_client));
            Assert.AreEqual(pl_server.Length, API.SocketRecv(&sock_client, &buf_client_recv, 1, &ep_client_recv));

            fixed(byte *bufptr = pl_server) Assert.True(Util.MemCmp(bufptr, 0, buf_client_recv.buf, 0, pl_server.Length));

            Assert.True(Util.MemCmp((byte *)&ep_server, 2, (byte *)&ep_client_recv, 2, sizeof(C.Endpoint) - 2));

            Assert.True(API.SocketClose(&sock_server));
            Assert.True(API.SocketClose(&sock_client));
            Assert.True(API.Cleanup(&ctx));
        }
        finally
        {
            API.SocketClose(&sock_server);
            API.SocketClose(&sock_client);
            API.Cleanup(&ctx);
        }
    }