Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.cpp Source File

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), _blocking(true), _timeout(1500) {
00024     
00025 }
00026 
00027 void Socket::set_blocking(bool blocking, unsigned int timeout) {
00028     _blocking = blocking;
00029     _timeout = timeout;
00030 }
00031 
00032 int Socket::init_socket(int type) {
00033     if (_sock_fd != -1)
00034         return -1;
00035     
00036     int fd = lwip_socket(AF_INET, type, 0);
00037     if (fd < 0)
00038         return -1;
00039     
00040     _sock_fd = fd;
00041     return 0;
00042 }
00043 
00044 int Socket::select(struct timeval *timeout, bool read, bool write) {
00045     fd_set fdSet;
00046     FD_ZERO(&fdSet);
00047     FD_SET(_sock_fd, &fdSet);
00048     
00049     fd_set* readset  = (read ) ? (&fdSet) : (NULL);
00050     fd_set* writeset = (write) ? (&fdSet) : (NULL);
00051     
00052     int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
00053     return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
00054 }
00055 
00056 int Socket::wait_readable(TimeInterval& timeout) {
00057     return select(&timeout._time, true, false);
00058 }
00059 
00060 int Socket::wait_writable(TimeInterval& timeout) {
00061     return select(&timeout._time, false, true);
00062 }
00063 
00064 int Socket::close() {
00065     if (_sock_fd < 0)
00066         return -1;
00067     
00068     lwip_close(_sock_fd);
00069     _sock_fd = -1;
00070     
00071     return 0;
00072 }
00073 
00074 Socket::~Socket() {
00075     close(); //Don't want to leak
00076 }
00077 
00078 TimeInterval::TimeInterval(unsigned int ms) {
00079     _time.tv_sec = ms / 1000;
00080     _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
00081 }