R6 convenience wrapper for socketR handles.

R6 convenience wrapper for socketR handles.

Value

An R6 Socket object.

Details

The public methods mirror the functional API: is_open(), fd(), info(), set_blocking(), bind(), listen(), accept(), connect(), send(), receive(), send_to(), receive_from(), shutdown(), close(), local_name(), peer_name(), poll(), get_option(), set_option(), and set_options(). They delegate to the corresponding documented functions while retaining the handle as object state.

Use Socket$new_auto() to create a connected client with IPv4/IPv6 fallback, or Socket$new_listener() to create a listening server with IPv4/IPv6 fallback.

Public fields

handle

The underlying socketr_socket external pointer.

Active bindings

address

Local address, when bound.

port

Local port, when bound.

peer_address

Connected peer address, when connected.

peer_port

Connected peer port, when connected.

family

Address family ("inet", "inet6", or "unix").

type

Socket type ("stream" or "dgram").

protocol

Numeric socket protocol.

blocking

Whether the socket is in blocking mode.

open

Whether the underlying socket is open.

Methods


Method new()

Create a socket wrapper or wrap an existing handle.

Usage

Socket$new(
  domain = c("inet", "inet6", "unix"),
  type = c("stream", "dgram"),
  protocol = 0L,
  nonblocking = FALSE,
  cloexec = TRUE,
  handle = NULL
)

Arguments

domain

Address family passed to socket_create().

type

Socket type passed to socket_create().

protocol

Numeric protocol passed to socket_create().

nonblocking

Whether the new socket is nonblocking.

cloexec

Whether the new socket is close-on-exec.

handle

An existing socketR handle to wrap.


Method is_open()

Test whether the wrapped socket is open.

Usage

Socket$is_open()


Method fd()

Return the wrapped socket file descriptor.

Usage

Socket$fd()


Method info()

Return metadata for the wrapped socket.

Usage

Socket$info()


Method set_blocking()

Set blocking mode.

Usage

Socket$set_blocking(blocking = TRUE)

Arguments

blocking

Whether operations should block.


Method bind()

Bind the wrapped socket.

Usage

Socket$bind(address = NULL, port = NULL)

Arguments

address

Local hostname, IP address, or Unix path.

port

Local port, or NULL for Unix sockets.


Method listen()

Listen for stream connections.

Usage

Socket$listen(backlog = 128L)

Arguments

backlog

Maximum pending connection queue length.


Method accept()

Accept a pending connection.

Usage

Socket$accept(nonblocking = NULL)

Arguments

nonblocking

Whether the accepted socket should be nonblocking.


Method connect()

Connect the wrapped socket.

Usage

Socket$connect(address, port = NULL)

Arguments

address

Remote hostname, IP address, or Unix path.

port

Remote port, or NULL for Unix sockets.


Method as_connection()

Return the wrapped socket as an R connection.

Usage

Socket$as_connection()


Method write()

Write bytes to the wrapped socket.

Usage

Socket$write(object, flags = 0L)

Arguments

object

Raw bytes or a character scalar.

flags

Native send() flags.


Method read()

Read bytes from the wrapped socket.

Usage

Socket$read(n = 4096L, flags = 0L)

Arguments

n

Maximum number of bytes.

flags

Native recv() flags.


Method send()

Send bytes on the wrapped socket.

Usage

Socket$send(data, flags = 0L)

Arguments

data

Raw bytes or a character scalar.

flags

Native send() flags.


Method receive()

Receive bytes from the wrapped socket.

Usage

Socket$receive(n = 4096L, flags = 0L)

Arguments

n

Maximum number of bytes.

flags

Native recv() flags.


Method send_to()

Send a datagram.

Usage

Socket$send_to(data, address, port = NULL, flags = 0L)

Arguments

data

Raw bytes or a character scalar.

address

Destination hostname or IP address.

port

Destination port.

flags

Native sendto() flags.


Method receive_from()

Receive a datagram.

Usage

Socket$receive_from(n = 4096L, flags = 0L)

Arguments

n

Maximum datagram payload size.

flags

Native recvfrom() flags.


Method shutdown()

Shut down part of the connection.

Usage

Socket$shutdown(how = c("both", "read", "write"))

Arguments

how

One of "read", "write", or "both".


Method close()

Close the wrapped socket.

Usage

Socket$close()


Method local_name()

Return the local socket name.

Usage

Socket$local_name()


Method peer_name()

Return the peer socket name.

Usage

Socket$peer_name()


Method poll()

Poll the wrapped socket.

Usage

Socket$poll(events = "read", timeout_ms = 60000L)

Arguments

events

Requested readiness events.

timeout_ms

Timeout in milliseconds.


Method get_option()

Get a socket option.

Usage

Socket$get_option(
  level = "socket",
  option,
  type = c("int", "logical", "timeval", "linger", "raw"),
  size = 256L
)

Arguments

level

Option level.

option

Option name or numeric constant.

type

Return type.

size

Maximum raw option size.


Method set_options()

Set multiple socket options.

Usage

Socket$set_options(options)

Arguments

options

Option specifications.


Method set_option()

Set a socket option.

Usage

Socket$set_option(
  level = "socket",
  option,
  value,
  type = c("int", "logical", "timeval", "linger", "raw")
)

Arguments

level

Option level.

option

Option name or numeric constant.

value

Option value.

type

Value type.


Method clone()

The objects of this class are cloneable with this method.

Usage

Socket$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.