diff --git a/Engine/source/app/net/tcpObject.cpp b/Engine/source/app/net/tcpObject.cpp index d1f127924..06f320576 100644 --- a/Engine/source/app/net/tcpObject.cpp +++ b/Engine/source/app/net/tcpObject.cpp @@ -139,6 +139,11 @@ IMPLEMENT_CALLBACK(TCPObject, onLine, void, (const char* line), (line), "@param line Data sent from the server.\n" ); +IMPLEMENT_CALLBACK(TCPObject, onPacket, bool, (const char* data), (data), + "@brief Called when we get a packet with no newlines or nulls (probably websocket).\n\n" + "@param data Data sent from the server.\n" + "@return true if script handled the packet.\n" + ); IMPLEMENT_CALLBACK(TCPObject, onEndReceive, void, (), (), "@brief Called when we are done reading all lines.\n\n" ); @@ -360,7 +365,7 @@ void TCPObject::onConnectFailed() onConnectFailed_callback(); } -void TCPObject::finishLastLine() +bool TCPObject::finishLastLine() { if(mBufferSize) { @@ -369,6 +374,25 @@ void TCPObject::finishLastLine() dFree(mBuffer); mBuffer = 0; mBufferSize = 0; + + return true; + } + + return false; +} + +bool TCPObject::isBufferEmpty() +{ + return (mBufferSize <= 0); +} + +void TCPObject::emptyBuffer() +{ + if(mBufferSize) + { + dFree(mBuffer); + mBuffer = 0; + mBufferSize = 0; } } @@ -538,6 +562,27 @@ void processConnectedReceiveEvent(NetSocket sock, RawData incomingData) buffer += ret; } + //If our buffer now has something in it then it's probably a web socket packet and lets handle it + if(!tcpo->isBufferEmpty()) + { + //Copy all the data into a string (may be a quicker way of doing this) + U8 *data = (U8*)incomingData.data; + String temp; + for(S32 i = 0; i < incomingData.size; i++) + { + temp += data[i]; + } + + //Send the packet to script + bool handled = tcpo->onPacket_callback(temp); + + //If the script did something with it, clear the buffer + if(handled) + { + tcpo->emptyBuffer(); + } + } + tcpo->onEndReceive_callback(); } diff --git a/Engine/source/app/net/tcpObject.h b/Engine/source/app/net/tcpObject.h index 39b9464ba..9a8b5e40d 100644 --- a/Engine/source/app/net/tcpObject.h +++ b/Engine/source/app/net/tcpObject.h @@ -36,6 +36,7 @@ public: DECLARE_CALLBACK(void, onConnectionRequest, (const char* address, const char* ID)); DECLARE_CALLBACK(void, onLine, (const char* line)); + DECLARE_CALLBACK(bool, onPacket, (const char* data)); DECLARE_CALLBACK(void, onEndReceive, ()); DECLARE_CALLBACK(void, onDNSResolved,()); DECLARE_CALLBACK(void, onDNSFailed, ()); @@ -61,7 +62,9 @@ public: virtual ~TCPObject(); void parseLine(U8 *buffer, U32 *start, U32 bufferLen); - void finishLastLine(); + bool finishLastLine(); + bool isBufferEmpty(); + void emptyBuffer(); static TCPObject *find(NetSocket tag);