Phpcraft
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Phpcraft\Connection Class Reference
Inheritance diagram for Phpcraft\Connection:
Inheritance graph
[legend]

Public Member Functions

 __construct (int $protocol_version=-1, $stream=null)
 
 isOpen ()
 
 setReadBuffer (string $buffer)
 
 hasRemainingData ()
 
 getRemainingData ()
 
 writeChat (ChatComponent $value)
 
 writeString (string $value)
 
 writeRaw (string $value)
 
 writeFloat (float $value)
 
 writePosition (Point3D $pos)
 
 writeGMP ($value, int $bytes, int $bits, bool $signed, int $gmp_export_options=GMP_MSW_FIRST|GMP_BIG_ENDIAN)
 
 writeLong ($value)
 
 writePrecisePosition (Point3D $pos)
 
 writeDouble (float $value)
 
 writeFixedPointPosition (Point3D $pos)
 
 writeInt ($value)
 
 writeSlot (Slot $slot)
 
 writeBoolean (bool $value)
 
 writeShort ($value)
 
 writeVarInt ($value)
 
 writeByte (int $value)
 
 writeUnsignedShort ($value)
 
 writeUnsignedByte (int $value)
 
 writeAngle (float $value)
 
 writeUUID (UUID $uuid)
 
 startPacket ($packet)
 
 send (bool $raw=false)
 
 close ()
 
 readRawPacket (float $timeout=3.000, int $bytes=0)
 
 getRemoteAddress ()
 
 readPacket (float $timeout=3.000)
 
 readVarInt ()
 
 readUnsignedByte ()
 
 readAngle ()
 
 readByte ()
 
 readChat ()
 
 readString (int $maxLength=32767, int $minLength=-1)
 
 readPosition ()
 
 readGMP (int $bytes, int $bits, bool $signed, int $gmp_import_options=GMP_MSW_FIRST|GMP_BIG_ENDIAN)
 
 readPrecisePosition ()
 
 readDouble ()
 
 readFixedPointPosition ()
 
 readInt ()
 
 readUUID ()
 
 readSlot (bool $additional_processing=true)
 
 readBoolean ()
 
 readShort ()
 
 readNBT (int $type=0)
 
 readRaw (int $bytes)
 
 readLong ()
 
 readFloat ()
 
 readUnsignedShort ()
 
 ignoreBytes (int $bytes)
 

Static Public Member Functions

static varInt ($value)
 

Public Attributes

const STATE_HANDSHAKE = 0
 
const STATE_STATUS = 1
 
const STATE_LOGIN = 2
 
const STATE_PLAY = 3
 
const LENIENCY_LENIENT = 1
 
const LENIENCY_VANILLA = 0
 
const LENIENCY_STRICT = -1
 
 $protocol_version
 
 $stream
 
 $compression_threshold = -1
 
 $state
 
 $write_buffer = ""
 
 $lenient = false
 
 $leniency = self::LENIENCY_VANILLA
 
 $read_buffer = ""
 
 $read_buffer_offset = 0
 

Static Public Attributes

static $zero
 
static $pow2 = []
 

Detailed Description

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";

Constructor & Destructor Documentation

◆ __construct()

Phpcraft\Connection::__construct ( int  $protocol_version = -1,
  $stream = null 
)
Parameters
int$protocol_version-1 will use the latest supported protocol version
resource | null$stream

Member Function Documentation

◆ 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

◆ hasRemainingData()

Phpcraft\Connection::hasRemainingData ( )
Since
0.5.22
Returns
bool

◆ ignoreBytes()

Phpcraft\Connection::ignoreBytes ( int  $bytes)

Skips over the given amount of bytes in the read buffer.

Parameters
int$bytes
Returns
Connection $this
Exceptions
IOExceptionWhen 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
IOExceptionWhen there are not enough bytes to read an angle.

◆ readBoolean()

Phpcraft\Connection::readBoolean ( )

Reads a boolean from the read buffer.

Returns
boolean
Exceptions
IOExceptionWhen there are not enough bytes to read a boolean.

◆ readByte()

Phpcraft\Connection::readByte ( )

Reads a signed byte from the read buffer.

Returns
int
Exceptions
IOExceptionWhen there are not enough bytes to read a byte.

◆ readChat()

Phpcraft\Connection::readChat ( )

Reads a chat component from the read buffer.

Returns
ChatComponent
Exceptions
IOExceptionWhen there are not enough bytes to read the string.

◆ readDouble()

Phpcraft\Connection::readDouble ( )

Reads a double from the read buffer.

Returns
float
Exceptions
IOExceptionWhen 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
IOExceptionWhen there are not enough bytes to read a position.

◆ readFloat()

Phpcraft\Connection::readFloat ( )

Reads a float from the read buffer.

Returns
float
Exceptions
IOExceptionWhen there are not enough bytes to read a float.

◆ readGMP()

Phpcraft\Connection::readGMP ( int  $bytes,
int  $bits,
bool  $signed,
int  $gmp_import_options = GMP_MSW_FIRST | GMP_BIG_ENDIAN 
)
Parameters
int$bytes
int$bits
bool$signed
int$gmp_import_options
Returns
GMP
Exceptions
IOException
Since
0.5.5

◆ readInt()

Phpcraft\Connection::readInt ( )

Reads a signed integer from the read buffer.

Returns
GMP
Exceptions
IOExceptionWhen there are not enough bytes to read an integer.

◆ readLong()

Phpcraft\Connection::readLong ( )

Reads a signed long from the read buffer.

Returns
GMP
Exceptions
IOExceptionWhen there are not enough bytes to read a long.

◆ readNBT()

Phpcraft\Connection::readNBT ( int  $type = 0)

Reads NBT data.

Parameters
int$typeIgnore this parameter.
Returns
NBT
Exceptions
IOException

◆ readPacket()

Phpcraft\Connection::readPacket ( float  $timeout = 3.000)

Puts a new packet into the read buffer.

Parameters
float$timeoutThe 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
IOException
See also
Connection::readRawPacket
ClientboundPacketId::getById
ServerboundPacketId::getById

◆ readPosition()

Phpcraft\Connection::readPosition ( )

Reads a position encoded as an unsigned long from the read buffer.

Returns
Point3D
Exceptions
IOExceptionWhen 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
IOExceptionWhen 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
int$bytes
Returns
string
Exceptions
IOExceptionWhen 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$timeoutThe amount of seconds to wait before the read is aborted.
int$bytesThe 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
IOExceptionWhen there are not enough bytes to read a short.

◆ readSlot()

Phpcraft\Connection::readSlot ( bool  $additional_processing = true)

Reads a Slot.

Parameters
boolean$additional_processingWhether 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
IOException

◆ readString()

Phpcraft\Connection::readString ( int  $maxLength = 32767,
int  $minLength = -1 
)

Reads a string from the read buffer.

Parameters
int$maxLengthThe maximum amount of bytes this string may use.
int$minLengthThe minimum amount of bytes this string must use.
Returns
string
Exceptions
LengthExceptionWhen the string doesn't fit the length requirements.
IOExceptionWhen there are not enough bytes to read a string.

◆ readUnsignedByte()

Phpcraft\Connection::readUnsignedByte ( )

Reads an unsigned byte from the read buffer.

Returns
int
Exceptions
IOExceptionWhen there are not enough bytes to read a byte.

◆ readUnsignedShort()

Phpcraft\Connection::readUnsignedShort ( )

Reads an unsigned short from the read buffer.

Returns
int
Exceptions
IOExceptionWhen there are not enough bytes to read a short.

◆ readUUID()

Phpcraft\Connection::readUUID ( )

Reads a UUID.

Returns
UUID
Exceptions
IOExceptionWhen 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
IOExceptionWhen 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$rawWhen true, the write buffer is sent as-is, without length prefix or compression, which you probably don't want.
Exceptions
NoConnectionExceptionif the stream is closed
IOExceptionif the packet is too big (LENIENCY_VANILLA)
Returns
Connection $this

◆ setReadBuffer()

Phpcraft\Connection::setReadBuffer ( string  $buffer)

Updates the read buffer correctly.

Parameters
string$bufferThe new read buffer binary string.
Returns
void

◆ startPacket()

Phpcraft\Connection::startPacket (   $packet)

Clears the write buffer and starts a new packet.

Parameters
string | integer | PacketId$packetThe name or ID of the new packet.
Returns
Connection $this

Reimplemented in Phpcraft\ServerConnection, and Phpcraft\ClientConnection.

◆ varInt()

static Phpcraft\Connection::varInt (   $value)
static

Converts a number to a VarInt binary string.

Parameters
GMP | int | string$value
Returns
string

◆ writeAngle()

Phpcraft\Connection::writeAngle ( float  $value)

Adds an angle to the write buffer.

Parameters
float$value
Returns
Connection $this

◆ writeBoolean()

Phpcraft\Connection::writeBoolean ( bool  $value)

Adds a boolean to the write buffer.

Parameters
boolean$value
Returns
Connection this

◆ writeByte()

Phpcraft\Connection::writeByte ( int  $value)

Adds a signed byte to the write buffer.

Parameters
int$value
Returns
Connection $this

◆ writeChat()

Phpcraft\Connection::writeChat ( ChatComponent  $value)

Adds a chat component to the write buffer.

Parameters
ChatComponent$value
Returns
Connection $this

◆ writeDouble()

Phpcraft\Connection::writeDouble ( float  $value)

Adds a double to the write buffer.

Parameters
double$value
Returns
Connection $this

◆ writeFixedPointPosition()

Phpcraft\Connection::writeFixedPointPosition ( Point3D  $pos)

Adds a position encoded as three ints to the write buffer.

Parameters
Point3D$pos
Returns
Connection $this

◆ writeFloat()

Phpcraft\Connection::writeFloat ( float  $value)

Adds a float to the write buffer.

Parameters
float$value
Returns
Connection $this

◆ writeGMP()

Phpcraft\Connection::writeGMP (   $value,
int  $bytes,
int  $bits,
bool  $signed,
int  $gmp_export_options = GMP_MSW_FIRST | GMP_BIG_ENDIAN 
)

◆ writeInt()

Phpcraft\Connection::writeInt (   $value)

Adds a signed integer to the write buffer.

Parameters
GMP | int | string$value
Returns
Connection $this

◆ writeLong()

Phpcraft\Connection::writeLong (   $value)

Adds a signed long to the write buffer.

Parameters
GMP | int | string$value
Returns
Connection $this

◆ writePosition()

Phpcraft\Connection::writePosition ( Point3D  $pos)

Adds a position encoded as an unsigned long to the write buffer.

Parameters
Point3D$pos
Returns
Connection $this

◆ writePrecisePosition()

Phpcraft\Connection::writePrecisePosition ( Point3D  $pos)

Adds a position encoded as three double to the write buffer.

Parameters
Point3D$pos
Returns
Connection $this

◆ writeRaw()

Phpcraft\Connection::writeRaw ( string  $value)

Adds the byte string to the write buffer.

Parameters
string$value
Returns
Connection $this

◆ writeShort()

Phpcraft\Connection::writeShort (   $value)

Adds a signed short to the write buffer.

Parameters
GMP | int | string$value
Returns
Connection $this

◆ writeSlot()

Phpcraft\Connection::writeSlot ( Slot  $slot)

Adds a slot to the write buffer.

Parameters
Slot$slot
Returns
Connection $this
Exceptions
MissingMetadataException

◆ writeString()

Phpcraft\Connection::writeString ( string  $value)

Adds a string to the write buffer.

Parameters
string$value
Returns
Connection $this

◆ writeUnsignedByte()

Phpcraft\Connection::writeUnsignedByte ( int  $value)

Adds an unsigned byte to the write buffer.

Parameters
int$value
Returns
Connection $this

◆ writeUnsignedShort()

Phpcraft\Connection::writeUnsignedShort (   $value)

Adds an unsigned short to the write buffer.

Parameters
GMP | int | string$value
Returns
Connection $this

◆ writeUUID()

Phpcraft\Connection::writeUUID ( UUID  $uuid)

Adds a UUID to the write buffer.

Parameters
UUID$uuid
Returns
Connection $this

◆ writeVarInt()

Phpcraft\Connection::writeVarInt (   $value)

Adds a VarInt to the write buffer.

Parameters
GMP | int | string$value
Returns
Connection $this

Member Data Documentation

◆ $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.

◆ $leniency

int Phpcraft\Connection::$leniency = self::LENIENCY_VANILLA

Determines the leniency that will be used when sending and receiving packets.

Since
0.5.4

◆ $lenient

bool Phpcraft\Connection::$lenient = false

If true, $leniency will be set to LENIENCY_LENIENT.

Since
0.5
Deprecated:
Set $leniency to LENIENCY_LENIENT, instead.

◆ $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 = ""

The read buffer binary string.

See also
Connection::setReadBuffer()

◆ $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

◆ LENIENCY_LENIENT

const Phpcraft\Connection::LENIENCY_LENIENT = 1

Allow packets that vanilla would reject.

◆ LENIENCY_STRICT

const Phpcraft\Connection::LENIENCY_STRICT = -1

Don't allow malformatted packets, even if vanilla would accept them. Only recommended for people who want to perfect their client and server code.

◆ LENIENCY_VANILLA

const Phpcraft\Connection::LENIENCY_VANILLA = 0

Reject packets that vanilla would reject.

◆ 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