mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-04-29 16:25:42 +00:00
Merge pull request #1186 from MusicMonkey5555/tcp-additions
Tcp additions
This commit is contained in:
commit
8c50cc70c3
2 changed files with 97 additions and 2 deletions
|
|
@ -27,6 +27,7 @@
|
||||||
#include "console/consoleInternal.h"
|
#include "console/consoleInternal.h"
|
||||||
#include "core/strings/stringUnit.h"
|
#include "core/strings/stringUnit.h"
|
||||||
#include "console/engineAPI.h"
|
#include "console/engineAPI.h"
|
||||||
|
#include "core/stream/fileStream.h"
|
||||||
|
|
||||||
TCPObject *TCPObject::table[TCPObject::TableSize] = {0, };
|
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"
|
"@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, (),(),
|
IMPLEMENT_CALLBACK(TCPObject, onDNSResolved, void, (),(),
|
||||||
"Called whenever the DNS has been resolved.\n"
|
"Called whenever the DNS has been resolved.\n"
|
||||||
);
|
);
|
||||||
|
|
@ -355,7 +365,7 @@ void TCPObject::onConnectFailed()
|
||||||
onConnectFailed_callback();
|
onConnectFailed_callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPObject::finishLastLine()
|
bool TCPObject::finishLastLine()
|
||||||
{
|
{
|
||||||
if(mBufferSize)
|
if(mBufferSize)
|
||||||
{
|
{
|
||||||
|
|
@ -364,6 +374,25 @@ void TCPObject::finishLastLine()
|
||||||
dFree(mBuffer);
|
dFree(mBuffer);
|
||||||
mBuffer = 0;
|
mBuffer = 0;
|
||||||
mBufferSize = 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));
|
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),,
|
DefineEngineMethod(TCPObject, send, void, (const char *data),,
|
||||||
"@brief Transmits the data string to the connected computer.\n\n"
|
"@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) );
|
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),,
|
DefineEngineMethod(TCPObject, listen, void, (U32 port),,
|
||||||
"@brief Start listening on the specified port for connections.\n\n"
|
"@brief Start listening on the specified port for connections.\n\n"
|
||||||
|
|
||||||
|
|
@ -499,6 +561,29 @@ void processConnectedReceiveEvent(NetSocket sock, RawData incomingData)
|
||||||
size -= ret;
|
size -= ret;
|
||||||
buffer += 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)
|
void processConnectedAcceptEvent(NetSocket listeningPort, NetSocket newConnection, NetAddress originatingAddress)
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,8 @@ public:
|
||||||
|
|
||||||
DECLARE_CALLBACK(void, onConnectionRequest, (const char* address, const char* ID));
|
DECLARE_CALLBACK(void, onConnectionRequest, (const char* address, const char* ID));
|
||||||
DECLARE_CALLBACK(void, onLine, (const char* line));
|
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, onDNSResolved,());
|
||||||
DECLARE_CALLBACK(void, onDNSFailed, ());
|
DECLARE_CALLBACK(void, onDNSFailed, ());
|
||||||
DECLARE_CALLBACK(void, onConnected, ());
|
DECLARE_CALLBACK(void, onConnected, ());
|
||||||
|
|
@ -60,7 +62,9 @@ public:
|
||||||
virtual ~TCPObject();
|
virtual ~TCPObject();
|
||||||
|
|
||||||
void parseLine(U8 *buffer, U32 *start, U32 bufferLen);
|
void parseLine(U8 *buffer, U32 *start, U32 bufferLen);
|
||||||
void finishLastLine();
|
bool finishLastLine();
|
||||||
|
bool isBufferEmpty();
|
||||||
|
void emptyBuffer();
|
||||||
|
|
||||||
static TCPObject *find(NetSocket tag);
|
static TCPObject *find(NetSocket tag);
|
||||||
|
|
||||||
|
|
@ -81,6 +85,12 @@ public:
|
||||||
|
|
||||||
bool processArguments(S32 argc, ConsoleValueRef *argv);
|
bool processArguments(S32 argc, ConsoleValueRef *argv);
|
||||||
void send(const U8 *buffer, U32 bufferLen);
|
void send(const U8 *buffer, U32 bufferLen);
|
||||||
|
|
||||||
|
///Send an entire file over tcp
|
||||||
|
///@arg fileName Full path to file you want to send
|
||||||
|
///@return true if file was sent, false if not (file doesn't exist)
|
||||||
|
bool sendFile(const char* fileName);
|
||||||
|
|
||||||
void addToTable(NetSocket newTag);
|
void addToTable(NetSocket newTag);
|
||||||
void removeFromTable();
|
void removeFromTable();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue