|
24 | 24 | */
|
25 | 25 | class Socket {
|
26 | 26 | public:
|
27 |
| - /** Socket lifetime |
| 27 | + /** Destroy a socket |
| 28 | + * |
| 29 | + * Closes socket if the socket is still open |
28 | 30 | */
|
29 | 31 | virtual ~Socket();
|
30 | 32 |
|
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 |
33 | 40 | */
|
34 | 41 | virtual int open(NetworkInterface *iface) = 0;
|
35 | 42 |
|
36 | 43 | /** 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 |
37 | 49 | */
|
38 | 50 | int close();
|
39 | 51 |
|
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. |
43 | 59 | */
|
44 | 60 | int bind(uint16_t port);
|
45 | 61 |
|
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. |
50 | 70 | */
|
51 | 71 | int bind(const char *address, uint16_t port);
|
52 | 72 |
|
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. |
56 | 80 | */
|
57 | 81 | int bind(const SocketAddress &address);
|
58 | 82 |
|
59 | 83 | /** 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. |
61 | 90 | */
|
62 | 91 | void set_blocking(bool blocking);
|
63 | 92 |
|
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 |
66 | 100 | */
|
67 | 101 | void set_timeout(unsigned int timeout);
|
68 | 102 |
|
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 |
72 | 111 | * @param optval Option value
|
73 | 112 | * @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 |
75 | 114 | */
|
76 | 115 | int setsockopt(int level, int optname, const void *optval, unsigned optlen);
|
77 | 116 |
|
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 |
82 | 126 | * @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 |
84 | 128 | */
|
85 | 129 | int getsockopt(int level, int optname, void *optval, unsigned *optlen);
|
86 | 130 |
|
87 | 131 | /** 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 | + * |
88 | 140 | * @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. |
92 | 141 | */
|
93 | 142 | void attach(FunctionPointer callback);
|
94 | 143 |
|
| 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 | + */ |
95 | 156 | template <typename T, typename M>
|
96 | 157 | void attach(T *tptr, M mptr) {
|
97 | 158 | attach(FunctionPointer(tptr, mptr));
|
|
0 commit comments