mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

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 #include "Socket/Socket.h"
elessair 0:f269e3021894 19 #include <cstring>
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 using std::memset;
elessair 0:f269e3021894 22
elessair 0:f269e3021894 23 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
elessair 0:f269e3021894 24
elessair 0:f269e3021894 25 }
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 void Socket::set_blocking(bool blocking, unsigned int timeout) {
elessair 0:f269e3021894 28 _blocking = blocking;
elessair 0:f269e3021894 29 _timeout = timeout;
elessair 0:f269e3021894 30 }
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 int Socket::init_socket(int type) {
elessair 0:f269e3021894 33 if (_sock_fd != -1)
elessair 0:f269e3021894 34 return -1;
elessair 0:f269e3021894 35
elessair 0:f269e3021894 36 int fd = lwip_socket(AF_INET, type, 0);
elessair 0:f269e3021894 37 if (fd < 0)
elessair 0:f269e3021894 38 return -1;
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 _sock_fd = fd;
elessair 0:f269e3021894 41 return 0;
elessair 0:f269e3021894 42 }
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
elessair 0:f269e3021894 45 return lwip_setsockopt(_sock_fd, level, optname, optval, optlen);
elessair 0:f269e3021894 46 }
elessair 0:f269e3021894 47
elessair 0:f269e3021894 48 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
elessair 0:f269e3021894 49 return lwip_getsockopt(_sock_fd, level, optname, optval, optlen);
elessair 0:f269e3021894 50 }
elessair 0:f269e3021894 51
elessair 0:f269e3021894 52 int Socket::select(struct timeval *timeout, bool read, bool write) {
elessair 0:f269e3021894 53 fd_set fdSet;
elessair 0:f269e3021894 54 FD_ZERO(&fdSet);
elessair 0:f269e3021894 55 FD_SET(_sock_fd, &fdSet);
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 fd_set* readset = (read ) ? (&fdSet) : (NULL);
elessair 0:f269e3021894 58 fd_set* writeset = (write) ? (&fdSet) : (NULL);
elessair 0:f269e3021894 59
elessair 0:f269e3021894 60 int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
elessair 0:f269e3021894 61 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
elessair 0:f269e3021894 62 }
elessair 0:f269e3021894 63
elessair 0:f269e3021894 64 int Socket::wait_readable(TimeInterval& timeout) {
elessair 0:f269e3021894 65 return select(&timeout._time, true, false);
elessair 0:f269e3021894 66 }
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 int Socket::wait_writable(TimeInterval& timeout) {
elessair 0:f269e3021894 69 return select(&timeout._time, false, true);
elessair 0:f269e3021894 70 }
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 int Socket::close(bool shutdown) {
elessair 0:f269e3021894 73 if (_sock_fd < 0)
elessair 0:f269e3021894 74 return -1;
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 if (shutdown)
elessair 0:f269e3021894 77 lwip_shutdown(_sock_fd, SHUT_RDWR);
elessair 0:f269e3021894 78 lwip_close(_sock_fd);
elessair 0:f269e3021894 79 _sock_fd = -1;
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 return 0;
elessair 0:f269e3021894 82 }
elessair 0:f269e3021894 83
elessair 0:f269e3021894 84 Socket::~Socket() {
elessair 0:f269e3021894 85 close(); //Don't want to leak
elessair 0:f269e3021894 86 }
elessair 0:f269e3021894 87
elessair 0:f269e3021894 88 TimeInterval::TimeInterval(unsigned int ms) {
elessair 0:f269e3021894 89 _time.tv_sec = ms / 1000;
elessair 0:f269e3021894 90 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
elessair 0:f269e3021894 91 }