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:
Wed Jul 18 13:20:32 2012 +0000
Revision:
4:d854fa394f85
Parent:
3:d30db8752485
Child:
7:9796742904fa
Implemented buffer class, some scaffolding for a TCP server. And fixed some bugs in the TCP socket.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NegativeBlack 4:d854fa394f85 1 /**
NegativeBlack 4:d854fa394f85 2 * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com>
NegativeBlack 4:d854fa394f85 3 * All rights reserved.
NegativeBlack 4:d854fa394f85 4 *
NegativeBlack 4:d854fa394f85 5 * Redistribution and use in source and binary forms, with or without
NegativeBlack 4:d854fa394f85 6 * modification, are permitted provided that the following conditions are met:
NegativeBlack 4:d854fa394f85 7 *
NegativeBlack 4:d854fa394f85 8 * 1. Redistributions of source code must retain the above copyright notice, this
NegativeBlack 4:d854fa394f85 9 * list of conditions and the following disclaimer.
NegativeBlack 4:d854fa394f85 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
NegativeBlack 4:d854fa394f85 11 * this list of conditions and the following disclaimer in the documentation
NegativeBlack 4:d854fa394f85 12 * and/or other materials provided with the distribution.
NegativeBlack 4:d854fa394f85 13 *
NegativeBlack 4:d854fa394f85 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
NegativeBlack 4:d854fa394f85 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
NegativeBlack 4:d854fa394f85 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NegativeBlack 4:d854fa394f85 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
NegativeBlack 4:d854fa394f85 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
NegativeBlack 4:d854fa394f85 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
NegativeBlack 4:d854fa394f85 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
NegativeBlack 4:d854fa394f85 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
NegativeBlack 4:d854fa394f85 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
NegativeBlack 4:d854fa394f85 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NegativeBlack 4:d854fa394f85 24 */
NegativeBlack 4:d854fa394f85 25
NegativeBlack 4:d854fa394f85 26 #include "buffer.hpp"
NegativeBlack 4:d854fa394f85 27 using namespace network;
NegativeBlack 4:d854fa394f85 28
NegativeBlack 4:d854fa394f85 29 Buffer::Buffer():
NegativeBlack 4:d854fa394f85 30 _memory(NULL), _size(0), _length(0)
NegativeBlack 4:d854fa394f85 31 {}
NegativeBlack 4:d854fa394f85 32
NegativeBlack 4:d854fa394f85 33 Buffer::Buffer(const size_t size):
NegativeBlack 4:d854fa394f85 34 _size(size), _length(0)
NegativeBlack 4:d854fa394f85 35 {
NegativeBlack 4:d854fa394f85 36 this->_memory_allocate(size);
NegativeBlack 4:d854fa394f85 37 }
NegativeBlack 4:d854fa394f85 38
NegativeBlack 4:d854fa394f85 39 Buffer::Buffer(const Buffer &other):
NegativeBlack 4:d854fa394f85 40 _size(other._size), _length(other._length)
NegativeBlack 4:d854fa394f85 41 {
NegativeBlack 4:d854fa394f85 42 if (this->_memory_allocate(other._size) != -1) {
NegativeBlack 4:d854fa394f85 43 std::memcpy(this->_memory, other._memory, other._size);
NegativeBlack 4:d854fa394f85 44 }
NegativeBlack 4:d854fa394f85 45 }
NegativeBlack 4:d854fa394f85 46
NegativeBlack 4:d854fa394f85 47 Buffer::~Buffer()
NegativeBlack 4:d854fa394f85 48 {
NegativeBlack 4:d854fa394f85 49 this->_memory_free();
NegativeBlack 4:d854fa394f85 50 }
NegativeBlack 4:d854fa394f85 51
NegativeBlack 4:d854fa394f85 52 Buffer &
NegativeBlack 4:d854fa394f85 53 Buffer::operator=(const Buffer &other)
NegativeBlack 4:d854fa394f85 54 {
NegativeBlack 4:d854fa394f85 55 this->_memory_free();
NegativeBlack 4:d854fa394f85 56 if (this->_memory_allocate(other._size) < 0) {
NegativeBlack 4:d854fa394f85 57 return (*this);
NegativeBlack 4:d854fa394f85 58 }
NegativeBlack 4:d854fa394f85 59
NegativeBlack 4:d854fa394f85 60 std::memcpy(this->_memory, other._memory, other._size);
NegativeBlack 4:d854fa394f85 61 this->_size = other._size;
NegativeBlack 4:d854fa394f85 62 this->_length = other._length;
NegativeBlack 4:d854fa394f85 63
NegativeBlack 4:d854fa394f85 64 return (*this);
NegativeBlack 4:d854fa394f85 65 }
NegativeBlack 4:d854fa394f85 66
NegativeBlack 4:d854fa394f85 67 int
NegativeBlack 4:d854fa394f85 68 Buffer::read(void *data, size_t max_length, size_t offset)
NegativeBlack 4:d854fa394f85 69 {
NegativeBlack 4:d854fa394f85 70 if ((offset >= this->_size) || (this->_memory == NULL)) {
NegativeBlack 4:d854fa394f85 71 return 0;
NegativeBlack 4:d854fa394f85 72 }
NegativeBlack 4:d854fa394f85 73
NegativeBlack 4:d854fa394f85 74 int bytes_to_copy = (max_length > this->_size - offset)
NegativeBlack 4:d854fa394f85 75 ? this->_size - offset
NegativeBlack 4:d854fa394f85 76 : max_length;
NegativeBlack 4:d854fa394f85 77
NegativeBlack 4:d854fa394f85 78 std::memcpy(data, (unsigned char*)this->_memory + offset, bytes_to_copy);
NegativeBlack 4:d854fa394f85 79 return bytes_to_copy;
NegativeBlack 4:d854fa394f85 80 }
NegativeBlack 4:d854fa394f85 81
NegativeBlack 4:d854fa394f85 82 int
NegativeBlack 4:d854fa394f85 83 Buffer::write(const void *data, size_t length, size_t offset)
NegativeBlack 4:d854fa394f85 84 {
NegativeBlack 4:d854fa394f85 85 if ((offset >= this->_size) || (this->_memory == NULL)) {
NegativeBlack 4:d854fa394f85 86 return 0;
NegativeBlack 4:d854fa394f85 87 }
NegativeBlack 4:d854fa394f85 88
NegativeBlack 4:d854fa394f85 89 int bytes_to_copy = (length > this->_size - offset)
NegativeBlack 4:d854fa394f85 90 ? this->_size - offset
NegativeBlack 4:d854fa394f85 91 : length;
NegativeBlack 4:d854fa394f85 92
NegativeBlack 4:d854fa394f85 93 std::memcpy((unsigned char*)this->_memory + offset, data, bytes_to_copy);
NegativeBlack 4:d854fa394f85 94 this->_length += bytes_to_copy;
NegativeBlack 4:d854fa394f85 95 return bytes_to_copy;
NegativeBlack 4:d854fa394f85 96 }
NegativeBlack 4:d854fa394f85 97
NegativeBlack 4:d854fa394f85 98 void *
NegativeBlack 4:d854fa394f85 99 Buffer::pointer(size_t offset)
NegativeBlack 4:d854fa394f85 100 {
NegativeBlack 4:d854fa394f85 101 if (this->_memory == NULL) {
NegativeBlack 4:d854fa394f85 102 return NULL;
NegativeBlack 4:d854fa394f85 103 }
NegativeBlack 4:d854fa394f85 104
NegativeBlack 4:d854fa394f85 105 if (offset >= this->_size) {
NegativeBlack 4:d854fa394f85 106 return NULL;
NegativeBlack 4:d854fa394f85 107 }
NegativeBlack 4:d854fa394f85 108
NegativeBlack 4:d854fa394f85 109 return ((unsigned char*)this->_memory + offset);
NegativeBlack 4:d854fa394f85 110 }
NegativeBlack 4:d854fa394f85 111
NegativeBlack 4:d854fa394f85 112 void
NegativeBlack 4:d854fa394f85 113 Buffer::setLength(size_t length)
NegativeBlack 4:d854fa394f85 114 {
NegativeBlack 4:d854fa394f85 115 if (length > this->_size) {
NegativeBlack 4:d854fa394f85 116 this->_length = this->_size;
NegativeBlack 4:d854fa394f85 117 } else {
NegativeBlack 4:d854fa394f85 118 this->_length = length;
NegativeBlack 4:d854fa394f85 119 }
NegativeBlack 4:d854fa394f85 120 }
NegativeBlack 4:d854fa394f85 121
NegativeBlack 4:d854fa394f85 122 size_t
NegativeBlack 4:d854fa394f85 123 Buffer::size()
NegativeBlack 4:d854fa394f85 124 {
NegativeBlack 4:d854fa394f85 125 return this->_size;
NegativeBlack 4:d854fa394f85 126 }
NegativeBlack 4:d854fa394f85 127
NegativeBlack 4:d854fa394f85 128 size_t
NegativeBlack 4:d854fa394f85 129 Buffer::length()
NegativeBlack 4:d854fa394f85 130 {
NegativeBlack 4:d854fa394f85 131 return this->_length;
NegativeBlack 4:d854fa394f85 132 }
NegativeBlack 4:d854fa394f85 133
NegativeBlack 4:d854fa394f85 134 size_t
NegativeBlack 4:d854fa394f85 135 Buffer::free()
NegativeBlack 4:d854fa394f85 136 {
NegativeBlack 4:d854fa394f85 137 return this->_size - this->_length;
NegativeBlack 4:d854fa394f85 138 }
NegativeBlack 4:d854fa394f85 139
NegativeBlack 4:d854fa394f85 140 int
NegativeBlack 4:d854fa394f85 141 Buffer::_memory_allocate(size_t size)
NegativeBlack 4:d854fa394f85 142 {
NegativeBlack 4:d854fa394f85 143 this->_memory = std::calloc(size, sizeof(unsigned char));
NegativeBlack 4:d854fa394f85 144
NegativeBlack 4:d854fa394f85 145 if (this->_memory == NULL) {
NegativeBlack 4:d854fa394f85 146 this->_size = 0;
NegativeBlack 4:d854fa394f85 147 this->_length = 0;
NegativeBlack 4:d854fa394f85 148 return -1;
NegativeBlack 4:d854fa394f85 149 }
NegativeBlack 4:d854fa394f85 150
NegativeBlack 4:d854fa394f85 151 return 0;
NegativeBlack 4:d854fa394f85 152 }
NegativeBlack 4:d854fa394f85 153
NegativeBlack 4:d854fa394f85 154 void
NegativeBlack 4:d854fa394f85 155 Buffer::_memory_free()
NegativeBlack 4:d854fa394f85 156 {
NegativeBlack 4:d854fa394f85 157 if (this->_memory != NULL) {
NegativeBlack 4:d854fa394f85 158 std::free(this->_memory);
NegativeBlack 4:d854fa394f85 159 }
NegativeBlack 4:d854fa394f85 160 }