sfml3.rb

Class: SFML::Packet

Inherits:
Object
  • Object
show all
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

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.

Returns:

  • (self)

Returns:

  • (self)


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.

Returns:

  • (Boolean)

Returns:

  • (Boolean) —

    whether the packet is in a valid reading state, i.e. whether the last read operation succeeded



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.

Returns:

  • (self)

Returns:

  • (self)


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.

Returns:

Returns:

  • (Packet) —

    an independent copy with the same data and read position



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.

Returns:

  • (String)

Returns:

  • (String) —

    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.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    the number of bytes in the packet's data



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.

Returns:

  • (Boolean)

Returns:

  • (Boolean) —

    whether the end of the packet has been reached, i.e. whether all the 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.

Returns:

  • (Boolean)

Returns:

  • (Boolean)

#read_double ⇒ Float

Reads a double-precision float from the packet.

Returns:

  • (Float)

Returns:

  • (Float) —

    a double-precision float

#read_float ⇒ Float

Reads a single-precision float from the packet.

Returns:

  • (Float)

Returns:

  • (Float) —

    a single-precision float

#read_int16 ⇒ Integer

Reads a signed 16-bit integer from the packet.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    a signed 16-bit integer

#read_int32 ⇒ Integer

Reads a signed 32-bit integer from the packet.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    a signed 32-bit integer

#read_int64 ⇒ Integer

Reads a signed 64-bit integer from the packet.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    a signed 64-bit integer

#read_int8 ⇒ Integer

Reads a signed 8-bit integer from the packet.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    a signed 8-bit integer

#read_position ⇒ Integer

Returns the current read position, in bytes.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    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.

Returns:

  • (String)

Returns:

  • (String)

#read_uint16 ⇒ Integer

Reads an unsigned 16-bit integer from the packet.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    an unsigned 16-bit integer

#read_uint32 ⇒ Integer

Reads an unsigned 32-bit integer from the packet.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    an unsigned 32-bit integer

#read_uint64 ⇒ Integer

Reads an unsigned 64-bit integer from the packet.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    an unsigned 64-bit integer

#read_uint8 ⇒ Integer

Reads an unsigned 8-bit integer from the packet.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    an unsigned 8-bit integer

#write_bool(value) ⇒ Boolean

Writes value as a boolean.

Returns:

  • (Boolean) —

    value

#write_double(value) ⇒ Float

Writes value as a double-precision float.

Returns:

  • (Float) —

    value

#write_float(value) ⇒ Float

Writes value as a single-precision float.

Returns:

  • (Float) —

    value

#write_int16(value) ⇒ Integer

Writes value as a signed 16-bit integer.

Returns:

  • (Integer) —

    value

#write_int32(value) ⇒ Integer

Writes value as a signed 32-bit integer.

Returns:

  • (Integer) —

    value

#write_int64(value) ⇒ Integer

Writes value as a signed 64-bit integer.

Returns:

  • (Integer) —

    value

#write_int8(value) ⇒ Integer

Writes value as a signed 8-bit integer.

Returns:

  • (Integer) —

    value

#write_string(value) ⇒ String

Writes a NUL-terminated string, readable back with #read_string.

Returns:

  • (String) —

    value



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.

Returns:

  • (Integer) —

    value

#write_uint32(value) ⇒ Integer

Writes value as an unsigned 32-bit integer.

Returns:

  • (Integer) —

    value

#write_uint64(value) ⇒ Integer

Writes value as an unsigned 64-bit integer.

Returns:

  • (Integer) —

    value

#write_uint8(value) ⇒ Integer

Writes value as an unsigned 8-bit integer.

Returns:

  • (Integer) —

    value