Networking layer

The basic networking layer with batching support is implemented in multiple parts:

  • tnt_net.c - client-side layer
  • tnt_io.c - network abstraction layer
  • tnt_iob.c - network buffer layer
  • tnt_opt.c - network options layer

Creating a connection

struct tnt_stream *tnt_net(struct tnt_stream *s)

Create a tarantool connection object. If s is NULL, then allocate memory for it.

Return NULL if can’t allocate memory.

Handling errors

Possible error codes:

TNT_EOK

Not an error.

TNT_EFAIL

Failed to parse URI, bad protocol for sockets, or bad configuration option for the tnt_set() function.

TNT_EMEMORY

Memory allocation failed.

TNT_ESYSTEM

System error, _errno will be set. Acquire it with the tnt_errno() function.

TNT_EBIG

Read/write fragment is too big (in case the send/read buffer is smaller than the fragment you’re trying to write into/read from).

TNT_ESIZE

Buffer size is incorrect.

TNT_ERESOLVE

Failed to resolve the hostname (the function gethostbyname(2)() failed).

TNT_ETMOUT

Timeout was reached during connect/read/write operations.

TNT_EBADVAL

Currently unused.

TNT_ELOGIN

Authentication error.

TNT_LAST

Pointer to the final element of an enumerated data structure (enum).

Functions to work with errors:

enum tnt_error tnt_error(struct tnt_stream *s)

Return the error code of the last stream operation.

char *tnt_strerror(struct tnt_stream *s)

Format the error as a string message. In case the error code is TNT_ESYSTEM, then the driver uses the system function strerror() to format the message.

int tnt_errno(struct tnt_stream *s)

Return the errno_ of the last stream operation (in case the error code is TNT_ESYSTEM).

Manipulating a connection

int tnt_set(struct tnt_stream *s, int opt, ...)

You can set the following options for a connection:

  • TNT_OPT_URI (const char *) - URI for connecting to tarantool.
  • TNT_OPT_TMOUT_CONNECT (struct timeval *) - timeout on connecting.
  • TNT_OPT_TMOUT_SEND (struct timeval *) - timeout on sending.
  • TNT_OPT_SEND_CB (ssize_t (*send_cb_t)(struct tnt_iob *b, void *buf, size_t len)) - a function to be called instead of writing into a socket; uses the buffer buf which is len bytes long.
  • TNT_OPT_SEND_CBV (ssize_t (*sendv_cb_t)(struct tnt_iob *b, const struct iovec *iov, int iov_count)) - a function to be called instead of writing into a socket; uses multiple (iov_count) buffers passed in iov.
  • TNT_OPT_SEND_BUF (int) - the maximum size (in bytes) of the buffer for outgoing messages.
  • TNT_OPT_SEND_CB_ARG (void *) - context for “send” callbacks.
  • TNT_OPT_RECV_CB (ssize_t (*recv_cb_t)(struct tnt_iob *b, void *buf, size_t len)) - a function to be called instead of reading from a socket; uses the buffer buf which is len bytes long.
  • TNT_OPT_RECV_BUF (int) - the maximum size (in bytes) of the buffer for incoming messages.
  • TNT_OPT_RECV_CB_ARG (void *) - context for “receive” callbacks.

Return -1 and store the error in the stream. The error code can be either TNT_EFAIL if can’t parse the URI or opt is not defined, or TNT_EMEMORY if failed to allocate memory for the URI.

int tnt_connect(struct tnt_stream *s)

Connect to tarantool with preconfigured and allocated settings.

Return -1 in the following cases:

  • Can’t connect
  • Can’t read greeting
  • Can’t authenticate (if login/password was provided with the URI)
  • OOM while authenticating and getting schema
  • Can’t parse schema
void tnt_close(struct tnt_stream *s)

Close connection to tarantool.

ssize_t tnt_flush(struct tnt_stream *s)

Flush all buffered data to the socket.

Return -1 in case of network error.

int tnt_fd(struct tnt_stream *s)

Return the file descriptor of the connection.

int tnt_reload_schema(struct tnt_stream *s)

Reload the schema from server. Delete the old schema and download/parse a new schema from server.

See also “Working with a schema”.

int32_t tnt_get_spaceno(struct tnt_stream *s, const char *space, size_t space_len)
int32_t tnt_get_indexno(struct tnt_stream *s, int space, const char *index, size_t index_len)

Get space/index number from their names. For tnt_get_indexno(), specify the length of the space name (in bytes) in space_len. For tnt_get_indexno(), specify the space ID number in space and the length of the index name (in bytes) in index_len.

Freeing a connection

Use the tnt_stream_free() function to free a connection object.