class SFML::Network::TcpSocket
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.
Public Class Methods
Source
# File lib/sfml/network/tcp_socket.rb, line 17 def initialize ptr = C::Network.sfTcpSocket_create raise NetworkError, "sfTcpSocket_create returned NULL" if ptr.null? @handle = FFI::AutoPointer.new(ptr, C::Network.method(:sfTcpSocket_destroy)) end
Public Instance Methods
Source
# File lib/sfml/network/tcp_socket.rb, line 83 def blocking=(value) C::Network.sfTcpSocket_setBlocking(@handle, value ? true : false) end
Set the blocking.
Source
# File lib/sfml/network/tcp_socket.rb, line 80 def blocking? = C::Network.sfTcpSocket_isBlocking(@handle) # Set the blocking. def blocking=(value) C::Network.sfTcpSocket_setBlocking(@handle, value ? true : false) end # Returns the local port. def local_port = C::Network.sfTcpSocket_getLocalPort(@handle) # Returns the remote port. def remote_port = C::Network.sfTcpSocket_getRemotePort(@handle) # Returns the remote address. def remote_address IpAddress.wrap(C::Network.sfTcpSocket_getRemoteAddress(@handle)) end attr_reader :handle # :nodoc: end end end
true if blocking.
Source
# 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
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.
Source
# File lib/sfml/network/tcp_socket.rb, line 33 def disconnect C::Network.sfTcpSocket_disconnect(@handle) self end
Returns the disconnect.
Source
# File lib/sfml/network/tcp_socket.rb, line 88 def local_port = C::Network.sfTcpSocket_getLocalPort(@handle) # Returns the remote port. def remote_port = C::Network.sfTcpSocket_getRemotePort(@handle) # Returns the remote address. def remote_address IpAddress.wrap(C::Network.sfTcpSocket_getRemoteAddress(@handle)) end attr_reader :handle # :nodoc: end end
Returns the local port.
Source
# File lib/sfml/network/tcp_socket.rb, line 49 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
Read up to max bytes. Returns [status, data] โ data is a binary String when status is :done, nil otherwise.
Source
# File lib/sfml/network/tcp_socket.rb, line 72 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
Receive into a fresh Packet. Returns [status, packet]; the packet is nil for non-:done statuses.
Source
# File lib/sfml/network/tcp_socket.rb, line 93 def remote_address IpAddress.wrap(C::Network.sfTcpSocket_getRemoteAddress(@handle)) end
Returns the remote address.
Source
# File lib/sfml/network/tcp_socket.rb, line 90 def remote_port = C::Network.sfTcpSocket_getRemotePort(@handle) # Returns the remote address. def remote_address IpAddress.wrap(C::Network.sfTcpSocket_getRemoteAddress(@handle)) end attr_reader :handle # :nodoc: end
Source
# File lib/sfml/network/tcp_socket.rb, line 39 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
Returns the send.
Source
# File lib/sfml/network/tcp_socket.rb, line 64 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
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).