Example #1
0
 /**
  * Inject frame and procotol objects.
  * 
  * @param McFrazier\PhpBinaryCql\CqlFrame $frame
  * @param NULL | McFrazier\PhpBinaryCql\CqlProtocol $protocol
  */
 public function __construct($frame, $protocol = NULL)
 {
     // set verison
     $this->_version = $frame->getVersion();
     // set flag
     $this->_flag = $frame->getFlag();
     // set stream
     $this->_stream = $frame->getStream();
     // set opcode
     $this->_opcode = $frame->getOpcode();
     // set bin body
     $this->_body = $frame->getBody();
     // parse the framebody
     $this->_data = $protocol->parseBinaryFrame($frame);
     // check if we have an error response
     if ((int) $this->_opcode === (int) \McFrazier\PhpBinaryCql\CqlConstants::FRAME_OPCODE_ERROR) {
         // we have an error set the error flag to true
         $this->_isError = TRUE;
         // set the error code
         $this->_errorCode = $this->_data->code;
         // set the error message
         $this->_errorMessage = $this->_data->msg;
     }
     $protocol->resetCounters();
 }
Example #2
0
 /**
  * Builds a binary frame, from a frame object.
  * 
  * @param McFrazier\PhpBinaryCql\CqlFrame $frame
  * @return string
  */
 public function generateBinaryFrame($frame)
 {
     // get flag status
     $flagStatus = $this->_checkFrameFlags($frame->getFlag());
     // binary body length
     $frameBody = $frame->getBody();
     $frameBodyLength = NULL;
     // @TODO need to add a check to check which compression was used
     // in the frame, can't assume snappy.
     if ($flagStatus->compression) {
         // compressed
         $frameBody = snappy_compress($frameBody);
         $bodyLength = strlen($frameBody);
         $frameBodyLength = $this->generateInt($bodyLength);
     } else {
         // not compressed
         $bodyLength = strlen($frameBody);
         $frameBodyLength = $this->generateInt($bodyLength);
     }
     // build frame header
     $header = pack("hhhh", $frame->getVersion(), $frame->getFlag(), $frame->getStream(), $frame->getOpcode());
     $header .= $frameBodyLength;
     return $header . $frameBody;
 }