Esempio n. 1
0
        public virtual void Run()
        {
            int nameTrnId;
            NameServicePacket response;

            try
            {
                while (Thread.CurrentThread().Equals(_thread))
                {
                    if (_thread.IsCanceled)
                    {
                        break;
                    }

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

                    var sockEvArg = new SocketAsyncEventArgs();
                    sockEvArg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, localPort);
                    sockEvArg.SetBuffer(_rcvBuf, 0, RcvBufSize);
                    sockEvArg.Completed += this.OnReceiveCompleted;

                    _socketSender.SoTimeOut = _closeTimeout;

                    this._recievedLength = -1;

                    //Log.Out($"NameServiceClient.Run - Wait Recieve: {IPAddress.Any}: {localPort}");
                    _socketSender.ReceiveFromAsync(sockEvArg);

                    while (this._recievedLength == -1)
                    {
                        if (_thread.IsCanceled)
                        {
                            break;
                        }

                        Task.Delay(300).GetAwaiter().GetResult();
                    }

                    sockEvArg?.Dispose();


                    if (_thread.IsCanceled)
                    {
                        break;
                    }

                    if (_log.Level > 3)
                    {
                        _log.WriteLine("NetBIOS: new data read from socket");
                    }
                    nameTrnId = NameServicePacket.ReadNameTrnId(_rcvBuf, 0);
                    response  = (NameServicePacket)_responseTable.Get(nameTrnId);


                    if (response == null || response.Received)
                    {
                        continue;
                    }

                    lock (response)
                    {
                        if (_thread.IsCanceled)
                        {
                            break;
                        }

                        response.ReadWireFormat(_rcvBuf, 0);

                        if (_log.Level > 3)
                        {
                            _log.WriteLine(response);
                            Hexdump.ToHexdump(_log, _rcvBuf, 0, this._recievedLength);
                        }

                        if (response.IsResponse)
                        {
                            response.Received = true;

                            Runtime.Notify(response);
                        }
                    }
                }
            }
            catch (TimeoutException) { }
            catch (Exception ex)
            {
                if (_log.Level > 2)
                {
                    Runtime.PrintStackTrace(ex, _log);
                }
            }
            finally
            {
                TryClose();
            }
        }