mbed socket API
Dependents: EthernetInterface EthernetInterface_RSF EthernetInterface EthernetInterface ... more
Socket.cpp
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 #include "Socket/Socket.h" 00019 #include <cstring> 00020 00021 using std::memset; 00022 00023 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) { 00024 00025 } 00026 00027 void Socket::set_blocking(bool blocking, unsigned int timeout) { 00028 _blocking = blocking; 00029 _timeout = timeout; 00030 } 00031 00032 int Socket::init_socket(int type) { 00033 if (_sock_fd != -1) 00034 return -1; 00035 00036 int fd = lwip_socket(AF_INET, type, 0); 00037 if (fd < 0) 00038 return -1; 00039 00040 _sock_fd = fd; 00041 return 0; 00042 } 00043 00044 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) { 00045 return lwip_setsockopt(_sock_fd, level, optname, optval, optlen); 00046 } 00047 00048 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) { 00049 return lwip_getsockopt(_sock_fd, level, optname, optval, optlen); 00050 } 00051 00052 int Socket::select(struct timeval *timeout, bool read, bool write) { 00053 fd_set fdSet; 00054 FD_ZERO(&fdSet); 00055 FD_SET(_sock_fd, &fdSet); 00056 00057 fd_set* readset = (read ) ? (&fdSet) : (NULL); 00058 fd_set* writeset = (write) ? (&fdSet) : (NULL); 00059 00060 int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout); 00061 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0); 00062 } 00063 00064 int Socket::wait_readable(TimeInterval& timeout) { 00065 return select(&timeout._time, true, false); 00066 } 00067 00068 int Socket::wait_writable(TimeInterval& timeout) { 00069 return select(&timeout._time, false, true); 00070 } 00071 00072 int Socket::close(bool shutdown) { 00073 if (_sock_fd < 0) 00074 return -1; 00075 00076 if (shutdown) 00077 lwip_shutdown(_sock_fd, SHUT_RDWR); 00078 lwip_close(_sock_fd); 00079 _sock_fd = -1; 00080 00081 return 0; 00082 } 00083 00084 Socket::~Socket() { 00085 close(); //Don't want to leak 00086 } 00087 00088 TimeInterval::TimeInterval(unsigned int ms) { 00089 _time.tv_sec = ms / 1000; 00090 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000; 00091 }
Generated on Tue Jul 12 2022 11:05:10 by 1.7.2