コード例 #1
0
        /**
         * Creates a listening point from the following address and attempts to
         * discover how it is mapped so that using inside the application is possible.
         * @param address the [address]:[port] pair where ther request should be
         * sent from.
         * @return a StunAddress object containing the mapped address or null if
         * discovery failed.
         * @throws StunException if something fails along the way.
         */
        public virtual StunAddress GetMappingFor(StunAddress address)
        {
            NetAccessPointDescriptor apDesc = new NetAccessPointDescriptor(address);

            stunStack.InstallNetAccessPoint(apDesc);

            requestSender = new BlockingRequestSender(stunProvider, apDesc);
            StunMessageEvent evt = null;

            try
            {
                evt = requestSender.SendRequestAndWaitForResponse(
                    MessageFactory.CreateBindingRequest(), serverAddress);
            }
            finally
            {
                //free the port to allow the application to use it.
                stunStack.RemoveNetAccessPoint(apDesc);
            }

            if (evt != null)
            {
                Response res = (Response)evt.GetMessage();
                MappedAddressAttribute maAtt =
                    (MappedAddressAttribute)
                    res.GetAttribute(Attribute.MAPPED_ADDRESS);
                if (maAtt != null)
                {
                    StunAddress sa = maAtt.GetAddress();
                    return(sa);
                }
            }

            return(null);
        }
コード例 #2
0
        /**
         * Creates a listening point for the specified socket and attempts to
         * discover how its local address is NAT mapped.
         * @param socket the socket whose address needs to be resolved.
         * @return a StunAddress object containing the mapped address or null if
         * discovery failed.
         * @throws StunException if something fails along the way.
         */

        public virtual StunAddress GetMappingFor(MyUdpClient socket)
        {
            NetAccessPointDescriptor apDesc = stunStack.InstallNetAccessPoint(socket);

            requestSender = new BlockingRequestSender(stunProvider, apDesc);
            StunMessageEvent evt = null;

            try
            {
                evt = requestSender.SendRequestAndWaitForResponse(
                    MessageFactory.CreateBindingRequest(), serverAddress);
            }
            finally
            {
                stunStack.RemoveNetAccessPoint(apDesc);
            }

            if (evt != null)
            {
                Response res = (Response)evt.GetMessage();
                MappedAddressAttribute maAtt =
                    (MappedAddressAttribute)
                    res.GetAttribute(Attribute.MAPPED_ADDRESS);
                if (maAtt != null)
                {
                    return(maAtt.GetAddress());
                }
            }

            return(null);
        }
コード例 #3
0
        /**
         * Shuts down the underlying stack and prepares the object for garbage
         * collection.
         */
        public virtual void ShutDown()
        {
            StunStack.ShutDown();
            stunStack     = null;
            stunProvider  = null;
            requestSender = null;

            this.started = false;
        }
コード例 #4
0
        /**
         * Puts the discoverer into an operational state.
         * @throws StunException if we fail to bind or some other error occurs.
         */
        virtual public void Start()
        {
            stunStack = StunStack.Instance;
            stunStack.Start();

            stunStack.InstallNetAccessPoint(apDescriptor);

            stunProvider = stunStack.GetProvider();

            requestSender = new BlockingRequestSender(stunProvider, apDescriptor);

            started = true;
        }
コード例 #5
0
        /**
         * Puts the discoverer into an operational state.
         * @throws StunException if we fail to bind or some other error occurs.
         */
        public virtual void Start()
        {
            stunStack = StunStack.Instance;
            stunStack.Start();

            stunStack.InstallNetAccessPoint(apDescriptor);

            stunProvider = stunStack.GetProvider();

            requestSender = new BlockingRequestSender(stunProvider, apDescriptor);

            started = true;
        }
コード例 #6
0
        /**
         * Shuts down the underlying stack and prepares the object for garbage
         * collection.
         */
        public virtual void ShutDown()
        {
            StunStack.ShutDown();
            stunStack     = null;
            stunProvider  = null;
            apDescriptor  = null;
            requestSender = null;

            this.started = false;
        }