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

Revision:
8:cdee0f2b6ff0
Parent:
7:9796742904fa
--- a/buffer.cpp	Wed Jul 18 19:43:12 2012 +0000
+++ b/buffer.cpp	Thu Sep 27 09:31:40 2012 +0000
@@ -44,6 +44,14 @@
     }
 }
 
+Buffer::Buffer(const std::string &other):
+    _size(other.size()), _length(other.length())
+{
+    if (this->_memory_allocate(other.size()) != -1) {
+        std::memcpy(this->_memory, other.data(), other.size());
+    }
+}
+
 Buffer::~Buffer()
 {
     this->_memory_free();
@@ -52,11 +60,15 @@
 Buffer &
 Buffer::operator=(const Buffer &other)
 {
-    this->_memory_free();
-    if (this->_memory_allocate(other._size) < 0) {
-        return (*this);
+    // Resize buffer if necessary
+    if (this->_size < other._size) {
+        this->_memory_free();
+        if (this->_memory_allocate(other._size) < 0) {
+            return (*this);
+        }
     }
     
+    // Copy information
     std::memcpy(this->_memory, other._memory, other._size);
     this->_size = other._size;
     this->_length = other._length;
@@ -64,6 +76,25 @@
     return (*this);
 }
 
+Buffer &
+Buffer::operator=(const std::string &other)
+{
+    // Resize buffer if necessary
+    if (this->_size < other.size()) {
+        this->_memory_free();
+        if (this->_memory_allocate(other.size()) < 0) {
+            return (*this);
+        }
+    }
+    
+    // Copy information
+    std::memcpy(this->_memory, other.data(), other.size());
+    this->_size = other.size();
+    this->_length = other.length();
+    
+    return (*this);
+}
+
 int
 Buffer::read(void *data, size_t max_length, size_t offset)
 {
@@ -102,11 +133,14 @@
         return -1;
     }
 
+    this->_length = 0;
     std::memset(this->_memory, 0, this->_size);
+    
+    return 0;
 }
 
 void *
-Buffer::pointer(size_t offset)
+Buffer::data(size_t offset)
 {
     if (this->_memory == NULL) {
         return NULL;
@@ -120,7 +154,7 @@
 }
 
 void
-Buffer::setLength(size_t length)
+Buffer::length(size_t length)
 {
     if (length > this->_size) {
         this->_length = this->_size;
@@ -130,18 +164,18 @@
 }
 
 size_t
+Buffer::length()
+{
+    return this->_length;
+}
+
+size_t
 Buffer::size()
 {
     return this->_size;
 }
 
 size_t
-Buffer::length()
-{
-    return this->_length;
-}
-
-size_t
 Buffer::free()
 {
     return this->_size - this->_length;