class SFML::Network::TcpListener
The server side of TCP. Listens on a port; accept blocks until a client connects, then returns a fresh TcpSocket for that peer.
listener = SFML::Network::TcpListener.new listener.listen(port: 8080) loop do status, peer = listener.accept break unless status == :done handle_client(peer) end
Public Class Methods
Source
# File lib/sfml/network/tcp_listener.rb, line 14 def initialize ptr = C::Network.sfTcpListener_create raise NetworkError, "sfTcpListener_create returned NULL" if ptr.null? @handle = FFI::AutoPointer.new(ptr, C::Network.method(:sfTcpListener_destroy)) end
Public Instance Methods
Source
# File lib/sfml/network/tcp_listener.rb, line 37 def accept out_ptr = FFI::MemoryPointer.new(:pointer) code = C::Network.sfTcpListener_accept(@handle, out_ptr) status = C::Network::STATUSES[code] return [status, nil] unless status == :done sock_ptr = out_ptr.read_pointer return [status, nil] if sock_ptr.null? sock = TcpSocket.allocate sock.instance_variable_set(:@handle, FFI::AutoPointer.new(sock_ptr, C::Network.method(:sfTcpSocket_destroy))) [status, sock] end
Returns [status, TcpSocket] — the socket is non-nil only when status == :done.
Source
# File lib/sfml/network/tcp_listener.rb, line 55 def blocking=(value) C::Network.sfTcpListener_setBlocking(@handle, value ? true : false) end
Set the blocking.
Source
# File lib/sfml/network/tcp_listener.rb, line 52 def blocking? = C::Network.sfTcpListener_isBlocking(@handle) # Set the blocking. def blocking=(value) C::Network.sfTcpListener_setBlocking(@handle, value ? true : false) end # Returns the local port. def local_port = C::Network.sfTcpListener_getLocalPort(@handle) attr_reader :handle # :nodoc: end end
true if blocking.
Source
# File lib/sfml/network/tcp_listener.rb, line 30 def close C::Network.sfTcpListener_close(@handle) self end
Returns the close.
Source
# File lib/sfml/network/tcp_listener.rb, line 23 def listen(port:, address: IpAddress::ANY) addr = address.is_a?(IpAddress) ? address : IpAddress.from_string(address) code = C::Network.sfTcpListener_listen(@handle, Integer(port), addr.struct) C::Network::STATUSES[code] end
Bind the listener to a port and start listening. address (default: any local interface) restricts which interface to listen on — pass IpAddress::LOCALHOST for loopback-only.