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