mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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