public virtual void Run() { int nameTrnId; NameServicePacket response; try { while (_thread == Thread.CurrentThread()) { _socket.SoTimeOut = _closeTimeout; 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; Runtime.Notify(response); } } } } catch (TimeoutException) { } catch (Exception ex) { if (_log.Level > 2) { Runtime.PrintStackTrace(ex, _log); } } finally { TryClose(); } }
/// <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); _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(); } } } } }
/// <exception cref="System.IO.IOException"></exception> internal virtual void Send(NameServicePacket request, NameServicePacket response, int timeout) { //Log.Out("NameSerciceClient.Send - Start"); int nid = 0; int max = NbtAddress.Nbns.Length; if (max == 0) { max = 1; } lock (response) { this._isActive = true; while (max-- > 0) { try { lock (_lock) { request.NameTrnId = GetNextNameTrnId(); nid = request.NameTrnId; response.Received = false; _responseTable.Put(nid, response); //Log.Out($"NameSerciceClient.Send - timeout = {timeout}"); EnsureOpen(timeout + 1000); int requestLenght = request.WriteWireFormat(_sndBuf, 0); byte[] msg = new byte[requestLenght]; Array.Copy(_sndBuf, msg, requestLenght); _socketSender.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, request.IsBroadcast ? 1 : 0); _socketSender.SendTo(msg, new IPEndPoint(request.Addr, _lport)); //Log.Out("NameSerciceClient.Send - Sended"); if (_log.Level > 3) { _log.WriteLine(request); Hexdump.ToHexdump(_log, _sndBuf, 0, requestLenght); } } if (_waitResponse) { long start = Runtime.CurrentTimeMillis(); var isRecieved = false; var startTime = DateTime.Now; while (timeout > 0) { Runtime.Wait(response, timeout); if (response.Received && request.QuestionType == response.RecordType) { //return; isRecieved = true; break; } response.Received = false; timeout -= (int)(Runtime.CurrentTimeMillis() - start); //if (timeout <= 0) //{ // Log.Out($"NameSerciceClient.Send Timeout! - {(DateTime.Now - startTime).TotalMilliseconds} msec"); //} } if (isRecieved) { break; } } } catch (Exception ie) { if (_waitResponse) { _responseTable.Remove(nid); } //Log.Out("NameSerciceClient.Send - IOException"); throw new IOException(ie.Message); } finally { 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(); } } } this._isActive = false; //Log.Out("NameSerciceClient.Send - Normaly Ended."); } }
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(); } }