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