Permalink
Browse files

Midway through implementing TalkyBuffer

  • Loading branch information...
1 parent e2872e5 commit f171645dcc2c53e40fe9fadd6d89fdd6709ef764 @elliotwoods committed Aug 12, 2011
Showing with 345 additions and 313 deletions.
  1. +135 −3 src/TalkyBuffer.cpp
  2. +57 −3 src/TalkyBuffer.h
  3. +0 −139 src/TalkyBundleMessage.cpp
  4. +0 −27 src/TalkyBundleMessage.h
  5. +110 −114 src/TalkyMessage.cpp
  6. +43 −27 src/TalkyMessage.h
View
@@ -9,17 +9,149 @@
#include "TalkyBuffer.h"
namespace Talky {
+
+ //------
TalkyBuffer::TalkyBuffer() {
- isAllocated = false;
+ init();
+ }
+
+ TalkyBuffer::TalkyBuffer(BufferOffset size) {
+ init();
+ allocate(size);
}
TalkyBuffer::~TalkyBuffer() {
- delete[] data;
+ deAllocate();
}
- TalkyBuffer::TalkyBuffer(BufferOffset size) {
+ //------
+
+ void TalkyBuffer::init() {
+ readOffset = 0;
+ writeOffset = 0;
+ isAllocated = false;
+ allocatedSize = 0;
+ writtenSize = 0;
+ }
+
+ void TalkyBuffer::allocate(BufferOffset size) {
+ deAllocate();
+
data = new char[size];
+ isAllocated = true;
+ allocatedSize = size;
+ }
+
+ void TalkyBuffer::deAllocate() {
+ if (!isAllocated)
+ return;
+
+ delete[] data;
+ isAllocated = false;
+ }
+
+ //------
+
+ BufferOffset TalkyBuffer::size() const {
+ return writtenSize;
+ }
+
+ const void * TalkyBuffer::getData() const {
+ return data;
+ }
+
+ void TalkyBuffer::setData(const void * d, BufferOffset size) {
+ allocate(size);
+ memcpy(data, d, size);
+ }
+
+ bool TalkyBuffer::hasSpaceToWrite(BufferOffset size) const {
+ if (size > allocatedSize - writeOffset)
+ return false;
+ else
+ return true;
+ }
+
+ bool TalkyBuffer::hasSpaceToRead(BufferOffset size) const {
+ if (size > writtenSize - readOffset)
+ return false;
+ else
+ return true;
+ }
+
+ bool TalkyBuffer::write(const void *data, BufferOffset size) {
+ if (!hasSpaceToWrite(size))
+ return false;
+
+ memcpy((char*)data + writeOffset, data, size);
+ writeOffset += size;
+ if (writeOffset > writtenSize)
+ writtenSize = writeOffset;
+
+ return true;
+ }
+
+ bool TalkyBuffer::read(void *data, BufferOffset size) {
+ if (!hasSpaceToRead(size))
+ return false;
+
+ memcpy(data, (char*)data + readOffset, size);
+ readOffset += size;
+
+ return true;
+ }
+
+ //------
+
+ TalkyBuffer& TalkyBuffer::operator<<(TalkyBuffer const &other) {
+
+ const BufferOffset size = other.size();
+
+ *this << size;
+
+ if (write(other.getData(), other.size()))
+ return *this;
+ else
+ throw("Buffer overrun - insufficient space to write");
+ }
+
+ bool TalkyBuffer::operator>>(TalkyBuffer &other) {
+
+ if (!hasSpaceToRead(sizeof(BufferOffset)))
+ return false;
+
+ BufferOffset size;
+
+ *this >> size;
+
+ if (!hasSpaceToRead(size))
+ return false;
+
+ other.allocate(size);
+ other.setData(data + readOffset, size);
+ readOffset += size;
+ }
+
+ //------
+
+ string TalkyBuffer::toString(unsigned short maxLength) const {
+
+ stringstream out;
+ unsigned short i;
+
+ if (maxLength > writtenSize)
+ maxLength = writtenSize;
+
+ for (i=0; i<maxLength; i++)
+ {
+ if (data[i] > 32)
+ out << data[i];
+ else
+ out << ".";
+ }
+
+ return out.str();
}
}
View
@@ -7,6 +7,11 @@
// Copyright 2011 Kimchi and Chips. All rights reserved.
//
+#include <sstream>
+#include <string>
+
+using namespace std;
+
namespace Talky {
/** BufferOffset is a Talky type.
@@ -18,11 +23,60 @@ namespace Talky {
class TalkyBuffer {
public:
TalkyBuffer();
- ~TalkyBuffer();
TalkyBuffer(BufferOffset size);
+ ~TalkyBuffer();
+
+ void allocate(BufferOffset size);
+ void deAllocate();
+
+ BufferOffset size() const;
+ const void * getData() const;
+ void setData(const void * d, BufferOffset size);
+
+ bool hasSpaceToWrite(BufferOffset size) const;
+ bool hasSpaceToRead(BufferOffset size) const;
+
+ TalkyBuffer& operator<<(TalkyBuffer const &other);
+ bool operator>>(TalkyBuffer &other) const;
+
+ template<class T>
+ TalkyBuffer& operator<<(T const &object) {
+ if (!write(&object, sizeof(T)))
+ throw("Buffer overrun - insufficient space to write");
+ return *this;
+ }
+
+/* template<class T>
+ TalkyBuffer& operator<<(T const object) {
+ if (!write(&object, sizeof(T)))
+ throw("Buffer overrun - insufficient space to write");
+ return *this
+ }
+*/
+ bool operator>>(TalkyBuffer &other);
+
+ template<class T>
+ bool operator>>(T& object) {
+ return read(&object, sizeof(T));
+ readOffset += sizeof(T);
+ }
+
+ string toString(unsigned short maxLength=10) const;
+
protected:
- char* data;
- bool isAllocated;
+ void init();
+
+ bool write(const void* data, BufferOffset size);
+ bool read(void* data, BufferOffset size);
+
+ char* data;
+ bool isAllocated;
+ BufferOffset allocatedSize;
+ BufferOffset writtenSize;
+
+ BufferOffset readOffset;
+ BufferOffset writeOffset;
+
};
}
View
@@ -1,139 +0,0 @@
-//
-// TalkyBundleMessage.cpp
-// TalkyShapes
-//
-// Created by Elliot Woods on 09/02/2011.
-// Copyright 2011 Kimchi and Chips. All rights reserved.
-//
-
-#include "TalkyBundleMessage.h"
-
-namespace Talky {
- TalkyMessage TalkyBundleMessage::pack()
- {
-
- ///////////////////////////////////
- // WRITE PAYLOAD
- ///////////////////////////////////
- //
-
-
- //setup message
- TalkyMessage bundleMsg;
- //
-
- //calculate payload size
- //
- //2 + sum(message + 2)
- //
- vector<TalkyMessage>::iterator it;
- unsigned short totalPayloadLength = 2;
- //
- for (it = bundle.begin(); it != bundle.end(); it++)
- totalPayloadLength += it->getPayloadLength() + 2;
- //
-
-
- //setup single master payload in message
- bundleMsg.initPayload(totalPayloadLength);
- char * tempPayload = bundleMsg.getPayload();
- //
- //nMessages
- * (unsigned short *)(tempPayload) = bundle.size();
- tempPayload += 2;
- //
-
-
- //add individual payloads to master payload
- unsigned short msgLength;
- //
- for (it = bundle.begin(); it != bundle.end(); it++)
- {
- msgLength = it->getPayloadLength();
-
- //payload length
- * (unsigned short *)(tempPayload) = msgLength;
- tempPayload += 2;
-
- //payload
- memcpy(tempPayload, it->getPayload(), msgLength);
- tempPayload += msgLength;
- }
- //
- ///////////////////////////////////
-
- return bundleMsg;
- }
-
- void TalkyBundleMessage::unpack(TalkyMessage &bundleMsg, unsigned short newType)
- {
-
- ///////////////////////////////////
- // OVERRIDE FOR SPECIFIC TYPE
- ///////////////////////////////////
- //
- if (newType == -1)
- newType = bundleMsg.ContentsType;
- //
- ///////////////////////////////////
-
-
-
- ///////////////////////////////////
- // READ PAYLOAD TO BUNDLE VECTOR
- ///////////////////////////////////
- //
- int remainingBytes;
- char * tempPayload = bundleMsg.getPayload(remainingBytes);
-
-
- //count messages
- unsigned short nMessages = * (unsigned short*) tempPayload;
- tempPayload+=2;
- remainingBytes -= 2;
- //
- bundle.resize(nMessages);
- //
-
- /////////////////////////
- //set individual messages
- unsigned short msgLength;
- for (unsigned short i=0; i<nMessages; i++)
- {
- msgLength = * (unsigned short*) tempPayload;
- tempPayload += 2;
- remainingBytes -= 2;
-
- if (remainingBytes < msgLength)
- throw("TalkyMessageBundle::deSerialse : Insufficient bytes in message");
-
- //set header
- TalkyMessage pushMsg;
- //
- pushMsg.Company[0] = bundleMsg.Company[0];
- pushMsg.Company[1] = bundleMsg.Company[1];
- pushMsg.Protocol[0] = bundleMsg.Protocol[0];
- pushMsg.Protocol[1] = bundleMsg.Protocol[1];
- pushMsg.ContentsType = newType;
- pushMsg.Version = bundleMsg.Version;
- pushMsg.Timestamp = bundleMsg.Timestamp;
- //
-
- pushMsg.setPayload (tempPayload, msgLength);
- tempPayload += msgLength;
- remainingBytes -= msgLength;
-
- bundle[i] = pushMsg;
- }
- /////////////////////////
-
- //
- ///////////////////////////////////
-
- }
-
- void TalkyBundleMessage::push(TalkyMessage &msg)
- {
- bundle.push_back(msg);
- }
-}
Oops, something went wrong.

0 comments on commit f171645

Please sign in to comment.