A new object oriented network api that can be used to replace the one provided by the EthernetInterface library.

Dependents:   NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more

Object oriented network interface for the mbed platform

Currently implemented:

  • Address
  • Endpoint
  • UDP Socket
  • TCP Socket
  • Databuffer
  • Select API

It depends on the EthernetInterface for the lwip network stack.

Please do not hesitate to contact me with any remarks, improvements or questions.

The API is also available for unix at GitHub: LibNosa

Examples

Committer:
NegativeBlack
Date:
Sat Nov 15 21:35:31 2014 +0000
Revision:
9:7ac7c29fea3d
Parent:
8:cdee0f2b6ff0
Fixed type warnings in address::formString().

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NegativeBlack 5:4b6bc10437cb 1 /**
NegativeBlack 5:4b6bc10437cb 2 * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com>
NegativeBlack 5:4b6bc10437cb 3 * All rights reserved.
NegativeBlack 5:4b6bc10437cb 4 *
NegativeBlack 5:4b6bc10437cb 5 * Redistribution and use in source and binary forms, with or without
NegativeBlack 5:4b6bc10437cb 6 * modification, are permitted provided that the following conditions are met:
NegativeBlack 5:4b6bc10437cb 7 *
NegativeBlack 5:4b6bc10437cb 8 * 1. Redistributions of source code must retain the above copyright notice, this
NegativeBlack 5:4b6bc10437cb 9 * list of conditions and the following disclaimer.
NegativeBlack 5:4b6bc10437cb 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
NegativeBlack 5:4b6bc10437cb 11 * this list of conditions and the following disclaimer in the documentation
NegativeBlack 5:4b6bc10437cb 12 * and/or other materials provided with the distribution.
NegativeBlack 5:4b6bc10437cb 13 *
NegativeBlack 5:4b6bc10437cb 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
NegativeBlack 5:4b6bc10437cb 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
NegativeBlack 5:4b6bc10437cb 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NegativeBlack 5:4b6bc10437cb 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
NegativeBlack 5:4b6bc10437cb 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
NegativeBlack 5:4b6bc10437cb 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
NegativeBlack 5:4b6bc10437cb 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
NegativeBlack 5:4b6bc10437cb 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
NegativeBlack 5:4b6bc10437cb 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
NegativeBlack 5:4b6bc10437cb 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NegativeBlack 5:4b6bc10437cb 24 */
NegativeBlack 5:4b6bc10437cb 25
NegativeBlack 5:4b6bc10437cb 26 #include "select.hpp"
NegativeBlack 5:4b6bc10437cb 27 using namespace network;
NegativeBlack 5:4b6bc10437cb 28
NegativeBlack 5:4b6bc10437cb 29 Select::~Select()
NegativeBlack 5:4b6bc10437cb 30 {
NegativeBlack 5:4b6bc10437cb 31 // Clear fd sets
NegativeBlack 5:4b6bc10437cb 32 FD_ZERO(&this->_read_set);
NegativeBlack 5:4b6bc10437cb 33 FD_ZERO(&this->_write_set);
NegativeBlack 5:4b6bc10437cb 34 }
NegativeBlack 5:4b6bc10437cb 35
NegativeBlack 5:4b6bc10437cb 36 int
NegativeBlack 5:4b6bc10437cb 37 Select::wait(int timeout)
NegativeBlack 5:4b6bc10437cb 38 {
NegativeBlack 5:4b6bc10437cb 39 int max_fd = 0;
NegativeBlack 5:4b6bc10437cb 40 Select::SocketList::iterator entry;
NegativeBlack 5:4b6bc10437cb 41
NegativeBlack 5:4b6bc10437cb 42 // Clear fd sets
NegativeBlack 5:4b6bc10437cb 43 FD_ZERO(&this->_read_set);
NegativeBlack 5:4b6bc10437cb 44 FD_ZERO(&this->_write_set);
NegativeBlack 5:4b6bc10437cb 45
NegativeBlack 5:4b6bc10437cb 46 // Create set for writing
NegativeBlack 5:4b6bc10437cb 47 entry = this->_write_list.begin();
NegativeBlack 5:4b6bc10437cb 48 for (; entry != this->_write_list.end(); entry++) {
NegativeBlack 5:4b6bc10437cb 49 int socket = (*entry)->getHandle();
NegativeBlack 5:4b6bc10437cb 50 if (socket == -1) {
NegativeBlack 5:4b6bc10437cb 51 continue;
NegativeBlack 5:4b6bc10437cb 52 }
NegativeBlack 5:4b6bc10437cb 53
NegativeBlack 5:4b6bc10437cb 54 FD_SET(socket, &this->_write_set);
NegativeBlack 8:cdee0f2b6ff0 55 max_fd = (max_fd > socket)
NegativeBlack 8:cdee0f2b6ff0 56 ? max_fd
NegativeBlack 8:cdee0f2b6ff0 57 : socket;
NegativeBlack 5:4b6bc10437cb 58 }
NegativeBlack 5:4b6bc10437cb 59
NegativeBlack 5:4b6bc10437cb 60 // Create set for reading
NegativeBlack 5:4b6bc10437cb 61 entry = this->_read_list.begin();
NegativeBlack 5:4b6bc10437cb 62 for (; entry != this->_read_list.end(); entry++) {
NegativeBlack 5:4b6bc10437cb 63 int socket = (*entry)->getHandle();
NegativeBlack 5:4b6bc10437cb 64 if (socket == -1) {
NegativeBlack 5:4b6bc10437cb 65 continue;
NegativeBlack 5:4b6bc10437cb 66 }
NegativeBlack 5:4b6bc10437cb 67
NegativeBlack 5:4b6bc10437cb 68 FD_SET(socket, &this->_read_set);
NegativeBlack 8:cdee0f2b6ff0 69 max_fd = (max_fd > socket)
NegativeBlack 8:cdee0f2b6ff0 70 ? max_fd
NegativeBlack 8:cdee0f2b6ff0 71 : socket;
NegativeBlack 5:4b6bc10437cb 72 }
NegativeBlack 5:4b6bc10437cb 73
NegativeBlack 5:4b6bc10437cb 74 // Reset iterators
NegativeBlack 5:4b6bc10437cb 75 this->_write_list_iterator = this->_write_list.begin();
NegativeBlack 5:4b6bc10437cb 76 this->_read_list_iterator = this->_read_list.begin();
NegativeBlack 5:4b6bc10437cb 77
NegativeBlack 5:4b6bc10437cb 78 // Select on the created sets, wait until timeout.
NegativeBlack 5:4b6bc10437cb 79 if (timeout > 0) {
NegativeBlack 5:4b6bc10437cb 80 struct timeval _timeout;
NegativeBlack 5:4b6bc10437cb 81 _timeout.tv_sec = timeout / 1000;
NegativeBlack 5:4b6bc10437cb 82 _timeout.tv_usec = (timeout - (_timeout.tv_sec * 1000)) * 1000;
NegativeBlack 5:4b6bc10437cb 83
NegativeBlack 8:cdee0f2b6ff0 84 return lwip_select(max_fd + 1, &this->_read_set, &this->_write_set, NULL, &_timeout);
NegativeBlack 5:4b6bc10437cb 85 } else {
NegativeBlack 5:4b6bc10437cb 86 // Select on the created sets, wait forever.
NegativeBlack 8:cdee0f2b6ff0 87 return lwip_select(max_fd + 1, &this->_read_set, &this->_write_set, NULL, NULL);
NegativeBlack 5:4b6bc10437cb 88 }
NegativeBlack 5:4b6bc10437cb 89 }
NegativeBlack 5:4b6bc10437cb 90
NegativeBlack 5:4b6bc10437cb 91 void
NegativeBlack 5:4b6bc10437cb 92 Select::clear()
NegativeBlack 5:4b6bc10437cb 93 {
NegativeBlack 5:4b6bc10437cb 94 this->_read_list.clear();
NegativeBlack 5:4b6bc10437cb 95 this->_write_list.clear();
NegativeBlack 5:4b6bc10437cb 96 }
NegativeBlack 5:4b6bc10437cb 97
NegativeBlack 5:4b6bc10437cb 98 void
NegativeBlack 5:4b6bc10437cb 99 Select::set(Socket *socket, enum Mode mode)
NegativeBlack 5:4b6bc10437cb 100 {
NegativeBlack 5:4b6bc10437cb 101 switch (mode) {
NegativeBlack 5:4b6bc10437cb 102 case Select::Read:
NegativeBlack 5:4b6bc10437cb 103 if (this->_inReadList(socket) != this->_read_list.end()) {
NegativeBlack 5:4b6bc10437cb 104 break;
NegativeBlack 5:4b6bc10437cb 105 }
NegativeBlack 5:4b6bc10437cb 106
NegativeBlack 5:4b6bc10437cb 107 this->_read_list.push_back(socket);
NegativeBlack 5:4b6bc10437cb 108 break;
NegativeBlack 5:4b6bc10437cb 109
NegativeBlack 5:4b6bc10437cb 110 case Select::Write:
NegativeBlack 5:4b6bc10437cb 111 if (this->_inWriteList(socket) != this->_write_list.end()) {
NegativeBlack 5:4b6bc10437cb 112 break;
NegativeBlack 5:4b6bc10437cb 113 }
NegativeBlack 5:4b6bc10437cb 114
NegativeBlack 5:4b6bc10437cb 115 this->_write_list.push_back(socket);
NegativeBlack 5:4b6bc10437cb 116 break;
NegativeBlack 5:4b6bc10437cb 117
NegativeBlack 5:4b6bc10437cb 118 case Select::ReadWrite:
NegativeBlack 5:4b6bc10437cb 119 this->set(socket, Select::Read);
NegativeBlack 5:4b6bc10437cb 120 this->set(socket, Select::Write);
NegativeBlack 5:4b6bc10437cb 121 break;
NegativeBlack 5:4b6bc10437cb 122 }
NegativeBlack 5:4b6bc10437cb 123 }
NegativeBlack 5:4b6bc10437cb 124
NegativeBlack 5:4b6bc10437cb 125 void
NegativeBlack 5:4b6bc10437cb 126 Select::unset(Socket *socket, enum Mode mode)
NegativeBlack 5:4b6bc10437cb 127 {
NegativeBlack 5:4b6bc10437cb 128 Select::SocketList::iterator entry;
NegativeBlack 5:4b6bc10437cb 129
NegativeBlack 5:4b6bc10437cb 130 switch (mode) {
NegativeBlack 5:4b6bc10437cb 131 case Select::Read:
NegativeBlack 5:4b6bc10437cb 132 entry = this->_inReadList(socket);
NegativeBlack 5:4b6bc10437cb 133 if (entry == this->_read_list.end()) {
NegativeBlack 5:4b6bc10437cb 134 break;
NegativeBlack 5:4b6bc10437cb 135 }
NegativeBlack 5:4b6bc10437cb 136
NegativeBlack 5:4b6bc10437cb 137 this->_read_list.erase(entry);
NegativeBlack 5:4b6bc10437cb 138 break;
NegativeBlack 5:4b6bc10437cb 139
NegativeBlack 5:4b6bc10437cb 140 case Select::Write:
NegativeBlack 5:4b6bc10437cb 141 entry = this->_inWriteList(socket);
NegativeBlack 5:4b6bc10437cb 142 if (entry == this->_write_list.end()) {
NegativeBlack 5:4b6bc10437cb 143 break;
NegativeBlack 5:4b6bc10437cb 144 }
NegativeBlack 5:4b6bc10437cb 145
NegativeBlack 5:4b6bc10437cb 146 this->_write_list.erase(entry);
NegativeBlack 5:4b6bc10437cb 147 break;
NegativeBlack 5:4b6bc10437cb 148
NegativeBlack 5:4b6bc10437cb 149 case Select::ReadWrite:
NegativeBlack 5:4b6bc10437cb 150 this->unset(socket, Select::Read);
NegativeBlack 5:4b6bc10437cb 151 this->unset(socket, Select::Write);
NegativeBlack 5:4b6bc10437cb 152 break;
NegativeBlack 5:4b6bc10437cb 153 }
NegativeBlack 5:4b6bc10437cb 154 }
NegativeBlack 5:4b6bc10437cb 155
NegativeBlack 5:4b6bc10437cb 156 Socket *
NegativeBlack 5:4b6bc10437cb 157 Select::getWritable()
NegativeBlack 5:4b6bc10437cb 158 {
NegativeBlack 5:4b6bc10437cb 159 if (this->_write_list_iterator == this->_write_list.end()) {
NegativeBlack 5:4b6bc10437cb 160 return NULL;
NegativeBlack 5:4b6bc10437cb 161 }
NegativeBlack 5:4b6bc10437cb 162
NegativeBlack 5:4b6bc10437cb 163 for (; this->_write_list_iterator != this->_write_list.end(); this->_write_list_iterator++) {
NegativeBlack 5:4b6bc10437cb 164 int socket = (*this->_write_list_iterator)->getHandle();
NegativeBlack 5:4b6bc10437cb 165 if (socket == -1) {
NegativeBlack 5:4b6bc10437cb 166 continue;
NegativeBlack 5:4b6bc10437cb 167 }
NegativeBlack 5:4b6bc10437cb 168
NegativeBlack 5:4b6bc10437cb 169 if (FD_ISSET(socket, &this->_write_set)) {
NegativeBlack 5:4b6bc10437cb 170 Select::SocketList::iterator result = this->_write_list_iterator;
NegativeBlack 5:4b6bc10437cb 171 this->_write_list_iterator++;
NegativeBlack 5:4b6bc10437cb 172 return (*result);
NegativeBlack 5:4b6bc10437cb 173 }
NegativeBlack 5:4b6bc10437cb 174 }
NegativeBlack 5:4b6bc10437cb 175
NegativeBlack 5:4b6bc10437cb 176 return NULL;
NegativeBlack 5:4b6bc10437cb 177 }
NegativeBlack 5:4b6bc10437cb 178
NegativeBlack 5:4b6bc10437cb 179 Socket *
NegativeBlack 5:4b6bc10437cb 180 Select::getReadable()
NegativeBlack 5:4b6bc10437cb 181 {
NegativeBlack 5:4b6bc10437cb 182 if (this->_read_list_iterator == this->_read_list.end()) {
NegativeBlack 5:4b6bc10437cb 183 return NULL;
NegativeBlack 5:4b6bc10437cb 184 }
NegativeBlack 5:4b6bc10437cb 185
NegativeBlack 5:4b6bc10437cb 186 for (; this->_read_list_iterator != this->_read_list.end(); this->_read_list_iterator++) {
NegativeBlack 5:4b6bc10437cb 187 int socket = (*this->_read_list_iterator)->getHandle();
NegativeBlack 5:4b6bc10437cb 188 if (socket == -1) {
NegativeBlack 5:4b6bc10437cb 189 continue;
NegativeBlack 5:4b6bc10437cb 190 }
NegativeBlack 5:4b6bc10437cb 191
NegativeBlack 5:4b6bc10437cb 192 if (FD_ISSET(socket, &this->_read_set)) {
NegativeBlack 5:4b6bc10437cb 193 Select::SocketList::iterator result = this->_read_list_iterator;
NegativeBlack 5:4b6bc10437cb 194 this->_read_list_iterator++;
NegativeBlack 5:4b6bc10437cb 195 return (*result);
NegativeBlack 5:4b6bc10437cb 196 }
NegativeBlack 5:4b6bc10437cb 197 }
NegativeBlack 5:4b6bc10437cb 198
NegativeBlack 5:4b6bc10437cb 199 return NULL;
NegativeBlack 5:4b6bc10437cb 200 }
NegativeBlack 5:4b6bc10437cb 201
NegativeBlack 5:4b6bc10437cb 202 Select::SocketList::iterator
NegativeBlack 5:4b6bc10437cb 203 Select::_inWriteList(Socket *socket)
NegativeBlack 5:4b6bc10437cb 204 {
NegativeBlack 5:4b6bc10437cb 205 Select::SocketList::iterator entry = this->_write_list.begin();
NegativeBlack 5:4b6bc10437cb 206 for (; entry != this->_write_list.end(); entry++) {
NegativeBlack 5:4b6bc10437cb 207 if ((*entry) == socket) {
NegativeBlack 5:4b6bc10437cb 208 return entry;
NegativeBlack 5:4b6bc10437cb 209 }
NegativeBlack 5:4b6bc10437cb 210 }
NegativeBlack 5:4b6bc10437cb 211
NegativeBlack 5:4b6bc10437cb 212 return this->_write_list.end();
NegativeBlack 5:4b6bc10437cb 213 }
NegativeBlack 5:4b6bc10437cb 214
NegativeBlack 5:4b6bc10437cb 215 Select::SocketList::iterator
NegativeBlack 5:4b6bc10437cb 216 Select::_inReadList(Socket *socket)
NegativeBlack 5:4b6bc10437cb 217 {
NegativeBlack 5:4b6bc10437cb 218 Select::SocketList::iterator entry = this->_read_list.begin();
NegativeBlack 5:4b6bc10437cb 219 for (; entry != this->_read_list.end(); entry++) {
NegativeBlack 5:4b6bc10437cb 220 if ((*entry) == socket) {
NegativeBlack 5:4b6bc10437cb 221 return entry;
NegativeBlack 5:4b6bc10437cb 222 }
NegativeBlack 5:4b6bc10437cb 223 }
NegativeBlack 5:4b6bc10437cb 224
NegativeBlack 5:4b6bc10437cb 225 return this->_read_list.end();
NegativeBlack 5:4b6bc10437cb 226 }