Skip to content

Commit aa2357a

Browse files
committed
Revisited documentation for Socket API
1 parent 0ae11b4 commit aa2357a

File tree

5 files changed

+260
-94
lines changed

5 files changed

+260
-94
lines changed

NetworkInterface.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Socket
1+
/* NetworkInterface
22
* Copyright (c) 2015 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -134,8 +134,8 @@ class NetworkInterface
134134

135135
/** Opens a socket
136136
*
137-
* Creates a socket for communication and stores it in the specified
138-
* handle. The handle must be passed to following calls on the socket.
137+
* Creates a network socket and stores it in the specified handle.
138+
* The handle must be passed to following calls on the socket.
139139
*
140140
* A stack may have a finite number of sockets, in this case
141141
* NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
@@ -148,8 +148,8 @@ class NetworkInterface
148148

149149
/** Close the socket
150150
*
151-
* Closes any open connection and deallocates any memory associated with
152-
* the socket.
151+
* Closes any open connection and deallocates any memory associated
152+
* with the socket.
153153
*
154154
* @param handle Socket handle
155155
* @return 0 on success, negative error code on failure
@@ -174,7 +174,7 @@ class NetworkInterface
174174
*
175175
* @param handle Socket handle
176176
* @param backlog Number of pending connections that can be queued
177-
* simultaneously, defaults to 1
177+
* simultaneously
178178
* @return 0 on success, negative error code on failure
179179
*/
180180
virtual int socket_listen(void *handle, int backlog) = 0;
@@ -193,8 +193,9 @@ class NetworkInterface
193193
/** Accepts a connection on a TCP socket
194194
*
195195
* The server socket must be bound and set to listen for connections.
196-
* On a new connection, creates a socket stores it in the specified
197-
* handle. The handle must be passed to following calls on the socket.
196+
* On a new connection, creates a network socket and stores it in the
197+
* specified handle. The handle must be passed to following calls on
198+
* the socket.
198199
*
199200
* A stack may have a finite number of sockets, in this case
200201
* NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
@@ -289,7 +290,7 @@ class NetworkInterface
289290
*/
290291
virtual void socket_attach(void *handle, void (*callback)(void *), void *data) = 0;
291292

292-
/* Set stack-specific socked options
293+
/* Set stack-specific socket options
293294
*
294295
* The setsockopt allow an application to pass stack-specific hints
295296
* to the underlying stack. For unsupported options,

Socket.h

Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,74 +24,135 @@
2424
*/
2525
class Socket {
2626
public:
27-
/** Socket lifetime
27+
/** Destroy a socket
28+
*
29+
* Closes socket if the socket is still open
2830
*/
2931
virtual ~Socket();
3032

31-
/** Open the socket
32-
* @param iface Interface to open socket on
33+
/** Opens a socket
34+
*
35+
* Creates a network socket on the specified network stack.
36+
* Not needed if stack is passed to the socket's constructor.
37+
*
38+
* @param iface Network stack as target for socket
39+
* @return 0 on success, negative error code on failure
3340
*/
3441
virtual int open(NetworkInterface *iface) = 0;
3542

3643
/** Close the socket
44+
*
45+
* Closes any open connection and deallocates any memory associated
46+
* with the socket. Called from destructor if socket is not closed.
47+
*
48+
* @return 0 on success, negative error code on failure
3749
*/
3850
int close();
3951

40-
/** Bind a socket to a specific port
41-
* @param port The port to listen for incoming connections on
42-
* @return 0 on success, negative on failure.
52+
/** Bind a specific address to a socket
53+
*
54+
* Binding a socket specifies the address and port on which to recieve
55+
* data.
56+
*
57+
* @param port Local port to bind
58+
* @return 0 on success, negative error code on failure.
4359
*/
4460
int bind(uint16_t port);
4561

46-
/** Bind a socket to a specific port
47-
* @param address The null-terminated address to listen for incoming connections on
48-
* @param port The port to listen for incoming connections on
49-
* @return 0 on success, negative on failure.
62+
/** Bind a specific address to a socket
63+
*
64+
* Binding a socket specifies the address and port on which to recieve
65+
* data. If the IP address is zeroed, only the port is bound.
66+
*
67+
* @param address Null-terminated local address to bind
68+
* @param port Local port to bind
69+
* @return 0 on success, negative error code on failure.
5070
*/
5171
int bind(const char *address, uint16_t port);
5272

53-
/** Bind a socket to a specific port
54-
* @param address The SocketAddress to listen for incoming connections on
55-
* @return 0 on success, negative on failure.
73+
/** Bind a specific address to a socket
74+
*
75+
* Binding a socket specifies the address and port on which to recieve
76+
* data. If the IP address is zeroed, only the port is bound.
77+
*
78+
* @param address Local address to bind
79+
* @return 0 on success, negative error code on failure.
5680
*/
5781
int bind(const SocketAddress &address);
5882

5983
/** Set blocking or non-blocking mode of the socket
60-
* @param blocking true for blocking mode, false for non-blocking mode.
84+
*
85+
* Initially all sockets are in blocking mode. In non-blocking mode
86+
* blocking operations such as send/recv/accept return
87+
* NSAPI_ERROR_WOULD_BLOCK if they can not continue.
88+
*
89+
* @param blocking True for blocking mode, false for non-blocking mode.
6190
*/
6291
void set_blocking(bool blocking);
6392

64-
/** Set timeout on a socket operation if blocking behaviour is enabled
65-
* @param timeout timeout in ms
93+
/** Set timeout on blocking socket operations
94+
*
95+
* Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK
96+
* is returned if a blocking operation takes longer than the specified
97+
* timeout. A timeout of 0 removes a timeout from the socket.
98+
*
99+
* @param timeout Timeout in milliseconds
66100
*/
67101
void set_timeout(unsigned int timeout);
68102

69-
/* Set socket options
70-
* @param level Option level
71-
* @param optname Option identifier
103+
/* Set stack-specific socket options
104+
*
105+
* The setsockopt allow an application to pass stack-specific hints
106+
* to the underlying stack. For unsupported options,
107+
* NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
108+
*
109+
* @param level Stack-specific protocol level
110+
* @param optname Stack-specific option identifier
72111
* @param optval Option value
73112
* @param optlen Length of the option value
74-
* @return 0 on success, negative on failure
113+
* @return 0 on success, negative error code on failure
75114
*/
76115
int setsockopt(int level, int optname, const void *optval, unsigned optlen);
77116

78-
/* Get socket options
79-
* @param level Option level
80-
* @param optname Option identifier
81-
* @param optval Buffer where to write option value
117+
/* Get stack-specific socket options
118+
*
119+
* The getstackopt allow an application to retrieve stack-specific hints
120+
* from the underlying stack. For unsupported options,
121+
* NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
122+
*
123+
* @param level Stack-specific protocol level
124+
* @param optname Stack-specific option identifier
125+
* @param optval Destination for option value
82126
* @param optlen Length of the option value
83-
* @return 0 on success, negative on failure
127+
* @return 0 on success, negative error code on failure
84128
*/
85129
int getsockopt(int level, int optname, void *optval, unsigned *optlen);
86130

87131
/** Register a callback on state change of the socket
132+
*
133+
* The specified callback will be called on state changes such as when
134+
* the socket can recv/send/accept successfully and on when an error
135+
* occurs. The callback may also be called spuriously without reason.
136+
*
137+
* The callback may be called in an interrupt context and should not
138+
* perform expensive operations such as recv/send calls.
139+
*
88140
* @param callback Function to call on state change
89-
* @note Callback may be called in an interrupt context.
90-
* The callback should not perform long operations
91-
* such as recv or send calls.
92141
*/
93142
void attach(FunctionPointer callback);
94143

144+
/** Register a callback on state change of the socket
145+
*
146+
* The specified callback will be called on state changes such as when
147+
* the socket can recv/send/accept successfully and on when an error
148+
* occurs. The callback may also be called spuriously without reason.
149+
*
150+
* The callback may be called in an interrupt context and should not
151+
* perform expensive operations such as recv/send calls.
152+
*
153+
* @param tptr Pointer to object to call method on
154+
* @param mptr Method to call on state change
155+
*/
95156
template <typename T, typename M>
96157
void attach(T *tptr, M mptr) {
97158
attach(FunctionPointer(tptr, mptr));

TCPServer.h

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,57 @@
2121
#include "TCPSocket.h"
2222
#include "NetworkInterface.h"
2323

24-
/** TCP Server.
24+
/** TCP socket server
2525
*/
2626
class TCPServer : public Socket {
2727
public:
28-
/** TCP Server lifetime
28+
/** Create an uninitialized socket
29+
*
30+
* Must call open to initialize the socket on a network stack.
2931
*/
3032
TCPServer();
33+
34+
/** Create a socket on a network stack
35+
*
36+
* Creates and opens a socket on the specified network stack.
37+
*
38+
* @param iface Network stack as target for socket
39+
*/
3140
TCPServer(NetworkInterface *iface);
32-
virtual ~TCPServer();
3341

34-
/** Open the socket
35-
* @param iface Interface to open socket on
42+
/** Opens a socket
43+
*
44+
* Creates a network socket on the specified network stack.
45+
* Not needed if stack is passed to the socket's constructor.
46+
*
47+
* @param iface Network stack as target for socket
48+
* @return 0 on success, negative error code on failure
3649
*/
3750
virtual int open(NetworkInterface *iface);
3851

39-
/** Start listening for incoming connections
40-
* @param backlog Number of pending connections that can be queued up at any
41-
* one time [Default: 1]
42-
* @return 0 on success, negative on failure
52+
/** Listen for connections on a TCP socket
53+
*
54+
* Marks the socket as a passive socket that can be used to accept
55+
* incoming connections.
56+
*
57+
* @param backlog Number of pending connections that can be queued
58+
* simultaneously, defaults to 1
59+
* @return 0 on success, negative error code on failure
4360
*/
44-
int listen(int backlog=1);
61+
int listen(int backlog = 1);
4562

46-
/** Accept a new connection.
47-
* @param socket A TCPSocket instance that will handle the incoming connection.
48-
* @return 0 on success, negative on failure.
63+
/** Accepts a connection on a TCP socket
64+
*
65+
* The server socket must be bound and set to listen for connections.
66+
* On a new connection, creates a network socket using the specified
67+
* socket instance.
68+
*
69+
* By default, accept blocks until data is sent. If socket is set to
70+
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
71+
* immediately.
72+
*
73+
* @param socket TCPSocket instance that will handle the incoming connection.
74+
* @return 0 on success, negative error code on failure
4975
*/
5076
int accept(TCPSocket *connection);
5177
};

TCPSocket.h

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,80 @@
2424
*/
2525
class TCPSocket : public Socket {
2626
public:
27-
/** TCP socket lifetime
27+
/** Create an uninitialized socket
28+
*
29+
* Must call open to initialize the socket on a network stack.
2830
*/
2931
TCPSocket();
32+
33+
/** Create a socket on a network stack
34+
*
35+
* Creates and opens a socket on the specified network stack.
36+
*
37+
* @param iface Network stack as target for socket
38+
*/
3039
TCPSocket(NetworkInterface *iface);
3140

32-
/** Open the socket
33-
* @param iface Interface to open socket on
41+
/** Opens a socket
42+
*
43+
* Creates a network socket on the specified network stack.
44+
* Not needed if stack is passed to the socket's constructor.
45+
*
46+
* @param iface Network stack as target for socket
47+
* @return 0 on success, negative error code on failure
3448
*/
3549
virtual int open(NetworkInterface *iface);
36-
37-
/** Connects this TCP socket to the server
38-
* @param host The host to connect to. It can either be an IP Address
39-
* or a hostname that will be resolved with DNS
40-
* @param port The host's port to connect to
41-
* @return 0 on success, negative on failure
50+
51+
/** Connects TCP socket to a remote host
52+
*
53+
* Initiates a connection to a remote server specified by either
54+
* a domain name or an IP address and a port.
55+
*
56+
* @param host Host name of the remote host
57+
* @param port Port of the remote host
58+
* @return 0 on success, negative error code on failure
4259
*/
4360
int connect(const char *host, uint16_t port);
4461

45-
/** Connects this TCP socket to the server
46-
* @param address SocketAddress to connect to
47-
* @return 0 on success, negative on failure
62+
/** Connects TCP socket to a remote host
63+
*
64+
* Initiates a connection to a remote server specified by the
65+
* indicated address.
66+
*
67+
* @param address The SocketAddress of the remote host
68+
* @return 0 on success, negative error code on failure
4869
*/
4970
int connect(const SocketAddress &address);
5071

51-
/** Send data to the remote host
52-
* @param data The buffer to send to the host
53-
* @param size The length of the buffer to send
54-
* @return Number of written bytes on success, negative on failure
72+
/** Send data over a TCP socket
73+
*
74+
* The socket must be connected to a remote host. Returns the number of
75+
* bytes sent from the buffer.
76+
*
77+
* By default, send blocks until data is sent. If socket is set to
78+
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
79+
* immediately.
80+
*
81+
* @param data Buffer of data to send to the host
82+
* @param size Size of the buffer in bytes
83+
* @return Number of sent bytes on success, negative error
84+
* code on failure
5585
*/
5686
int send(const void *data, unsigned size);
5787

58-
/** Receive data from the remote host
59-
* @param data The buffer in which to store the data received from the host
60-
* @param size The maximum length of the buffer
61-
* @return Number of received bytes on success, negative on failure
88+
/** Receive data over a TCP socket
89+
*
90+
* The socket must be connected to a remote host. Returns the number of
91+
* bytes received into the buffer.
92+
*
93+
* By default, recv blocks until data is sent. If socket is set to
94+
* non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
95+
* immediately.
96+
*
97+
* @param data Destination buffer for data received from the host
98+
* @param size Size of the buffer in bytes
99+
* @return Number of received bytes on success, negative error
100+
* code on failure
62101
*/
63102
int recv(void *data, unsigned size);
64103

0 commit comments

Comments
 (0)