Class: SFML::Packet
- Inherits:
-
Object
- Object
- SFML::Packet
- Defined in:
- ext/network/packet.c,
ext/network/packet.c
Overview
A structured byte buffer for use with TcpSocket/UdpSocket, with typed sequential read/write access. Reads and writes must happen in the same order the data was written, since the packet keeps no type tags on the wire.
Instance Method Summary collapse
-
#append(data) ⇒ self
Appends raw bytes to the end of the packet, growing it as needed.
-
#can_read? ⇒ Boolean
Returns
trueif the packet is still in a valid reading state. -
#clear ⇒ self
Empties the packet and resets its read position to zero.
-
#copy ⇒ Packet
Returns an independent copy of the packet.
-
#data ⇒ String
Returns the packet's raw byte content.
-
#data_size ⇒ Integer
Returns the number of bytes held by the packet.
-
#end_of_packet? ⇒ Boolean
Returns
trueonce all of the packet's data has been read. -
#new ⇒ Packet
constructor
Creates a new, empty packet.
-
#read_bool ⇒ Boolean
Reads a boolean from the packet.
-
#read_double ⇒ Float
Reads a double-precision float from the packet.
-
#read_float ⇒ Float
Reads a single-precision float from the packet.
-
#read_int16 ⇒ Integer
Reads a signed 16-bit integer from the packet.
-
#read_int32 ⇒ Integer
Reads a signed 32-bit integer from the packet.
-
#read_int64 ⇒ Integer
Reads a signed 64-bit integer from the packet.
-
#read_int8 ⇒ Integer
Reads a signed 8-bit integer from the packet.
-
#read_position ⇒ Integer
Returns the current read position, in bytes.
-
#read_string ⇒ String
Reads a NUL-terminated string previously written with #write_string.
-
#read_uint16 ⇒ Integer
Reads an unsigned 16-bit integer from the packet.
-
#read_uint32 ⇒ Integer
Reads an unsigned 32-bit integer from the packet.
-
#read_uint64 ⇒ Integer
Reads an unsigned 64-bit integer from the packet.
-
#read_uint8 ⇒ Integer
Reads an unsigned 8-bit integer from the packet.
-
#write_bool(value) ⇒ Boolean
Writes
valueas a boolean. -
#write_double(value) ⇒ Float
Writes
valueas a double-precision float. -
#write_float(value) ⇒ Float
Writes
valueas a single-precision float. -
#write_int16(value) ⇒ Integer
Writes
valueas a signed 16-bit integer. -
#write_int32(value) ⇒ Integer
Writes
valueas a signed 32-bit integer. -
#write_int64(value) ⇒ Integer
Writes
valueas a signed 64-bit integer. -
#write_int8(value) ⇒ Integer
Writes
valueas a signed 8-bit integer. -
#write_string(value) ⇒ String
Writes a NUL-terminated string, readable back with #read_string.
-
#write_uint16(value) ⇒ Integer
Writes
valueas an unsigned 16-bit integer. -
#write_uint32(value) ⇒ Integer
Writes
valueas an unsigned 32-bit integer. -
#write_uint64(value) ⇒ Integer
Writes
valueas an unsigned 64-bit integer. -
#write_uint8(value) ⇒ Integer
Writes
valueas an unsigned 8-bit integer.
Constructor Details
#new ⇒ Packet
Creates a new, empty packet.
52 53 54 |
# File 'ext/network/packet.c', line 52 static VALUE Packet_initialize(VALUE self) { return self; } |
Instance Method Details
#append(data) ⇒ self
Appends raw bytes to the end of the packet, growing it as needed.
data is any String-convertible value.
74 75 76 77 78 79 80 |
# File 'ext/network/packet.c', line 74
static VALUE Packet_append(VALUE self, VALUE rb_data) {
StringValue(rb_data);
sfPacket_append(Get_Packet_Struct(self), RSTRING_PTR(rb_data), (size_t)RSTRING_LEN(rb_data));
return self;
}
|
#can_read? ⇒ Boolean
Returns true if the packet is still in a valid reading state.
143 144 145 |
# File 'ext/network/packet.c', line 143 static VALUE Packet_can_read(VALUE self) { return BOOL2RB(sfPacket_canRead(Get_Packet_Struct(self))); } |
#clear ⇒ self
Empties the packet and resets its read position to zero.
88 89 90 91 |
# File 'ext/network/packet.c', line 88 static VALUE Packet_clear(VALUE self) { sfPacket_clear(Get_Packet_Struct(self)); return self; } |
#copy ⇒ Packet
Returns an independent copy of the packet.
62 63 64 |
# File 'ext/network/packet.c', line 62 static VALUE Packet_copy(VALUE self) { return Packet_wrap(Get_Klass_Packet(), sfPacket_copy(Get_Packet_Struct(self))); } |
#data ⇒ String
Returns the packet's raw byte content.
99 100 101 102 103 |
# File 'ext/network/packet.c', line 99
static VALUE Packet_data(VALUE self) {
const void* data = sfPacket_getData(Get_Packet_Struct(self));
return rb_str_new((const char*)data, (long)sfPacket_getDataSize(Get_Packet_Struct(self)));
}
|
#data_size ⇒ Integer
Returns the number of bytes held by the packet.
111 112 113 |
# File 'ext/network/packet.c', line 111 static VALUE Packet_data_size(VALUE self) { return SIZET2NUM(sfPacket_getDataSize(Get_Packet_Struct(self))); } |
#end_of_packet? ⇒ Boolean
Returns true once all of the packet's data has been read.
132 133 134 |
# File 'ext/network/packet.c', line 132 static VALUE Packet_end_of_packet(VALUE self) { return BOOL2RB(sfPacket_endOfPacket(Get_Packet_Struct(self))); } |
#read_bool ⇒ Boolean
Reads a boolean from the packet.
#read_double ⇒ Float
Reads a double-precision float from the packet.
#read_float ⇒ Float
Reads a single-precision float from the packet.
#read_int16 ⇒ Integer
Reads a signed 16-bit integer from the packet.
#read_int32 ⇒ Integer
Reads a signed 32-bit integer from the packet.
#read_int64 ⇒ Integer
Reads a signed 64-bit integer from the packet.
#read_int8 ⇒ Integer
Reads a signed 8-bit integer from the packet.
#read_position ⇒ Integer
Returns the current read position, in bytes.
121 122 123 |
# File 'ext/network/packet.c', line 121 static VALUE Packet_read_position(VALUE self) { return SIZET2NUM(sfPacket_getReadPosition(Get_Packet_Struct(self))); } |
#read_string ⇒ String
Reads a NUL-terminated string previously written with #write_string.
#read_uint16 ⇒ Integer
Reads an unsigned 16-bit integer from the packet.
#read_uint32 ⇒ Integer
Reads an unsigned 32-bit integer from the packet.
#read_uint64 ⇒ Integer
Reads an unsigned 64-bit integer from the packet.
#read_uint8 ⇒ Integer
Reads an unsigned 8-bit integer from the packet.
#write_bool(value) ⇒ Boolean
Writes value as a boolean.
#write_double(value) ⇒ Float
Writes value as a double-precision float.
#write_float(value) ⇒ Float
Writes value as a single-precision float.
#write_int16(value) ⇒ Integer
Writes value as a signed 16-bit integer.
#write_int32(value) ⇒ Integer
Writes value as a signed 32-bit integer.
#write_int64(value) ⇒ Integer
Writes value as a signed 64-bit integer.
#write_int8(value) ⇒ Integer
Writes value as a signed 8-bit integer.
#write_string(value) ⇒ String
Writes a NUL-terminated string, readable back with #read_string.
224 225 226 227 |
# File 'ext/network/packet.c', line 224
static VALUE Packet_write_string(VALUE self, VALUE rb_value) {
sfPacket_writeString(Get_Packet_Struct(self), StringValueCStr(rb_value));
return rb_value;
}
|
#write_uint16(value) ⇒ Integer
Writes value as an unsigned 16-bit integer.
#write_uint32(value) ⇒ Integer
Writes value as an unsigned 32-bit integer.
#write_uint64(value) ⇒ Integer
Writes value as an unsigned 64-bit integer.
#write_uint8(value) ⇒ Integer
Writes value as an unsigned 8-bit integer.