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 |
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 |
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 |
int GetFD(Socket socket)
Get underlying file descriptor
PARAMETERS
Socket |
socket |
socket to get fd for |
RETURNS
int |
file-descriptor |
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 |
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 |
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 |
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 |
const char* ResultToString(Result result)
Convert result value to string
PARAMETERS
Result |
result |
Result to convert |
RETURNS
const char* |
Result as string |
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 |
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 |
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. |
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 |
void SelectorZero(Selector* selector)
Clear selector (all kinds). Similar to FD_ZERO
PARAMETERS
Selector* |
selector |
Selector |
RETURNS
void |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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.
TYPE
struct Selector
Selector
Invalid socket handle
Socket default timeout value
Socket type definition
Domain type
DOMAIN_MISSING |
|
DOMAIN_IPV4 |
|
DOMAIN_IPV6 |
|
DOMAIN_UNKNOWN |
Network protocol
PROTOCOL_TCP |
|
PROTOCOL_UDP |
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 |
Selector kind
SELECTOR_KIND_READ |
|
SELECTOR_KIND_WRITE |
|
SELECTOR_KIND_EXCEPT |
Socket shutdown type
SHUTDOWNTYPE_READ |
|
SHUTDOWNTYPE_WRITE |
|
SHUTDOWNTYPE_READWRITE |
Socket type
TYPE_STREAM |
|
TYPE_DGRAM |