Component TCPCONNECT
Component index
Section index

Component TCPCONNECT


Search

Type

Object

Summary

Manage a single client TCP session

Description

It establish the connection, calls its init functag when successful and process the data received line by line. Supports both TCP and Unix domain sockets

int close ()void end (TCPCONNECT &info)
int connect ()void fail (TCPCONNECT_INFO &info)
bool is_ok ()void init (bool &end, TCPCONNECT_INFO &info)
int loop (int timeout)void oneline (const char *line, bool &end, TCPCONNECT_INFO &info)
int poll ()void time_out (bool &end, TCPCONNECT &info)
int reconnect ()
int send (const char *str)
int send (const void *buf, int len)
int sendf (const char *ctl, ...)
void set_timeout (int seconds)
bool setrawmode (bool mode)

Prototypes

TCPCONNECT (const char *host, const char *port)

If the host is "unix:", the port is the unix domain socket path.

Methods

int close ()

Close the connection

Return -1 if any error. The end functag will be called.

int connect ()

Try to establish the connection if it is not already connected

This method is seldom used as TCPCONNECT handle it all by itself.

bool is_ok ()

Return true if the connection is currently established

int loop (int timeout)

Gives control to the object so it manages the connection

This function will end only when the session ends.

int poll ()

Allow the object to check for inputs and end of session

The input is process and the function returns immediatly. It returns -1 if any error, 0 otherwise.

int reconnect ()

Try to re-establish the connection

Return -1 if it failed

int send (const char *str)

Send a string to the server

Return the number of bytes sent or -1 if any error. This method may trigger the end() functag if the server has closed the connection.

int send (const void *buf, int len)

Send some bytes to the server

Return the number of bytes sent or -1 if any error. This method may trigger the end() functag if the server has closed the connection.

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

Send a string formatted using printf syntax

Return the number of bytes sent or -1 if any error. This method may trigger the end() functag if the server has closed the connection.

void set_timeout (int seconds)

Set the timeout (seconds) used for the next network operation

At connect time, a timeout leads to the fail functag. Later, the time_out functag is called and the application decides if it wants to end the session.

bool setrawmode (bool mode)

Turn the processing from line oriented ot binary

Functags

void end (TCPCONNECT &info) Optional

Called when the connection ends

void fail (TCPCONNECT_INFO &info) Optional

Called when the connection failed

void init (bool &end, TCPCONNECT_INFO &info) Optional

Called when the connection is established

You generally used this to send the first request and do various initialisation.

void oneline (const char *line, bool &end, TCPCONNECT_INFO &info) Mandatory

Called when one complete line is received

This assumes that the protocol is line oriented (ASCII). In raw mode (binary) the info.linelen field may be used to learn how many bytes are pointed by the "line" pointer.

void time_out (bool &end, TCPCONNECT &info) Optional

Is called when the specified timeout has ellapsed

No message was received for some time. The timeout is specified as an argument of the tcpconnect function.

Fhelpers

int send (const char *s)

Send a string to the server

Return the number of bytes sent or -1 if any error. This method may trigger the end() functag if the server has closed the connection.

int send (const char *buf, int len)

Send some characters to the server

Return the number of bytes sent or -1 if any error. This method may trigger the end() functag if the server has closed the connection.

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

Send some bytes formatted using a printf syntax

Return the number of bytes sent or -1 if any error. This method may trigger the end() functag if the server has closed the connection.

void set_timeout (int seconds)

Set the timeout (seconds) used for the next network operation

At connect time, a timeout leads to the fail functag. Later, the time_out functag is called and the application decides if it wants to end the session.

void set_timeout (int seconds, int usec)

Set the timeout (seconds,micro-seconds) used for the next network operation

At connect time, a timeout leads to the fail functag. Later, the time_out functag is called and the application decides if it wants to end the session.

bool setrawmode (bool mode)

Turn the processing from line oriented to binary mode

By default the object assumes line oriented protocol.

void settcpnodelay (bool on)

Control the TCP_NODELAY flag of the connection

Normally, the OS sends packets only when the transmit buffer is full, after some delay, or when the program waits for the other side. This is efficient network wise. Sometime you expect the data to leave immediatly (when you call send()). This helper lets you set the flag to on or off. It is off by default