Class: SFML::Network::TcpSocket

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeTcpSocket

Returns a new instance of TcpSocket.

Raises:



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

#handleObject (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

Returns:

  • (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

#disconnectObject



32
33
34
35
# File 'lib/sfml/network/tcp_socket.rb', line 32

def disconnect
  C::Network.sfTcpSocket_disconnect(@handle)
  self
end

#local_portObject



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_addressObject



67
68
69
# File 'lib/sfml/network/tcp_socket.rb', line 67

def remote_address
  IpAddress.wrap(C::Network.sfTcpSocket_getRemoteAddress(@handle))
end

#remote_portObject



65
# File 'lib/sfml/network/tcp_socket.rb', line 65

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