From 5f0b3984fc0137382560f74dffdec90468b2c290 Mon Sep 17 00:00:00 2001 From: Nathan Bowhay Date: Thu, 5 Feb 2015 11:43:56 -0800 Subject: [PATCH] Added sendFile method Added a method to send an entire file over tcp. --- Engine/source/app/net/tcpObject.cpp | 33 +++++++++++++++++++++++++++++ Engine/source/app/net/tcpObject.h | 6 ++++++ 2 files changed, 39 insertions(+) diff --git a/Engine/source/app/net/tcpObject.cpp b/Engine/source/app/net/tcpObject.cpp index 6d1ccaa49..b156bfa04 100644 --- a/Engine/source/app/net/tcpObject.cpp +++ b/Engine/source/app/net/tcpObject.cpp @@ -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, }; @@ -404,6 +405,29 @@ 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 buffer(readFile.getStreamSize()); + U8 byte; + while(readFile.read(&byte)) + { + buffer.push_back(byte); + } + + //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" @@ -425,6 +449,15 @@ 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, listen, void, (U32 port),, "@brief Start listening on the specified port for connections.\n\n" diff --git a/Engine/source/app/net/tcpObject.h b/Engine/source/app/net/tcpObject.h index 9d14868ad..39b9464ba 100644 --- a/Engine/source/app/net/tcpObject.h +++ b/Engine/source/app/net/tcpObject.h @@ -82,6 +82,12 @@ public: bool processArguments(S32 argc, ConsoleValueRef *argv); 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 removeFromTable();