Sdk socket api documentation

Socket functions.

Namespace: dmSocket
Include: #include <dmsdk/dlib/socket.h>
TYPES
Socket Socket type definition
CONSTANTS
INVALID_SOCKET_HANDLE Invalid socket handle
SOCKET_TIMEOUT Socket default timeout value
ENUMS
Domain domain type
Protocol network protocol
Result socket result
SelectorKind Selector kind
ShutdownType socket shutdown type
Type socket type
STRUCTS
struct Address network address
struct Selector Selector
FUNCTIONS
Result Connect(Socket socket, Address address, int port) make a connection
Result Delete(Socket socket) delete a socket
int GetFD(Socket socket) get underlying file descriptor
Result GetHostByName(const char* name, Address* address, bool ipv4, bool ipv6) get host by name
Result GetHostByName(const char* name, Address* address, uint64_t timeout, int* cancelflag, bool ipv4, bool ipv6) get host by name with timeout and cancelability
Result New(Type type, Protocol protocol, Socket* socket) create a socket
Result Receive(Socket socket, void* buffer[out], int length, int* received_bytes[out]) Receive data on a socket
const char* ResultToString(Result result) Convert result value to string
Result Select(Selector* selector, int timeout) Select for pending data
void SelectorClear(Selector* selector, SelectorKind selector_kind, Socket socket) Clear selector for socket. Similar to FD_CLR
bool SelectorIsSet(Selector* selector, SelectorKind selector_kind, Socket socket) Check if selector is set. Similar to FD_ISSET
void SelectorSet(Selector* selector, SelectorKind selector_kind, Socket socket) Set selector for socket. Similar to FD_SET
void SelectorZero(Selector* selector) Clear selector (all kinds). Similar to FD_ZERO
Result Send(Socket socket, void* buffer, int length, int* sent_bytes[out]) Send a message on a socket
Result SetBlocking(Socket socket, bool blocking) Set blocking option on a socket
Result SetBroadcast(Socket socket, bool broadcast) Set broadcast address option on socket. Socket opt...
Result SetNoDelay(Socket socket, bool no_delay) Set TCP_NODELAY on socket
Result SetQuickAck(Socket socket, bool use_quick_ack) Set TCP_QUICKACK on socket
Result SetReceiveTimeout(Socket socket, uint64_t timeout) Set socket receive timeout
Result SetReuseAddress(Socket socket, bool reuse) Set reuse socket address option on socket. Socket ...
Result SetSendTimeout(Socket socket, uint64_t timeout) Set socket send timeout
Result Shutdown(Socket socket, ShutdownType how) close socket

Functions

Connect

Result Connect(Socket socket, Address address, int port)

Initiate a connection on a socket

PARAMETERS

Socket socket Socket to initiate connection on
Address address Address to connect to
int port Port to connect to

RETURNS

Result RESULT_OK on success

Delete

Result Delete(Socket socket)

Delete a socket. Corresponds to BSD socket function close()

PARAMETERS

Socket socket Socket to close

RETURNS

Result RESULT_OK on success

GetFD

int GetFD(Socket socket)

Get underlying file descriptor

PARAMETERS

Socket socket socket to get fd for

RETURNS

int file-descriptor

GetHostByName

Result GetHostByName(const char* name, Address* address, bool ipv4, bool ipv6)

Get host by name

PARAMETERS

const char* name Hostname to resolve
Address* address Host address result
bool ipv4 Whether or not to search for IPv4 addresses
bool ipv6 Whether or not to search for IPv6 addresses

RETURNS

Result RESULT_OK on success

GetHostByName

Result GetHostByName(const char* name, Address* address, uint64_t timeout, int* cancelflag, bool ipv4, bool ipv6)

Get host by name with timeout and cancelability

PARAMETERS

const char* name Hostname to resolve
Address* address Host address result
uint64_t timeout Timeout in microseconds
int* cancelflag if non null and set, will abort the call
bool ipv4 Whether or not to search for IPv4 addresses
bool ipv6 Whether or not to search for IPv6 addresses

RETURNS

Result RESULT_OK on success

New

Result New(Type type, Protocol protocol, Socket* socket)

Create a new socket. Corresponds to BSD socket function socket().

PARAMETERS

Type type Socket type
Protocol protocol Protocol
Socket* socket Pointer to socket

RETURNS

Result RESULT_OK on succcess

Receive

Result Receive(Socket socket, void* buffer[out], int length, int* received_bytes[out])

Receive data on a socket

PARAMETERS

Socket socket Socket to receive data on
void* buffer[out] Buffer to receive to
int length Receive buffer length
int* received_bytes[out] Number of received bytes (result)

RETURNS

Result RESULT_OK on success

ResultToString

const char* ResultToString(Result result)

Convert result value to string

PARAMETERS

Result result Result to convert

RETURNS

const char* Result as string

Select

Result Select(Selector* selector, int timeout)

Select for pending data

PARAMETERS

Selector* selector Selector
int timeout Timeout. For blocking pass -1. (microseconds)

RETURNS

Result RESULT_OK on success

SelectorClear

void SelectorClear(Selector* selector, SelectorKind selector_kind, Socket socket)

Clear selector for socket. Similar to FD_CLR

PARAMETERS

Selector* selector Selector
SelectorKind selector_kind Kind to clear
Socket socket Socket to clear

RETURNS

void

SelectorIsSet

bool SelectorIsSet(Selector* selector, SelectorKind selector_kind, Socket socket)

Check if selector is set. Similar to FD_ISSET

PARAMETERS

Selector* selector Selector
SelectorKind selector_kind Selector kind
Socket socket Socket to check for

RETURNS

bool True if set.

SelectorSet

void SelectorSet(Selector* selector, SelectorKind selector_kind, Socket socket)

Set selector for socket. Similar to FD_SET

PARAMETERS

Selector* selector Selector
SelectorKind selector_kind Kind to clear
Socket socket Socket to set

RETURNS

void

SelectorZero

void SelectorZero(Selector* selector)

Clear selector (all kinds). Similar to FD_ZERO

PARAMETERS

Selector* selector Selector

RETURNS

void

Send

Result Send(Socket socket, void* buffer, int length, int* sent_bytes[out])

Send a message on a socket

PARAMETERS

Socket socket Socket to send a message on
void* buffer Buffer to send
int length Length of buffer to send
int* sent_bytes[out] Number of bytes sent (result)

RETURNS

Result RESULT_OK on success

SetBlocking

Result SetBlocking(Socket socket, bool blocking)

Set blocking option on a socket

PARAMETERS

Socket socket Socket to set blocking on
bool blocking True to block

RETURNS

Result RESULT_OK on success

SetBroadcast

Result SetBroadcast(Socket socket, bool broadcast)

Set broadcast address option on socket. Socket option SO_BROADCAST on most platforms.

PARAMETERS

Socket socket Socket to set reuse address to
bool broadcast True if broadcast

RETURNS

Result RESULT_OK on success

SetNoDelay

Result SetNoDelay(Socket socket, bool no_delay)

Set TCP_NODELAY on socket

PARAMETERS

Socket socket Socket to set TCP_NODELAY on
bool no_delay True for no delay

RETURNS

Result RESULT_OK on success

SetQuickAck

Result SetQuickAck(Socket socket, bool use_quick_ack)

Set TCP_QUICKACK on socket

PARAMETERS

Socket socket Socket to set TCP_QUICKACK on
bool use_quick_ack False to disable quick ack

RETURNS

Result RESULT_OK on success

SetReceiveTimeout

Result SetReceiveTimeout(Socket socket, uint64_t timeout)

Set socket receive timeout

PARAMETERS

Socket socket socket
uint64_t timeout timeout in microseconds

RETURNS

Result RESULT_OK on success

SetReuseAddress

Result SetReuseAddress(Socket socket, bool reuse)

Set reuse socket address option on socket. Socket option SO_REUSEADDR on most platforms

PARAMETERS

Socket socket Socket to set reuse address to
bool reuse True if reuse

RETURNS

Result RESULT_OK on success

SetSendTimeout

Result SetSendTimeout(Socket socket, uint64_t timeout)

Set socket send timeout

PARAMETERS

Socket socket socket
uint64_t timeout timeout in microseconds

RETURNS

Result RESULT_OK on success

Shutdown

Result Shutdown(Socket socket, ShutdownType how)

Shutdown part of a socket connection

PARAMETERS

Socket socket Socket to shutdown connection ow
ShutdownType how Shutdown type

RETURNS

Result RESULT_OK on success

Structs

Address

TYPE

struct Address

Network addresses were previously represented as an uint32_t, but in order to support IPv6 the internal representation was changed to a struct.


Selector

TYPE

struct Selector

Selector


Constants

INVALID_SOCKET_HANDLE

Invalid socket handle


SOCKET_TIMEOUT

Socket default timeout value


Types

Socket

Socket type definition


Enums

Domain

Domain type

DOMAIN_MISSING
DOMAIN_IPV4
DOMAIN_IPV6
DOMAIN_UNKNOWN

Protocol

Network protocol

PROTOCOL_TCP
PROTOCOL_UDP

Result

Socket result

RESULT_OK 0
RESULT_ACCES -1
RESULT_AFNOSUPPORT -2
RESULT_WOULDBLOCK -3
RESULT_BADF -4
RESULT_CONNRESET -5
RESULT_DESTADDRREQ -6
RESULT_FAULT -7
RESULT_HOSTUNREACH -8
RESULT_INTR -9
RESULT_INVAL -10
RESULT_ISCONN -11
RESULT_MFILE -12
RESULT_MSGSIZE -13
RESULT_NETDOWN -14
RESULT_NETUNREACH -15
RESULT_NOBUFS -17
RESULT_NOTCONN -20
RESULT_NOTSOCK -22
RESULT_OPNOTSUPP -23
RESULT_PIPE -24
RESULT_PROTONOSUPPORT -25
RESULT_PROTOTYPE -26
RESULT_TIMEDOUT -27
RESULT_ADDRNOTAVAIL -28
RESULT_CONNREFUSED -29
RESULT_ADDRINUSE -30
RESULT_CONNABORTED -31
RESULT_INPROGRESS -32
RESULT_HOST_NOT_FOUND -100
RESULT_TRY_AGAIN -101
RESULT_NO_RECOVERY -102
RESULT_NO_DATA -103
RESULT_UNKNOWN -1000

SelectorKind

Selector kind

SELECTOR_KIND_READ
SELECTOR_KIND_WRITE
SELECTOR_KIND_EXCEPT

ShutdownType

Socket shutdown type

SHUTDOWNTYPE_READ
SHUTDOWNTYPE_WRITE
SHUTDOWNTYPE_READWRITE

Type

Socket type

TYPE_STREAM
TYPE_DGRAM