Class: SFML::Network::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/network/packet.rb

Overview

Binary serializer with explicit, typed read/write — wire-compatible with SFML's sf::Packet. Used by Tcp/UdpSocket's send_packet /receive_packet (the network layer prepends a 4-byte length header for TCP framing, so the receiver always gets a whole packet).

All read calls consume bytes in FIFO order. After a write you can always re-read by sending the packet over the wire; the local read position only advances on the receive side. To rewind a locally-built packet, just construct a new one.

pkt = SFML::Network::Packet.new
pkt.write_int32(42)
pkt.write_string("hello")
pkt.write_float(0.5)

# … send over a TcpSocket, receive on the other end …

incoming.read_int32     #=> 42
incoming.read_string    #=> "hello"
incoming.read_float     #=> 0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Packet

Returns a new instance of Packet.

Raises:



24
25
26
27
28
# File 'lib/sfml/network/packet.rb', line 24

def initialize
  ptr = C::Network.sfPacket_create
  raise NetworkError, "sfPacket_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Network.method(:sfPacket_destroy))
end

Instance Attribute Details

#handle ⇒ Object (readonly)

:nodoc:



150
151
152
# File 'lib/sfml/network/packet.rb', line 150

def handle
  @handle
end

Instance Method Details

#append(bytes) ⇒ Object

Append a raw byte buffer (no length prefix, no type tag) — for interop with hand-crafted binary protocols. Most callers want write_string / write_int32 etc. instead.



39
40
41
42
43
44
45
# File 'lib/sfml/network/packet.rb', line 39

def append(bytes)
  s = bytes.to_s
  buf = FFI::MemoryPointer.new(:uint8, s.bytesize)
  buf.write_bytes(s)
  C::Network.sfPacket_append(@handle, buf, s.bytesize)
  self
end

#clear ⇒ Object

Returns the clear.



31
32
33
34
# File 'lib/sfml/network/packet.rb', line 31

def clear
  C::Network.sfPacket_clear(@handle)
  self
end

#data ⇒ Object

Returns the full packet as a binary String. Includes any length prefixes / type encoding sfPacket added on write.



49
50
51
52
53
54
# File 'lib/sfml/network/packet.rb', line 49

def data
  size = C::Network.sfPacket_getDataSize(@handle)
  return "".b if size.zero?
  ptr = C::Network.sfPacket_getData(@handle)
  ptr.read_bytes(size).force_encoding(Encoding::ASCII_8BIT)
end

#dup ⇒ Object Also known as: clone

Returns the dup.

Raises:



139
140
141
142
143
144
145
146
147
# File 'lib/sfml/network/packet.rb', line 139

def dup
  ptr = C::Network.sfPacket_copy(@handle)
  raise NetworkError, "sfPacket_copy returned NULL" if ptr.null?

  copy = self.class.allocate
  copy.instance_variable_set(:@handle,
    FFI::AutoPointer.new(ptr, C::Network.method(:sfPacket_destroy)))
  copy
end

#end_of_packet? ⇒ Boolean

true if end of packet.

Returns:

  • (Boolean)


61
# File 'lib/sfml/network/packet.rb', line 61

def end_of_packet?  = C::Network.sfPacket_endOfPacket(@handle)

#ok? ⇒ Boolean

false if the last read overran the packet — sfPacket "fails" quietly rather than raising, and subsequent reads return zero. Check this after a read sequence to validate the frame.

Returns:

  • (Boolean)


66
# File 'lib/sfml/network/packet.rb', line 66

def ok? = C::Network.sfPacket_canRead(@handle)

#read_bool ⇒ Object

---- typed readers ----



104
105
# File 'lib/sfml/network/packet.rb', line 104

def read_bool   = C::Network.sfPacket_readBool(@handle)
# Returns the read int8.

#read_double ⇒ Object

Returns the read double.



124
# File 'lib/sfml/network/packet.rb', line 124

def read_double = C::Network.sfPacket_readDouble(@handle)

#read_float ⇒ Object

Returns the read float.



122
123
# File 'lib/sfml/network/packet.rb', line 122

def read_float  = C::Network.sfPacket_readFloat(@handle)
# Returns the read double.

#read_int16 ⇒ Object

Returns the read int16.



110
111
# File 'lib/sfml/network/packet.rb', line 110

def read_int16  = C::Network.sfPacket_readInt16(@handle)
# Returns the read uint16.

#read_int32 ⇒ Object

Returns the read int32.



114
115
# File 'lib/sfml/network/packet.rb', line 114

def read_int32  = C::Network.sfPacket_readInt32(@handle)
# Returns the read uint32.

#read_int64 ⇒ Object

Returns the read int64.



118
119
# File 'lib/sfml/network/packet.rb', line 118

def read_int64  = C::Network.sfPacket_readInt64(@handle)
# Returns the read uint64.

#read_int8 ⇒ Object

Returns the read int8.



106
107
# File 'lib/sfml/network/packet.rb', line 106

def read_int8   = C::Network.sfPacket_readInt8(@handle)
# Returns the read uint8.

#read_position ⇒ Object

Returns the read position.



59
60
# File 'lib/sfml/network/packet.rb', line 59

def read_position   = C::Network.sfPacket_getReadPosition(@handle)
# `true` if end of packet.

#read_string ⇒ Object

Read a length-prefixed string. CSFML expects a caller-allocated C buffer of "string length + 1 NUL" bytes — we size it from the remaining packet bytes, which is always an upper bound.



129
130
131
132
133
134
135
136
# File 'lib/sfml/network/packet.rb', line 129

def read_string
  remaining = size - read_position
  return "" if remaining <= 0

  buf = FFI::MemoryPointer.new(:char, remaining + 1)
  C::Network.sfPacket_readString(@handle, buf)
  buf.read_string.force_encoding(Encoding::UTF_8)
end

#read_uint16 ⇒ Object

Returns the read uint16.



112
113
# File 'lib/sfml/network/packet.rb', line 112

def read_uint16 = C::Network.sfPacket_readUint16(@handle)
# Returns the read int32.

#read_uint32 ⇒ Object

Returns the read uint32.



116
117
# File 'lib/sfml/network/packet.rb', line 116

def read_uint32 = C::Network.sfPacket_readUint32(@handle)
# Returns the read int64.

#read_uint64 ⇒ Object

Returns the read uint64.



120
121
# File 'lib/sfml/network/packet.rb', line 120

def read_uint64 = C::Network.sfPacket_readUint64(@handle)
# Returns the read float.

#read_uint8 ⇒ Object

Returns the read uint8.



108
109
# File 'lib/sfml/network/packet.rb', line 108

def read_uint8  = C::Network.sfPacket_readUint8(@handle)
# Returns the read int16.

#size ⇒ Object

Returns the size.



57
58
# File 'lib/sfml/network/packet.rb', line 57

def size            = C::Network.sfPacket_getDataSize(@handle)
# Returns the read position.

#write_bool(v) ⇒ Object

Append a Bool.



73
74
75
# File 'lib/sfml/network/packet.rb', line 73

def write_bool(v)
  C::Network.sfPacket_writeBool(@handle, v ? true : false); self
end

#write_double(v) ⇒ Object

Append a double-precision Float.



95
# File 'lib/sfml/network/packet.rb', line 95

def write_double(v) = (C::Network.sfPacket_writeDouble(@handle, Float(v));   self)

#write_float(v) ⇒ Object

Append a single-precision Float.



93
94
# File 'lib/sfml/network/packet.rb', line 93

def write_float(v)  = (C::Network.sfPacket_writeFloat(@handle,  Float(v));   self)
# Append a double-precision Float.

#write_int16(v) ⇒ Object

Append a signed 16-bit Integer.



81
82
# File 'lib/sfml/network/packet.rb', line 81

def write_int16(v)  = (C::Network.sfPacket_writeInt16(@handle,  Integer(v)); self)
# Append an unsigned 16-bit Integer.

#write_int32(v) ⇒ Object

Append a signed 32-bit Integer.



85
86
# File 'lib/sfml/network/packet.rb', line 85

def write_int32(v)  = (C::Network.sfPacket_writeInt32(@handle,  Integer(v)); self)
# Append an unsigned 32-bit Integer.

#write_int64(v) ⇒ Object

Append a signed 64-bit Integer.



89
90
# File 'lib/sfml/network/packet.rb', line 89

def write_int64(v)  = (C::Network.sfPacket_writeInt64(@handle,  Integer(v)); self)
# Append an unsigned 64-bit Integer.

#write_int8(v) ⇒ Object

Append a signed 8-bit Integer.



77
78
# File 'lib/sfml/network/packet.rb', line 77

def write_int8(v)   = (C::Network.sfPacket_writeInt8(@handle,   Integer(v)); self)
# Append an unsigned 8-bit Integer.

#write_string(str) ⇒ Object

Append a length-prefixed UTF-8 String.



98
99
100
101
# File 'lib/sfml/network/packet.rb', line 98

def write_string(str)
  C::Network.sfPacket_writeString(@handle, str.to_s)
  self
end

#write_uint16(v) ⇒ Object

Append an unsigned 16-bit Integer.



83
84
# File 'lib/sfml/network/packet.rb', line 83

def write_uint16(v) = (C::Network.sfPacket_writeUint16(@handle, Integer(v)); self)
# Append a signed 32-bit Integer.

#write_uint32(v) ⇒ Object

Append an unsigned 32-bit Integer.



87
88
# File 'lib/sfml/network/packet.rb', line 87

def write_uint32(v) = (C::Network.sfPacket_writeUint32(@handle, Integer(v)); self)
# Append a signed 64-bit Integer.

#write_uint64(v) ⇒ Object

Append an unsigned 64-bit Integer.



91
92
# File 'lib/sfml/network/packet.rb', line 91

def write_uint64(v) = (C::Network.sfPacket_writeUint64(@handle, Integer(v)); self)
# Append a single-precision Float.

#write_uint8(v) ⇒ Object

Append an unsigned 8-bit Integer.



79
80
# File 'lib/sfml/network/packet.rb', line 79

def write_uint8(v)  = (C::Network.sfPacket_writeUint8(@handle,  Integer(v)); self)
# Append a signed 16-bit Integer.