Exemple #1
0
 /// <summary>
 /// Gracefully stops communication on the socket and then closes it.
 /// </summary>
 /// <remarks>
 /// <note>
 /// This method is safe to call when the socket is already shut down or closed.
 /// </note>
 /// </remarks>
 public void ShutdownAndClose()
 {
     lock (syncLock)
     {
         if (isTcp)
         {
             sock.ShutdownAndClose();
         }
         else
         {
             sock.Close();
         }
     }
 }
Exemple #2
0
        public void HttpServer_SweepIdle()
        {
            HttpServer     server;
            HttpRequest    request;
            BlockArray     blocks;
            EnhancedSocket sock;

            server = new HttpServer(new IPEndPoint[] { new IPEndPoint(IPAddress.Any, ServerPort) }, new IHttpModule[] { new TestModule() }, 5, 100, int.MaxValue);
            server.Start();

            try
            {
                request             = new HttpRequest("GET", "/foo.htm", null);
                request["Response"] = "abcd";
                request["Close"]    = "yes";
                blocks = request.Serialize(4096);

                sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect("localhost", ServerPort);
                Thread.Sleep(100);
                Assert.AreEqual(1, server.ConnectionCount);
                Thread.Sleep(1000);
                server.SweepIdle(TimeSpan.FromMilliseconds(500));
                Assert.AreEqual(0, server.ConnectionCount);

                sock.Close();
            }
            finally
            {
                server.Stop();
            }
        }
Exemple #3
0
        public void HttpConnection_Query_Timeout()
        {
            EnhancedSocket sockListen = null;
            EnhancedSocket sockAccept = null;
            HttpConnection con;
            HttpRequest    request;
            HttpResponse   response;
            string         content;
            TimeSpan       orgTimeout;
            IAsyncResult   ar;

            orgTimeout = HttpStack.TimeoutSweepInterval;
            HttpStack.TimeoutSweepInterval = TimeSpan.FromMilliseconds(250);

            sockListen = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sockListen.Bind(new IPEndPoint(IPAddress.Any, ServerPort));
            sockListen.Listen(10);

            try
            {
                ar = sockListen.BeginAccept(null, null);

                con = new HttpConnection(HttpOption.None);
                con.Connect("http://localhost:" + ServerPort.ToString());

                sockAccept = sockListen.EndAccept(ar);

                content             = "Test: Timeout";
                request             = new HttpRequest("GET", "/foo.htm", null);
                request["Response"] = content;
                request["Close"]    = "no";

                try
                {
                    response = con.Query(request, SysTime.Now + TimeSpan.FromMilliseconds(250));
                    Thread.Sleep(1000);
                    Assert.Fail();
                }
                catch (TimeoutException)
                {
                }

                Assert.IsTrue(con.IsClosed);
                con.Close();
            }
            finally
            {
                HttpStack.TimeoutSweepInterval = orgTimeout;

                if (sockListen != null)
                {
                    sockListen.Close();
                }

                if (sockAccept != null)
                {
                    sockAccept.Close();
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Closes the client, releasing all resources.
        /// </summary>
        public void Close()
        {
            using (TimedLock.Lock(this))
            {
                if (sock != null)
                {
                    sock.Close();
                }

                if (transactions != null)
                {
                    // Terminate any outstanding authentication requests.

                    foreach (AuthTransaction transaction in transactions)
                    {
                        if (transaction != null)
                        {
                            transaction.AsyncResult.Notify(new RadiusException("RADIUS client is closed."));
                        }
                    }

                    transactions = null;
                }

                isOpen  = false;
                servers = null;
            }
        }
Exemple #5
0
        /// <summary>
        /// Stops the server if it's currently running.
        /// </summary>
        public void Stop()
        {
            using (TimedLock.Lock(this))
            {
                if (sock != null)
                {
                    sock.Close();
                }

                if (bkTimer != null)
                {
                    bkTimer.Dispose();
                    bkTimer = null;
                }

                devices           = null;
                deviceMap         = null;
                accounts          = null;
                LogEvent          = null;
                AuthenticateEvent = null;
                GetNasInfoEvent   = null;
                isRunning         = false;
            }

            Thread.Sleep(1000);     // Give any pending authentication event calls a chance
                                    // to unwind.
        }
Exemple #6
0
        public void EnhancedSocket_CreateDatagramPair()
        {
            for (int i = 0; i < 1000; i++)
            {
                EnhancedSocket sock0 = null;
                EnhancedSocket sock1 = null;
                int            port0, port1;

                try
                {
                    EnhancedSocket.CreateDatagramPair(IPAddress.Any, out sock0, out sock1);

                    port0 = ((IPEndPoint)sock0.LocalEndPoint).Port;
                    port1 = ((IPEndPoint)sock1.LocalEndPoint).Port;
                }
                finally
                {
                    if (sock0 != null)
                    {
                        sock0.Close();
                    }

                    if (sock1 != null)
                    {
                        sock1.Close();
                    }
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Starts the transport.
        /// </summary>
        /// <param name="binding">The network binding the transport will use.</param>
        /// <param name="cbSocketBuffer">Size of the socket's send and receive buffers in bytes.</param>
        /// <param name="router">The <see cref="ISipMessageRouter" /> instance that will handle the routing of received messages.</param>
        /// <exception cref="SocketException">Thrown if there's a conflict with the requested and existing socket bindings.</exception>
        public void Start(NetworkBinding binding, int cbSocketBuffer, ISipMessageRouter router)
        {
            try
            {
                sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                sock.SendBufferSize           = cbSocketBuffer;
                sock.ReceiveBufferSize        = cbSocketBuffer;
                sock.IgnoreUdpConnectionReset = true;
                sock.Bind(binding);

                this.localEP   = (IPEndPoint)sock.LocalEndPoint;
                this.onRecv    = new AsyncCallback(OnReceive);
                this.recvBuf   = new byte[64 * 1024];
                this.recvEP    = new IPEndPoint(IPAddress.Any, 0);
                this.router    = router;
                this.traceMode = SipTraceMode.None;

                recvPending = true;
                sock.BeginReceiveFrom(recvBuf, 0, recvBuf.Length, SocketFlags.None, ref recvEP, onRecv, null);
            }
            catch
            {
                if (sock.IsOpen)
                {
                    sock.Close();
                }

                sock = null;
                throw;
            }
        }
Exemple #8
0
 public void Stop()
 {
     lock (syncLock)
     {
         udpSock.Close();
         listener.StopAll();
         CloseConnections();
     }
 }
Exemple #9
0
        public void HttpServer_MaxQuerySize()
        {
            HttpServer   server;
            HttpRequest  request;
            HttpResponse response;
            BlockArray   blocks;

            byte[]         buf;
            int            cb;
            EnhancedSocket sock;
            IAsyncResult   ar;

            server = new HttpServer(new IPEndPoint[] { new IPEndPoint(IPAddress.Any, ServerPort) }, new IHttpModule[] { new TestModule() }, 5, 100, 200);
            server.Start();

            try
            {
                request                   = new HttpRequest("PUT", "/foo.htm", null);
                request.Content           = new BlockArray(500);
                request["Response"]       = "abcd";
                request["Close"]          = "yes";
                request["Content-Length"] = request.Content.Size.ToString();
                blocks = request.Serialize(4096);

                sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect("localhost", ServerPort);
                Thread.Sleep(100);
                Assert.AreEqual(1, server.ConnectionCount);

                try
                {
                    ar = sock.BeginSendAll(blocks, SocketFlags.None, null, null);
                    sock.EndSendAll(ar);
                }
                catch
                {
                }

                response = new HttpResponse();
                response.BeginParse();

                buf = new byte[4096];
                cb  = sock.Receive(buf, buf.Length, SocketFlags.None);

                Assert.IsTrue(response.Parse(buf, cb));
                response.EndParse();

                Assert.AreEqual(HttpStatus.RequestEntityTooLarge, response.Status);
                sock.Close();
            }
            finally
            {
                server.Stop();
            }
        }
Exemple #10
0
        /// <summary>
        /// Stops the server, removing it from the cluster.
        /// </summary>
        /// <remarks>
        /// <note>
        /// It is not an error to call this method then the instance has already been closed.
        /// </note>
        /// </remarks>
        public void Close()
        {
            lock (syncLock)
            {
                if (closePending || socket == null)
                {
                    return;
                }

                var packet = GetMessageBytes(UdpBroadcastMessageType.ServerUnregister);

                foreach (var server in settings.Servers)
                {
                    if (!PauseNetwork)
                    {
                        socket.SendTo(packet, server);
                    }
                }

                closePending = true;
            }

            // Sleep for a couple seconds so that any broadcast messages in transit
            // will still be retransmitted during the time it will take for the
            // other servers in the cluster to decide on a new master.

            Thread.Sleep(2000);

            socket.Close();
            socket = null;

            if (bkTimer != null)
            {
                bkTimer.Dispose();
                bkTimer = null;
            }

            servers.Clear();
            clients.Clear();

            GC.SuppressFinalize(this);
        }
Exemple #11
0
 /// <summary>
 /// Stops the DNS server if it is running.
 /// </summary>
 public void Stop()
 {
     lock (syncLock)
     {
         if (sock != null)
         {
             sock.Close();
             sock = null;
         }
     }
 }
Exemple #12
0
        public void HttpServer_QueryResponse_Close()
        {
            HttpServer   server;
            HttpRequest  request;
            HttpResponse response;
            BlockArray   blocks;

            byte[]         buf;
            int            cb;
            EnhancedSocket sock;
            IAsyncResult   ar;

            server = new HttpServer(new IPEndPoint[] { new IPEndPoint(IPAddress.Any, ServerPort) }, new IHttpModule[] { new TestModule() }, 5, 100, int.MaxValue);
            server.Start();

            try
            {
                request          = new HttpRequest("GET", "/foo.htm", null);
                request.Content  = new BlockArray(Encoding.ASCII.GetBytes("abcd"));
                request["Close"] = "yes";
                blocks           = request.Serialize(4096);

                sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect("localhost", ServerPort);
                Thread.Sleep(100);
                Assert.AreEqual(1, server.ConnectionCount);

                ar = sock.BeginSendAll(blocks, SocketFlags.None, null, null);
                sock.EndSendAll(ar);

                response = new HttpResponse();
                response.BeginParse();

                buf = new byte[4096];
                cb  = sock.Receive(buf, buf.Length, SocketFlags.None);

                Assert.IsTrue(response.Parse(buf, cb));
                response.EndParse();

                CollectionAssert.AreEqual(Encoding.ASCII.GetBytes("abcd"), response.Content.ToByteArray());

                buf = new byte[4096];
                cb  = sock.Receive(buf, buf.Length, SocketFlags.None);
                Assert.AreEqual(0, cb);

                sock.Close();
            }
            finally
            {
                server.Stop();
            }
        }
Exemple #13
0
        /// <summary>
        /// Closes the connection if open.
        /// </summary>
        public void Close()
        {
            using (TimedLock.Lock(this))
            {
                if (sock == null || !sock.IsOpen)
                {
                    return;
                }

                sock.Close();
            }

            HttpStack.RemoveConnection(this);
        }
Exemple #14
0
        /// <summary>
        /// Stops the transport if it's running.
        /// </summary>
        public void Stop()
        {
            lock (syncLock)
            {
                if (sock == null)
                {
                    return;
                }

                sock.Close();
                onRecv  = null;
                recvBuf = null;
            }
        }
Exemple #15
0
        public void SipTcpTransport_MultipleChunks()
        {
            // Transmit a message to a transport in several chunks to make
            // sure it can be reassembled properly.

            TestTransport  transport = new TestTransport();
            EnhancedSocket sock      = null;
            SipRequest     msg;
            SipRequest     recvMsg;

            byte[] buf;

            try
            {
                transport.Start(new NetworkBinding("127.0.0.1:5311"));

                sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect("127.0.0.1", 5311);
                sock.NoDelay = true;

                msg = new SipRequest(SipMethod.Register, "sip:[email protected]", SipHelper.SIP20);
                msg.AddHeader(SipHeader.Via, string.Format("SIP/2.0/TCP {0}", transport.LocalEndpoint));
                msg.AddHeader("Count", "0");
                msg.Contents = GetContents(25);

                buf = msg.ToArray();
                for (int i = 0; i < buf.Length; i++)
                {
                    sock.Send(buf, i, 1, SocketFlags.None);
                    Thread.Sleep(1);
                }

                recvMsg = (SipRequest)transport.Receive();
                Assert.AreEqual(SipMethod.Register, recvMsg.Method);
                Assert.AreEqual("sip:[email protected]", recvMsg.Uri);
                Assert.AreEqual(SipHelper.SIP20, recvMsg.SipVersion);
                Assert.AreEqual(msg[SipHeader.Via].FullText, recvMsg[SipHeader.Via].FullText);
                Assert.AreEqual("0", recvMsg["Count"].FullText);
                CollectionAssert.AreEqual(msg.Contents, recvMsg.Contents);
            }
            finally
            {
                if (sock != null)
                {
                    sock.Close();
                }

                transport.Stop();
            }
        }
Exemple #16
0
        /// <summary>
        /// Closes the instance, removing it from the broadcast group.
        /// </summary>
        /// <remarks>
        /// <note>
        /// It is not an error to call this method then the instance has already been closed.
        /// </note>
        /// </remarks>
        public void Close()
        {
            lock (syncLock)
            {
                if (socket != null)
                {
                    var packet = GetMessageBytes(UdpBroadcastMessageType.ClientUnregister, settings.BroadcastGroup);

                    foreach (var server in servers)
                    {
                        if (!PauseNetwork)
                        {
                            try
                            {
                                socket.SendTo(packet, server);
                            }
                            catch (Exception e)
                            {
                                SysLog.LogException(e);
                            }
                        }
                    }

                    socket.Close();
                    socket = null;
                }

                if (bkTimer != null)
                {
                    bkTimer.Dispose();
                    bkTimer = null;
                }
            }

            GC.SuppressFinalize(this);
        }
Exemple #17
0
        public void EnhancedSocket_Async_ConnectToHost()
        {
            EnhancedSocket sock;
            IAsyncResult   ar;
            IPAddress      addr;

            sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            ar   = sock.BeginConnect("www.google.com", 80, null, null);
            sock.EndConnect(ar);
            addr = ((IPEndPoint)sock.RemoteEndPoint).Address;
            sock.Close();

            sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            ar   = sock.BeginConnect(addr.ToString(), 80, null, null);
            sock.EndConnect(ar);
            sock.Close();
        }
Exemple #18
0
        private void Init(SocketListener listener)
        {
            if (sockAccept != null)
            {
                sockAccept.Close();
                sockAccept = null;
            }

            if (wait != null)
            {
                wait.Close();
                wait = null;
            }

            wait = new AutoResetEvent(false);
            listener.SocketAcceptEvent += new SocketAcceptDelegate(OnAccept);
        }
Exemple #19
0
        /// <summary>
        /// Closes the channel if it's currently open.
        /// </summary>
        public void Close()
        {
            using (TimedLock.Lock(router.SyncRoot))
            {
                if (!isOpen)
                {
                    return;
                }

                if (sock != null)
                {
                    if (localEP != null)
                    {
                        router.Trace(1, "UDP: Close", "LocalEP=" + localEP.NetEP.ToString(), null);
                    }

                    sock.Close();
                    sock = null;
                }
                else if (broadcastClient != null)
                {
                    router.Trace(1, "UDP: Close(UDP-BROADCAST)", null, null);

                    broadcastClient.Close();
                    broadcastClient = null;
                }

                isOpen          = false;
                port            = 0;
                sendQueue       = null;
                onSend          = null;
                sendBuf         = null;
                sendMsg         = null;
                onSocketReceive = null;
                recvBuf         = null;
            }
        }
Exemple #20
0
        public bool Connect(IPEndPoint endPoint)
        {
            EnhancedSocket sock       = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            EnhancedSocket sockAccept = null;

            try
            {
                sock.Connect(endPoint);
                sockAccept = WaitForAccept();
                return(sockAccept != null);
            }
            catch
            {
                return(false);
            }
            finally
            {
                sock.Close();
                if (sockAccept != null)
                {
                    sockAccept.Close();
                }
            }
        }
Exemple #21
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);
            }
        }
Exemple #22
0
 public void Close()
 {
     sock.Close();
 }
Exemple #23
0
        /// <summary>
        /// Handles socket receive completions.
        /// </summary>
        /// <param name="ar"></param>
        private void OnReceive(IAsyncResult ar)
        {
            HttpAsyncState httpState = (HttpAsyncState)ar.AsyncState;
            HttpRequest    request   = httpState.Request;
            EnhancedSocket sock      = httpState.Socket;

            byte[]       recvBuf = httpState.Buffer;
            int          cbRecv;
            HttpResponse response;
            bool         closeCon;
            bool         close;
            bool         firstRequest;

            try
            {
                cbRecv = sock.EndReceive(ar);
            }
            catch
            {
                using (TimedLock.Lock(syncLock))
                    connections.Remove(sock);

                sock.Close();
                return;
            }

            if (cbRecv == 0)
            {
                using (TimedLock.Lock(syncLock))
                    connections.Remove(sock);

                sock.ShutdownAndClose();
                return;
            }

            if (perfBytesRecv != null)
            {
                perfBytesRecv.IncrementBy(cbRecv);
            }

            httpState.RecvSize += cbRecv;
            if (httpState.RecvSize > cbQueryMax)
            {
                // The request is too large so respond with a HttpStatus.RequestEntityTooLarge
                // and close the socket.

                response = new HttpResponse(HttpStatus.RequestEntityTooLarge);
                sock.AsyncSendClose(response.Serialize(SendBlockSize));

                using (TimedLock.Lock(syncLock))
                    connections.Remove(sock);

                return;
            }

            if (!request.Parse(recvBuf, cbRecv))
            {
                recvBuf          = new byte[RecvBlockSize];
                httpState.Buffer = recvBuf;
                sock.BeginReceive(recvBuf, 0, recvBuf.Length, SocketFlags.None, onRecv, httpState);
                return;
            }

            // We have a complete request so process it.

            request.EndParse();

            firstRequest           = httpState.FirstRequest;
            httpState.FirstRequest = false;

            try
            {
                sock.AppState = this;   // Indicate that we're processing a request

                closeCon = false;
                for (int i = 0; i < modules.Length; i++)
                {
                    response = modules[i].OnRequest(this, request, firstRequest, out close);
                    closeCon = closeCon || close;

                    if (response != null)
                    {
                        BlockArray blocks;

                        // Make sure the response version is reasonable

                        if (request.HttpVersion < response.HttpVersion)
                        {
                            response.HttpVersion = request.HttpVersion;
                        }

                        // Truncate any content data for HEAD requests

                        if (request.Method == "HEAD")
                        {
                            response.Content = null;
                            if (response["Content-Length"] != null)
                            {
                                response["Content-Length"] = "0";
                            }
                        }

                        blocks = response.Serialize(SendBlockSize);
                        if (perfBytesSent != null)
                        {
                            perfBytesSent.IncrementBy(blocks.Size);
                        }

                        if (closeCon)
                        {
                            sock.AsyncSendClose(blocks);

                            using (TimedLock.Lock(syncLock))
                                connections.Remove(sock);
                        }
                        else
                        {
                            sock.BeginSendAll(blocks, SocketFlags.None, onSend, httpState);
                        }

                        break;
                    }
                }
            }
            finally
            {
                sock.AppState = null;   // Indicate that we're done processing the request
            }
        }
Exemple #24
0
        public void HttpConnection_Send_Fail()
        {
            EnhancedSocket sockListen = null;
            EnhancedSocket sockAccept = null;
            HttpConnection con;
            HttpRequest    request;
            HttpResponse   response;
            TimeSpan       orgTimeout;
            IAsyncResult   ar;

            orgTimeout = HttpStack.TimeoutSweepInterval;
            HttpStack.TimeoutSweepInterval = TimeSpan.FromMilliseconds(250);

            sockListen = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sockListen.Bind(new IPEndPoint(IPAddress.Any, ServerPort));
            sockListen.Listen(10);

            try
            {
                ar = sockListen.BeginAccept(null, null);

                con = new HttpConnection(HttpOption.None);
                con.Connect("http://localhost:" + ServerPort.ToString());

                sockAccept = sockListen.EndAccept(ar);

                request                   = new HttpRequest("GET", "/foo.htm", null);
                request.Content           = new BlockArray(new byte[100000]);
                request["Content-Length"] = request.Content.Size.ToString();

                ar = con.BeginQuery(request, DateTime.MaxValue, null, null);
                sockAccept.Close();
                sockAccept = null;

                try
                {
                    response = con.EndQuery(ar);
                    Assert.Fail();
                }
                catch
                {
                }

                Assert.IsTrue(con.IsClosed);
                con.Close();
            }
            finally
            {
                HttpStack.TimeoutSweepInterval = orgTimeout;

                if (sockListen != null)
                {
                    sockListen.Close();
                }

                if (sockAccept != null)
                {
                    sockAccept.Close();
                }
            }
        }
Exemple #25
0
        /// <summary>
        /// Releases all resources associated with the instance.
        /// </summary>
        public void Close()
        {
            using (TimedLock.Lock(syncLock))
            {
                if (!isOpen)
                {
                    return;
                }

                // If we're running in UDP mode, then send a couple deregister messages
                // for all host entries to each known name server.  This will ensure that
                // the DNS updated quickly when servers are shut down gracefully.  Note
                // that I'm going to send duplicate messages to each server just to be
                // on the safe side.

                if (settings.Mode == DynDnsMode.Udp)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        DynDnsMessage message;
                        byte[]        packet;

                        foreach (var entry in hosts.Values)
                        {
                            message = new DynDnsMessage(DynDnsMessageFlag.OpUnregister, entry);
                            packet  = message.ToArray(settings.SharedKey);

                            foreach (var nameServer in settings.NameServers)
                            {
                                socket.SendTo(packet, nameServer);
                            }

                            Thread.Sleep(500);
                        }
                    }
                }

                // Shut down the client.

                if (cluster != null)
                {
                    cluster.Stop();
                    cluster = null;
                }

                if (bkTimer != null)
                {
                    bkTimer.Dispose();
                    bkTimer = null;
                }

                if (socket != null)
                {
                    socket.Close();
                    socket = null;
                }

                router = null;
                isOpen = false;
            }
        }
Exemple #26
0
        public void SipTcpTransport_MultipleBuffered()
        {
            // Render two messages into a single buffer and transmit them
            // to a TCP transport in a single send.  This will result in
            // the two messages being processed out of the headerBuf which
            // is what we want to test here.

            TestTransport  transport = new TestTransport();
            EnhancedSocket sock = null;
            SipRequest     msg1, msg2;
            SipRequest     recvMsg;

            byte[] buf;
            int    cb;

            try
            {
                transport.Start(new NetworkBinding("127.0.0.1:5311"));

                sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect("127.0.0.1", 5311);

                msg1 = new SipRequest(SipMethod.Register, "sip:[email protected]", SipHelper.SIP20);
                msg1.AddHeader(SipHeader.Via, string.Format("SIP/2.0/TCP {0}", transport.LocalEndpoint));
                msg1.AddHeader("Count", "0");
                msg1.Contents = GetContents(0);

                msg2 = new SipRequest(SipMethod.Register, "sip:[email protected]", SipHelper.SIP20);
                msg2.AddHeader(SipHeader.Via, string.Format("SIP/2.0/TCP {0}", transport.LocalEndpoint));
                msg2.AddHeader("Count", "1");
                msg2.Contents = GetContents(0);

                buf = Helper.Concat(msg1.ToArray(), msg2.ToArray());
                cb  = sock.Send(buf);
                Assert.AreEqual(buf.Length, cb);

                recvMsg = (SipRequest)transport.Receive();
                Assert.AreEqual(SipMethod.Register, recvMsg.Method);
                Assert.AreEqual("sip:[email protected]", recvMsg.Uri);
                Assert.AreEqual(SipHelper.SIP20, recvMsg.SipVersion);
                Assert.AreEqual(msg1[SipHeader.Via].FullText, recvMsg[SipHeader.Via].FullText);
                Assert.AreEqual("0", recvMsg["Count"].FullText);
                CollectionAssert.AreEqual(msg1.Contents, recvMsg.Contents);

                recvMsg = (SipRequest)transport.Receive();
                Assert.AreEqual(SipMethod.Register, recvMsg.Method);
                Assert.AreEqual("sip:[email protected]", recvMsg.Uri);
                Assert.AreEqual(SipHelper.SIP20, recvMsg.SipVersion);
                Assert.AreEqual(msg2[SipHeader.Via].FullText, recvMsg[SipHeader.Via].FullText);
                Assert.AreEqual("1", recvMsg["Count"].FullText);
                CollectionAssert.AreEqual(msg2.Contents, recvMsg.Contents);

                // Try it again, this time with some data.

                msg1 = new SipRequest(SipMethod.Register, "sip:[email protected]", SipHelper.SIP20);
                msg1.AddHeader(SipHeader.Via, string.Format("SIP/2.0/TCP {0}", transport.LocalEndpoint));
                msg1.AddHeader("Count", "0");
                msg1.Contents = GetContents(10);

                msg2 = new SipRequest(SipMethod.Register, "sip:[email protected]", SipHelper.SIP20);
                msg2.AddHeader(SipHeader.Via, string.Format("SIP/2.0/TCP {0}", transport.LocalEndpoint));
                msg2.AddHeader("Count", "1");
                msg2.Contents = GetContents(20);

                buf = Helper.Concat(msg1.ToArray(), msg2.ToArray());
                cb  = sock.Send(buf);
                Assert.AreEqual(buf.Length, cb);

                recvMsg = (SipRequest)transport.Receive();
                Assert.AreEqual(SipMethod.Register, recvMsg.Method);
                Assert.AreEqual("sip:[email protected]", recvMsg.Uri);
                Assert.AreEqual(SipHelper.SIP20, recvMsg.SipVersion);
                Assert.AreEqual(msg1[SipHeader.Via].FullText, recvMsg[SipHeader.Via].FullText);
                Assert.AreEqual("0", recvMsg["Count"].FullText);
                CollectionAssert.AreEqual(msg1.Contents, recvMsg.Contents);

                recvMsg = (SipRequest)transport.Receive();
                Assert.AreEqual(SipMethod.Register, recvMsg.Method);
                Assert.AreEqual("sip:[email protected]", recvMsg.Uri);
                Assert.AreEqual(SipHelper.SIP20, recvMsg.SipVersion);
                Assert.AreEqual(msg2[SipHeader.Via].FullText, recvMsg[SipHeader.Via].FullText);
                Assert.AreEqual("1", recvMsg["Count"].FullText);
                CollectionAssert.AreEqual(msg2.Contents, recvMsg.Contents);

                // Try it one more time, this time adding a leading CRLF

                msg1 = new SipRequest(SipMethod.Register, "sip:[email protected]", SipHelper.SIP20);
                msg1.AddHeader(SipHeader.Via, string.Format("SIP/2.0/TCP {0}", transport.LocalEndpoint));
                msg1.AddHeader("Count", "0");
                msg1.Contents = GetContents(10);

                msg2 = new SipRequest(SipMethod.Register, "sip:[email protected]", SipHelper.SIP20);
                msg2.AddHeader(SipHeader.Via, string.Format("SIP/2.0/TCP {0}", transport.LocalEndpoint));
                msg2.AddHeader("Count", "1");
                msg2.Contents = GetContents(20);

                buf = Helper.Concat(new byte[] { 0x0D, 0x0A }, msg1.ToArray(), new byte[] { 0x0D, 0x0A }, msg2.ToArray());
                cb  = sock.Send(buf);
                Assert.AreEqual(buf.Length, cb);

                recvMsg = (SipRequest)transport.Receive();
                Assert.AreEqual(SipMethod.Register, recvMsg.Method);
                Assert.AreEqual("sip:[email protected]", recvMsg.Uri);
                Assert.AreEqual(SipHelper.SIP20, recvMsg.SipVersion);
                Assert.AreEqual(msg1[SipHeader.Via].FullText, recvMsg[SipHeader.Via].FullText);
                Assert.AreEqual("0", recvMsg["Count"].FullText);
                CollectionAssert.AreEqual(msg1.Contents, recvMsg.Contents);

                recvMsg = (SipRequest)transport.Receive();
                Assert.AreEqual(SipMethod.Register, recvMsg.Method);
                Assert.AreEqual("sip:[email protected]", recvMsg.Uri);
                Assert.AreEqual(SipHelper.SIP20, recvMsg.SipVersion);
                Assert.AreEqual(msg2[SipHeader.Via].FullText, recvMsg[SipHeader.Via].FullText);
                Assert.AreEqual("1", recvMsg["Count"].FullText);
                CollectionAssert.AreEqual(msg2.Contents, recvMsg.Contents);
            }
            finally
            {
                if (sock != null)
                {
                    sock.Close();
                }

                transport.Stop();
            }
        }