fort Socket

Fork of Socket by mbed official

Committer:
emilmont
Date:
Wed Aug 01 13:02:32 2012 +0000
Revision:
11:3d83c348fb8b
Parent:
10:d24738f4ef99
Child:
16:2d471deff212
[Socket library] Remove redundant UDPPacket class. Use centralized socket destructor. Add is_connected check.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 3:e6474399e057 1 /* Copyright (C) 2012 mbed.org, MIT License
emilmont 3:e6474399e057 2 *
emilmont 3:e6474399e057 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
emilmont 3:e6474399e057 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
emilmont 3:e6474399e057 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
emilmont 3:e6474399e057 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
emilmont 3:e6474399e057 7 * furnished to do so, subject to the following conditions:
emilmont 3:e6474399e057 8 *
emilmont 3:e6474399e057 9 * The above copyright notice and this permission notice shall be included in all copies or
emilmont 3:e6474399e057 10 * substantial portions of the Software.
emilmont 3:e6474399e057 11 *
emilmont 3:e6474399e057 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
emilmont 3:e6474399e057 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
emilmont 3:e6474399e057 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
emilmont 3:e6474399e057 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emilmont 3:e6474399e057 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
emilmont 3:e6474399e057 17 */
emilmont 3:e6474399e057 18 #include "Socket/Socket.h"
emilmont 3:e6474399e057 19 #include <cstring>
emilmont 3:e6474399e057 20
emilmont 3:e6474399e057 21 using std::memset;
emilmont 3:e6474399e057 22
emilmont 11:3d83c348fb8b 23 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
emilmont 3:e6474399e057 24
emilmont 3:e6474399e057 25 }
emilmont 3:e6474399e057 26
emilmont 10:d24738f4ef99 27 void Socket::set_blocking(bool blocking, unsigned int timeout) {
emilmont 10:d24738f4ef99 28 _blocking = blocking;
emilmont 10:d24738f4ef99 29 _timeout = timeout;
emilmont 10:d24738f4ef99 30 }
emilmont 10:d24738f4ef99 31
emilmont 5:300e7ad2dc1d 32 int Socket::init_socket(int type) {
emilmont 3:e6474399e057 33 if (_sock_fd != -1)
emilmont 3:e6474399e057 34 return -1;
emilmont 3:e6474399e057 35
emilmont 3:e6474399e057 36 int fd = lwip_socket(AF_INET, type, 0);
emilmont 3:e6474399e057 37 if (fd < 0)
emilmont 3:e6474399e057 38 return -1;
emilmont 3:e6474399e057 39
emilmont 3:e6474399e057 40 _sock_fd = fd;
emilmont 3:e6474399e057 41 return 0;
emilmont 3:e6474399e057 42 }
emilmont 3:e6474399e057 43
emilmont 6:cd2e5559786d 44 int Socket::select(struct timeval *timeout, bool read, bool write) {
emilmont 6:cd2e5559786d 45 fd_set fdSet;
emilmont 6:cd2e5559786d 46 FD_ZERO(&fdSet);
emilmont 6:cd2e5559786d 47 FD_SET(_sock_fd, &fdSet);
emilmont 3:e6474399e057 48
emilmont 6:cd2e5559786d 49 fd_set* readset = (read ) ? (&fdSet) : (NULL);
emilmont 6:cd2e5559786d 50 fd_set* writeset = (write) ? (&fdSet) : (NULL);
emilmont 6:cd2e5559786d 51
emilmont 6:cd2e5559786d 52 int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
emilmont 6:cd2e5559786d 53 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
emilmont 3:e6474399e057 54 }
emilmont 3:e6474399e057 55
emilmont 6:cd2e5559786d 56 int Socket::wait_readable(TimeInterval& timeout) {
emilmont 6:cd2e5559786d 57 return select(&timeout._time, true, false);
emilmont 3:e6474399e057 58 }
emilmont 3:e6474399e057 59
emilmont 6:cd2e5559786d 60 int Socket::wait_writable(TimeInterval& timeout) {
emilmont 6:cd2e5559786d 61 return select(&timeout._time, false, true);
emilmont 3:e6474399e057 62 }
emilmont 3:e6474399e057 63
emilmont 3:e6474399e057 64 int Socket::close() {
emilmont 3:e6474399e057 65 if (_sock_fd < 0)
emilmont 3:e6474399e057 66 return -1;
emilmont 3:e6474399e057 67
emilmont 3:e6474399e057 68 lwip_close(_sock_fd);
emilmont 3:e6474399e057 69 _sock_fd = -1;
emilmont 3:e6474399e057 70
emilmont 3:e6474399e057 71 return 0;
emilmont 3:e6474399e057 72 }
emilmont 6:cd2e5559786d 73
emilmont 11:3d83c348fb8b 74 Socket::~Socket() {
emilmont 11:3d83c348fb8b 75 close(); //Don't want to leak
emilmont 11:3d83c348fb8b 76 }
emilmont 11:3d83c348fb8b 77
emilmont 10:d24738f4ef99 78 TimeInterval::TimeInterval(unsigned int ms) {
emilmont 6:cd2e5559786d 79 _time.tv_sec = ms / 1000;
emilmont 6:cd2e5559786d 80 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
emilmont 6:cd2e5559786d 81 }