network.sockets.tcp - Palamecia/mint GitHub Wiki
Module network.sockets.tcp
Description
load network.sockets.tcp
This module provides the Network.TcpSocket implementation for TCP/IP communication and its scheduler-based asynchronous companion.
Packages
Enums
Network.TcpSocketOption
This enum describes the options of a TCP socket. The effective value of the option code can depend on the current system.
| Constant | Value | Description ||----------|-------|-------------|
| MAXSEG | 0 | TCP maximum segment size (number). |
| NODELAY | 1 | Disable Nagle algorithm (boolean). |
Classes
Network.AsyncTcpSocket
Inherits
Public members
| Modifiers | Member | Description |
|---|---|---|
class |
AcceptOperation | Scheduler operation used to accept incoming connections asynchronously. Insta... |
class |
ConnectOperation | Scheduler operation used to establish a connection asynchronously. Instances ... |
class |
ReadOperation | Scheduler operation used to receive data asynchronously. Instances of this op... |
class |
ReadSomeOperation | Scheduler operation used to receive data asynchronously. Instances of this op... |
class |
WriteOperation | Scheduler operation used to send data asynchronously. Instances of this opera... |
| accept | Waits asynchronously for the next incoming connection. The current coroutine ... | |
const |
bind | Tells the socket to listen for incoming messages from endpoint. Returns true ... |
| connect | Attempts to establish a connection to the remote device identified by endpoint... |
|
const |
listen | Tells the socket to listen for incoming connections from endpoint. Returns true... |
const |
open | Attempts to open the socket without connection to the remote device. The sock... |
| read | ||
| readSome | Asynchronously receives at most count bytes of data from the peer. The operat... |
|
| write | Asynchronously sends the data described by data to the peer. The data paramet... |
Private members
| Modifiers | Member | Description |
|---|---|---|
@ const |
g_lib | Global library handle. |
Network.AsyncTcpSocket.AcceptOperation
Scheduler operation used to accept incoming connections asynchronously.
Instances of this operation are created by Network.AsyncTcpSocket.accept.
The operation asynchronously accepts a pending connection on the listening socket. The coroutine is suspended until the operating system completes the accept operation, at which point the coroutine resumes with the result.
When a connection is successfully accepted, the operation returns a new socket instance representing the client connection. The returned socket is already initialized and ready for communication.
An instance of Exception.SocketError is raised on error.
Inherits
Public members
| Modifiers | Member | Description |
|---|---|---|
override const |
check | Checks for the operation completion. The operation succeed if a connection is... |
override const |
get | Returns the accepted socket or none if no connection was established. |
const |
new | Creates a new asynchronous operation to accept a pending connection on socket. |
Protected members
| Modifiers | Member | Description |
|---|---|---|
override const |
setup | Setup the operation by calling the underlying asynchronous accept function. R... |
Private members
| Modifiers | Member | Description |
|---|---|---|
final |
client | Internal accepted client connection socket. |
@ const |
g_lib | Global library handle. |
final |
scheduler | Internal scheduler. |
final |
socket | Internal socket. |
Network.AsyncTcpSocket.ConnectOperation
Scheduler operation used to establish a connection asynchronously.
Instances of this operation are created by Network.AsyncTcpSocket.connect.
The operation asynchronously connects the socket. Once the connection operation completes, the coroutine resumes and the result of the operation is checked.
If the connection succeeds the socket enters the Network.Socket.State.Connected state.
If the connection fails the socket enters the Network.Socket.State.Error state and an instance of Exception.SocketError is raised.
Inherits
Public members
| Modifiers | Member | Description |
|---|---|---|
override const |
check | Checks for the operation completion. The operation succeed if the underlying ... |
override const |
get | Returns true if the connection was correctly established; otherwise returns f... |
const |
new | Creates a new asynchronous operation to wait for socket connection. |
Protected members
| Modifiers | Member | Description |
|---|---|---|
override const |
setup | Setup the operation by calling the underlying asynchronous connect function. ... |
Private members
| Modifiers | Member | Description |
|---|---|---|
final |
endpoint | Internal endpoint. |
@ const |
g_lib | Global library handle. |
final |
socket | Internal socket. |
final |
status | Internal connection status. |
Network.AsyncTcpSocket.ReadOperation
Scheduler operation used to receive data asynchronously.
Instances of this operation are created by Network.AsyncTcpSocket.read.
The operation asynchronously reads data from the socket and appends it to
the internal receive buffer until the completion predicate returns true.
The completion predicate receives the current buffer and determines when the operation should finish. This allows callers to implement custom read conditions such as:
- reading a fixed number of bytes
- reading until a delimiter is encountered
- reading until the buffer is non-empty
If the peer closes the connection before the completion condition is satisfied, the operation completes with the data accumulated so far.
An instance of Exception.SocketError is raised on error.
Inherits
Public members
| Modifiers | Member | Description |
|---|---|---|
override const |
check | Checks for the operation completion. The operation succeed if data is availab... |
override const |
get | Returns the received data as an instance of Network.AsyncTcpSocket.ReadOperat... |
const |
new | Creates a new asynchronous operation to read data from socket until the compl... |
Protected members
| Modifiers | Member | Description |
|---|---|---|
override const |
setup | Setup the operation by calling the underlying asynchronous read function. Ret... |
Private members
| Modifiers | Member | Description |
|---|---|---|
final |
buffer | Internal read buffer. |
final |
completion | Internal combination condition. |
@ const |
g_lib | Global library handle. |
final |
socket | Internal socket. |
Network.AsyncTcpSocket.ReadSomeOperation
Scheduler operation used to receive data asynchronously.
Instances of this operation are created by Network.AsyncTcpSocket.readSome.
The operation asynchronously reads data from the socket and appends it to the internal receive buffer until enough data has been received.
If the peer closes the connection before enough data is received, the operation completes with the data accumulated so far.
An instance of Exception.SocketError is raised on error.
Inherits
Public members
| Modifiers | Member | Description |
|---|---|---|
override const |
check | Checks for the operation completion. The operation succeed if data is availab... |
override const |
get | Returns the received data as an instance of Network.AsyncTcpSocket.ReadSomeOp... |
const |
new | Creates a new asynchronous operation to read at most count bytes of data from ``... |
Protected members
| Modifiers | Member | Description |
|---|---|---|
override const |
setup | Setup the operation by calling the underlying asynchronous read function. Ret... |
Private members
| Modifiers | Member | Description |
|---|---|---|
final |
buffer | Internal read buffer. |
final |
count | Internal expected byte count. |
@ const |
g_lib | Global library handle. |
final |
socket | Internal socket. |
Network.AsyncTcpSocket.WriteOperation
Scheduler operation used to send data asynchronously.
Instances of this operation are created by Network.AsyncTcpSocket.write.
The operation asynchronously sends the provided Serializer.DataStream using the socket.
Because network writes may be partial, the operation may perform multiple asynchronous writes until the entire buffer has been transmitted. The coroutine is suspended while the operation is in progress and resumes once the entire buffer has been sent.
Returns an iterator containing true and the amount of sent data in
bytes if the data was successfully sent to the peer; otherwise returns
false.
An instance of Exception.SocketError is raised on error.
Inherits
Public members
| Modifiers | Member | Description |
|---|---|---|
override const |
check | Checks for the operation completion. The operation succeed if all the data in... |
override const |
get | Returns an iterator containing true and the amount of sent data in bytes if t... |
const |
new | Creates a new asynchronous operation to send the data provided by buffer to s... |
Protected members
| Modifiers | Member | Description |
|---|---|---|
override const |
setup | Setup the operation by calling the underlying asynchronous write function. Re... |
Private members
| Modifiers | Member | Description |
|---|---|---|
final |
buffer | Internal write buffer. |
final |
count | Internal sent byte counter. |
@ const |
g_lib | Global library handle. |
final |
socket | Internal socket. |
final |
status | Internal write status. |
Network.TcpSocket
This class provides a socket implementation for communication with a peer device using the TCP/IP protocol.
Inherits
Public members
| Modifiers | Member | Description |
|---|---|---|
const |
accept | Returns a new end point initialized for the next waiting connection if any; o... |
const |
asyncOn | Returns a new instance of Network.TcpSocket.Network.AsyncTcpSocket initialize... |
const |
bind | Binds the socket to endpoint. Returns true on success; otherwise returns false. ... |
const |
connect | Attempts to make a connection to the device identified by the endpoint parame... |
override const |
getSocketOption | Returns the value of the socket option described by option. The option parame... |
const |
listen | Tells the socket to listen for incoming connections from endpoint. Returns true... |
const |
open | Attempts to open the socket without connection to the remote device. The sock... |
const |
read | Returns all available data received from the peer as an instance of Network.T... |
const |
readSome | Returns at most count bytes of data received from the peer as an instance of ``... |
override const |
setSocketOption | Sets the value of the socket option described by option to value. The option ... |
const |
write | Sends the data described by the data parameter to the peer. The data paramete... |
Private members
| Modifiers | Member | Description |
|---|---|---|
@ const |
g_lib | Global library handle. |
Descriptions
Network.AsyncTcpSocket.AcceptOperation.check
def (self, result)
Checks for the operation completion. The operation succeed if a connection is accepted. It fails on error.
Network.AsyncTcpSocket.AcceptOperation.client
none
Internal accepted client connection socket.
Network.AsyncTcpSocket.AcceptOperation.g_lib
lib ('libmint-network')
Global library handle.
Network.AsyncTcpSocket.AcceptOperation.get
def (self)
Returns the accepted socket or none if no connection was established.
Network.AsyncTcpSocket.AcceptOperation.new
def (self, socket)
Creates a new asynchronous operation to accept a pending connection on socket.
Network.AsyncTcpSocket.AcceptOperation.scheduler
null
Internal scheduler.
Network.AsyncTcpSocket.AcceptOperation.setup
def (self, scheduler)
Setup the operation by calling the underlying asynchronous accept function. Returns the internal object data
of the operation. The operation will be completed using scheduler.
Network.AsyncTcpSocket.AcceptOperation.socket
null
Internal socket.
Network.AsyncTcpSocket.ConnectOperation.check
def (self, result)
Checks for the operation completion. The operation succeed if the underlying socket enter the Network.Socket.State.Connected state. It fails on error.
Network.AsyncTcpSocket.ConnectOperation.endpoint
null
Internal endpoint.
Network.AsyncTcpSocket.ConnectOperation.g_lib
lib ('libmint-network')
Global library handle.
Network.AsyncTcpSocket.ConnectOperation.get
def (self)
Returns true if the connection was correctly established; otherwise returns false.
Network.AsyncTcpSocket.ConnectOperation.new
def (self, socket, endpoint)
Creates a new asynchronous operation to wait for socket connection.
Network.AsyncTcpSocket.ConnectOperation.setup
def (self, scheduler)
Setup the operation by calling the underlying asynchronous connect function. Returns the internal object data
of the operation. The operation will be completed using scheduler.
Network.AsyncTcpSocket.ConnectOperation.socket
null
Internal socket.
Network.AsyncTcpSocket.ConnectOperation.status
none
Internal connection status.
Network.AsyncTcpSocket.ReadOperation.buffer
null
Internal read buffer.
Network.AsyncTcpSocket.ReadOperation.check
def (self, result)
Checks for the operation completion. The operation succeed if data is available and the completion
predicate returns true for the current read buffer. It fails on error.
Network.AsyncTcpSocket.ReadOperation.completion
null
Internal combination condition.
Network.AsyncTcpSocket.ReadOperation.g_lib
lib ('libmint-network')
Global library handle.
Network.AsyncTcpSocket.ReadOperation.get
def (self)
Returns the received data as an instance of Serializer.DataStream.
Network.AsyncTcpSocket.ReadOperation.new
def (self, socket, completion)
Creates a new asynchronous operation to read data from socket until the completion predicate
returns true.
The completion parameter is a function that receives the current
receive buffer and returns true when the operation should complete.
Network.AsyncTcpSocket.ReadOperation.setup
def (self, scheduler)
Setup the operation by calling the underlying asynchronous read function. Returns the internal object data
of the operation. The operation will be completed using scheduler.
Network.AsyncTcpSocket.ReadOperation.socket
null
Internal socket.
Network.AsyncTcpSocket.ReadSomeOperation.buffer
null
Internal read buffer.
Network.AsyncTcpSocket.ReadSomeOperation.check
def (self, result)
Checks for the operation completion. The operation succeed if data is available and the completion
predicate returns true for the current read buffer. It fails on error.
Network.AsyncTcpSocket.ReadSomeOperation.count
null
Internal expected byte count.
Network.AsyncTcpSocket.ReadSomeOperation.g_lib
lib ('libmint-network')
Global library handle.
Network.AsyncTcpSocket.ReadSomeOperation.get
def (self)
Returns the received data as an instance of Serializer.DataStream.
Network.AsyncTcpSocket.ReadSomeOperation.new
def (self, socket, count)
Creates a new asynchronous operation to read at most count bytes of data from socket.
Network.AsyncTcpSocket.ReadSomeOperation.setup
def (self, scheduler)
Setup the operation by calling the underlying asynchronous read function. Returns the internal object data
of the operation. The operation will be completed using scheduler.
Network.AsyncTcpSocket.ReadSomeOperation.socket
null
Internal socket.
Network.AsyncTcpSocket.WriteOperation.buffer
null
Internal write buffer.
Network.AsyncTcpSocket.WriteOperation.check
def (self, result)
Checks for the operation completion. The operation succeed if all the data in the buffer is sent. It fails on error.
Network.AsyncTcpSocket.WriteOperation.count
0
Internal sent byte counter.
Network.AsyncTcpSocket.WriteOperation.g_lib
lib ('libmint-network')
Global library handle.
Network.AsyncTcpSocket.WriteOperation.get
def (self)
Returns an iterator containing true and the amount of sent data in bytes if the data was successfully
sent to the peer; otherwise returns false.
Network.AsyncTcpSocket.WriteOperation.new
def (self, socket, buffer)
Creates a new asynchronous operation to send the data provided by buffer to socket.
Network.AsyncTcpSocket.WriteOperation.setup
def (self, scheduler)
Setup the operation by calling the underlying asynchronous write function. Returns the internal object data
of the operation. The operation will be completed using scheduler.
Network.AsyncTcpSocket.WriteOperation.socket
null
Internal socket.
Network.AsyncTcpSocket.WriteOperation.status
false
Internal write status.
Network.AsyncTcpSocket.accept
def (self)
Waits asynchronously for the next incoming connection.
The current coroutine is suspended until a client connection becomes available or an error occurs.
Returns a new socket instance representing the accepted client connection.
The socket must be in the Network.Socket.State.Listening state.
The returned socket is already initialized and ready for I/O operations.
An instance of Exception.SocketError is raised if the accept operation fails.
Network.AsyncTcpSocket.bind
def (self, endpoint)
Tells the socket to listen for incoming messages from endpoint. Returns true on success; otherwise
returns false.
An instance of Exception.SocketError is raised on error.
Network.AsyncTcpSocket.connect
def (self, endpoint)
Attempts to establish a connection to the remote device identified by endpoint asynchronously.
This method suspends the current coroutine until the connection attempt either succeeds or fails.
Returns true if the connection was successfully established; otherwise returns false.
On success, the socket should enter the Network.Socket.State.Connected state.
On failure, the socket should enter the Network.Socket.State.Error state.
Network.AsyncTcpSocket.g_lib
lib ('libmint-network')
Global library handle.
Network.AsyncTcpSocket.listen
def (self, endpoint, backlog = 1024)
Tells the socket to listen for incoming connections from endpoint. Returns true on success; otherwise
returns false. The backlog parameter provides the maximum number of waiting connection allowed before new
connections get rejected.
An instance of Exception.SocketError is raised on error.
Network.AsyncTcpSocket.open
def (self, ipVersion)
Attempts to open the socket without connection to the remote device. The socket is open using the ipVersion
version of the IP protocol. Returns true if the socket was correctly opened; otherwise returns false.
An instance of Exception.SocketError is raised on error.
Network.AsyncTcpSocket.read
def (buffer)
Network.AsyncTcpSocket.readSome
def (self, count)
Asynchronously receives at most count bytes of data from the peer.
The operation suspends the current coroutine until new data becomes available.
Returns the received data as an instance of Serializer.DataStream.
If the peer closes the connection before the completion condition is met, the returned buffer may contain fewer bytes than expected.
An instance of Exception.SocketError is raised on error.
Network.AsyncTcpSocket.write
def (self, data)
Asynchronously sends the data described by data to the peer.
The data parameter must be an instance of Serializer.DataStream.
The operation suspends the current coroutine until all bytes have been written or an error occurs. Partial writes may occur internally and the operation will continue sending the remaining data until the buffer is empty.
Returns an iterator containing true and the amount of sent data in
bytes if the data was successfully sent to the peer; otherwise returns
false.
The socket must be in the Network.Socket.State.Connected state.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.accept
def (self)
Returns a new end point initialized for the next waiting connection
if any; otherwise returns none. The waiting connection will then
be accepted and dequeued.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.asyncOn
def (self, scheduler)
Returns a new instance of Network.AsyncTcpSocket initialized with the current socket and the scheduler
parameter.
Network.TcpSocket.bind
def (self, endpoint)
Binds the socket to endpoint. Returns true on success; otherwise returns false.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.connect
def (self, endpoint)
Attempts to make a connection to the device identified by the endpoint parameter. Returns true if
connection succeed or if the connection request was successfully sent and is waiting for acknowledgment;
otherwise returns false.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.g_lib
lib ('libmint-network')
Global library handle.
Network.TcpSocket.getSocketOption
def (self, % option)
Returns the value of the socket option described by option. The option parameter must be a value of the
Network.TcpSocketOption, Network.IpV4SocketOption, Network.IpV6SocketOption, Network.IpSocketOption
or Network.SocketOption enum.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.listen
def (self, endpoint, backlog = 1024)
Tells the socket to listen for incoming connections from endpoint. Returns true on success; otherwise
returns false. The backlog parameter provides the maximum number of waiting connection allowed before new
connections get rejected.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.open
def (self, ipVersion)
Attempts to open the socket without connection to the remote device. The socket is open using the ipVersion
version of the IP protocol. Returns true if the socket was correctly opened; otherwise returns false.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.read
def (self)
Returns all available data received from the peer as an instance of Serializer.DataStream or none if no
data is available.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.readSome
def (self, count)
Returns at most count bytes of data received from the peer as an instance of Serializer.DataStream or
none if no data is available.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.setSocketOption
def (self, % option, value)
Sets the value of the socket option described by option to value. The option parameter must be a value
of the Network.TcpSocketOption, Network.IpV4SocketOption, Network.IpV6SocketOption,
Network.IpSocketOption or Network.SocketOption enum.
An instance of Exception.SocketError is raised on error.
Network.TcpSocket.write
def (self, data)
Sends the data described by the data parameter to the peer. The data parameter must be an instance of
Serializer.DataStream. Returns an iterator containing true and the amount of sent data in bytes if the
data was successfully sent to the peer; otherwise returns false.
An instance of Exception.SocketError is raised on error.
Network.TcpSocketOption.MAXSEG
0
TCP maximum segment size (number).
Network.TcpSocketOption.NODELAY
1
Disable Nagle algorithm (boolean).