Exemplo n.º 1
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void Send(NameServicePacket request, NameServicePacket response,
                                   int timeout)
        {
            int nid = 0;
            int max = NbtAddress.Nbns.Length;

            if (max == 0)
            {
                max = 1;
            }

            lock (response)
            {
                while (max-- > 0)
                {
                    try
                    {
                        lock (_lock)
                        {
                            request.NameTrnId = GetNextNameTrnId();
                            nid = request.NameTrnId;
                            response.Received = false;
                            _responseTable.Put(nid, response);
                            EnsureOpen(timeout + 1000);

                            int requestLenght = request.WriteWireFormat(_sndBuf, 0);
                            //NEW SOCKET
                            _socket.Send(_sndBuf, 0, requestLenght, SocketFlags.None);
                            //_socket.Send(_sndBuf, 0, requestLenght, new IPEndPoint(request.Addr, _lport));
                            if (_log.Level > 3)
                            {
                                _log.WriteLine(request);
                                Hexdump.ToHexdump(_log, _sndBuf, 0, requestLenght);
                            }
                        }
                        if (_waitResponse)
                        {
                            long start = Runtime.CurrentTimeMillis();
                            while (timeout > 0)
                            {
                                Runtime.Wait(response, timeout);
                                if (response.Received && request.QuestionType == response.RecordType)
                                {
                                    return;
                                }
                                response.Received = false;
                                timeout          -= (int)(Runtime.CurrentTimeMillis() - start);
                            }
                        }
                    }
                    catch (Exception ie)
                    {
                        throw new IOException(ie.Message);
                    }
                    finally
                    {
                        //Sharpen.Collections.Remove(responseTable, nid);
                        if (_waitResponse)
                        {
                            _responseTable.Remove(nid);
                        }
                    }
                    if (_waitResponse)
                    {
                        lock (_lock)
                        {
                            if (NbtAddress.IsWins(request.Addr) == false)
                            {
                                break;
                            }
                            if (request.Addr == NbtAddress.GetWinsAddress())
                            {
                                NbtAddress.SwitchWins();
                            }
                            request.Addr = NbtAddress.GetWinsAddress();
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public virtual void Run()
        {
            int nameTrnId;
            NameServicePacket response;

            try
            {
                //winrt
                //while (_thread == Thread.CurrentThread())
                while (true)
                {
                    _socket.SoTimeOut = _closeTimeout;

                    //NEW SOCKET
                    int len = _socket.Receive(_rcvBuf, 0, RcvBufSize, System.Net.Sockets.SocketFlags.None);
                    // int len = _socket.Receive(_rcvBuf, 0, RcvBufSize);

                    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)
                    {
                        response.ReadWireFormat(_rcvBuf, 0);

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

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

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