Пример #1
0
        private void InitiateUDP(IPEndPoint sendTarget)
        {
            #if !SILVERLIGHT
            MyUDPSocket = new Socket(MyTCPSocket.AddressFamily == AddressFamily.InterNetworkV6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            // Must bind the UDP socket, even though we are not receiving with it (UDPListener receives)
            // Binding sets the appropriate local endpoint by which the remote
            // connection identifies all packets we send
            UDPSendTarget = sendTarget;

            // Bind to new local address - do not share with TCP socket binding... does not work in Mono/Linux
            MyUDPSocket.Bind(new IPEndPoint(MyTCPSocket.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0));
            //MyUDPSocket.Bind(MyTCPSocket.LocalEndPoint);

            if (BlockingMode)
            {
                m_UDPListener = new UDPListenerDuplexBlocking();
                //m_UDPListener = new UDPListenerSimplex();
            }
            else
            {
                m_UDPListener = new UDPListener();
            }

            IPEndPoint local = MyUDPSocket.LocalEndPoint as IPEndPoint;
            m_UDPListener.Socket = MyUDPSocket;
            m_UDPListener.StartListening(MyUDPSocket.AddressFamily, local.Port, 2, this);

            if (LoggedIn && EnableUDPKeepAlive)
            {
                // poke TWICE! The first poke doesn't get responded to, because ACKs get sent
                // BEFORE a packet is processed... and the proper port wont be known until
                // AFTER the packet is processed.  So the first poke will be responded to on the
                // wrong port.
                OnUDPKeepAlive(null, null);
                OnUDPKeepAlive(null, null);
            }
            #endif
        }
Пример #2
0
        /// <summary>
        /// Gets called when the socket for this connection was closed, for any reason
        /// </summary>
        /// <param name="msg"> a message explaining the socket closure, if any</param>
        protected override void OnSocketKilled(string msg)
        {
            #if !SILVERLIGHT
            m_UDPKeepAlive.Elapsed -= new System.Timers.ElapsedEventHandler(OnUDPKeepAlive);
            m_UDPKeepAlive.Stop();
            m_UDPKeepAlive.Dispose();

            try
            {
                if (m_UDPListener != null)
                {
                    m_UDPListener.StopListening();
                    if (m_UDPListener is UDPListenerSimplex)
                    {
                        ((UDPListenerSimplex)m_UDPListener).StopNetworkingPump();
                    }
                    m_UDPListener = null;
                }
            }
            catch { }
            #endif

            base.OnSocketKilled(msg);
            m_ConnectionInProgress = false;
            m_ConnectionWasKilled = true;
        }