/**
  * Handles received data
  *
  * @param string $data
  *
  * @return SocketMessage|null
  */
 public function handle($data)
 {
     try {
         if (!$data) {
             return null;
         }
         if (is_null($this->currentMessage)) {
             $buffer = new Buffer($data);
             $dataLength = strlen($data);
             $messageLength = $buffer->readInt32LE(0) + MessageConfiguration::INT_32_LENGTH;
             if ($dataLength == $messageLength) {
                 return $this->decomposeMessage($data);
             }
             if ($dataLength > $messageLength) {
                 $this->currentMessage = substr($data, $messageLength + 1, $dataLength);
                 return $this->decomposeMessage(substr($data, 0, $messageLength));
             }
             $this->currentMessage = $this->currentMessage . $data;
             return null;
         }
         $buffer = new Buffer($this->currentMessage);
         $messageLength = $buffer->readInt32LE(0) + MessageConfiguration::INT_32_LENGTH;
         $mergedMessages = $this->currentMessage . $data;
         $dataLength = strlen($mergedMessages);
         if ($messageLength <= $dataLength) {
             $this->currentMessage = null;
             return $this->decomposeMessage($mergedMessages);
         }
         $this->currentMessage .= $data;
         return null;
     } catch (\Exception $e) {
         $this->logger->critical('Error during handling incoming message.' . ' Message Error: ' . $e->getMessage());
     }
 }
 /**
  * Handles received data
  *
  * @param string $data
  *
  * @return SocketMessage|null
  */
 public function handle($data)
 {
     try {
         if (!$data) {
             return null;
         }
         $socketMessages = array();
         if (!is_null($this->currentMessage)) {
             $data = $this->currentMessage . $data;
         }
         do {
             $buffer = new Buffer($data);
             $dataLength = strlen($data);
             $messageLength = $buffer->readInt32LE(0) + MessageConfiguration::INT_32_LENGTH;
             if ($dataLength == $messageLength) {
                 $socketMessages[] = $this->decomposeMessage($data);
                 $this->currentMessage = null;
             } elseif ($dataLength > $messageLength) {
                 $message = substr($data, 0, $messageLength);
                 $socketMessages[] = $this->decomposeMessage($message);
                 // reset data to next message
                 $data = substr($data, $messageLength, $dataLength);
                 $this->currentMessage = null;
             } else {
                 $this->currentMessage .= $data;
             }
         } while ($dataLength > $messageLength);
         return $socketMessages;
     } catch (\Exception $e) {
         $this->logger->critical('Error during handling incoming message.' . ' Message Error: ' . $e->getMessage());
     }
 }
 /**
  * @test
  * @expectedException \RuntimeException
  */
 public function it_should_compose_empty_message_without_auth()
 {
     $this->socketMessage = $this->socketMessage->reveal();
     $binaryMessage = $this->messageComposer->compose($this->socketMessage);
     $buffer = new Buffer($binaryMessage);
     \PHPUnit_Framework_Assert::assertEquals(18, $buffer->readInt32LE(0));
     \PHPUnit_Framework_TestCase::assertEquals(MessageType::HEARTBEAT_REQUEST, $buffer->readInt8(4));
     \PHPUnit_Framework_TestCase::assertEquals(MessageConfiguration::FLAGS_NONE, $buffer->readInt8(5));
     \PHPUnit_Framework_TestCase::assertEquals('12350000000000000000000000000000', bin2hex($buffer->read(6, 16)));
     //should throw exception out of range
     $buffer->read(22, 1);
 }
 /**
  * Decomposes binary message, which comes from the stream
  *
  * @param string $message
  *
  * @return SocketMessage
  */
 public function decomposeMessage($message)
 {
     $buffer = new Buffer($message);
     // Information about how long message is. To help it decode. Comes from the server
     // $messageLenght = (whole stream length) - (4 bytes for saved length).
     $messageLength = $buffer->readInt32LE(0);
     $messageType = new MessageType($buffer->readInt8(MessageConfiguration::MESSAGE_TYPE_OFFSET));
     $flag = $buffer->readInt8(MessageConfiguration::FLAG_OFFSET);
     $correlationID = bin2hex($buffer->read(MessageConfiguration::CORRELATION_ID_OFFSET, MessageConfiguration::CORRELATION_ID_LENGTH));
     $data = $buffer->read(MessageConfiguration::DATA_OFFSET, $messageLength - MessageConfiguration::HEADER_LENGTH);
     $communicable = $this->communicationFactory->create($messageType);
     return $communicable->handle($messageType, $correlationID, $data);
 }
示例#5
0
 public function testReadInt32LE()
 {
     $buffer = new Buffer(pack('V', 0.0));
     $this->assertSame(0.0, $buffer->readInt32LE(0));
 }