Create socket_receive_all_nonblocking

This commit is contained in:
twinaphex 2016-05-02 02:09:22 +02:00
parent fab90f4b96
commit 31f3fc0abb
3 changed files with 36 additions and 27 deletions

View File

@ -50,6 +50,9 @@ int socket_send_all_blocking(int fd, const void *data_, size_t size, bool no_sig
int socket_receive_all_blocking(int fd, void *data_, size_t size);
ssize_t socket_receive_all_nonblocking(int fd, bool *error,
void *data_, size_t size);
bool socket_bind(int fd, void *data);
int socket_connect(int fd, void *data, bool timeout_enable);

View File

@ -105,33 +105,12 @@ static void net_http_send_str(int fd, bool *error, const char *text)
}
}
static ssize_t net_http_recv(int fd, bool *error,
uint8_t *data, size_t maxlen)
{
ssize_t bytes;
if (*error)
return -1;
bytes = recv(fd, (char*)data, maxlen, 0);
if (bytes > 0)
return bytes;
else if (bytes == 0)
return -1;
else if (isagain(bytes))
return 0;
*error=true;
return -1;
}
static char* urlencode(const char* url)
{
unsigned i;
unsigned outpos = 0;
unsigned outlen = 0;
char *ret = NULL;
char *ret = NULL;
for (i = 0; url[i] != '\0'; i++)
{
@ -326,8 +305,11 @@ bool net_http_update(struct http_t *state, size_t* progress, size_t* total)
if (state->part < P_BODY)
{
newlen = net_http_recv(state->fd, &state->error,
(uint8_t*)state->data + state->pos, state->buflen - state->pos);
if (state->error)
newlen = -1;
else
newlen = socket_receive_all_nonblocking(state->fd, &state->error,
(uint8_t*)state->data + state->pos, state->buflen - state->pos);
if (newlen < 0)
goto fail;
@ -392,9 +374,14 @@ bool net_http_update(struct http_t *state, size_t* progress, size_t* total)
{
if (!newlen)
{
newlen = net_http_recv(state->fd, &state->error,
(uint8_t*)state->data + state->pos,
state->buflen - state->pos);
if (state->error)
newlen = -1;
else
newlen = socket_receive_all_nonblocking(
state->fd,
&state->error,
(uint8_t*)state->data + state->pos,
state->buflen - state->pos);
if (newlen < 0)
{

View File

@ -70,6 +70,25 @@ error:
return -1;
}
ssize_t socket_receive_all_nonblocking(int fd, bool *error,
void *data_, size_t size)
{
const uint8_t *data = (const uint8_t*)data_;
ssize_t ret = recv(fd, (char*)data, size, 0);
if (ret > 0)
return ret;
if (ret == 0)
return -1;
if (isagain(ret))
return 0;
*error = true;
return -1;
}
int socket_receive_all_blocking(int fd, void *data_, size_t size)
{
const uint8_t *data = (const uint8_t*)data_;