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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers buffer.cpp Source File

buffer.cpp

00001 /**
00002  * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice, this
00009  *    list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright notice,
00011  *    this list of conditions and the following disclaimer in the documentation
00012  *    and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00015  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00016  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00017  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00018  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00019  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00020  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00021  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00023  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024  */
00025 
00026 #include "buffer.hpp"
00027 using namespace network;
00028 
00029 Buffer::Buffer():
00030     _memory(NULL), _size(0), _length(0)
00031 {}
00032 
00033 Buffer::Buffer(const size_t size):
00034     _size(size), _length(0)
00035 {
00036     this->_memory_allocate(size);
00037 }
00038 
00039 Buffer::Buffer(const Buffer &other):
00040     _size(other._size), _length(other._length)
00041 {
00042     if (this->_memory_allocate(other._size) != -1) {
00043         std::memcpy(this->_memory, other._memory, other._size);
00044     }
00045 }
00046 
00047 Buffer::Buffer(const std::string &other):
00048     _size(other.size()), _length(other.length())
00049 {
00050     if (this->_memory_allocate(other.size()) != -1) {
00051         std::memcpy(this->_memory, other.data(), other.size());
00052     }
00053 }
00054 
00055 Buffer::~Buffer()
00056 {
00057     this->_memory_free();
00058 }
00059 
00060 Buffer &
00061 Buffer::operator=(const Buffer &other)
00062 {
00063     // Resize buffer if necessary
00064     if (this->_size < other._size) {
00065         this->_memory_free();
00066         if (this->_memory_allocate(other._size) < 0) {
00067             return (*this);
00068         }
00069     }
00070     
00071     // Copy information
00072     std::memcpy(this->_memory, other._memory, other._size);
00073     this->_size = other._size;
00074     this->_length = other._length;
00075     
00076     return (*this);
00077 }
00078 
00079 Buffer &
00080 Buffer::operator=(const std::string &other)
00081 {
00082     // Resize buffer if necessary
00083     if (this->_size < other.size()) {
00084         this->_memory_free();
00085         if (this->_memory_allocate(other.size()) < 0) {
00086             return (*this);
00087         }
00088     }
00089     
00090     // Copy information
00091     std::memcpy(this->_memory, other.data(), other.size());
00092     this->_size = other.size();
00093     this->_length = other.length();
00094     
00095     return (*this);
00096 }
00097 
00098 int
00099 Buffer::read(void *data, size_t max_length, size_t offset)
00100 {
00101     if ((offset >= this->_size) || (this->_memory == NULL)) {
00102         return 0;
00103     }
00104     
00105     int bytes_to_copy = (max_length > this->_size - offset)
00106         ? this->_size - offset
00107         : max_length;
00108     
00109     std::memcpy(data, (unsigned char*)this->_memory + offset, bytes_to_copy);
00110     return bytes_to_copy;
00111 }
00112 
00113 int
00114 Buffer::write(const void *data, size_t length, size_t offset)
00115 {
00116     if ((offset >= this->_size) || (this->_memory == NULL)) {
00117         return 0;
00118     }
00119     
00120     int bytes_to_copy = (length > this->_size - offset)
00121         ? this->_size - offset
00122         : length;
00123         
00124     std::memcpy((unsigned char*)this->_memory + offset, data, bytes_to_copy);
00125     this->_length += bytes_to_copy;
00126     return bytes_to_copy;
00127 }
00128 
00129 int
00130 Buffer::flush()
00131 {
00132     if (this->_memory == NULL) {
00133         return -1;
00134     }
00135 
00136     this->_length = 0;
00137     std::memset(this->_memory, 0, this->_size);
00138     
00139     return 0;
00140 }
00141 
00142 void *
00143 Buffer::data(size_t offset)
00144 {
00145     if (this->_memory == NULL) {
00146         return NULL;
00147     }
00148 
00149     if (offset >= this->_size) {
00150         return NULL;
00151     }
00152 
00153     return ((unsigned char*)this->_memory + offset);
00154 }
00155 
00156 void
00157 Buffer::length(size_t length)
00158 {
00159     if (length > this->_size) {
00160         this->_length = this->_size;
00161     } else {
00162         this->_length = length;
00163     }
00164 }
00165 
00166 size_t
00167 Buffer::length()
00168 {
00169     return this->_length;
00170 }
00171 
00172 size_t
00173 Buffer::size()
00174 {
00175     return this->_size;
00176 }
00177 
00178 size_t
00179 Buffer::free()
00180 {
00181     return this->_size - this->_length;
00182 }
00183 
00184 int
00185 Buffer::_memory_allocate(size_t size)
00186 {
00187     this->_memory = std::calloc(size, sizeof(unsigned char));
00188 
00189     if (this->_memory == NULL) {
00190         this->_size = 0;
00191         this->_length = 0;
00192         return -1;
00193     }
00194     
00195     return 0;
00196 }
00197 
00198 void
00199 Buffer::_memory_free()
00200 {
00201     if (this->_memory != NULL) {
00202         std::free(this->_memory);
00203     }
00204 }