Skip to content
Browse files

patched it up to compile. needs testing

  • Loading branch information...
1 parent 48b790b commit 41b28cb571eb00e938968b2d1e1dd8d353b92f57 @elliotwoods committed Oct 13, 2011
Showing with 212 additions and 70 deletions.
  1. +13 −0 .gitignore
  2. +10 −0 readme.md
  3. +0 −4 readme.txt
  4. +27 −20 src/TalkyBase.cpp
  5. +5 −6 src/TalkyBase.h
  6. +64 −18 src/TalkyBuffer.cpp
  7. +29 −12 src/TalkyBuffer.h
  8. +45 −4 src/TalkyMessage.cpp
  9. +19 −6 src/TalkyMessage.h
View
13 .gitignore
@@ -0,0 +1,13 @@
+build/
+bin/
+obj/
+xcuserdata/
+
+*.depend
+*.layout
+*.mode*v3
+*.pbxuser
+*.app*
+*.DS_*
+*.perspectivev3
+*.xcworkspace
View
10 readme.md
@@ -0,0 +1,10 @@
+#Talky
+
+Talky is a simple network protocol designed that sits on top of TCP network transmissions, and is designed to work across platforms.
+
+The main feature of Talky is that it trivially serialises any binary class using a simple << operator.
+
+This means that no extra serialisation/deserialisation code is required, except for when the class holds pointers. In this case, look into extending TalkySerialisable.
+
+##TODO:
+What happens when there's a buffer overrun in the buffer when inputting from platform in rxClient/rxServer? (even though this is very++ unlikely to happen)
View
4 readme.txt
@@ -1,4 +0,0 @@
-Talky
-
-TODO:
-What happens when there's a buffer overrun in the buffer when inputting from platform in rxClient/rxServer? (even though this is very++ unlikely to happen)
View
47 src/TalkyBase.cpp
@@ -126,33 +126,21 @@ namespace Talky {
//-----------------------------------------------------------
- TalkyBase& TalkyBase::operator<<(TalkyMessage& msg) {
- send(msg);
- return *this;
- }
-
- bool TalkyBase::operator>>(TalkyMessage& msg) {
- return popMessage(msg);
- }
-
- //-----------------------------------------------------------
-
-
- void TalkyBase::send(TalkyMessage &msg)
- {
+ TalkyBase& TalkyBase::operator<<(const TalkyMessage &m) {
lockThread();
- sendQueue.push_back(msg);
+ sendQueue.push_back(m);
unlockThread();
+
+ return *this;
}
-
- bool TalkyBase::popMessage(TalkyMessage &msg)
- {
+
+ bool TalkyBase::operator>>(TalkyMessage& m) {
if (!lockThread())
return false;
if (receiveQueue.size() > 0)
{
- msg = receiveQueue.front();
+ m = receiveQueue.front();
receiveQueue.erase(receiveQueue.begin());
unlockThread();
@@ -161,7 +149,6 @@ namespace Talky {
unlockThread();
return false;
}
-
}
void TalkyBase::clearMessages()
@@ -369,4 +356,24 @@ namespace Talky {
int msgCount = receiveQueue.size();
notifyReceiveEvent(msgCount);
}
+
+ string TalkyBase::toString() {
+ stringstream out;
+
+ out << "Talky node is ";
+ switch (nodeType) {
+ case 0:
+ out << "uninitialised." << endl;
+ break;
+
+ case 1:
+ out << "a client, connecting to " << _remoteHost << " on port " << _remotePort << "." << endl;
+ break;
+
+ case 2:
+ out << "a server on local port " << _localPort << ", with " << getNumClients() << " clients" << endl;
+ }
+
+ return out.str();
+ }
}
View
11 src/TalkyBase.h
@@ -20,6 +20,7 @@
#include <vector>
#include <ctime>
#include <iostream>
+#include <sstream>
using namespace std;
@@ -51,13 +52,10 @@ namespace Talky {
float getTimeUntilNextConnectNorm();
string getRemoteHost(); ///< Get the hostname of remote machine. Perhaps split this into 2 functions? or warn if we use this for client?
-
- TalkyBase& operator<<(TalkyMessage& msg);
- bool operator>>(TalkyMessage& msg);
- void send(TalkyMessage &msg);
- bool popMessage(TalkyMessage &msg);
- void clearMessages();
+ TalkyBase& operator<<(const TalkyMessage& m); ///<send message
+ bool operator>>(TalkyMessage& m); ///<receive message
+ void clearMessages();
/** Direct access to the receiveQueue.
@@ -66,6 +64,7 @@ namespace Talky {
vector<TalkyMessage> const &getReceiveQueue() {return receiveQueue; };
vector<TalkyMessage> const &getSendQueue() { return sendQueue;};
+ string toString();
protected:
void update();
View
82 src/TalkyBuffer.cpp
@@ -182,35 +182,81 @@ namespace Talky {
//------
- TalkyBuffer& TalkyBuffer::operator<<(TalkyBuffer const &other) {
-
- const BufferOffset size = other.size();
-
- *this << size;
+ void TalkyBuffer::serialiseToBuffer(TalkyBuffer &other) const {
+ const BufferOffset s = this->size();
+ other << s;
- if (write(other.getData(), other.size()))
- return *this;
- else
+ if (!other.write(getData(), s))
throw("Buffer overrun - insufficient space to write");
}
- bool TalkyBuffer::operator>>(TalkyBuffer &other) {
+ bool TalkyBuffer::deSerialiseFromBuffer(TalkyBuffer &other) {
+ BufferOffset s;
+ if (!(other >> s))
+ return false;
- if (!hasSpaceToRead(sizeof(BufferOffset)))
+ if (!other.hasSpaceToRead(s))
return false;
+
+ //clear our local data and replace
+ //with section of other buffer's data
+ setData(other.getData(), s);
+
+ return true;
+ }
+
+ //------
+ bool TalkyBuffer::loadFile(string filename) {
+
+
+ // this is untested
+
+ ifstream inFile;
+
+ if (inFile.is_open()) {
+ try {
+
+ inFile.open(filename.c_str(), ios::binary);
+
+ //find filesize
+ long begin, end;
+ begin = inFile.tellg();
+ inFile.seekg (0, ios::end);
+ end = inFile.tellg();
+
+ //allocate to this size
+ allocate(end - begin);
+
+ inFile.read(_data, end-begin);
- BufferOffset size;
+ if (inFile.fail())
+ throw;
+ } catch (...) {
+ if (inFile.is_open())
+ inFile.close();
+ return false;
+ }
- *this >> size;
-
- if (!hasSpaceToRead(size))
+ inFile.close();
+ return true;
+ } else {
return false;
-
- other.allocate(size);
- other.setData(_data + readOffset, size);
- readOffset += size;
+ }
}
+ bool TalkyBuffer::saveFile(string filename) const {
+ ofstream outFile;
+ try {
+ outFile.open(filename.c_str(), ios::binary | ios::out | ios::trunc);
+ outFile.write(_data, size());
+ } catch (...) {
+ if (outFile.is_open())
+ outFile.close();
+ return false;
+ }
+ outFile.close();
+ return true;
+ }
//------
string TalkyBuffer::toString(unsigned short maxLength) const {
View
41 src/TalkyBuffer.h
@@ -10,6 +10,7 @@
#include <sstream>
#include <string>
#include <iostream>
+#include <fstream>
using namespace std;
@@ -21,7 +22,18 @@ namespace Talky {
*/
typedef unsigned long BufferOffset;
- class TalkyBuffer {
+ class TalkyBuffer;
+
+ /** TalkySerialisable is an interface class for when
+ you want to override standard serialisation methods.
+ */
+ class TalkySerialisable {
+ public:
+ virtual void serialiseToBuffer(TalkyBuffer &b) const = 0;
+ virtual bool deSerialiseFromBuffer(TalkyBuffer &b) = 0;
+ };
+
+ class TalkyBuffer : TalkySerialisable {
public:
TalkyBuffer();
TalkyBuffer(BufferOffset size);
@@ -38,18 +50,16 @@ namespace Talky {
BufferOffset size() const;
const void * getData() const;
- void setData(const void * d, BufferOffset size);
+ void setData(const void * d, BufferOffset size);
+
+ bool write(const void* d, BufferOffset size);
+ bool read(void* d, BufferOffset size);
bool hasSpaceToWrite(BufferOffset size) const;
bool hasSpaceToRead(BufferOffset size) const;
BufferOffset getRemainingWriteSpace() const;
BufferOffset getRemainingReadSpace() const;
-
- ///Used when putting a payload onto main buffers
- TalkyBuffer& operator<<(TalkyBuffer const &other);
- ///Used when pulling a payload off main buffers
- bool operator>>(TalkyBuffer &other) const;
-
+
template<class T>
TalkyBuffer& operator<<(T const &object) {
if (!write(&object, sizeof(T)))
@@ -64,7 +74,6 @@ namespace Talky {
return *this
}
*/
- bool operator>>(TalkyBuffer &other);
template<class T>
bool operator>>(T& object) {
@@ -106,12 +115,19 @@ namespace Talky {
BufferOffset getReadOffset() const { return readOffset; };
BufferOffset getWriteOffset() const { return writeOffset; };
+
+ //TalkyBuffer inherits TalkySerialisable
+ void serialiseToBuffer(TalkyBuffer &b) const;
+ bool deSerialiseFromBuffer(TalkyBuffer &b);
+
+
+ //File access
+ bool loadFile(string filename);
+ bool saveFile(string filename) const;
+
protected:
void init();
- 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);
@@ -134,4 +150,5 @@ namespace Talky {
BufferOffset writeOffset;
};
+
}
View
49 src/TalkyMessage.cpp
@@ -20,15 +20,35 @@ namespace Talky {
contentsType(0) {
}
- TalkyMessageHeader::TalkyMessageHeader(const char * Company, const char * Protocol, unsigned short Version, unsigned short ContentsType)
+ TalkyMessageHeader::TalkyMessageHeader(TalkyMessageHeader& other, ContentsType t)
+ {
+ setCompany(other.getCompany());
+ setProtocol(other.getProtocol());
+ setVersion(other.getVersion());
+ setContentsType(t);
+ }
+
+ TalkyMessageHeader::TalkyMessageHeader(const char * Company, const char * Protocol, ProtocolVersion v, ContentsType t)
{
setCompany(Company);
setProtocol(Protocol);
- setVersion(Version);
- setContentsType(ContentsType);
+ setVersion(v);
+ setContentsType(t);
+ }
+
+ const char* TalkyMessageHeader::getCompany() const {
+ return company;
+ }
+
+ const char* TalkyMessageHeader::getProtocol() const {
+ return protocol;
+ }
+
+ ProtocolVersion TalkyMessageHeader::getVersion() const {
+ return version;
}
- unsigned short TalkyMessageHeader::getContentsType() const {
+ ContentsType TalkyMessageHeader::getContentsType() const {
return contentsType;
}
@@ -69,6 +89,14 @@ namespace Talky {
return out.str();
}
+
+ bool TalkyMessageHeader::operator==(const TalkyMessageHeader &other) const {
+ return
+ company == other.getCompany() &&
+ protocol == other.getProtocol() &&
+ version == other.getVersion() &&
+ contentsType == other.getContentsType();
+ }
//
//------------------------------------------------------
@@ -78,6 +106,11 @@ namespace Talky {
//------------------------------------------------------
//
TalkyMessage::TalkyMessage() {
+ header = defaultHeader;
+ }
+
+ TalkyMessage::TalkyMessage(TalkyMessageHeader const &h) {
+ header = h;
}
bool TalkyMessage::serialise(TalkyBuffer &buf) const {
@@ -163,6 +196,14 @@ namespace Talky {
{
payload.allocate(length);
}
+
+ //------
+
+ TalkyMessageHeader TalkyMessage::defaultHeader = TalkyMessageHeader("KC", "Ta", 0, 0);
+
+ void TalkyMessage::setDefaultHeader(Talky::TalkyMessageHeader &h) {
+ defaultHeader = h;
+ }
//
//------------------------------------------------------
View
25 src/TalkyMessage.h
@@ -22,12 +22,19 @@ using namespace std;
namespace Talky {
+ typedef unsigned short ContentsType;
+ typedef unsigned short ProtocolVersion;
+
class TalkyMessageHeader {
public:
TalkyMessageHeader();
- TalkyMessageHeader(const char * Company, const char * Protocol, unsigned short Version, unsigned short ContentsType);
+ TalkyMessageHeader(TalkyMessageHeader& other, ContentsType t);
+ TalkyMessageHeader(const char * Company, const char * Protocol, ProtocolVersion v, ContentsType t);
- unsigned short getContentsType() const;
+ const char* getCompany() const;
+ const char* getProtocol() const;
+ ProtocolVersion getVersion() const;
+ ContentsType getContentsType() const;
unsigned long getTimestamp() const;
void setCompany(const char * s);
@@ -38,6 +45,8 @@ namespace Talky {
string toString();
+ bool operator==(const TalkyMessageHeader &other) const;
+
protected:
char company[2];
@@ -60,9 +69,10 @@ namespace Talky {
friend class TalkyBuffer;
TalkyMessage();
+ TalkyMessage(TalkyMessageHeader const &h);
template <class T>
- TalkyMessage& operator<<(T &object)
+ TalkyMessage& operator<<(const T &object)
{
payload << object;
}
@@ -102,10 +112,14 @@ namespace Talky {
syntax. Deserialise may be depreciated / made private
*/
bool deSerialise(TalkyBuffer &buf);
-
+
+ static void setDefaultHeader(TalkyMessageHeader &h);
+
protected:
TalkyMessageHeader header;
- TalkyBuffer payload;
+ TalkyBuffer payload;
+
+ static TalkyMessageHeader defaultHeader;
};
//-----
@@ -114,5 +128,4 @@ namespace Talky {
TalkyBuffer& operator<<(TalkyBuffer& b, TalkyMessage const &m);
///Used when pulling a message off main buffer
bool operator>>(TalkyBuffer& b, TalkyMessage &m);
-
}

0 comments on commit 41b28cb

Please sign in to comment.
Something went wrong with that request. Please try again.