/// <summary> /// Send a command and returns the reply as a raw string /// </summary> /// <param name="command">The command to be sent</param> /// <returns>Returns the raw string replied by the server</returns> public virtual string SendRawCommand(TCCommand command) { string rawReply = ""; canListen = false; lock (controlSocket) { //Send the command controlSocket.Send(command.Raw()); //Wait for response int timeout = (int)ResponseTimeout / sleep; int i = 1; while (!controlSocket.ResponseAvailable && i < timeout) { Thread.Sleep(sleep); i++; } //Read Response rawReply = controlSocket.Receive(); } canListen = true; return rawReply; }
/// <summary> /// /// </summary> /// <param name="command"></param> /// <returns></returns> public virtual List<Reply> SendCommand(TCCommand command) { List<Reply> replies = Reply.Parse(SendRawCommand(command)); List<Reply> asyncEventsReply = new List<Reply>(); List<TorEvent> asyncEvents = new List<TorEvent>(); foreach (var r in replies) { if (r.Code == ReplyCodes.ASYNC_EVENT_NOTIFICATION) { asyncEventsReply.Add(r); } } foreach (var e in asyncEventsReply) { replies.Remove(e); asyncEvents.Add(TorEvent.Parse(e)); } AsyncEventDispatcher(asyncEvents); return replies; }
/// <summary> /// Send a command and returns all replies /// </summary> /// <param name="command">The command to be sent</param> /// <returns>List of all received replies</returns> public virtual List<Reply> SendCommand(TCCommand command) { _controlSocket.Send(command.Raw()); List<Reply> replies = new List<Reply>(); // receive replies until we get an end reply for (;;) { lock (_replyBuffer) { while (_replyBuffer.Count > 0) { Reply reply = _replyBuffer.Dequeue(); replies.Add(reply); if (reply.RawString[3] == ' ') { // end reply received --> finished return replies; } } } #if DEBUG if (!_replyReceivedHdl.WaitOne()) #else if(!_replyReceivedHdl.WaitOne((int)ResponseTimeout)) #endif { throw new TimeoutException("Timeout while receiving replies"); } } }