Class: SFML::Network::UdpSocket
- Inherits:
-
Object
- Object
- SFML::Network::UdpSocket
- 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 =
Max UDP payload CSFML will send in one call.
C::Network.sfUdpSocket_maxDatagramSize
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#bind(port:, address: IpAddress::ANY) ⇒ Object
Returns the bind.
-
#blocking=(value) ⇒ Object
Set the blocking.
-
#blocking? ⇒ Boolean
trueif blocking. -
#initialize ⇒ UdpSocket
constructor
A new instance of UdpSocket.
-
#local_port ⇒ Object
Returns the local port.
-
#receive(max: 1024) ⇒ Object
Returns [status, data, sender_ip, sender_port].
-
#receive_packet ⇒ Object
Returns [status, packet, sender_ip, sender_port].
-
#send(data, to:, port:) ⇒ Object
Returns the send.
-
#send_packet(packet, to:, port:) ⇒ Object
Send a structured SFML::Network::Packet to (to, port).
-
#unbind ⇒ Object
Returns the unbind.
Constructor Details
#initialize ⇒ UdpSocket
Returns a new instance of UdpSocket.
15 16 17 18 19 |
# File 'lib/sfml/network/udp_socket.rb', line 15 def initialize ptr = C::Network.sfUdpSocket_create raise NetworkError, "sfUdpSocket_create returned NULL" if ptr.null? @handle = FFI::AutoPointer.new(ptr, C::Network.method(:sfUdpSocket_destroy)) end |
Instance Attribute Details
#handle ⇒ Object (readonly)
:nodoc:
98 99 100 |
# File 'lib/sfml/network/udp_socket.rb', line 98 def handle @handle end |
Instance Method Details
#bind(port:, address: IpAddress::ANY) ⇒ Object
Returns the bind.
22 23 24 25 26 |
# File 'lib/sfml/network/udp_socket.rb', line 22 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
Set the blocking.
91 92 93 |
# File 'lib/sfml/network/udp_socket.rb', line 91 def blocking=(value) C::Network.sfUdpSocket_setBlocking(@handle, value ? true : false) end |
#blocking? ⇒ Boolean
true if blocking.
88 |
# File 'lib/sfml/network/udp_socket.rb', line 88 def blocking? = C::Network.sfUdpSocket_isBlocking(@handle) |
#local_port ⇒ Object
Returns the local port.
96 |
# File 'lib/sfml/network/udp_socket.rb', line 96 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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sfml/network/udp_socket.rb', line 46 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 |
#receive_packet ⇒ Object
Returns [status, packet, sender_ip, sender_port]. Packet is nil for non-:done statuses.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sfml/network/udp_socket.rb', line 74 def receive_packet pkt = Packet.new sender_addr = C::Network::IpAddress.new sender_port = FFI::MemoryPointer.new(:uint16) code = C::Network.sfUdpSocket_receivePacket( @handle, pkt.handle, sender_addr.pointer, sender_port, ) status = C::Network::STATUSES[code] return [status, nil, nil, nil] unless status == :done [status, pkt, IpAddress.wrap(sender_addr), sender_port.read(:uint16)] end |
#send(data, to:, port:) ⇒ Object
Returns the send.
35 36 37 38 39 40 41 42 |
# File 'lib/sfml/network/udp_socket.rb', line 35 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 |
#send_packet(packet, to:, port:) ⇒ Object
Send a structured SFML::Network::Packet to (to, port).
65 66 67 68 69 70 |
# File 'lib/sfml/network/udp_socket.rb', line 65 def send_packet(packet, to:, port:) raise ArgumentError, "expected SFML::Network::Packet" unless packet.is_a?(Packet) addr = to.is_a?(IpAddress) ? to : IpAddress.from_string(to) code = C::Network.sfUdpSocket_sendPacket(@handle, packet.handle, addr.struct, Integer(port)) C::Network::STATUSES[code] end |