Initializes dyad. This should be called before any other dyad function is called.
Handles all the active streams and events. This should be called with each
iteration of the program's main loop. By default dyad_update()
will block for
a maximum of 1 second, dyad_update()
can be made not to block by using
dyad_setUpdateTimeout()
to set the timeout to 0
.
Emits the DYAD_EVENT_CLOSE
event on all current streams then shuts down and
clears up everything. This should be called at the end of the program or when
we no longer need dyad.
Returns the version of the library as a string.
Returns the current time in seconds. This time should only be used for comparisons, as no specific epoch is guaranteed.
Returns the number of currently active streams.
Sets the maximum number of seconds the dyad_update()
function can block for.
If seconds
is 0
then dyad_update()
will not block.
Sets the interval in seconds that the DYAD_EVENT_TICK
event is emited to all
streams; the default is 1 second.
Sets the function which will be called when dyad encounters an error it cannot
recover from. The old atPanic function is returned or NULL
if none was set.
Creates and returns a new stream. The returned stream is in a closed state by
default, dyad_listen()
, dyad_listenEx()
or dyad_connect()
should be
called on the stream to take it out of the closed state. See
dyad_Stream.
Makes the stream
begin listening on the given port
on all local interfaces.
Performs the same task as dyad_listen()
but provides additional options:
host
is the address of the local interface the stream should listen on.
backlog
is the maximum length of the queue of pending connections.
Connects the stream
to the remote host
.
Adds a listener for the event
to the stream
. When the event occurs the
callback
is called and the event's udata field is set to udata
. If several
listeners are added for the same event they are emitted in the order which they
were added. See Events.
Removes an event listener which was added with the dyad_addListener()
function.
Removes all listeners for the given event
. If event
is DYAD_EVENT_NULL
then all listeners for all events are removed.
Writes the data
of the given size
to the stream. If you want to send a
null terminated string use dyad_writef()
instead.
Writes a formatted string to the stream
. The function is similar to
sprintf()
with the following differences:
- Dyad takes care of allocating enough memory for the result.
- There are no flags, widths or precisions; only the following standard
specifiers are supported:
%%
%s
%f
%g
%d
%i
%c
%x
%X
%p
. - The
%r
specifier is provided, this takes aFILE*
argument and writes the contents until the EOF is reached. - The
%b
specifier is provided, this takes avoid*
argument followed by anint
argument representing the number of bytes to be written.
dyad_vwritef()
is to dyad_writef()
what vsprintf()
is to sprintf()
.
Finishes sending any data the stream
has left in its write buffer then closes
the stream. The stream will stop receiving data once this function is called.
Immediately closes the stream
, discarding any data left in its write buffer.
Sets the number of seconds the stream
can go without any activity (receiving
or sending data) before it is automatically closed. If 0
seconds is set then
the stream will never timeout, this is the default.
If opt
is 1
then Nagle's algorithm is disabled for the stream's socket,
0
enables it; by default it is enabled.
Returns the current state of the stream
, for example a connected stream would
return the value DYAD_STATE_CONNECTED
. See States.
Returns the current IP address for the stream
. For listening streams this is
the local address, for connected streams it is the remote address.
Returns the current port
the stream
is using. For listening streams this is
the local port, for connected streams it is the remote port.
Returns the number of bytes which have been received by the stream
since it
was made.
Returns the number of bytes which have been sent by the stream
since it was
made. This does not include the data in the stream's write buffer which is
still waiting to be sent.
Returns the socket used by the stream
.
Emitted when a stream is destroyed. Once all the listeners for this event have returned then the associated stream's pointer should no longer be considered valid.
Emitted when a listening stream accepts a connection. The remote
field of the
event represents the connected client's stream.
Emitted when a listening stream begins listening.
Emitted when a connecting stream successfully makes the connection to its host.
Emitted when a stream is closed. Closed streams are automatically destroyed by
dyad_update()
, see DYAD_EVENT_DESTROY
Emitted once each time the stream becomes ready to send more data. This event is useful when writing a large file to a stream, allowing you to send it in smaller chunks rather than copying all the data to the stream's write buffer at once.
Emitted whenever the stream receives data. The event's data
field is the
received data (null terminated), the size
field is set to the size of the
received data (excluding the null terminator).
Emitted whenever the stream receives a line of data. The event's data
field
is set to the received line stripped of the \n
or \r\n
and null terminated,
the size
field is the length of string.
Emitted whenever an error occurs on a stream. For example, when creating a listening socket this event will be emitted if it cannot be bound. The stream is immediately closed after this event.
Emitted when the stream times out (see dyad_setTimeout()
). The stream is
immediately closed after this event.
Emitted every time a tick occurs. A tick is an event which is emitted on every
stream at a constant interval; this interval can be set using
dyad_setTickInterval()
.
The stream is closed. Streams are created in this state, and, if left in this state, are destroyed automatically.
The stream is connected but still has data in its write buffer thats waiting to be sent before it closes.
The stream is attempting to connect to a host.
The stream is connected and can send or receive data.
The stream is currently listening for connections to accept.
Represents a socket for the given platform.
A stream created with dyad_newStream()
or by a listening server when it
accepts a connection. Dyad handles the resources allocated for all streams;
closed streams are automatically destroyed by the dyad_update()
function.
Contains an event's information, a pointer of which is passed to a
dyad_Callback
function when an event occurs. This struct contains the
following fields:
Field | Description |
---|---|
int type | The type of event which was emitted |
void *udata | The udata pointer passed to dyad_addEventListener() |
dyad_Stream *stream | The stream which emitted this event |
dyad_Stream *remote | The client stream when a connection is accepted |
const char *msg | A description of the event or error message |
char *data | The events associated data |
int size | The size in bytes of the event's associated data |
The dyad_Event
struct and the data pointed to by its data
field should
never be modified by an event callback.
An event listener callback function which can be passed to
dyad_addListener()
. The function should be of the following
form:
void func(dyad_Event *event);
An atPanic callback function which can be passed to dyad_atPanic()
. The
function should be of the following form:
void func(const char *message);