Author Topic: Runtime Error TCPObject  (Read 916 times)

I'm making code that sends and receives data from a c++ program.

Every time I receive data with setBinary(true) and restart the server, before I spawn I get a runtime error, I have no clue why this might be?

My code is as below so far, what it will do is build terrain generated in the c++ application.

Code: [Select]
package buildTerrain {
function buildTerrain(%pos1, %pos2)
{
%tcp = new TCPObject(TerrainTCPObject);
%tcp.connect("127.0.0.1:4878");
%tcp.send(%pos1 SPC %pos2);
%tcp.setBinary(true);
}

function TerrainTCPObject::downloadComplete(%this)
{
%this.saveBufferToFile("base/server/temp/terrainData");
%this.setBinary(false);
%this.disconnect();
%this.delete();

%file = new FileObject();
%file.openForRead("base/server/temp/terrainData");
%coords = %file.readLine();
%xx1 = getWord(%coords,0);
%yy1 = getWord(%coords,1);
%zz1 = getWord(%coords,2);
%xx2 = getWord(%coords,3);
%yy2 = getWord(%coords,4);
%zz2 = getWord(%coords,5);

%pos = 0;
for(%i=%xx1; %i<%xx2; %i++)
for(%j=%yy1; %j<%yy2; %j++)
for(%k=%zz1; %k<%zz2; %k++)
{
%type = %file.readLine();
}

%file.close();
%file.delete();
fileDelete("base/server/temp/terrainData");
}

function TerrainTCPObject::onBinChunk(%this,%chunk)
{
if(!%this.lengthRecieved)
{
%this.saveBufferToFile("base/server/temp/terrainData");
%file = new FileObject();
%file.openForRead("base/server/temp/terrainData");
%this.contentLength = %file.readLine();
%file.close();
%file.delete();
fileDelete("base/server/temp/terrainData");
%this.lengthRecieved = 1;
%this.setBinary(false);
%this.setBinary(true);
%this.send(NULL);
}
else
if(%chunk == %this.contentLength)
%this.downloadComplete();
}
};
deactivatePackage("buildTerrain");
activatePackage("buildTerrain");

It's probably worth mentioning that the error message provides no useful information, and since it's a runtime error there is no info echoed onto console.

I'm no longer using binary to receive data, I am using onLine and this problem no longer occurs. However, if anyone knows the cause of this problem then please let me know.

I'd just like to take a moment to plug my TCPClient, which makes things like this much easier: http://www.greek2me.us/code/Support_TCPClient.cs

Put some echos in there and show us the console.log to see where it's actually stopping/crashing. Also, could be something to do with lengths, don't take my word as gospel though.

Is TCPObject blocking? That looks like a local variable. If it's not blocking then as soon as you're out of scope, nothing will be received because the object won't exist anymore. After a bit of googling, I was able to fudge this together (I have tested):
Code: [Select]
package buildTerrain {
function buildTerrain(%pos1, %pos2)
{
%TCPI = new TCPObject(TerrainTCPObject);
%TCPI.setBinary(true);

%TCPI.connect("127.0.0.1:4878");
}

function TerrainTCPObject::onConnected(%this)
{
echo("Connected.");
}

function TerrainTCPObject::finish(%this)
{
%this.disconnect();
echo("Disconnected.");
}

function TerrainTCPObject::onBinChunk(%this, %size)
{
echo("Received new data.");
if(%this.contentLength !$= 0) {
echo("Saving data to file.");
%this.saveBufferToFile("base/server/temp/terrainData");
}
}
};
deactivatePackage("buildTerrain");
activatePackage("buildTerrain");

Send it whatever and it will save it. Now how to handle buffer size you will need to get an initial content length then you can resize the buffer using setBinarySize() and close the connection within onBinChunk() using the existing if statement with an 'else' statement. If you use setBinarySize(), you won't need to call setBinary() unless you want to go back to non-binary mode.
« Last Edit: February 03, 2017, 06:49:44 PM by Meldaril »

Is TCPObject blocking? That looks like a local variable. If it's not blocking then as soon as you're out of scope, nothing will be received because the object won't exist anymore. After a bit of googling, I was able to fudge this together (I have tested):

Torkscript objects aren't garbage collected. If you create something and forget to delete it it'll stay around forever.

Torkscript objects aren't garbage collected. If you create something and forget to delete it it'll stay around forever.
Thanks for the clarification. It's unusual to have explicit memory/object management like this in an interpreted language. This echos of C++ horror.