From: Charles McGarvey Date: Thu, 13 May 2010 19:10:18 +0000 (-0600) Subject: resizable packets X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=19d555013569026c9e68784ea560cd2e5a21bc5e;p=chaz%2Fyoink resizable packets --- diff --git a/src/Moof/Packet.cc b/src/Moof/Packet.cc index 62da00b..8862f29 100644 --- a/src/Moof/Packet.cc +++ b/src/Moof/Packet.cc @@ -15,6 +15,7 @@ #if HAVE_BYTESWAP_H #include #endif +#include #if HAVE_ARPA_INET_H #include @@ -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(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(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; diff --git a/src/Moof/Packet.hh b/src/Moof/Packet.hh index 208b22e..096f112 100644 --- a/src/Moof/Packet.hh +++ b/src/Moof/Packet.hh @@ -16,7 +16,10 @@ #include #include -#include + +#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 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; }; diff --git a/src/Moof/Socket.hh b/src/Moof/Socket.hh index 6b0fa88..a0f2a7c 100644 --- a/src/Moof/Socket.hh +++ b/src/Moof/Socket.hh @@ -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);