Subversion Repositories SvarDOS

Compare Revisions

Ignore whitespace Rev 1131 → Rev 1132

/pkgnet/trunk/net.c
27,9 → 27,7
* 32K = 98 KiB/s
* 60K = 98 KiB/s
*/
#define TCPBUFF_SIZE (16 * 1024)
 
 
struct net_tcpsocket {
tcp_Socket *sock;
tcp_Socket _sock; /* watt32 socket */
64,8 → 62,8
}
 
 
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port) {
struct net_tcpsocket *resultsock, *resizsock;
struct net_tcpsocket *net_connect(const char *ipstr, unsigned short port, unsigned short buffsz) {
struct net_tcpsocket *resultsock;
unsigned long ipaddr;
 
/* convert ip to value */
72,7 → 70,10
ipaddr = _inet_addr(ipstr);
if (ipaddr == 0) return(NULL);
 
resultsock = calloc(sizeof(struct net_tcpsocket), 1);
/* ignore buffsz smaller than 2K (wattcp already has a 2K buffer) */
if (buffsz <= 2048) buffsz = 0;
 
resultsock = calloc(sizeof(struct net_tcpsocket) + buffsz, 1);
if (resultsock == NULL) return(NULL);
resultsock->sock = &(resultsock->_sock);
 
82,14 → 83,10
return(NULL);
}
 
/* set user-managed buffer if possible (watt32's default is only 2K)
/* set user-managed buffer if requested (watt32's default is only 2K)
* this must be set AFTER tcp_open(), since the latter rewrites the tcp
* rx buffer */
resizsock = realloc(resultsock, sizeof(struct net_tcpsocket) + TCPBUFF_SIZE);
if (resizsock != NULL) {
resultsock = resizsock;
sock_setbuf(resultsock->sock, resultsock->tcpbuff, TCPBUFF_SIZE);
}
if (buffsz > 0) sock_setbuf(resultsock->sock, resultsock->tcpbuff, buffsz);
 
return(resultsock);
}
/pkgnet/trunk/net.h
18,8 → 18,9
 
/* 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. */
struct net_tcpsocket *net_connect(const char *ip, unsigned short port);
* 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);
 
/* 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) {
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) {
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);
sock = net_connect(ipaddr, 80, tcpbuflen);
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;
size_t membufsz = 5000;
unsigned short tcpbufsz = (16 * 1024);
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)) {
membufsz = newsz;
printf("WILL USE CUSTOM RECV BUFF SIZE = %u\r\n", membufsz);
if ((newsz >= 0) && (newsz < 65500)) {
tcpbufsz = newsz;
printf("WILL USE CUSTOM TCP BUFF SIZE = %u\r\n", tcpbufsz);
}
}
}
 
/* allocate memory */
mem = malloc(sizeof(*mem) + membufsz);
mem = malloc(sizeof(*mem));
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, membufsz);
flen = htget(mem->ipaddr, mem->url, mem->outfname, &bsum, ispost, mem->buffer, sizeof(mem->buffer), tcpbufsz);
if (flen < 1) return(1);
 
if (mem->outfname[0] != 0) {