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.
Fork of Socket by
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) { 00024 00025 } 00026 00027 int Socket::init_socket(int type) { 00028 if (_sock_fd != -1) 00029 return -1; 00030 00031 int fd = lwip_socket(AF_INET, type, 0); 00032 if (fd < 0) 00033 return -1; 00034 00035 _sock_fd = fd; 00036 return 0; 00037 } 00038 00039 int Socket::select(struct timeval *timeout, bool read, bool write) { 00040 if ((timeout->tv_sec == 0) && (timeout->tv_usec == 0)) 00041 return 0; 00042 00043 fd_set fdSet; 00044 FD_ZERO(&fdSet); 00045 FD_SET(_sock_fd, &fdSet); 00046 00047 fd_set* readset = (read ) ? (&fdSet) : (NULL); 00048 fd_set* writeset = (write) ? (&fdSet) : (NULL); 00049 00050 int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout); 00051 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0); 00052 } 00053 00054 int Socket::wait_readable(TimeInterval& timeout) { 00055 return select(&timeout._time, true, false); 00056 } 00057 00058 int Socket::wait_writable(TimeInterval& timeout) { 00059 return select(&timeout._time, false, true); 00060 } 00061 00062 int Socket::shutdown(int how) 00063 { 00064 if (_sock_fd < 0) 00065 return -1; 00066 00067 return lwip_shutdown(_sock_fd,how); 00068 } 00069 00070 int Socket::close() { 00071 if (_sock_fd < 0) 00072 return -1; 00073 00074 lwip_close(_sock_fd); 00075 _sock_fd = -1; 00076 00077 return 0; 00078 } 00079 00080 TimeInterval::TimeInterval(int ms) { 00081 _time.tv_sec = ms / 1000; 00082 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000; 00083 }
Generated on Wed Jul 27 2022 19:46:00 by
