Raised This Month: $32 Target: $400
 8% 

Download Files with different Sizes


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
dordnung
Veteran Member
Join Date: Apr 2010
Old 12-23-2010 , 06:58   Download Files with different Sizes
Reply With Quote #1

Hi,

i use the socket extension to download files.
All the Files are .zip, or .rar files, and have a maxsize of 200 mb.

Now i wrote a little script with the socket extension (i took the example.sp).
But it do not download the hole file, only some parts. When i print the dataSize to the server, and add them, i get the right size of the file, but when i count the receiveData String, it's far too little.

PHP Code:
#include <sourcemod>
#include <socket>

new String:TheUrl[257];

public 
OnConfigsExecuted()
{
    
DownloadMap("maps/weapons.zip""addons/sourcemod/test.zip");
}

public 
DownloadMap(const String:url[], const String:FileName[])
{
    
Format(TheUrlsizeof(TheUrl), url);
    new 
Handle:socket SocketCreate(SOCKET_TCPOnSocketError);
    new 
Handle:hFile OpenFile(FileName"wb");
    
SocketSetArg(sockethFile);
    
SocketConnect(socketOnSocketConnectedOnSocketReceiveOnSocketDisconnected"files.fpsbanana.com"80)
}

public 
OnSocketError(Handle:socket, const errorType, const errorNumany:hFile
{
    
LogError("socket error %d (errno %d)"errorTypeerrorNum);
    
CloseHandle(hFile);
    
CloseHandle(socket);
}

public 
OnSocketConnected(Handle:socketany:arg
{
    
decl String:requestStr[256];
    
Format(requestStrsizeof(requestStr), "GET /%s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n"TheUrl"files.fpsbanana.com");
    
SocketSend(socketrequestStr);
}

public 
OnSocketReceive(Handle:socketString:receiveData[], const dataSizeany:hFile
{
    new 
symbols strlen(receiveData);
    
PrintToServer("%i - %i"symbolsdataSize)
    
WriteFileString(hFilereceiveDatafalse);
}

public 
OnSocketDisconnected(Handle:socketany:hFile
{
    
CloseHandle(hFile);
    
CloseHandle(socket);

__________________
dordnung is offline
AltPluzF4
Senior Member
Join Date: Aug 2007
Old 12-23-2010 , 10:01   Re: Download Files with different Sizes
Reply With Quote #2

You're working with binary files, not strings.
Socket's include file says it's binary safe as long as you use the dataSize.

new symbols = strlen(receiveData);
Is bad, since if the data contains a null byte anywhere before the end (dataSize) then it will stop there. Giving less than expected size.
Also, WriteFileString takes the second parameter as LocalToString and runs strlen on it. So, same issue.

What you should do, is instead replace it with something like this: (just an example, I can't test, but should work for you.)
PHP Code:
OnSocketReceive(Handle:socketString:receiveData[], const dataSizeany:hFile
{
    
PrintToServer("OnSocketReceive: %d bytes"dataSize);
    
WriteFile(hFile_:receiveDatadataSize1);

__________________
AltPluzF4 is offline
dordnung
Veteran Member
Join Date: Apr 2010
Old 12-23-2010 , 10:39   Re: Download Files with different Sizes
Reply With Quote #3

Ok, the file size is now rigth.

But i can't open the file, and the content is also not the same as when i open the downloaded file.

BTW. what means "_:"?
__________________
dordnung is offline
AltPluzF4
Senior Member
Join Date: Aug 2007
Old 12-23-2010 , 10:49   Re: Download Files with different Sizes
Reply With Quote #4

_: strips a tag.
WriteFile expects an untagged cell array. Passing a String: tagged array would throw a warning about tag mismatch.

Please refer here if you are still confused:
http://wiki.alliedmods.net/Introduct...wn#Variables_2

Also, I'm not sure how socket passes the data.
Create a global cell called g_FileSize. Set it to 0 before sending the SocketConnect function.
Now in the OnSocketReceive function, do g_FileSize += dataSize;
Then in OnSocketDisconnected, print out the value of g_FileSize

Then please post back here with:
A.) g_FileSize's value after successful download.
B.) File size of downloaded file (through plugin)
C.) File size of downloaded file (through web browser)

This should let me see what's wrong. Thanks for your time.
__________________
AltPluzF4 is offline
dordnung
Veteran Member
Join Date: Apr 2010
Old 12-23-2010 , 11:00   Re: Download Files with different Sizes
Reply With Quote #5

Ah ok, i understand (:

A.) 44152 bytes
B.) 44152 bytes
C.) 43844 bytes

I added the Original File(weapons.zip) and the through plugin downloaded file(test.zip).
Attached Files
File Type: zip weapons.zip (42.8 KB, 62 views)
File Type: zip test.zip (43.1 KB, 129 views)
__________________
dordnung is offline
AltPluzF4
Senior Member
Join Date: Aug 2007
Old 12-23-2010 , 11:17   Re: Download Files with different Sizes
Reply With Quote #6

Sorry for the trouble, but can you please redownload the test.zip by using
PHP Code:
WriteFile(hFile_:receiveDatadataSize4); 
Just so I can see what it does assuming it's using a 4 byte cell :-/
__________________
AltPluzF4 is offline
dordnung
Veteran Member
Join Date: Apr 2010
Old 12-23-2010 , 11:41   Re: Download Files with different Sizes
Reply With Quote #7

A.) 44152 bytes
B.) 176608 bytes?

The File is 4 times bigger (;
Attached Files
File Type: zip test.zip (172.5 KB, 137 views)
__________________
dordnung is offline
AltPluzF4
Senior Member
Join Date: Aug 2007
Old 12-23-2010 , 11:45   Re: Download Files with different Sizes
Reply With Quote #8

Yeah, just as I expected. It's in 4 bytes.

Code:
HTTP/1.0 200 OK
Expires: Sat, 22 Jan 2011 16:39:12 GMT
Cache-Control: max-age=2592000
Content-Type: application/zip
Accept-Ranges: bytes
ETag: "-1177987035"
Last-Modified: Wed, 20 Oct 2010 15:27:52 GMT
Content-Length: 43844
Connection: close
Date: Thu, 23 Dec 2010 16:39:12 GMT
Server: lighttpd

PK
So, you need to check the response, then start handling the buffer
__________________
AltPluzF4 is offline
dordnung
Veteran Member
Join Date: Apr 2010
Old 12-23-2010 , 11:51   Re: Download Files with different Sizes
Reply With Quote #9

But when i delete it, it also not working, so when i delete the Header
__________________
dordnung is offline
AltPluzF4
Senior Member
Join Date: Aug 2007
Old 12-23-2010 , 13:15   Re: Download Files with different Sizes
Reply With Quote #10

Alright, so, it's sending multiple chunks, each as a 4 byte cell representing a single byte...

This code should work for you.
PHP Code:

#pragma semicolon 1
#include <sourcemod>
#include <socket>

new String:TheUrl[257];
new 
g_iChunk;

public 
OnConfigsExecuted()
{
    
DownloadMap("maps/weapons.zip""addons/sourcemod/test.zip");
}

public 
DownloadMap(const String:url[], const String:FileName[])
{
    
Format(TheUrlsizeof(TheUrl), url);
    new 
Handle:socket SocketCreate(SOCKET_TCPOnSocketError);
    new 
Handle:hFile OpenFile(FileName"wb");
    
SocketSetArg(sockethFile);
    
SocketConnect(socketOnSocketConnectedOnSocketReceiveOnSocketDisconnected"files.fpsbanana.com"80);
}

public 
OnSocketError(Handle:socket, const errorType, const errorNumany:hFile
{
    
LogError("socket error %d (errno %d)"errorTypeerrorNum);
    
CloseHandle(hFile);
    
CloseHandle(socket);
}

public 
OnSocketConnected(Handle:socketany:arg
{
    
decl String:requestStr[256];
    
Format(requestStrsizeof(requestStr), "GET /%s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n"TheUrl"files.fpsbanana.com");
    
g_iChunk 0;
    
SocketSend(socketrequestStr);
}

public 
OnSocketReceive(Handle:socketString:receiveData[], const dataSizeany:hFile)
{
    
g_iChunk++;
    new 
pos 0;
    if (
g_iChunk == 1)
    { 
//If first chunk, strip response header (ends in double CRLF)
        
pos 4;
        while (!(
receiveData[pos-4] == '\r' && receiveData[pos-3] == '\n'
            
&& receiveData[pos-2] == '\r' && receiveData[pos-1] == '\n'))
            
pos++;
    }
    
PrintToServer("Chunk %d is %d bytes Offset: %d"g_iChunkdataSizepos);
    
WriteFile(hFile_:receiveData[pos], (dataSize-pos)/44); //We get each byte in a 4 byte cell
}

public 
OnSocketDisconnected(Handle:socketany:hFile
{
    
CloseHandle(hFile);
    
CloseHandle(socket);

__________________
AltPluzF4 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode