Ejemplo n.º 1
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void EnsureOpen(int timeout)
        {
            //Log.Out($"NameServiceClient.EnsureOpen");

            _closeTimeout = 0;
            if (SoTimeout != 0)
            {
                _closeTimeout = Math.Max(SoTimeout, timeout);
            }

            var localPort = (SmbConstants.Lport == 0) ? _lport : SmbConstants.Lport;

            // If socket is still good, the new closeTimeout will
            // be ignored; see tryClose comment.
            if (
                _socketSender == null ||
                _socketSender.LocalEndPoint == null ||
                _socketSender.GetLocalPort() != localPort ||
                !IPAddress.Any.Equals(_socketSender.GetLocalInetAddress())
                )
            {
                if (_socketSender != null)
                {
                    _socketSender.Dispose();
                    _socketSender = null;
                }

                _socketSender = new SocketEx(AddressFamily.InterNetwork,
                                             SocketType.Dgram,
                                             ProtocolType.Udp);

                _socketSender.Bind(new IPEndPoint(IPAddress.Any, localPort));


                if (_waitResponse)
                {
                    if (_thread != null)
                    {
                        _thread.Cancel(true);
                        _thread.Dispose();
                    }

                    _thread = new Thread(this);
                    _thread.SetDaemon(true);
                    _thread.Start(true);
                }
            }
        }