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