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 19:43:12 2012 +0000
Revision:
7:9796742904fa
Parent:
4:d854fa394f85
Child:
8:cdee0f2b6ff0
Small update, added Buffer::flush() method.

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 7:9796742904fa 98 int
NegativeBlack 7:9796742904fa 99 Buffer::flush()
NegativeBlack 7:9796742904fa 100 {
NegativeBlack 7:9796742904fa 101 if (this->_memory == NULL) {
NegativeBlack 7:9796742904fa 102 return -1;
NegativeBlack 7:9796742904fa 103 }
NegativeBlack 7:9796742904fa 104
NegativeBlack 7:9796742904fa 105 std::memset(this->_memory, 0, this->_size);
NegativeBlack 7:9796742904fa 106 }
NegativeBlack 7:9796742904fa 107
NegativeBlack 4:d854fa394f85 108 void *
NegativeBlack 4:d854fa394f85 109 Buffer::pointer(size_t offset)
NegativeBlack 4:d854fa394f85 110 {
NegativeBlack 4:d854fa394f85 111 if (this->_memory == NULL) {
NegativeBlack 4:d854fa394f85 112 return NULL;
NegativeBlack 4:d854fa394f85 113 }
NegativeBlack 4:d854fa394f85 114
NegativeBlack 4:d854fa394f85 115 if (offset >= this->_size) {
NegativeBlack 4:d854fa394f85 116 return NULL;
NegativeBlack 4:d854fa394f85 117 }
NegativeBlack 4:d854fa394f85 118
NegativeBlack 4:d854fa394f85 119 return ((unsigned char*)this->_memory + offset);
NegativeBlack 4:d854fa394f85 120 }
NegativeBlack 4:d854fa394f85 121
NegativeBlack 4:d854fa394f85 122 void
NegativeBlack 4:d854fa394f85 123 Buffer::setLength(size_t length)
NegativeBlack 4:d854fa394f85 124 {
NegativeBlack 4:d854fa394f85 125 if (length > this->_size) {
NegativeBlack 4:d854fa394f85 126 this->_length = this->_size;
NegativeBlack 4:d854fa394f85 127 } else {
NegativeBlack 4:d854fa394f85 128 this->_length = length;
NegativeBlack 4:d854fa394f85 129 }
NegativeBlack 4:d854fa394f85 130 }
NegativeBlack 4:d854fa394f85 131
NegativeBlack 4:d854fa394f85 132 size_t
NegativeBlack 4:d854fa394f85 133 Buffer::size()
NegativeBlack 4:d854fa394f85 134 {
NegativeBlack 4:d854fa394f85 135 return this->_size;
NegativeBlack 4:d854fa394f85 136 }
NegativeBlack 4:d854fa394f85 137
NegativeBlack 4:d854fa394f85 138 size_t
NegativeBlack 4:d854fa394f85 139 Buffer::length()
NegativeBlack 4:d854fa394f85 140 {
NegativeBlack 4:d854fa394f85 141 return this->_length;
NegativeBlack 4:d854fa394f85 142 }
NegativeBlack 4:d854fa394f85 143
NegativeBlack 4:d854fa394f85 144 size_t
NegativeBlack 4:d854fa394f85 145 Buffer::free()
NegativeBlack 4:d854fa394f85 146 {
NegativeBlack 4:d854fa394f85 147 return this->_size - this->_length;
NegativeBlack 4:d854fa394f85 148 }
NegativeBlack 4:d854fa394f85 149
NegativeBlack 4:d854fa394f85 150 int
NegativeBlack 4:d854fa394f85 151 Buffer::_memory_allocate(size_t size)
NegativeBlack 4:d854fa394f85 152 {
NegativeBlack 4:d854fa394f85 153 this->_memory = std::calloc(size, sizeof(unsigned char));
NegativeBlack 4:d854fa394f85 154
NegativeBlack 4:d854fa394f85 155 if (this->_memory == NULL) {
NegativeBlack 4:d854fa394f85 156 this->_size = 0;
NegativeBlack 4:d854fa394f85 157 this->_length = 0;
NegativeBlack 4:d854fa394f85 158 return -1;
NegativeBlack 4:d854fa394f85 159 }
NegativeBlack 4:d854fa394f85 160
NegativeBlack 4:d854fa394f85 161 return 0;
NegativeBlack 4:d854fa394f85 162 }
NegativeBlack 4:d854fa394f85 163
NegativeBlack 4:d854fa394f85 164 void
NegativeBlack 4:d854fa394f85 165 Buffer::_memory_free()
NegativeBlack 4:d854fa394f85 166 {
NegativeBlack 4:d854fa394f85 167 if (this->_memory != NULL) {
NegativeBlack 4:d854fa394f85 168 std::free(this->_memory);
NegativeBlack 4:d854fa394f85 169 }
NegativeBlack 4:d854fa394f85 170 }