Skip to content
Steven Arnow edited this page May 25, 2013 · 4 revisions

DARNIT_SOCKET

DARNIT_SOCKET provides a basic platform-independent wrapper for TCP-sockets. The set of calls is incomplete, and UDP is also planned for some time later.

  • DARNIT_SOCKET *d_socket_connect(const char *host, int port, void (*callback)(int ret, void *data, void *socket), void *data);
  • int d_socket_recv(DARNIT_SOCKET *sock, void *buff, int len);
  • int d_socket_recv_try(DARNIT_SOCKET *sock, void *buff, int len);
  • int d_socket_send(DARNIT_SOCKET *sock, void *buff, int len);
  • DARNIT_SOCKET *d_socket_close(DARNIT_SOCKET *sock);

d_socket_connect

DARNIT_SOCKET *d_socket_connect(const char *host, int port, void (*callback)(int ret, void *data, void *socket), void *data);

Connects to host at port port. This can be done in the background, with a callback being runned when the connection is complete or has failed.

Arguments

  • host - The hostname to connect to. Can also be a string containing the IP-address
  • port - The TCP port to connect to
  • callback - The callback function to run
  • ret - Tells callback whether or not the connection was successful (-1 on fail, 0 on success)
  • data - Any user data that the callback needs
  • socket - The socket that is used for the connection
  • data - Userdata to pass to the callback. Not used by libdarnit.

Return value

NULL on error, anything else is a valid DARNIT_SOCKET.

d_socket_recv

int d_socket_recv(DARNIT_SOCKET *sock, void *buff, int len);

Receives len bytes and puts them into buff. This call is non-blocking.

Arguments

  • sock - You get this handle from d_socket_connect
  • buff - A memory buffer to put the received bytes in
  • len - The number of bytes you want

Return value

Returns -1 on failure, otherwise the number of bytes received.

d_socket_recv_try

int d_socket_recv_try(DARNIT_SOCKET *sock, void *buff, int len);

Attempts to receive a block len bytes long, but returns immediatly if the complete block isn't available. Even if the entire block isn't ready yet, the buffer is modified, as the it is used for a data peek operation, copying what's already available to it.

Arguments

  • sock - You get this handle from d_socket_connect
  • buff - A memory buffer to put the received bytes in
  • len - The number of bytes you want

Return value

  • -1 - Returned on failure
  • 0 - Returned if len bytes hadn't been received yet
  • len - Returned if the requested amount of bytes was available for transfer to the buffer

d_socket_send

int d_socket_send(DARNIT_SOCKET *sock, void *buff, int len);

Attempts to send len bytes, returning the amount of bytes actually copied to the send buffer.

Arguments

  • sock - You get this handle from d_socket_connect
  • buff - A memory buffer with the bytes you want to send
  • len - The number of bytes you want to send

Return value

Returns -1 on error, other values is the number of bytes copied to the send buffer, awaiting to be sent.

d_socket_close

DARNIT_SOCKET *d_socket_close(DARNIT_SOCKET *sock);

Closes a connection and frees up resources used by the socket.

Argument

  • sock - The socket you want to close

Return value

Returns NULL for compact pointer clearing

Clone this wiki locally