|
@@ -27,6 +27,7 @@
|
|
|
#include "console/consoleInternal.h"
|
|
|
#include "core/strings/stringUnit.h"
|
|
|
#include "console/engineAPI.h"
|
|
|
+#include "core/stream/fileStream.h"
|
|
|
|
|
|
TCPObject *TCPObject::table[TCPObject::TableSize] = {0, };
|
|
|
|
|
@@ -138,6 +139,15 @@ 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"
|
|
|
+ );
|
|
|
+
|
|
|
IMPLEMENT_CALLBACK(TCPObject, onDNSResolved, void, (),(),
|
|
|
"Called whenever the DNS has been resolved.\n"
|
|
|
);
|
|
@@ -355,7 +365,7 @@ void TCPObject::onConnectFailed()
|
|
|
onConnectFailed_callback();
|
|
|
}
|
|
|
|
|
|
-void TCPObject::finishLastLine()
|
|
|
+bool TCPObject::finishLastLine()
|
|
|
{
|
|
|
if(mBufferSize)
|
|
|
{
|
|
@@ -364,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;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -400,6 +429,25 @@ void TCPObject::send(const U8 *buffer, U32 len)
|
|
|
Net::sendtoSocket(mTag, buffer, S32(len));
|
|
|
}
|
|
|
|
|
|
+bool TCPObject::sendFile(const char* fileName)
|
|
|
+{
|
|
|
+ //Open the file for reading
|
|
|
+ FileStream readFile;
|
|
|
+ if(!readFile.open(fileName, Torque::FS::File::Read))
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //Read each byte into our buffer
|
|
|
+ Vector<U8> buffer(readFile.getStreamSize());
|
|
|
+ readFile.read(buffer.size(), &buffer);
|
|
|
+
|
|
|
+ //Send the buffer
|
|
|
+ send(buffer.address(), buffer.size());
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
DefineEngineMethod(TCPObject, send, void, (const char *data),,
|
|
|
"@brief Transmits the data string to the connected computer.\n\n"
|
|
|
|
|
@@ -421,6 +469,20 @@ DefineEngineMethod(TCPObject, send, void, (const char *data),,
|
|
|
object->send( (const U8*)data, dStrlen(data) );
|
|
|
}
|
|
|
|
|
|
+DefineEngineMethod(TCPObject, sendFile, bool, (const char *fileName),,
|
|
|
+ "@brief Transmits the file in binary to the connected computer.\n\n"
|
|
|
+
|
|
|
+ "@param fileName The filename of the file to transfer.\n")
|
|
|
+{
|
|
|
+ return object->sendFile(fileName);
|
|
|
+}
|
|
|
+
|
|
|
+DefineEngineMethod(TCPObject, finishLastLine, void, (),,
|
|
|
+ "@brief Eat the rest of the lines.\n")
|
|
|
+{
|
|
|
+ object->finishLastLine();
|
|
|
+}
|
|
|
+
|
|
|
DefineEngineMethod(TCPObject, listen, void, (U32 port),,
|
|
|
"@brief Start listening on the specified port for connections.\n\n"
|
|
|
|
|
@@ -499,6 +561,29 @@ void processConnectedReceiveEvent(NetSocket sock, RawData incomingData)
|
|
|
size -= ret;
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
void processConnectedAcceptEvent(NetSocket listeningPort, NetSocket newConnection, NetAddress originatingAddress)
|