Browse Source

Added sendFile method

Added a method to send an entire file over tcp.
Nathan Bowhay 10 years ago
parent
commit
5f0b3984fc
2 changed files with 39 additions and 0 deletions
  1. 33 0
      Engine/source/app/net/tcpObject.cpp
  2. 6 0
      Engine/source/app/net/tcpObject.h

+ 33 - 0
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<U8> 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"
 

+ 6 - 0
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();