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