Class: SFML::Network::TcpSocket
- Inherits:
-
Object
- Object
- SFML::Network::TcpSocket
- Defined in:
- lib/sfml/network/tcp_socket.rb
Overview
A TCP client socket. Connect to a server, send/receive bytes, disconnect.
sock = SFML::Network::TcpSocket.new
case sock.connect(SFML::Network::IpAddress::LOCALHOST, port: 8080)
when :done then ...
end
sock.send("hello")
status, bytes = sock.receive(max: 1024)
By default sockets are blocking; set ‘socket.blocking = false` for non-blocking polling, where send/receive may return :not_ready.
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #blocking=(value) ⇒ Object
- #blocking? ⇒ Boolean
-
#connect(address, port:, timeout: SFML::Time.zero) ⇒ Object
Open a connection.
- #disconnect ⇒ Object
-
#initialize ⇒ TcpSocket
constructor
A new instance of TcpSocket.
- #local_port ⇒ Object
-
#receive(max: 4096) ⇒ Object
Read up to ‘max` bytes.
-
#receive_packet ⇒ Object
Receive into a fresh Packet.
- #remote_address ⇒ Object
- #remote_port ⇒ Object
- #send(data) ⇒ Object
-
#send_packet(packet) ⇒ Object
Send a structured SFML::Network::Packet.
Constructor Details
#initialize ⇒ TcpSocket
Returns a new instance of TcpSocket.
17 18 19 20 21 |
# File 'lib/sfml/network/tcp_socket.rb', line 17 def initialize ptr = C::Network.sfTcpSocket_create raise Error, "sfTcpSocket_create returned NULL" if ptr.null? @handle = FFI::AutoPointer.new(ptr, C::Network.method(:sfTcpSocket_destroy)) end |
Instance Attribute Details
#handle ⇒ Object (readonly)
:nodoc:
90 91 92 |
# File 'lib/sfml/network/tcp_socket.rb', line 90 def handle @handle end |
Instance Method Details
#blocking=(value) ⇒ Object
79 80 81 |
# File 'lib/sfml/network/tcp_socket.rb', line 79 def blocking=(value) C::Network.sfTcpSocket_setBlocking(@handle, value ? true : false) end |
#blocking? ⇒ Boolean
77 |
# File 'lib/sfml/network/tcp_socket.rb', line 77 def blocking? = C::Network.sfTcpSocket_isBlocking(@handle) |
#connect(address, port:, timeout: SFML::Time.zero) ⇒ Object
Open a connection. Returns one of the SOCKET_STATUSES symbols (:done, :not_ready, :disconnected, :error). ‘timeout` is a SFML::Time; SFML::Time.zero (default) blocks forever.
26 27 28 29 30 |
# File 'lib/sfml/network/tcp_socket.rb', line 26 def connect(address, port:, timeout: SFML::Time.zero) addr = address.is_a?(IpAddress) ? address : IpAddress.from_string(address) code = C::Network.sfTcpSocket_connect(@handle, addr.struct, Integer(port), timeout.to_native) C::Network::STATUSES[code] end |
#disconnect ⇒ Object
32 33 34 35 |
# File 'lib/sfml/network/tcp_socket.rb', line 32 def disconnect C::Network.sfTcpSocket_disconnect(@handle) self end |
#local_port ⇒ Object
83 |
# File 'lib/sfml/network/tcp_socket.rb', line 83 def local_port = C::Network.sfTcpSocket_getLocalPort(@handle) |
#receive(max: 4096) ⇒ Object
Read up to ‘max` bytes. Returns [status, data] — `data` is a binary String when status is :done, nil otherwise.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sfml/network/tcp_socket.rb', line 47 def receive(max: 4096) buf = FFI::MemoryPointer.new(:uint8, Integer(max)) received = FFI::MemoryPointer.new(:size_t) code = C::Network.sfTcpSocket_receive(@handle, buf, Integer(max), received) status = C::Network::STATUSES[code] return [status, nil] unless status == :done n = received.read(:size_t) [status, buf.read_bytes(n)] end |
#receive_packet ⇒ Object
Receive into a fresh Packet. Returns [status, packet]; the packet is nil for non-:done statuses.
70 71 72 73 74 75 |
# File 'lib/sfml/network/tcp_socket.rb', line 70 def receive_packet pkt = Packet.new code = C::Network.sfTcpSocket_receivePacket(@handle, pkt.handle) status = C::Network::STATUSES[code] [status, status == :done ? pkt : nil] end |
#remote_address ⇒ Object
86 87 88 |
# File 'lib/sfml/network/tcp_socket.rb', line 86 def remote_address IpAddress.wrap(C::Network.sfTcpSocket_getRemoteAddress(@handle)) end |
#remote_port ⇒ Object
84 |
# File 'lib/sfml/network/tcp_socket.rb', line 84 def remote_port = C::Network.sfTcpSocket_getRemotePort(@handle) |
#send(data) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/sfml/network/tcp_socket.rb', line 37 def send(data) bytes = data.to_s buf = FFI::MemoryPointer.new(:uint8, bytes.bytesize) buf.write_bytes(bytes) code = C::Network.sfTcpSocket_send(@handle, buf, bytes.bytesize) C::Network::STATUSES[code] end |
#send_packet(packet) ⇒ Object
Send a structured SFML::Network::Packet. CSFML frames the wire bytes with a length prefix so the peer’s receive_packet always gets a whole packet (no need to handle TCP boundary fragments at the Ruby layer).
62 63 64 65 66 |
# File 'lib/sfml/network/tcp_socket.rb', line 62 def send_packet(packet) raise ArgumentError, "expected SFML::Network::Packet" unless packet.is_a?(Packet) code = C::Network.sfTcpSocket_sendPacket(@handle, packet.handle) C::Network::STATUSES[code] end |