Permalink
Browse files

TalkyBuffer, stream operators implemented

Talky v1.0
  • Loading branch information...
1 parent 90d8ada commit 2260beb3722a62894261173d66155adbabcd8da3 @elliotwoods committed Aug 14, 2011
Showing with 72 additions and 35 deletions.
  1. +6 −9 src/TalkyBase.cpp
  2. +4 −4 src/TalkyBase.h
  3. +39 −17 src/TalkyBuffer.cpp
  4. +16 −3 src/TalkyBuffer.h
  5. +1 −1 src/TalkyMessage.cpp
  6. +6 −1 src/TalkyMessage.h
View
@@ -233,15 +233,18 @@ namespace Talky {
+ lockThread();
+
/////////////////////////////////////
// RECEIVE DATA
/////////////////////////////////////
//
//
+ //
if (nodeType == 1)
{
if (isClientConnected())
- {
+ {
//client
_bufferIn.clean();
rxClient();
@@ -298,13 +301,11 @@ namespace Talky {
if (hasDataToSend)
tx();
-
-
}
//
/////////////////////////////////////
-
+ unlockThread();
}
bool TalkyBase::rxServer(int iClient) {
@@ -354,9 +355,7 @@ namespace Talky {
_bufferOut.clean();
}
- void TalkyBase::processIncomingBuffer() {
- lockThread();
-
+ void TalkyBase::processIncomingBuffer() {
//perhaps recode this so we dont copy?
TalkyMessage msg;
while (_bufferIn >> msg)
@@ -369,7 +368,5 @@ namespace Talky {
//processing will be performed in this thread
int msgCount = receiveQueue.size();
notifyReceiveEvent(msgCount);
-
- unlockThread();
}
}
View
@@ -35,11 +35,11 @@ namespace Talky {
// FOR ERROR HANDLING
////////////////////////////////
//
- //setup as client
- virtual void setup(string remoteHost, int remotePort);
+ ///setup as client on (default port=5001)
+ virtual void setup(string remoteHost, int remotePort=5001);
- //setup as server
- virtual void setup(int localPort);
+ ///setup as server (default port=5001)
+ virtual void setup(int localPort=5001);
virtual bool getIsServerBound();
virtual int getNumClients();
View
@@ -21,12 +21,33 @@ namespace Talky {
allocate(size);
}
+ TalkyBuffer::TalkyBuffer(const TalkyBuffer& other) {
+ operator=(other);
+ }
+
TalkyBuffer::~TalkyBuffer() {
deAllocate();
}
//------
+ TalkyBuffer& TalkyBuffer::operator= (const TalkyBuffer & other) {
+
+ isAllocated = other.getIsAllocated();
+ isDynamicallyAllocated = other.getIsDynamicallyAllocated();
+ quickReallocation = other.getIsQuickReallocation();
+ allocatedSize = other.getAllocatedSize();
+ writtenSize = other.getWrittenSize();
+ readOffset = other.getReadOffset();
+ writeOffset = other.getWriteOffset();
+
+
+ _data = new char[allocatedSize];
+ memcpy(_data, other.getData(), allocatedSize);
+ }
+
+ //------
+
void TalkyBuffer::init() {
readOffset = 0;
writeOffset = 0;
@@ -42,7 +63,7 @@ namespace Talky {
void TalkyBuffer::allocate(BufferOffset size) {
deAllocate();
- data = new char[size];
+ _data = new char[size];
isAllocated = true;
isDynamicallyAllocated = false;
allocatedSize = size;
@@ -52,7 +73,7 @@ namespace Talky {
if (!isAllocated)
return;
- delete[] data;
+ delete[] _data;
isAllocated = false;
}
@@ -76,11 +97,11 @@ namespace Talky {
if (isAllocated)
{
- memcpy(newDataArea, data, writtenSize);
- delete[] data;
+ memcpy(newDataArea, _data, writtenSize);
+ delete[] _data;
}
- data = newDataArea;
+ _data = newDataArea;
isAllocated = true;
allocatedSize = s;
@@ -99,12 +120,13 @@ namespace Talky {
}
const void * TalkyBuffer::getData() const {
- return data;
+ return _data;
}
void TalkyBuffer::setData(const void * d, BufferOffset size) {
allocate(size);
- memcpy(data, d, size);
+ memcpy(_data, d, size);
+ writtenSize = size;
}
bool TalkyBuffer::hasSpaceToWrite(BufferOffset size) const {
@@ -132,26 +154,26 @@ namespace Talky {
if (readOffset >= allocatedSize)
return 0;
else
- return (allocatedSize - readOffset);
+ return (writtenSize - readOffset);
}
- bool TalkyBuffer::write(const void *data, BufferOffset size) {
+ bool TalkyBuffer::write(const void *d, BufferOffset size) {
if (!hasSpaceToWrite(size))
if (!reAllocate(writeOffset + size))
return false;
- memcpy((char*)data + writeOffset, data, size);
+ memcpy(this->getWritePointer(), d, size);
advanceWritePointer(size);
return true;
}
- bool TalkyBuffer::read(void *data, BufferOffset size) {
+ bool TalkyBuffer::read(void *d, BufferOffset size) {
if (!hasSpaceToRead(size))
return false;
- memcpy(data, (char*)data + readOffset, size);
+ memcpy(d, this->getReadPointer(), size);
advanceReadPointer(size);
@@ -185,7 +207,7 @@ namespace Talky {
return false;
other.allocate(size);
- other.setData(data + readOffset, size);
+ other.setData(_data + readOffset, size);
readOffset += size;
}
@@ -201,8 +223,8 @@ namespace Talky {
for (i=0; i<maxLength; i++)
{
- if (data[i] > 32)
- out << data[i];
+ if (_data[i] > 32)
+ out << _data[i];
else
out << ".";
}
@@ -214,7 +236,7 @@ namespace Talky {
//------
char* TalkyBuffer::getWritePointer() {
- return data + writeOffset;
+ return _data + writeOffset;
}
void TalkyBuffer::advanceWritePointer(BufferOffset s) {
@@ -227,7 +249,7 @@ namespace Talky {
}
char* TalkyBuffer::getReadPointer() {
- return data + readOffset;
+ return _data + readOffset;
}
void TalkyBuffer::advanceReadPointer(BufferOffset s) {
View
@@ -9,6 +9,7 @@
#include <sstream>
#include <string>
+#include <iostream>
using namespace std;
@@ -24,9 +25,13 @@ namespace Talky {
public:
TalkyBuffer();
TalkyBuffer(BufferOffset size);
+ TalkyBuffer(const TalkyBuffer& other);
~TalkyBuffer();
+ ///copy operator
+ TalkyBuffer& operator= (const TalkyBuffer & other);
+
void allocate(BufferOffset size);
void deAllocate();
void clean();
@@ -93,16 +98,24 @@ namespace Talky {
*/
void advanceReadPointer(BufferOffset);
+ bool getIsAllocated() const { return isAllocated; };
+ bool getIsDynamicallyAllocated() const { return isDynamicallyAllocated; };
+ bool getIsQuickReallocation() const { return quickReallocation; };
+ BufferOffset getAllocatedSize() const { return allocatedSize; };
+ BufferOffset getWrittenSize() const { return writtenSize; };
+ BufferOffset getReadOffset() const { return readOffset; };
+ BufferOffset getWriteOffset() const { return writeOffset; };
+
protected:
void init();
- bool write(const void* data, BufferOffset size);
- bool read(void* data, BufferOffset size);
+ bool write(const void* d, BufferOffset size);
+ bool read(void* d, BufferOffset size);
///Used to perform dynamic reallocation. Returns false if we're dynamic allocation is turned off.
bool reAllocate(BufferOffset s);
- char* data;
+ char* _data;
///Does this buffer have space allocated?
bool isAllocated;
View
@@ -18,7 +18,6 @@ namespace Talky {
TalkyMessageHeader::TalkyMessageHeader() :
version(0),
contentsType(0) {
- setTimestamp();
}
TalkyMessageHeader::TalkyMessageHeader(const char * Company, const char * Protocol, unsigned short Version, unsigned short ContentsType)
@@ -124,6 +123,7 @@ namespace Talky {
void TalkyMessage::setHeader(Talky::TalkyMessageHeader const &h) {
header = h;
+ header.setTimestamp();
}
int TalkyMessage::getTotalLength() const {
View
@@ -43,9 +43,14 @@ namespace Talky {
char protocol[2];
unsigned short version;
- unsigned long timestamp;
unsigned short contentsType;
+
+ /** Timestamp for the message.
+ This is set automatically when the header
+ is assigned to the message
+ */
+ unsigned long timestamp;
};
//------

0 comments on commit 2260beb

Please sign in to comment.