/** * Write a single message to the server. This cannot be called after * wait is called. * * @param ByteBuffer $data The data to write * @param array $options an array of options, possible keys: * 'flags' => a number */ public function write($data, array $options = []) { $message_array = ['message' => $data->serialize()]; if (array_key_exists('flags', $options)) { $message_array['flags'] = $options['flags']; } $this->call->startBatch([OP_SEND_MESSAGE => $message_array]); }
/** * Write a single message to the server. This cannot be called after * writesDone is called. * * @param ByteBuffer $data The data to write * @param array $options an array of options, possible keys: * 'flags' => a number */ public function write($data, $options = []) { $message_array = ['message' => $data->serialize()]; if (isset($options['flags'])) { $message_array['flags'] = $options['flags']; } $this->call->startBatch([OP_SEND_MESSAGE => $message_array]); }
/** * Creates a new packet object based on the header byte of the given raw * data * * @param string $rawData The raw data of the packet * @return RCONPacket The packet object generated from the packet data * @throws PacketFormatException if the packet header is not recognized */ public static function getPacketFromData($rawData) { $byteBuffer = new ByteBuffer($rawData); $requestId = $byteBuffer->getLong(); $header = $byteBuffer->getLong(); $data = $byteBuffer->getString(); switch ($header) { case RCONPacket::SERVERDATA_AUTH_RESPONSE: return new RCONAuthResponse($requestId); case RCONPacket::SERVERDATA_RESPONSE_VALUE: return new RCONExecResponse($requestId, $data); default: throw new PacketFormatException('Unknown packet with header ' . dechex($header) . ' received.'); } }
public function send(RCONPacket $dataPacket) { if (!$this->channel->isConnected()) { $this->channel->connect($this->remoteSocket[0], $this->remoteSocket[1]); } $this->buffer = ByteBuffer::wrap($dataPacket->getBytes()); $this->channel->write($this->buffer); }
public function testReceiveIntoExistingBuffer() { $this->socket->buffer = ByteBuffer::allocate(10); $this->udpSocket->expects($this->once())->method('select')->will($this->returnValue(true)); $this->udpSocket->expects($this->once())->method('recv')->with(4)->will($this->returnValue('test')); $this->assertEquals(4, $this->socket->receivePacket(4)); $buffer = $this->socket->buffer; $this->assertEquals(0, $buffer->position()); $this->assertEquals(4, $buffer->remaining()); $this->assertEquals('test', $buffer->_array()); }
public function read($callable, $num = false) { if (is_callable($callable) && $num) { return $this->readIterable($callable, $num); } else { $num = $callable; } $buffer = ByteBuffer::create(); $count = 0; while ($count < $num) { $byte = fgetc($this->socket); $buffer->push($byte); $count++; } $this->count += $count; return $buffer; }
/** * @return int */ public function receivePacket($bufferLength = 0) { if (!$this->socket->select(self::$timeout)) { throw new TimeoutException(); } if ($bufferLength == 0) { $this->buffer->clear(); } else { $this->buffer = ByteBuffer::allocate($bufferLength); } $data = $this->socket->recv($this->buffer->remaining()); $this->buffer->put($data); $bytesRead = $this->buffer->position(); $this->buffer->rewind(); $this->buffer->limit($bytesRead); return $bytesRead; }
/** * @param byte $headerData * @param byte[] $contentData * @param bool $splitPacket */ public function __construct($headerData, $contentData = null) { $this->headerData = $headerData; $this->contentData = ByteBuffer::wrap($contentData); }
/** * @param $index * @return int */ public function getInt($index) { $result = $this->readLittleEndian($index, 4); $sign = $index + (ByteBuffer::isLittleEndian() ? 3 : 0); $issigned = isset($this->_buffer[$sign]) && ord($this->_buffer[$sign]) & 0x80; if (PHP_INT_SIZE > 4) { // 4294967296 = 1 << 32 = Maximum unsigned 32-bit int return $issigned ? $result - self::UINT + 1 : $result; } else { // 32bit / Windows treated number as signed integer. return $result; } }
/** * @param $index * @return int */ public function getInt($index) { $result = $this->readLittleEndian($index, 4); $sign = $index + (ByteBuffer::isLittleEndian() ? 3 : 0); $issigned = isset($this->_buffer[$sign]) && ord($this->_buffer[$sign]) & 0x80; // 4294967296 = 1 << 32 = Maximum unsigned 32-bit int return $issigned ? $result - 4294967296 : $result; }
/** * @param ByteBuffer $bb * @param string $ident * @return bool * @throws \ArgumentException */ protected static function __has_identifier($bb, $ident) { if (strlen($ident) != self::FILE_IDENTIFIER_LENGTH) { throw new \ArgumentException("FlatBuffers: file identifier must be length " . self::FILE_IDENTIFIER_LENGTH); } for ($i = 0; $i < 4; $i++) { if ($ident[$i] != $bb->get($bb->getPosition() + self::SIZEOF_INT + $i)) { return false; } } return true; }
protected function generateMask() { return ByteBuffer::create(array(rand(0, 255), rand(0, 255), rand(0, 255), rand(0, 255))); }
/** * */ public function send(SteamPacket $dataPacket) { trigger_error("Sending packet of type \"" . get_class($dataPacket) . "\"..."); $this->buffer = ByteBuffer::wrap($dataPacket->__toString()); $this->channel->write($this->buffer); }
/** * Write a single message to the server. This cannot be called after * writesDone is called. * @param ByteBuffer $data The data to write */ public function write($data) { $this->call->start_batch([OP_SEND_MESSAGE => $data->serialize()]); }
public function __construct(InetAddress $ipAddress, $portNumber) { $this->buffer = ByteBuffer::allocate(1400); $this->ipAddress = $ipAddress; $this->portNumber = $portNumber; }
public function write(ByteBuffer $sourceBuffer) { return $this->socket->send($sourceBuffer->get()); }
/** * read little endian value from the buffer * * @param $offset * @param $count acutal size * @return int */ public function readLittleEndian($offset, $count, $force_bigendian = false) { $this->assertOffsetAndLength($offset, $count); $r = 0; if (ByteBuffer::isLittleEndian() && $force_bigendian == false) { for ($i = 0; $i < $count; $i++) { $r |= ord($this->_buffer[$offset + $i]) << $i * 8; } } else { for ($i = 0; $i < $count; $i++) { $r |= ord($this->_buffer[$offset + $count - 1 - $i]) << $i * 8; } } return $r; }
/** * parseNumberToCountBuffer function. * * @access public * @static * @param mixed $num * @return void */ public static function parseNumberToCountBuffer($count, $mask = true) { if ($count < 125) { if ($mask) { $count += 128; } $count = ByteBuffer::create(array((int) $count)); } else { if ($count > 125 && $count < self::DOUBLE_BYTE_LENGTH) { $count = ByteBuffer::parseNumberToBuffer($count); $extraByte = $mask ? self::DOUBLE_BYTE : self::DOUBLE_BYTE - 128; $count->unshift($extraByte); } else { $count = ByteBuffer::parseNumberToBuffer($count); $extraByte = $mask ? self::QUAD_BYTE : self::QUAD_BYTE - 128; $count->unshift($extraByte); } } return $count; }