/pkgnet/net.c |
---|
68,8 → 68,7 |
} |
int net_isconnected(struct net_tcpsocket *s, int waitstate) { |
waitstate = waitstate; /* gcc warning shut */ |
int net_isconnected(struct net_tcpsocket *s) { |
if (tcp_tick(s->sock) == 0) return(-1); |
if (sock_established(s->sock) == 0) return(0); |
return(1); |
78,24 → 77,20 |
/* Sends data on socket 'socket'. |
Returns the number of bytes sent on success, and < 0 otherwise */ |
int net_send(struct net_tcpsocket *socket, const char *line, long len) { |
int res; |
int net_send(struct net_tcpsocket *socket, const void *line, long len) { |
/* call this to let Watt-32 handle its internal stuff */ |
if (tcp_tick(socket->sock) == 0) return(-1); |
/* send bytes */ |
res = sock_write(socket->sock, (void *)line, len); |
return(res); |
return(sock_write(socket->sock, line, len)); |
} |
/* Reads data from socket 'sock' and write it into buffer 'buff', until end of connection. Will fall into error if the amount of data is bigger than 'maxlen' bytes. |
Returns the amount of data read (in bytes) on success, or a negative value otherwise. The error code can be translated into a human error message via libtcp_strerr(). */ |
int net_recv(struct net_tcpsocket *socket, char *buff, long maxlen) { |
int i; |
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen) { |
/* call this to let WatTCP handle its internal stuff */ |
if (tcp_tick(socket->sock) == 0) return(-1); |
i = sock_fastread(socket->sock, (void *)buff, maxlen); |
return(i); |
return(sock_fastread(socket->sock, buff, maxlen)); |
} |
/pkgnet/net.h |
---|
27,19 → 27,16 |
/* checks whether or not a socket is connected. returns: |
* 0 = not connected, |
* 1 = connected |
* -1 = error |
* |
* if waitstate is non-zero, then net_isconnected() may release a few cpu |
* cycles (useful when net_isconnected() is used within a busy loop). */ |
int net_isconnected(struct net_tcpsocket *s, int waitstate); |
* -1 = error */ |
int net_isconnected(struct net_tcpsocket *s); |
/* Sends data on socket 'socket'. |
Returns the number of bytes sent on success, and <0 otherwise. The error code can be translated into a human error message via libtcp_strerr(). */ |
int net_send(struct net_tcpsocket *socket, const char *line, long len); |
int net_send(struct net_tcpsocket *socket, const void *line, long len); |
/* Reads data from socket 'sock' and write it into buffer 'buff', until end of connection. Will fall into error if the amount of data is bigger than 'maxlen' bytes. |
Returns the amount of data read (in bytes) on success, or a negative value otherwise. The error code can be translated into a human error message via libtcp_strerr(). */ |
int net_recv(struct net_tcpsocket *socket, char *buff, long maxlen); |
int net_recv(struct net_tcpsocket *socket, void *buff, long maxlen); |
/* Close the 'sock' socket. */ |
void net_close(struct net_tcpsocket *socket); |
/pkgnet/pkgnet.c |
---|
128,7 → 128,7 |
/* wait for net_connect() to actually connect */ |
for (;;) { |
int connstate = net_isconnected(sock, 1); |
int connstate = net_isconnected(sock); |
if (connstate > 0) break; |
if (connstate < 0) { |
puts("ERROR: connection error"); |
140,7 → 140,7 |
/* socket is connected - send the http request */ |
snprintf((char *)buffer, sizeof(buffer), "GET %s HTTP/1.1\r\nHOST: " HOSTADDR "\r\nUSER-AGENT: pkgnet\r\nConnection: close\r\n\r\n", url); |
if (net_send(sock, (char *)buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) { |
if (net_send(sock, buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) { |
puts("ERROR: failed to send HTTP query to remote server"); |
goto SHITQUIT; |
} |
147,7 → 147,7 |
lastactivity = time(NULL); |
for (;;) { |
int byteread = net_recv(sock, (char *)buffer, sizeof(buffer) - 1); /* -1 because I will append a NULL terminator */ |
int byteread = net_recv(sock, buffer, sizeof(buffer) - 1); /* -1 because I will append a NULL terminator */ |
if (byteread < 0) break; /* end of connection */ |