]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/net/lwip/Socket/Socket.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / net / lwip / Socket / Socket.h
1 /* Copyright (C) 2012 mbed.org, MIT License
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 */
18 #ifndef SOCKET_H_
19 #define SOCKET_H_
20
21 #include "lwip/sockets.h"
22 #include "lwip/netdb.h"
23
24 //DNS
25 inline struct hostent *gethostbyname(const char *name) {
26 return lwip_gethostbyname(name);
27 }
28
29 inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) {
30 return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
31 }
32
33 class TimeInterval;
34
35 /** Socket file descriptor and select wrapper
36 */
37 class Socket {
38 public:
39 /** Socket
40 */
41 Socket();
42
43 /** Set blocking or non-blocking mode of the socket and a timeout on
44 blocking socket operations
45 \param blocking true for blocking mode, false for non-blocking mode.
46 \param timeout timeout in ms [Default: (1500)ms].
47 */
48 void set_blocking(bool blocking, unsigned int timeout=1500);
49
50 /** Set socket options
51 \param level stack level (see: lwip/sockets.h)
52 \param optname option ID
53 \param optval option value
54 \param socklen_t length of the option value
55 \return 0 on success, -1 on failure
56 */
57 int set_option(int level, int optname, const void *optval, socklen_t optlen);
58
59 /** Get socket options
60 \param level stack level (see: lwip/sockets.h)
61 \param optname option ID
62 \param optval buffer pointer where to write the option value
63 \param socklen_t length of the option value
64 \return 0 on success, -1 on failure
65 */
66 int get_option(int level, int optname, void *optval, socklen_t *optlen);
67
68 /** Close the socket
69 \param shutdown free the left-over data in message queues
70 */
71 int close(bool shutdown=true);
72
73 ~Socket();
74
75 protected:
76 int _sock_fd;
77 int init_socket(int type);
78
79 int wait_readable(TimeInterval& timeout);
80 int wait_writable(TimeInterval& timeout);
81
82 bool _blocking;
83 unsigned int _timeout;
84
85 private:
86 int select(struct timeval *timeout, bool read, bool write);
87 };
88
89 /** Time interval class used to specify timeouts
90 */
91 class TimeInterval {
92 friend class Socket;
93
94 public:
95 /** Time Interval
96 \param ms time interval expressed in milliseconds
97 */
98 TimeInterval(unsigned int ms);
99
100 private:
101 struct timeval _time;
102 };
103
104 #endif /* SOCKET_H_ */
Imprint / Impressum