예제 #1
0
        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);
            }
        }
예제 #2
0
        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();
            }
        }