Class: SFML::Network::UdpSocket

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

Overview

Connectionless UDP datagram socket. Bind a port to receive, send to (address, port). Stateless — every send/receive specifies the peer.

sock = SFML::Network::UdpSocket.new
sock.bind(port: 9999)
sock.send("hello", to: SFML::Network::IpAddress::LOCALHOST, port: 9000)
status, bytes, addr, port = sock.receive(max: 1024)

Constant Summary collapse

MAX_DATAGRAM_SIZE =
C::Network.sfUdpSocket_maxDatagramSize

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUdpSocket

Returns a new instance of UdpSocket.

Raises:



14
15
16
17
18
# File 'lib/sfml/network/udp_socket.rb', line 14

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

Instance Attribute Details

#handleObject (readonly)

:nodoc:



68
69
70
# File 'lib/sfml/network/udp_socket.rb', line 68

def handle
  @handle
end

Instance Method Details

#bind(port:, address: IpAddress::ANY) ⇒ Object



20
21
22
23
24
# File 'lib/sfml/network/udp_socket.rb', line 20

def bind(port:, address: IpAddress::ANY)
  addr = address.is_a?(IpAddress) ? address : IpAddress.from_string(address)
  code = C::Network.sfUdpSocket_bind(@handle, Integer(port), addr.struct)
  C::Network::STATUSES[code]
end

#blocking=(value) ⇒ Object



62
63
64
# File 'lib/sfml/network/udp_socket.rb', line 62

def blocking=(value)
  C::Network.sfUdpSocket_setBlocking(@handle, value ? true : false)
end

#blocking?Boolean

Returns:

  • (Boolean)


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

def blocking? = C::Network.sfUdpSocket_isBlocking(@handle)

#local_portObject



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

def local_port = C::Network.sfUdpSocket_getLocalPort(@handle)

#receive(max: 1024) ⇒ Object

Returns [status, data, sender_ip, sender_port]. ‘data` is a binary String when status is :done, otherwise nil.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sfml/network/udp_socket.rb', line 42

def receive(max: 1024)
  buf            = FFI::MemoryPointer.new(:uint8, Integer(max))
  received_size  = FFI::MemoryPointer.new(:size_t)
  sender_addr    = C::Network::IpAddress.new
  sender_port    = FFI::MemoryPointer.new(:uint16)

  code = C::Network.sfUdpSocket_receive(
    @handle, buf, Integer(max), received_size, sender_addr.pointer, sender_port,
  )
  status = C::Network::STATUSES[code]

  return [status, nil, nil, nil] unless status == :done
  n     = received_size.read(:size_t)
  sip   = IpAddress.wrap(sender_addr)
  sport = sender_port.read(:uint16)
  [status, buf.read_bytes(n), sip, sport]
end

#send(data, to:, port:) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/sfml/network/udp_socket.rb', line 31

def send(data, to:, port:)
  bytes = data.to_s
  addr  = to.is_a?(IpAddress) ? to : IpAddress.from_string(to)
  buf   = FFI::MemoryPointer.new(:uint8, bytes.bytesize)
  buf.write_bytes(bytes)
  code = C::Network.sfUdpSocket_send(@handle, buf, bytes.bytesize, addr.struct, Integer(port))
  C::Network::STATUSES[code]
end

#unbindObject



26
27
28
29
# File 'lib/sfml/network/udp_socket.rb', line 26

def unbind
  C::Network.sfUdpSocket_unbind(@handle)
  self
end