A wrapper to read and write from streams. The Connection object can also be utilized without a stream, e.g.:
$con = new \Phpcraft\Connection($protocol_version);
$packet = new \Phpcraft\SpawnMobPacket();
// $packet->...
$packet->send($con);
echo \Phpcraft\Phpcraft::binaryStringToHex($con->write_buffer)."\n";
◆ __construct()
| Phpcraft\Connection::__construct |
( |
int |
$protocol_version = -1, |
|
|
|
$stream = null |
|
) |
| |
- Parameters
-
| int | $protocol_version | |
| resource | null | $stream | |
◆ close()
| Phpcraft\Connection::close |
( |
| ) |
|
Closes the connection's stream, if it has one.
- Returns
- void
◆ getRemainingData()
| Phpcraft\Connection::getRemainingData |
( |
| ) |
|
Returns all the data in the read buffer that is yet to be read.
- Returns
- string
◆ getRemoteAddress()
| Phpcraft\Connection::getRemoteAddress |
( |
| ) |
|
The address of the connection's peer or null if the connection has no stream.
- Returns
- string|null
◆ ignoreBytes()
| Phpcraft\Connection::ignoreBytes |
( |
int |
$bytes | ) |
|
Skips over the given amount of bytes in the read buffer.
- Parameters
-
- Returns
- Connection $this
- Exceptions
-
| IOException | When there are not enough bytes in the buffer to ignore the given number. |
◆ isOpen()
| Phpcraft\Connection::isOpen |
( |
| ) |
|
Returns true if this connection has an open stream.
- Returns
- boolean
◆ readAngle()
| Phpcraft\Connection::readAngle |
( |
| ) |
|
Reads an angle from the read buffer.
- Returns
- float
- Exceptions
-
| IOException | When there are not enough bytes to read an angle. |
◆ readBoolean()
| Phpcraft\Connection::readBoolean |
( |
| ) |
|
Reads a boolean from the read buffer.
- Returns
- boolean
- Exceptions
-
| IOException | When there are not enough bytes to read a boolean. |
◆ readByte()
| Phpcraft\Connection::readByte |
( |
| ) |
|
Reads a signed byte from the read buffer.
- Returns
- int
- Exceptions
-
| IOException | When there are not enough bytes to read a byte. |
◆ readChat()
| Phpcraft\Connection::readChat |
( |
| ) |
|
Reads a chat object from the read buffer.
- Returns
- array
- Exceptions
-
| IOException | When there are not enough bytes to read the string. |
◆ readDouble()
| Phpcraft\Connection::readDouble |
( |
| ) |
|
Reads a double from the read buffer.
- Returns
- float
- Exceptions
-
| IOException | When there are not enough bytes to read a double. |
◆ readFixedPointPosition()
| Phpcraft\Connection::readFixedPointPosition |
( |
| ) |
|
Reads a position encoded as three ints from the read buffer.
- Returns
- Point3D
- Exceptions
-
| IOException | When there are not enough bytes to read a position. |
◆ readFloat()
| Phpcraft\Connection::readFloat |
( |
| ) |
|
Reads a float from the read buffer.
- Returns
- float
- Exceptions
-
| IOException | When there are not enough bytes to read a float. |
◆ readInt()
| Phpcraft\Connection::readInt |
( |
| ) |
|
Reads a signed integer from the read buffer.
- Returns
- GMP
- Exceptions
-
| IOException | When there are not enough bytes to read an integer. |
◆ readLong()
| Phpcraft\Connection::readLong |
( |
| ) |
|
Reads a signed long from the read buffer.
- Returns
- GMP
- Exceptions
-
| IOException | When there are not enough bytes to read a long. |
◆ readNBT()
| Phpcraft\Connection::readNBT |
( |
int |
$type = 0 | ) |
|
Reads NBT data.
- Parameters
-
| int | $type | Ignore this parameter. |
- Returns
- NBT
- Exceptions
-
◆ readPacket()
| Phpcraft\Connection::readPacket |
( |
float |
$timeout = 3.000 | ) |
|
Puts a new packet into the read buffer.
- Parameters
-
| float | $timeout | The amount of seconds to wait before read is aborted. |
- Returns
- int|boolean The packet's ID or false if no packet was received within the time limit.
- Exceptions
-
- See also
- Connection::readRawPacket
-
Packet::clientboundPacketIdToName()
-
Packet::serverboundPacketIdToName()
◆ readPosition()
| Phpcraft\Connection::readPosition |
( |
| ) |
|
Reads a position encoded as an unsigned long from the read buffer.
- Returns
- Point3D
- Exceptions
-
| IOException | When there are not enough bytes to read a position. |
◆ readPrecisePosition()
| Phpcraft\Connection::readPrecisePosition |
( |
| ) |
|
Reads a position encoded as three doubles from the read buffer.
- Returns
- Point3D
- Exceptions
-
| IOException | When there are not enough bytes to read a position. |
◆ readRaw()
| Phpcraft\Connection::readRaw |
( |
int |
$bytes | ) |
|
Read the specified amount of bytes from the read buffer.
- Parameters
-
- Returns
- string
- Exceptions
-
| IOException | When there are not enough bytes in the buffer to read the given number. |
◆ readRawPacket()
| Phpcraft\Connection::readRawPacket |
( |
float |
$timeout = 3.000, |
|
|
int |
$bytes = 0 |
|
) |
| |
Puts raw bytes from the stream into the read buffer.
- Parameters
-
| float | $timeout | The amount of seconds to wait before the read is aborted. |
| int | $bytes | The exact amount of bytes you would like to receive. 0 means read up to 8 KiB. |
- Returns
- boolean True on success.
- See also
- Connection::readPacket
◆ readShort()
| Phpcraft\Connection::readShort |
( |
| ) |
|
Reads a signed short from the read buffer.
- Returns
- int
- Exceptions
-
| IOException | When there are not enough bytes to read a short. |
◆ readSlot()
| Phpcraft\Connection::readSlot |
( |
bool |
$additional_processing = true | ) |
|
Reads a Slot.
- Parameters
-
| boolean | $additional_processing | Whether additional processing should occur to properly receive pre-1.13 data. You should only set this to false if you want a lazy read, and don't even care about the slot. |
- Returns
- Slot
- Exceptions
-
◆ readString()
| Phpcraft\Connection::readString |
( |
int |
$maxLength = 32767, |
|
|
int |
$minLength = -1 |
|
) |
| |
Reads a string from the read buffer.
- Parameters
-
| int | $maxLength | The maxmium amount of bytes this string may use. |
| int | $minLength | The minimum amount of bytes this string must use. |
- Returns
- string
- Exceptions
-
| LengthException | When the string doesn't fit the length requirements. |
| IOException | When there are not enough bytes to read a string. |
◆ readUnsignedByte()
| Phpcraft\Connection::readUnsignedByte |
( |
| ) |
|
Reads an unsigned byte from the read buffer.
- Returns
- int
- Exceptions
-
| IOException | When there are not enough bytes to read a byte. |
◆ readUnsignedShort()
| Phpcraft\Connection::readUnsignedShort |
( |
| ) |
|
Reads an unsigned short from the read buffer.
- Returns
- int
- Exceptions
-
| IOException | When there are not enough bytes to read a short. |
◆ readUUID()
| Phpcraft\Connection::readUUID |
( |
| ) |
|
Reads a UUID.
- Returns
- UUID
- Exceptions
-
| IOException | When there are not enough bytes to read a UUID. |
◆ readVarInt()
| Phpcraft\Connection::readVarInt |
( |
| ) |
|
Reads an integer encoded as a VarInt from the read buffer.
- Returns
- GMP
- Exceptions
-
| IOException | When the VarInt is too big or there are not enough bytes to read or continue reading a VarInt |
◆ send()
| Phpcraft\Connection::send |
( |
bool |
$raw = false | ) |
|
Sends the contents of the write buffer over the stream and clears the write buffer. Does nothing if the connection has no stream.
- Parameters
-
| boolean | $raw | When true, the write buffer is sent as-is, without length prefix or compression, which you probably don't want. |
- Exceptions
-
| IOException | If the connection is not open. |
- Returns
- Connection $this
◆ setReadBuffer()
| Phpcraft\Connection::setReadBuffer |
( |
string |
$buffer | ) |
|
Updates the read buffer correctly.
- Parameters
-
| string | $buffer | The new read buffer binary string. |
- Returns
- void
◆ startPacket()
| Phpcraft\Connection::startPacket |
( |
|
$packet | ) |
|
◆ varInt()
| static Phpcraft\Connection::varInt |
( |
|
$value | ) |
|
|
static |
Converts a number to a VarInt binary string.
- Parameters
-
- Returns
- string
◆ writeAngle()
| Phpcraft\Connection::writeAngle |
( |
float |
$value | ) |
|
Adds an angle to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeBoolean()
| Phpcraft\Connection::writeBoolean |
( |
bool |
$value | ) |
|
Adds a boolean to the write buffer.
- Parameters
-
- Returns
- Connection this
◆ writeByte()
| Phpcraft\Connection::writeByte |
( |
int |
$value | ) |
|
Adds a signed byte to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeChat()
| Phpcraft\Connection::writeChat |
( |
|
$value | ) |
|
Adds a chat object to the read buffer.
- Parameters
-
| array | string | $value | The chat object or a strings that will be converted into a chat object. |
- Returns
- Connection $this
◆ writeDouble()
| Phpcraft\Connection::writeDouble |
( |
float |
$value | ) |
|
Adds a double to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeFixedPointPosition()
| Phpcraft\Connection::writeFixedPointPosition |
( |
Point3D |
$pos | ) |
|
Adds a position encoded as three ints to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeFloat()
| Phpcraft\Connection::writeFloat |
( |
float |
$value | ) |
|
Adds a float to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeInt()
| Phpcraft\Connection::writeInt |
( |
|
$value | ) |
|
Adds a signed integer to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeLong()
| Phpcraft\Connection::writeLong |
( |
|
$value | ) |
|
Adds a signed long to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writePosition()
| Phpcraft\Connection::writePosition |
( |
Point3D |
$pos | ) |
|
Adds a position encoded as an unsigned long to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writePrecisePosition()
| Phpcraft\Connection::writePrecisePosition |
( |
Point3D |
$pos | ) |
|
Adds a position encoded as three double to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeRaw()
| Phpcraft\Connection::writeRaw |
( |
string |
$value | ) |
|
Adds the byte string to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeShort()
| Phpcraft\Connection::writeShort |
( |
|
$value | ) |
|
Adds a signed short to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeSlot()
| Phpcraft\Connection::writeSlot |
( |
Slot |
$slot | ) |
|
Adds a slot to the write buffer.
- Parameters
-
- Returns
- Connection $this
- Exceptions
-
◆ writeString()
| Phpcraft\Connection::writeString |
( |
string |
$value | ) |
|
Adds a string to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeUnsignedByte()
| Phpcraft\Connection::writeUnsignedByte |
( |
int |
$value | ) |
|
Adds an unsigned byte to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeUnsignedShort()
| Phpcraft\Connection::writeUnsignedShort |
( |
|
$value | ) |
|
Adds an unsigned short to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeUUID()
| Phpcraft\Connection::writeUUID |
( |
UUID |
$uuid | ) |
|
Adds a UUID to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ writeVarInt()
| Phpcraft\Connection::writeVarInt |
( |
|
$value | ) |
|
Adds a VarInt to the write buffer.
- Parameters
-
- Returns
- Connection $this
◆ $compression_threshold
| int Phpcraft\Connection::$compression_threshold = -1 |
The amount of bytes a packet needs for it to be compressed as an integer or -1 if disabled.
◆ $pow2
| Phpcraft\Connection::$pow2 = [] |
|
static |
◆ $protocol_version
| int Phpcraft\Connection::$protocol_version |
The protocol version that is used for this connection.
◆ $read_buffer
| string Phpcraft\Connection::$read_buffer = "" |
◆ $read_buffer_offset
| Phpcraft\Connection::$read_buffer_offset = 0 |
◆ $state
| int Phpcraft\Connection::$state |
The state of the connection. 1 stands for status, 2 for logging in, and 3 for playing.
◆ $stream
| resource null Phpcraft\Connection::$stream |
The stream of the connection or null.
◆ $write_buffer
| string Phpcraft\Connection::$write_buffer = "" |
The write buffer binary string.
◆ $zero
| Phpcraft\Connection::$zero |
|
static |
◆ STATE_HANDSHAKE
| const Phpcraft\Connection::STATE_HANDSHAKE = 0 |
◆ STATE_LOGIN
| const Phpcraft\Connection::STATE_LOGIN = 2 |
◆ STATE_PLAY
| const Phpcraft\Connection::STATE_PLAY = 3 |
◆ STATE_STATUS
| const Phpcraft\Connection::STATE_STATUS = 1 |