//pkgnet/trunk/net.c |
---|
27,7 → 27,9 |
* 32K = 98 KiB/s |
* 60K = 98 KiB/s |
*/ |
#define TCPBUFF_SIZE (16 * 1024) |
struct net_tcpsocket { |
tcp_Socket *sock; |
tcp_Socket _sock; /* watt32 socket */ |
62,8 → 64,8 |
} |
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port, unsigned short buffsz) { |
struct net_tcpsocket *resultsock; |
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port) { |
struct net_tcpsocket *resultsock, *resizsock; |
unsigned long ipaddr; |
/* convert ip to value */ |
70,10 → 72,7 |
ipaddr = _inet_addr(ipstr); |
if (ipaddr == 0) return(NULL); |
/* ignore buffsz smaller than 2K (wattcp already has a 2K buffer) */ |
if (buffsz <= 2048) buffsz = 0; |
resultsock = calloc(sizeof(struct net_tcpsocket) + buffsz, 1); |
resultsock = calloc(sizeof(struct net_tcpsocket), 1); |
if (resultsock == NULL) return(NULL); |
resultsock->sock = &(resultsock->_sock); |
83,10 → 82,14 |
return(NULL); |
} |
/* set user-managed buffer if requested (watt32's default is only 2K) |
/* set user-managed buffer if possible (watt32's default is only 2K) |
* this must be set AFTER tcp_open(), since the latter rewrites the tcp |
* rx buffer */ |
if (buffsz > 0) sock_setbuf(resultsock->sock, resultsock->tcpbuff, buffsz); |
resizsock = realloc(resultsock, sizeof(struct net_tcpsocket) + TCPBUFF_SIZE); |
if (resizsock != NULL) { |
resultsock = resizsock; |
sock_setbuf(resultsock->sock, resultsock->tcpbuff, TCPBUFF_SIZE); |
} |
return(resultsock); |
} |
//pkgnet/trunk/net.h |
---|
18,9 → 18,8 |
/* initiates a connection to an IP host and returns a socket pointer (or NULL |
* on error) - note that connection is NOT established at this point! |
* use net_isconnected() to know when the connection is connected. |
* buffsz is the size of the TCP buffer that will be allocated by net_connect. */ |
struct net_tcpsocket *net_connect(const char *ip, unsigned short port, unsigned short buffsz); |
* use net_isconnected() to know when the connection is connected. */ |
struct net_tcpsocket *net_connect(const char *ip, unsigned short port); |
/* checks whether or not a socket is connected. returns: |
* 0 = not connected, |
//pkgnet/trunk/pkgnet.c |
---|
246,7 → 246,7 |
* is ispost is non-zero, then the request is a POST and its body data is |
* obtained through repeated calls to checkupdata() |
* returns the length of data obtained, or neg value on error */ |
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum, int ispost, unsigned char *buffer, size_t buffersz, unsigned short tcpbuflen) { |
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum, int ispost, unsigned char *buffer, size_t buffersz) { |
struct net_tcpsocket *sock; |
time_t lastactivity, lastprogressoutput = 0; |
int httpcode = -1, ischunked = 0; |
260,7 → 260,7 |
buffersz -= sizeof(*unchstate); |
memset(unchstate, 0, sizeof(*unchstate)); |
sock = net_connect(ipaddr, 80, tcpbuflen); |
sock = net_connect(ipaddr, 80); |
if (sock == NULL) { |
printf(svarlang_strid(0x0902), HOSTADDR); /* "ERROR: failed to connect to " HOSTADDR */ |
puts(""); |
413,14 → 413,14 |
int main(int argc, char **argv) { |
unsigned short bsum = 0; |
long flen; |
unsigned short tcpbufsz = (16 * 1024); |
size_t membufsz = 5000; |
int ispost; /* is the request a POST? */ |
struct { |
unsigned char buffer[5000]; |
char ipaddr[64]; |
char url[64]; |
char outfname[16]; |
unsigned char buffer[1]; |
} *mem; |
svarlang_autoload("PKGNET"); |
430,15 → 430,15 |
const char *ptr = getenv("PKGNETBUFSZ"); |
if (ptr != NULL) { |
long newsz = atol(ptr); |
if ((newsz >= 0) && (newsz < 65500)) { |
tcpbufsz = newsz; |
printf("WILL USE CUSTOM TCP BUFF SIZE = %u\r\n", tcpbufsz); |
if ((newsz > 0) && (newsz < 65500)) { |
membufsz = newsz; |
printf("WILL USE CUSTOM RECV BUFF SIZE = %u\r\n", membufsz); |
} |
} |
} |
/* allocate memory */ |
mem = malloc(sizeof(*mem)); |
mem = malloc(sizeof(*mem) + membufsz); |
if (mem == NULL) { |
putsnls(9, 9); /* "ERROR: out of memory" */ |
return(1); |
467,7 → 467,7 |
return(1); |
} |
flen = htget(mem->ipaddr, mem->url, mem->outfname, &bsum, ispost, mem->buffer, sizeof(mem->buffer), tcpbufsz); |
flen = htget(mem->ipaddr, mem->url, mem->outfname, &bsum, ispost, mem->buffer, membufsz); |
if (flen < 1) return(1); |
if (mem->outfname[0] != 0) { |