Free (GPLv2) TCP/IP stack developed by TASS Belgium

Dependents:   lpc1768-picotcp-demo ZeroMQ_PicoTCP_Publisher_demo TCPSocket_HelloWorld_PicoTCP Pico_TCP_UDP_Test ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.h Source File

Socket.h

00001 /* 
00002  *
00003  * PicoTCP Socket interface for mbed.
00004  * Copyright (C) 2013 TASS Belgium NV
00005  * 
00006  * Released under GPL v2
00007  *
00008  * Other licensing models might apply at the sole discretion of the copyright holders.
00009  *
00010  *
00011  * This software is based on the mbed.org EthernetInterface implementation:
00012  * Copyright (C) 2012 mbed.org, MIT License
00013  *
00014  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00015  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00016  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00017  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00018  * furnished to do so, subject to the following conditions:
00019  *
00020  * The above copyright notice and this permission notice shall be included in all copies or
00021  * substantial portions of the Software.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00024  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00025  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00026  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00028  */
00029 
00030 #ifndef SOCKET_H_
00031 #define SOCKET_H_
00032 
00033 #ifdef __cplusplus
00034 extern "C" {
00035 #endif
00036     #include "pico_dns_client.h"
00037 #ifdef __cplusplus
00038 }
00039 #endif
00040 
00041 #include "wrapper.h"
00042 #include "proxy_endpoint.h"
00043 
00044 class TimeInterval;
00045 
00046 /** Socket file descriptor and select wrapper
00047   */
00048 class Socket {
00049 public:
00050     /** Socket
00051      */
00052     Socket();
00053     
00054     /** Set blocking or non-blocking mode of the socket and a timeout on
00055         blocking socket operations
00056     \param blocking  true for blocking mode, false for non-blocking mode.
00057     \param timeout   timeout in ms [Default: (1500)ms].
00058     */
00059     void set_blocking(bool blocking, unsigned int timeout=1500);
00060     
00061     /** Set socket options
00062     \param level     stack level - parameter is ignored (kept for compatibility)
00063     \param optname   option ID - please check pico_socket.h
00064     \param optval    option value
00065     \param socklen_t length of the option value
00066     \return 0 on success, -1 on failure
00067     */
00068     int set_option(int level, int optname, const void *optval, socklen_t optlen);
00069     
00070     /** Get socket options
00071         \param level     stack level - parameter is ignored (kept for compatibility)
00072         \param optname   option ID - please check pico_socket.h or lower in the page
00073         \param optval    buffer pointer where to write the option value
00074         \param socklen_t length of the option value
00075         \return 0 on success, -1 on failure
00076         */
00077     int get_option(int level, int optname, void *optval, socklen_t *optlen);
00078     
00079     /** Close the socket file descriptor
00080      */
00081     int close();
00082     
00083     ~Socket();
00084     
00085 protected:
00086     struct stack_endpoint *_ep;
00087     int init_socket(int type);
00088     
00089     int wait_readable(TimeInterval& timeout);
00090     int wait_writable(TimeInterval& timeout);
00091 
00092     int is_readable(void);
00093     int is_writable(void);
00094     
00095     bool _blocking;
00096     pico_time _timeout;
00097     
00098 private:
00099     int select(struct timeval *timeout, bool read, bool write);
00100 };
00101 
00102 /** Time interval class used to specify timeouts
00103  */
00104 class TimeInterval {
00105     friend class Socket;
00106 
00107 public:
00108     /** Time Interval
00109      \param ms time interval expressed in milliseconds
00110       */
00111     TimeInterval(unsigned int ms);
00112     
00113 private:
00114     struct timeval _time;
00115     struct stack_endpoint * _ep;
00116 };
00117 
00118 inline struct hostent *gethostbyname(const char *name) { return picotcp_gethostbyname(name); }
00119 /* DNS 
00120 inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) {
00121   return picotcp_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
00122 }*/
00123 #endif /* SOCKET_H_ */