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.
- #remote_address ⇒ Object
- #remote_port ⇒ Object
- #send(data) ⇒ Object
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:
71 72 73 |
# File 'lib/sfml/network/tcp_socket.rb', line 71 def handle @handle end |
Instance Method Details
#blocking=(value) ⇒ Object
60 61 62 |
# File 'lib/sfml/network/tcp_socket.rb', line 60 def blocking=(value) C::Network.sfTcpSocket_setBlocking(@handle, value ? true : false) end |
#blocking? ⇒ Boolean
58 |
# File 'lib/sfml/network/tcp_socket.rb', line 58 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
64 |
# File 'lib/sfml/network/tcp_socket.rb', line 64 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 |
#remote_address ⇒ Object
67 68 69 |
# File 'lib/sfml/network/tcp_socket.rb', line 67 def remote_address IpAddress.wrap(C::Network.sfTcpSocket_getRemoteAddress(@handle)) end |