Author Topic: TCP Object Help  (Read 3301 times)

Ok so I made up this code
Code: [Select]
function zgui_serverlist::loadserverimage(%this, %id)
{
if(%id < %this.getcount())
{
filedelete("config/zgui_serverdetail.jpg");
%ip = %this.getobject(%id).ip;
%port = %this.getobject(%id).port;
%dir = strreplace(%ip, "." , "-") @ "_" @ %port;
%request = "GET /detail.php?q=" @ %dir @ " HTTP/1.0\r\nHost:image.blockland.us\r\n\r\n";
%tcp = new TCPObject(zgui_dlserverimagetcp)
{
request = %request;
};
zgui_container.add(%tcp);
%this.dlimagetcp = %tcp;
%tcp.connect("image.blockland.us:80");
}
}

function zgui_dlserverimagetcp::onConnected(%this)
{
%this.send(%this.request);
}

function zgui_dlserverimagetcp::onLine(%this, %line)
{
if(%line $= "")
%this.setBinary(1);
}

function zgui_dlserverimagetcp::onBinChunk(%this, %chunk)
{
cancel(%this.timer);
%this.timer = %this.schedule(1000,endload);
}

function zgui_dlserverimagetcp::endload(%this)
{
%this.saveBufferToFile("config/client/zgui_serverdetail.jpg");
%this.schedule(10,delete);
zgui_serverdetail_image.setbitmap("config/client/zgui_serverdetail.jpg");
}
It's supposed to download a server preview image, like


If I turn on trace it shows what I would expect, it's sending a correct request and then the onLine being called alot and then the onBinChunk being called alot and then the endload,
and then
IT WRITES A loving BLANK FILE

Can someone tell me what is going wrong here?

Halp pls
Why does it write a blank file :(


So change
Code: [Select]
function zgui_dlserverimagetcp::onBinChunk(%this, %chunk)
{
cancel(%this.timer);
%this.timer = %this.schedule(1000,endload);
}

function zgui_dlserverimagetcp::endload(%this)
{
%this.saveBufferToFile("config/client/zgui_serverdetail.jpg");
%this.schedule(10,delete);
zgui_serverdetail_image.setbitmap("config/client/zgui_serverdetail.jpg");
}
to
Code: [Select]

function zgui_dlserverimagetcp::onBinChunk(%this, %chunk)
{
cancel(%this.schedule);
cancel(%this.schedule2);
cancel(zgui_serverdetail_image.schedule);

%this.schedule = %this.schedule(1000,saveBufferToFile,"config/client/zgui_serverdetail.jpg");
%this.schedule2 = %this.schedule(1100, delete);
zgui_serverdetail_image.schedule = zgui_serverdetail_image.schedule(1100,"setbitmap", "config/client/zgui_serverdetail.jpg");
}
?
It's still writing a blank file D:

There's plenty of examples of the right way to download files so look at one of those first. RTB would be a good place to start.

It seems like the server can respond in 2 different ways, depeding on the file size

For a small image (mostly one color, of empty server):
-It tells me the content length
-I can save the image successfully

For a big image (lots of colors, big build)
-It doesn't tell me the content length
-It puts a wierd "2000" on the line after the empty line
-The image saved is incomplete

I added a bunch of debug echo in the code:
Code: [Select]
function zgui_serverlist::loadserverimage(%this, %id)
{
if(%id < %this.getcount())
{
filedelete("config/zgui_serverdetail.jpg");
%ip = %this.getobject(%id).ip;
%port = %this.getobject(%id).port;
%data = "q=" @ strreplace(%ip, "." , "-") @ "_" @ %port;
%len = strlen(%data);
%request = "GET /detail.php?" @ %data @ " HTTP/1.1\r\nHost:image.blockland.us\r\nUser-Agent: Torque/1.0\r\nContent-Type: text/html\r\nContent-Length: " @ %len @"\r\n\r\n" @ %data;
%tcp = new TCPObject(zgui_dlserverimagetcp)
{
request = %request;
};
zgui_container.add(%tcp);
%this.dlimagetcp = %tcp;
%tcp.connect("image.blockland.us:80");
}
}

function zgui_dlserverimagetcp::onConnected(%this)
{
echo("CONNECTED");
echo("SEND " @ %this.request);
%this.send(%this.request);
}

function zgui_dlserverimagetcp::onLine(%this, %line)
{
echo("RECEIVE" SPC %line);
if(strstr(%line, "Content-Length:") == 0)
{
%this.haslen = 1;
%this.len = getword(%line, 1);
echo("We know length, save when chunk is bigger than len");
}
else if(%line $= "" && !%this.haslen)
{
%this.getnext = 1;
echo("Server didn't tell us the length wtf wtf");
}
else if(%line $= "" && %this.haslen)
{
%this.setbinary(1);
}
else if(%this.getnext)
{
echo("Ok what is up with this 2000 after the empty line?");
%this.setbinary(1);
}
}

function zgui_dlserverimagetcp::onBinChunk(%this, %chunk)
{
echo("CHUNK" SPC %chunk);
if(%this.haslen)
{
if(%chunk >= %this.len)
{
echo("save because chunk is bigger than len");
%this.savebuffertofile("config/client/zgui_serverdetail.jpg");
%this.schedule(10,delete);
zgui_serverdetail_image.setbitmap("config/client/zgui_serverdetail.jpg");
}
}
else
{
cancel(%this.schedule);
%this.schedule = %this.schedule(1000,endload);
}
}

function zgui_dlserverimagetcp::endload(%this)
{
echo("server didn't tell us the length, saving by timeout");
%this.savebuffertofile("config/client/zgui_serverdetail.jpg");
%this.schedule(10,delete);
zgui_serverdetail_image.setbitmap("config/client/zgui_serverdetail.jpg");
}

Here is what it looks like for the 2 ways the server responds:
Code: [Select]
CONNECTED
SEND GET /detail.php?q=69-64-43-11_32500 HTTP/1.1
Host:image.blockland.us
User-Agent: Torque/1.0
Content-Type: text/html
Content-Length: 19

q=69-64-43-11_32500
RECEIVE HTTP/1.1 200 OK
RECEIVE Date: Wed, 27 Feb 2013 16:40:59 GMT
RECEIVE Server: Apache/2.2.3 (Red Hat)
RECEIVE X-Powered-By: PHP/5.1.6
RECEIVE Vary: Accept-Encoding
RECEIVE Content-Length: 6864
We know length, save when chunk is bigger than len
RECEIVE Content-Type: image/jpeg
RECEIVE
CHUNK 1316
CHUNK 2816
CHUNK 4316
CHUNK 5816
CHUNK 6864
save because chunk is bigger than len

Code: [Select]
CONNECTED
SEND GET /detail.php?q=68-168-212-106_30200 HTTP/1.1
Host:image.blockland.us
User-Agent: Torque/1.0
Content-Type: text/html
Content-Length: 22

q=68-168-212-106_30200
RECEIVE HTTP/1.1 200 OK
RECEIVE Date: Wed, 27 Feb 2013 16:41:19 GMT
RECEIVE Server: Apache/2.2.3 (Red Hat)
RECEIVE X-Powered-By: PHP/5.1.6
RECEIVE Vary: Accept-Encoding
RECEIVE Transfer-Encoding: chunked
RECEIVE Content-Type: image/jpeg
RECEIVE
Server didn't tell us the length wtf wtf
RECEIVE 2000
Ok what is up with this 2000 after the empty line?
CHUNK 1304
CHUNK 2804
-snip-
CHUNK 66494
CHUNK 67994
CHUNK 69478
server didn't tell us the length, saving by timeout

Can someone tell me how to handle saving the file if the server responds in the second way, probably something to do with this line:
Code: [Select]
Transfer-Encoding: chunked
« Last Edit: February 27, 2013, 11:49:11 AM by Zeblote »

Why does noone respond :(

Some images use chunked encoding which can't be downloaded properly using torque.
The solution I used for blota was a proxy hosted online that downloaded them from the blockland website and relayed the data to the torque client in a sensible fashion.

The solution I used for blota was a proxy hosted online that downloaded them from the blockland website and relayed the data to the torque client in a sensible fashion.
Ok
How does I do this

Some images use chunked encoding which can't be downloaded properly using torque.
The solution I used for blota was a proxy hosted online that downloaded them from the blockland website and relayed the data to the torque client in a sensible fashion.
Blockland's server previews by default use encoded chunks, and I managed to get it to work fine. Although, I believe if you specify that you are connecting through HTTP 1.0 it no longer chunks it.

I believe if you specify that you are connecting through HTTP 1.0 it no longer chunks it.
If I replace 1.1 with 1.0 it doesn't tell me that it sends it chunked, it still doesn't tell me the content length, and it saves a file with the amazing size of 0b!

D:

Ok I found this
http://forum.blockland.us/index.php?topic=203181.msg5638599#msg5638599
mctwist has already made a relay site :D it's working :D

But, how would I make it that my mod doesn't depend on his site?

Ok I found this
http://forum.blockland.us/index.php?topic=203181.msg5638599#msg5638599
mctwist has already made a relay site :D it's working :D
mctwists site doesn't work anymore D:

Help what do