示例#1
0
 /**
  * Extracts a new response from a communicator.
  * 
  * @param Communicator $com        The communicator from which to extract
  *     the new response.
  * @param bool         $asStream   Whether to populate the argument values
  *     with streams instead of strings.
  * @param int          $timeout_s  If a response is not immediatly
  *     available, wait this many seconds. If NULL, wait indefinetly.
  * @param int          $timeout_us Microseconds to add to the waiting time.
  * @param Registry     $reg        An optional registry to sync the
  *     response with.
  * 
  * @see getType()
  * @see getArgument()
  */
 public function __construct(Communicator $com, $asStream = false, $timeout_s = 0, $timeout_us = null, Registry $reg = null)
 {
     if (null === $reg) {
         if ($com->getTransmitter()->isPersistent()) {
             $old = $com->getTransmitter()->lock(T\Stream::DIRECTION_RECEIVE);
             try {
                 $this->_receive($com, $asStream, $timeout_s, $timeout_us);
             } catch (E $e) {
                 $com->getTransmitter()->lock($old, true);
                 throw $e;
             }
             $com->getTransmitter()->lock($old, true);
         } else {
             $this->_receive($com, $asStream, $timeout_s, $timeout_us);
         }
     } else {
         while (null === ($response = $reg->getNextResponse())) {
             $newResponse = new self($com, true, $timeout_s, $timeout_us);
             $tagInfo = $reg::parseTag($newResponse->getTag());
             $newResponse->setTag($tagInfo[1]);
             if (!$reg->add($newResponse, $tagInfo[0])) {
                 $response = $newResponse;
                 break;
             }
         }
         $this->_type = $response->_type;
         $this->arguments = $response->arguments;
         $this->unrecognizedWords = $response->unrecognizedWords;
         $this->setTag($response->getTag());
         if (!$asStream) {
             foreach ($this->arguments as $name => $value) {
                 $this->setArgument($name, stream_get_contents($value));
             }
             foreach ($response->unrecognizedWords as $i => $value) {
                 $this->unrecognizedWords[$i] = stream_get_contents($value);
             }
         }
     }
 }
示例#2
0
 /**
  * Sends a request over a communicator.
  * 
  * @param Communicator $com The communicator to send the request over.
  * @param Registry     $reg An optional registry to sync the request with.
  * 
  * @return int The number of bytes sent.
  * @see Client::sendSync()
  * @see Client::sendAsync()
  */
 public function send(Communicator $com, Registry $reg = null)
 {
     if (null !== $reg && (null != $this->getTag() || !$reg->isTaglessModeOwner())) {
         $originalTag = $this->getTag();
         $this->setTag($reg->getOwnershipTag() . $originalTag);
         $bytes = $this->send($com);
         $this->setTag($originalTag);
         return $bytes;
     }
     if ($com->getTransmitter()->isPersistent()) {
         $old = $com->getTransmitter()->lock(T\Stream::DIRECTION_SEND);
         $bytes = $this->_send($com);
         $com->getTransmitter()->lock($old, true);
         return $bytes;
     }
     return $this->_send($com);
 }