コード例 #1
0
        public void LiteSocket_Tcp_ReceiveAllSync()
        {
            // Test the synchronous TCP socket methods.

            var reflector = new NetReflector(port);
            var buffer    = new byte[1024];

            try
            {
                using (var sock = LiteSocket.CreateTcp(8192, 8192))
                {
                    sock.Connect(new NetworkBinding("dev.lilltek.com", port), null);

                    // Send/receive 1024 blocks.

                    for (int i = 0; i < 1024; i++)
                    {
                        Fill(buffer, i);
                        sock.Send(buffer, 0, 256);

                        Fill(buffer, 0);
                        sock.ReceiveAll(buffer, 0, 256);

                        for (int j = 0; j < 256; j++)
                        {
                            if (buffer[j] != (byte)i)
                            {
                                Assert.Fail("Bad data");
                            }
                        }
                    }

                    // Initiate another receive and then gracefully close the connection
                    // on the server and verify that ReceiveAll() throws ServerClosedException.

                    Helper.EnqueueAction(
                        () =>
                    {
                        Thread.Sleep(500);
                        reflector.ShutdownAndCloseConnections();
                    });

                    try
                    {
                        sock.ReceiveAll(buffer, 0, 256);
                    }
                    catch (SocketClosedException e)
                    {
                        Assert.AreEqual(10057, e.ErrorCode);     // WSAENOTCONN
                    }
                }
            }
            finally
            {
                reflector.Stop();
            }
        }
コード例 #2
0
        public void LiteSocket_Tcp_ServerClose()
        {
            // Establish a TCP connection simulated server on the local machine
            // via an IP address and then send/receive a packet of data, then
            // close the socket gracefully on the server and verify that everything
            // is OK.

            var reflector = new NetReflector(port);
            var connected = false;
            var received  = false;
            var exception = (Exception)null;
            var recvData  = (byte[])null;
            var gotEOF    = false;

            try
            {
                reflector.TcpConnected += (s, a) => connected = true;
                reflector.TcpReceived  += (s, a) => reflector.ShutdownAndCloseConnections();

                using (var sock = LiteSocket.CreateTcp())
                {
                    sock.BeginConnect(new NetworkBinding(IPAddress.Loopback, port), null,
                                      ar =>
                    {
                        try
                        {
                            sock.EndConnect(ar);
                            connected = true;

                            var recvBuf = new byte[1024];

                            sock.BeginReceive(recvBuf, 0, recvBuf.Length,
                                              ar2 =>
                            {
                                try
                                {
                                    var cbRecv = sock.EndReceive(ar2);

                                    received = true;
                                    recvData = Helper.Extract(recvBuf, 0, cbRecv);

                                    sock.BeginReceive(new byte[1024], 0, 1024,
                                                      ar3 =>
                                    {
                                        try
                                        {
                                            gotEOF = sock.EndReceive(ar) == 0;
                                        }
                                        catch (Exception e)
                                        {
                                            exception = e;
                                        }
                                    },
                                                      null);
                                }
                                catch (Exception e)
                                {
                                    exception = e;
                                }
                            },
                                              null);

                            var sendBuf = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

                            sock.BeginSend(sendBuf, 0, sendBuf.Length,
                                           ar2 =>
                            {
                                try
                                {
                                    sock.EndSend(ar2);
                                }
                                catch (Exception e)
                                {
                                    exception = e;
                                }
                            },
                                           null);
                        }
                        catch (Exception e)
                        {
                            exception = e;
                        }
                    },
                                      null);

                    Helper.WaitFor(() => (connected && received && gotEOF) || exception != null, waitTime);

                    if (exception != null)
                    {
                        throw exception;
                    }

                    CollectionAssert.AreEqual(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, recvData);
                }
            }
            finally
            {
                reflector.Stop();
            }
        }
コード例 #3
0
        public void LiteSocket_Tcp_ReceiveSync()
        {
            // Test the synchronous TCP socket methods.

            var reflector = new NetReflector(port);
            var buffer    = new byte[1024];
            var cb        = 0;

            try
            {
                using (var sock = LiteSocket.CreateTcp(8192, 8192))
                {
                    sock.Connect(new NetworkBinding("dev.lilltek.com", port), null);

                    // Verify that we can receive partial data.

                    Fill(buffer, 128);
                    sock.Send(buffer, 0, 256);

                    Fill(buffer, 0);
                    cb = sock.Receive(buffer, 0, 1024);

                    Assert.AreEqual(256, cb);
                    for (int j = 0; j < 256; j++)
                    {
                        if (buffer[j] != 128)
                        {
                            Assert.Fail("Bad data");
                        }
                    }

                    // Send/receive 1024 blocks.

                    for (int i = 0; i < 1024; i++)
                    {
                        Fill(buffer, i);
                        sock.Send(buffer, 0, 256);

                        Fill(buffer, 0);
                        cb = sock.Receive(buffer, 0, 256);

                        Assert.AreEqual(256, cb);
                        for (int j = 0; j < 256; j++)
                        {
                            if (buffer[j] != (byte)i)
                            {
                                Assert.Fail("Bad data");
                            }
                        }
                    }

                    // Initiate another receive and then gracefully close the connection
                    // on the server and verify that Receive() returns 0.

                    Helper.EnqueueAction(
                        () =>
                    {
                        Thread.Sleep(500);
                        reflector.ShutdownAndCloseConnections();
                    });

                    cb = sock.Receive(buffer, 0, 256);
                    Assert.AreEqual(0, cb);
                }
            }
            finally
            {
                reflector.Stop();
            }
        }