wip
Fork of Socket by
Socket.h@11:3d83c348fb8b, 2012-08-01 (annotated)
- Committer:
- emilmont
- Date:
- Wed Aug 01 13:02:32 2012 +0000
- Revision:
- 11:3d83c348fb8b
- Parent:
- 10:d24738f4ef99
- Child:
- 16:2d471deff212
[Socket library] Remove redundant UDPPacket class. Use centralized socket destructor. Add is_connected check.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 3:e6474399e057 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
emilmont | 3:e6474399e057 | 2 | * |
emilmont | 3:e6474399e057 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
emilmont | 3:e6474399e057 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
emilmont | 3:e6474399e057 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
emilmont | 3:e6474399e057 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
emilmont | 3:e6474399e057 | 7 | * furnished to do so, subject to the following conditions: |
emilmont | 3:e6474399e057 | 8 | * |
emilmont | 3:e6474399e057 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
emilmont | 3:e6474399e057 | 10 | * substantial portions of the Software. |
emilmont | 3:e6474399e057 | 11 | * |
emilmont | 3:e6474399e057 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
emilmont | 3:e6474399e057 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
emilmont | 3:e6474399e057 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
emilmont | 3:e6474399e057 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emilmont | 3:e6474399e057 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
emilmont | 3:e6474399e057 | 17 | */ |
emilmont | 3:e6474399e057 | 18 | #ifndef SOCKET_H_ |
emilmont | 3:e6474399e057 | 19 | #define SOCKET_H_ |
emilmont | 3:e6474399e057 | 20 | |
emilmont | 3:e6474399e057 | 21 | #include "lwip/sockets.h" |
emilmont | 3:e6474399e057 | 22 | #include "lwip/netdb.h" |
emilmont | 3:e6474399e057 | 23 | |
emilmont | 3:e6474399e057 | 24 | //DNS |
emilmont | 3:e6474399e057 | 25 | inline struct hostent *gethostbyname(const char *name) { |
emilmont | 3:e6474399e057 | 26 | return lwip_gethostbyname(name); |
emilmont | 3:e6474399e057 | 27 | } |
emilmont | 3:e6474399e057 | 28 | |
emilmont | 3:e6474399e057 | 29 | inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) { |
emilmont | 3:e6474399e057 | 30 | return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop); |
emilmont | 3:e6474399e057 | 31 | } |
emilmont | 3:e6474399e057 | 32 | |
emilmont | 6:cd2e5559786d | 33 | class TimeInterval; |
emilmont | 6:cd2e5559786d | 34 | |
emilmont | 8:9cf9c2d45264 | 35 | /** Socket file descriptor and select wrapper |
emilmont | 8:9cf9c2d45264 | 36 | */ |
emilmont | 3:e6474399e057 | 37 | class Socket { |
emilmont | 3:e6474399e057 | 38 | public: |
emilmont | 8:9cf9c2d45264 | 39 | /** Socket |
emilmont | 8:9cf9c2d45264 | 40 | */ |
emilmont | 3:e6474399e057 | 41 | Socket(); |
emilmont | 3:e6474399e057 | 42 | |
emilmont | 10:d24738f4ef99 | 43 | /** Set blocking or non-blocking mode of the socket and a timeout on |
emilmont | 10:d24738f4ef99 | 44 | blocking socket operations |
emilmont | 10:d24738f4ef99 | 45 | \param blocking true for blocking mode, false for non-blocking mode. |
emilmont | 11:3d83c348fb8b | 46 | \param timeout timeout in ms [Default: (1500)ms]. |
emilmont | 10:d24738f4ef99 | 47 | */ |
emilmont | 10:d24738f4ef99 | 48 | void set_blocking(bool blocking, unsigned int timeout=1500); |
emilmont | 10:d24738f4ef99 | 49 | |
emilmont | 8:9cf9c2d45264 | 50 | /** Close the socket file descriptor |
emilmont | 8:9cf9c2d45264 | 51 | */ |
emilmont | 3:e6474399e057 | 52 | int close(); |
emilmont | 3:e6474399e057 | 53 | |
emilmont | 11:3d83c348fb8b | 54 | ~Socket(); |
emilmont | 11:3d83c348fb8b | 55 | |
emilmont | 3:e6474399e057 | 56 | protected: |
emilmont | 3:e6474399e057 | 57 | int _sock_fd; |
emilmont | 5:300e7ad2dc1d | 58 | int init_socket(int type); |
emilmont | 3:e6474399e057 | 59 | |
emilmont | 6:cd2e5559786d | 60 | int wait_readable(TimeInterval& timeout); |
emilmont | 6:cd2e5559786d | 61 | int wait_writable(TimeInterval& timeout); |
emilmont | 10:d24738f4ef99 | 62 | |
emilmont | 10:d24738f4ef99 | 63 | bool _blocking; |
emilmont | 10:d24738f4ef99 | 64 | unsigned int _timeout; |
emilmont | 10:d24738f4ef99 | 65 | |
emilmont | 3:e6474399e057 | 66 | private: |
emilmont | 6:cd2e5559786d | 67 | int select(struct timeval *timeout, bool read, bool write); |
emilmont | 6:cd2e5559786d | 68 | }; |
emilmont | 6:cd2e5559786d | 69 | |
emilmont | 8:9cf9c2d45264 | 70 | /** Time interval class used to specify timeouts |
emilmont | 8:9cf9c2d45264 | 71 | */ |
emilmont | 6:cd2e5559786d | 72 | class TimeInterval { |
emilmont | 6:cd2e5559786d | 73 | friend class Socket; |
emilmont | 6:cd2e5559786d | 74 | |
emilmont | 6:cd2e5559786d | 75 | public: |
emilmont | 8:9cf9c2d45264 | 76 | /** Time Interval |
emilmont | 8:9cf9c2d45264 | 77 | \param ms time interval expressed in milliseconds |
emilmont | 8:9cf9c2d45264 | 78 | */ |
emilmont | 10:d24738f4ef99 | 79 | TimeInterval(unsigned int ms); |
emilmont | 6:cd2e5559786d | 80 | |
emilmont | 6:cd2e5559786d | 81 | private: |
emilmont | 6:cd2e5559786d | 82 | struct timeval _time; |
emilmont | 3:e6474399e057 | 83 | }; |
emilmont | 3:e6474399e057 | 84 | |
emilmont | 3:e6474399e057 | 85 | #endif /* SOCKET_H_ */ |