]> Dogcows Code - chaz/yoink/commitdiff
resizable packets
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 May 2010 19:10:18 +0000 (13:10 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 May 2010 19:10:18 +0000 (13:10 -0600)
src/Moof/Packet.cc
src/Moof/Packet.hh
src/Moof/Socket.hh

index 62da00b17f98141d307573232c2d23ca07444fa8..8862f29bc9ba918ef23fb4608d539681fbb4f90a 100644 (file)
@@ -15,6 +15,7 @@
 #if HAVE_BYTESWAP_H
 #include <byteswap.h>
 #endif
+#include <cstdlib>
 
 #if HAVE_ARPA_INET_H
 #include <arpa/inet.h>
@@ -95,6 +96,8 @@ namespace Mf {
        
 
 Packet::Packet(size_t size) :
+       mBuffer((char*)malloc(size)),
+       mSize(size),
        mR(0),
        mW(0),
        mBoolR(0),
@@ -102,21 +105,25 @@ Packet::Packet(size_t size) :
        mBoolNumR(0),
        mBoolNumW(0)
 {
-       mBuffer = boost::shared_array<char>(new char[size]);
-       mSize = size;
 }
 
 Packet::Packet(const char* data, size_t size) :
+       mBuffer((char*)malloc(size)),
+       mSize(size),
        mR(0),
+       mW(size),
        mBoolR(0),
        mBoolW(0),
        mBoolNumR(0),
        mBoolNumW(0)
 {
-       mBuffer = boost::shared_array<char>(new char[size]);
-       mSize = size;
-       mW = size;
-       memcpy(mBuffer.get(), data, mSize);
+       memcpy(mBuffer, data, size);
+}
+
+
+Packet::~Packet()
+{
+       free(mBuffer);
 }
 
 
@@ -207,6 +214,17 @@ Packet& Packet::operator<<(double value)
 size_t Packet::write(const void* bytes, size_t size)
 {
        size_t nBytes = std::min(size, mSize - mW);
+       if (!mBuffer || nBytes < size)
+       {
+               int numPages = 1 + size / PAGE_SIZE;
+               int newSize = mSize + numPages * PAGE_SIZE;
+               char* newBuffer = (char*)realloc(mBuffer, newSize);
+               if (newBuffer)
+               {
+                       mBuffer = newBuffer;
+                       mSize = newSize;
+               }
+       }
        memcpy(&mBuffer[mW], bytes, nBytes);
        mW += nBytes;
        return nBytes;
index 208b22e9bc75fa51af739546708e7023206b6bdc..096f112ca063289f502dc7b5f60395188a1f8d70 100644 (file)
 #include <string>
 #include <vector>
 
-#include <boost/shared_array.hpp>
+
+#ifndef PAGE_SIZE
+#define PAGE_SIZE 4096
+#endif
 
 
 namespace Mf {
@@ -26,9 +29,11 @@ class Packet
 {
 public:
 
-       Packet(size_t size = 1024);
+       Packet(size_t size = PAGE_SIZE);
        Packet(const char* data, size_t size);
 
+       ~Packet();
+
        Packet& operator<<(bool value);
        Packet& operator<<(int8_t  value);
        Packet& operator<<(int16_t value);
@@ -60,7 +65,7 @@ public:
 
        const char* bytes() const
        {
-               return &mBuffer.get()[mR];
+               return mBuffer + mR;
        }
 
        size_t size() const
@@ -71,16 +76,16 @@ public:
 
 private:
 
-       boost::shared_array<char>       mBuffer;
-       size_t                                          mSize;
+       char*   mBuffer;
+       size_t  mSize;
 
-       size_t                                          mR;
-       size_t                                          mW;
+       size_t  mR;
+       size_t  mW;
 
-       size_t                                          mBoolR;
-       size_t                                          mBoolW;
-       size_t                                          mBoolNumR;
-       size_t                                          mBoolNumW;
+       size_t  mBoolR;
+       size_t  mBoolW;
+       size_t  mBoolNumR;
+       size_t  mBoolNumW;
 };
 
 
index 6b0fa88fa6bbe7a72c52eef4773687f217c4a351..a0f2a7cae65bf2da03ee8b57af64f6b754e06c19 100644 (file)
@@ -202,11 +202,10 @@ public:
                                service.length() > 0 ? service.c_str() : 0, &hints, &list);
                if (status == 0)
                {
-                       struct addrinfo* addr = list;
-                       while (addr != 0)
+                       for (struct addrinfo* addr = list;
+                                       addr != 0; addr = addr->ai_next)
                        {
                                resolved.push_back(SocketAddress(addr, name));
-                               addr = addr->ai_next;
                        }
 
                        freeaddrinfo(list);
This page took 0.031089 seconds and 4 git commands to generate.