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(); } }
public void LiteSocket_Udp_Sync() { // Test the synchronous UDP socket methods. var reflector = new NetReflector(port); var buffer = new byte[256]; var cb = 0; try { using (var sock = LiteSocket.CreateUdp(8192, 8192)) { // Connect and send/receive 1024 packets. Fill(buffer, 0); sock.Connect(new NetworkBinding("dev.lilltek.com", port), buffer); for (int i = 0; i < 1024; i++) { 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"); } } Fill(buffer, i + 1); sock.Send(buffer, 0, 256); } } } finally { reflector.Stop(); } }
private static int Reflector(string portArg) { try { NetReflector reflector; int port; if (!int.TryParse(portArg, out port)) { Program.Error("Invalid network port."); return(1); } Console.WriteLine(); Console.WriteLine("Starting network reflector on port [{0}]", port); Console.WriteLine("Press [C] to close all connections and [X] to exit the test."); Console.WriteLine(); reflector = new NetReflector(port); while (true) { var ch = Console.ReadKey(true); switch (ch.KeyChar) { case 'c': case 'C': reflector.CloseConnections(); break; case 'x': case 'X': reflector.Stop(); return(0); #if TEST // UDP test code case 'u': case 'U': { var sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); var buf = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var ep = (EndPoint) new IPEndPoint(IPAddress.Any, 0); sock.Bind(); sock.SendTo(buf, 0, buf.Length, SocketFlags.None, new IPEndPoint(IPAddress.Loopback, port)); for (int i = 0; i < buf.Length; i++) { buf[i] = 0; } sock.ReceiveFrom(buf, ref ep); sock.Close(); } break; // TCP test code case 't': case 'T': { var sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.NoDelay = true; sock.Connect(new IPEndPoint(IPAddress.Loopback, port)); for (int i = 0; i < 10; i++) { var buf = new byte[i + 1]; for (int j = 0; j < i + 1; j++) { buf[j] = (byte)j; } sock.Send(buf); for (int j = 0; j < i + 1; j++) { buf[j] = 0; } var cbRecv = sock.Receive(buf); } sock.Close(); } break; #endif } } } catch (Exception e) { Program.Error("Error ({0}): {1}", e.GetType().Name, e.Message); return(1); } }
public void LiteSocket_Udp_BasicIP() { // Send/receive [TestPacketCount] UDP packets from a local reflector using an IP address. var reflector = new NetReflector(port); var connected = false; var exception = (Exception)null; var sendState = new UdpSendState(); var recvState = new UdpRecvState(); try { using (var sock = LiteSocket.CreateUdp(8192, 8192)) { sendState.Sock = sock; recvState.Sock = sock; var sendBuf = new byte[256]; Fill(sendBuf, 0); sock.BeginConnect(new NetworkBinding(IPAddress.Loopback, port), sendBuf, ar => { try { sock.EndConnect(ar); connected = true; sock.BeginReceive(recvState.Buffer, 0, recvState.Buffer.Length, new AsyncCallback(OnUdpBasicRecv), recvState); sendBuf = new byte[256]; Fill(sendBuf, 1); sock.BeginSend(sendBuf, 0, sendBuf.Length, new AsyncCallback(OnUdpBasicSent), sendState); } catch (Exception e) { exception = e; } }, null); Helper.WaitFor(() => (connected && recvState.Count == TestPacketCount + 1) || exception != null || sendState.Exception != null || recvState.Exception != null, waitTime); if (exception != null) { throw exception; } if (sendState.Exception != null) { throw sendState.Exception; } if (recvState.Exception != null) { throw recvState.Exception; } Assert.AreEqual(TestPacketCount + 1, recvState.Count); for (int i = 0; i < recvState.Packets.Count; i++) { var p = new byte[256]; Fill(p, i); CollectionAssert.AreEqual(p, recvState.Packets[i]); } } } finally { reflector.Stop(); } }
public void LiteSocket_Tcp_ExceedsBuffers() { // Establish a TCP connection simulated server on the local machine // via an IP address and then send a block of data much larger than // the socket's buffers and the make sure that all of the data is // echoed back correctly. const int blockSize = 128 * 1024; var reflector = new NetReflector(port); var connected = false; var closed = false; var received = false; var exception = (Exception)null; var recvData = (byte[])null; try { reflector.TcpConnected += (s, a) => connected = true; reflector.TcpDisconnected += (s, a) => closed = true; using (var sock = LiteSocket.CreateTcp(8192, 8192)) { sock.BeginConnect(new NetworkBinding(IPAddress.Loopback, port), null, ar => { try { sock.EndConnect(ar); connected = true; var recvBuf = new byte[blockSize]; sock.BeginReceiveAll(recvBuf, 0, recvBuf.Length, ar2 => { try { sock.EndReceiveAll(ar2); received = true; recvData = recvBuf; sock.ShutdownAndClose(); } catch (Exception e) { exception = e; } }, null); var sendBuf = new byte[blockSize]; for (int i = 0; i < sendBuf.Length; i++) { sendBuf[i] = (byte)i; } 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 && closed) || exception != null, waitTime); if (exception != null) { throw exception; } var checkBuf = new byte[blockSize]; for (int i = 0; i < checkBuf.Length; i++) { checkBuf[i] = (byte)i; } CollectionAssert.AreEqual(checkBuf, recvData); } } finally { reflector.Stop(); } }
public void LiteSocket_Tcp_ServerReset() { // Establish a TCP connection simulated server on the local machine // via an IP address and then send/receive a packet of data, then // reset the socket 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.CloseConnections(); 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(); } }
public void LiteSocket_Tcp_BasicHost() { // Establish a TCP connection simulated server on the local machine // via a host name and then send/receive a packet of data, then // close the socket gracefully on the client and verify that everything // is OK. var reflector = new NetReflector(port); var connected = false; var closed = false; var received = false; var exception = (Exception)null; var recvData = (byte[])null; try { reflector.TcpConnected += (s, a) => connected = true; reflector.TcpDisconnected += (s, a) => closed = true; using (var sock = LiteSocket.CreateTcp()) { sock.BeginConnect(new NetworkBinding("dev.lilltek.com", 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.ShutdownAndClose(); } 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 && closed) || 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(); } }
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(); } }