Component TCPSERVER
Component index
Section index

Component TCPSERVER


Search

Type

Object

Summary

Multi-client TCP server

Description

Works standalone and with inetd. This is useful for line oriented protocol. It manages several clients at once using a single process. It can also server unix domain socket. See the prototype.

void closeclient (int handle)void endclient (int no, bool &endserver, TCPSERVER_INFO &info)
int getnbclients ()void idle (int since, bool &endserver, TCPSERVER_INFO &info)
void inject (int handle, ARRAY_OBJ *data)void newclient (int no, unsigned long from, bool &endclient, bool &endserver, TCPSERVER_INFO &info)
void inject (int handle)void receive (int no, const char *line, bool &endclient, bool &endserver, int &state, TCPSERVER_INFO &info)
void inject_output ()void time_out (int no, bool &endserver, TCPSERVER_INFO &info)
bool islistening (int handle)
int listen (const char *port)
int listen (const char *bindaddr, const char *port)
int listen (int port)
void loop ()
int sendall (const void *buf, int len)
int sendall (const char *buf)
int sendallf (const char *ctl, ...)
int sendto (int cli, const char *data)
int sendto (int cli, const void *data, int len)
int sendtof (int cli, const void *ctl, ...)
bool setlisten (int handle, bool on)
void setmaxclients (int maxclient)
bool setnonblock (bool mode, int bufsize)
bool setrawmode (bool mode)

Prototypes

TCPSERVER ()

This version takes no argument. Initially the component does nothing. It just waits for listen() or inject() calls to learn which handle to monitor.

TCPSERVER (const char *bindaddr, const char *port, int timeout)

TCPSERVER (int port, int timeout)

TCPSERVER (const char *port, int timeout)

It can serve either TCP socket, or unix domain socket (use the unix: prefix)

Methods

void closeclient (int handle)

Close one connection

int getnbclients ()

Return the number of clients currently connected

void inject (int handle, ARRAY_OBJ *data)

Request the TCPSERVER to manage one socket

The socket was created/accepted using other means, but we want the TCPSERVER to handle it from now on.

void inject (int handle)

Request the TCPSERVER to manage one socket

The socket was created/accepted using other means, but we want the TCPSERVER to handle it from now on.

void inject_output ()

bool islistening (int handle)

Tells if a socket is currently enabled

setlisten lets you turn a socket on and off (input wise).

int listen (const char *port)

Ask the TCPSERVER to listen on another port

Return -1 if the port can't be opened. If the port starts with "unix:", the rest is taken as the path of a unix domain socket. TCPSERVER may listen on several socket, including TCP and Unix all at once.

int listen (const char *bindaddr, const char *port)

Ask the TCPSERVER to listen on another port

Return -1 if the port can't be opened. If the port starts with "unix:", the rest is taken as the path of a unix domain socket. TCPSERVER may listen on several socket, including TCP and Unix all at once. For Unix domain socket, bindaddr is not used.

int listen (int port)

Ask the TCPSERVER to listen on another port

Return -1 if the port can't be opened. If the port starts with "unix:", the rest is taken as the path of a unix domain socket. TCPSERVER may listen on several socket, including TCP and Unix all at once.

void loop ()

Give control to the object so it can process connections

int sendall (const void *buf, int len)

Send a buffer to all connected client

The same buffer will be sent to all client. The function return -1 if any "write" fail.

int sendall (const char *buf)

Send a string to all connected client

The same string will be sent to all client. The function return -1 if any "write" fail.

int sendallf (const char *ctl, ...)

Send a formatted (like printf) string to all connected client

The same string will be sent to all client. The function return -1 if any "write" fail.

int sendto (int cli, const char *data)

Send some data to one client

The client must be handled by this TCPSERVER

int sendto (int cli, const void *data, int len)

Send some data to one client

The client must be handled by this TCPSERVER

int sendtof (int cli, const void *ctl, ...)

Send some data to one client

The client must be handled by this TCPSERVER

bool setlisten (int handle, bool on)

Stop listening on this connection

You generally use this to prevent more request to be queued while you know the output might be stopped (blocked).

void setmaxclients (int maxclient)

Record the maximum number of client manageable

If more clients connects, they are rejected. This is the size of the handle table, so this is not exactly the maximum number of client, but the largest file handle.

bool setnonblock (bool mode, int bufsize)

Turn on and off nonblocking mode

When you turn non-blocking, the various send* fhelper won't block. They will always return the total amount of bytes. Extra bytes not wrtten to the socket are buffered.

bool setrawmode (bool mode)

Sets the default operation mode

When mode is true, inputs from clients are processed as binary raw data. When mode is false, inputs is processed as text lines.

Functags

void endclient (int no, bool &endserver, TCPSERVER_INFO &info) Optional

Called when a client disconnect

void idle (int since, bool &endserver, TCPSERVER_INFO &info) Optional

Called when no activity has occured for some time (loop method parameter)

void newclient (int no, unsigned long from, bool &endclient, bool &endserver, TCPSERVER_INFO &info) Mandatory

Called whenever a new client connects

void receive (int no, const char *line, bool &endclient, bool &endserver, int &state, TCPSERVER_INFO &info) Mandatory

Called when one line of text is received from one client

void time_out (int no, bool &endserver, TCPSERVER_INFO &info) Optional

Called when a client has ellapsed its timeout

endclient is also called, after time_out.

Fhelpers

void closeclient (int cli)

Close the connection to a client

endclient won't be called.

void forgetclient ()

Stop managing this connection

The connection is still alive, but TCPSERVER will forget about it. Useful when you pass the connection to a thread for example.

int getnbclients ()

Get the number of clients currently connected

int getnbpending ()

How many socket have data waiting to be processed.

void inject (int handle, ARRAY_OBJ *data)

Ask the TCPSERVER to manage a socket

The socket will be managed exactly like it was accepted by the TCPSERVER. In general, application will establish a new connection and pass control over to the TCPSERVER.

void inject_output (int handle)

Ask the TCPSERVER to manage only the output of a socket/handle

This is useful in non-blocking mode. This allows the usage of the sendto() helper on socket not accepted by the TCPSERVER.

bool is_blocked ()

Return true if the output of the current connection is blocked

bool is_blocked (int handle)

Return true if the output of the connection is blocked

bool islistening (int handle)

Tells if the socket is currently enabled

setlisten lets you turn a socket on and off (input wise).

int iter_init ()

Iterate over all connections

Starts the iteration over all connected client, returning the handle of each client and optionally the application stored data (info.data). iter_init() returns the first connection and iter_next() returns the rest. Both function returns -1 if there is no more connections.

int iter_init (void *&data)

Iterate over all connections

Starts the iteration over all connected client, returning the handle of each client and optionally the application stored data (info.data). iter_init() returns the first connection and iter_next() returns the rest. Both function returns -1 if there is no more connections.

int iter_next ()

Returns the next connection

This is called in a loop after iter_init.

int iter_next (void *&data)

Returns the next connection

This is called in a loop after iter_init.

int send (const char *buf)

Send a string to the current client

Return the number of byte sent or -1 if any error.

int send (const void *buf, int len)

Send a buffer to the current client

Return the number of bytes sent or -1 if any error.

int sendall (const void *buf, int len)

Send a buffer to all connected client

The same buffer is sent to all connected client and -1 is returned whenever one write fails.

int sendall (const char *buf)

Send a string to all connected client

The same string is sent to all connected client and -1 is returned whenever one write fails.

int sendallf (const char *ctl, ...)

Send a formatted string to all connected client using printf syntax

The same string is sent to all connected client and -1 is returned whenever one write fails.

int sendf (const char *ctl, ...)

Send a format string using a printf syntax

Return the number of bytes sent or -1 if any error.

int sendto (int cli, const void *data, int len)

Send a packet to a specific client (handle)

This handle is (must be) managed by TCPSERVER.

int sendto (int cli, const char *data)

Send a packet to a specific client (handle)

This handle is (must be) managed by TCPSERVER.

int sendtof (int cli, const char *ctl, ...)

Send a format string using a printf syntax to a client

Return the number of bytes sent or -1 if any error.

void set_timeout (int nbseconds)

Set the timeout for the current connection

Each client may have a different timeout. If the timeout is reached the time_out functag will be called. The time_out will then decide what to do.

bool setlisten (int handle, bool on)

Stop listening on this connection

You generally use this to prevent more request to be queued while you know the output might be stopped (blocked).

bool setrawmode (bool mode)

Sets the operation mode for the current client

In raw mode, bytes read are passed to the receive functag as is. The info.linelen variable is used to obtain the lenght of the data in line. In rawmode==false (default), data is processed as ASCII lines

void settcpnodelay (bool on)

Set the TCP_NODELAY flag on the current connection