refactor(socket): update sock_readall
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#include "mysocket.h"
|
#include "mysocket.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
int sock_platform_init() {
|
int sock_platform_init() {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -38,32 +38,38 @@ int sock_platform_shutdown() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sock_readall(int sockfd, void *buf, size_t bufsize) {
|
||||||
int sock_readall(int socket, void *out, size_t n) {
|
char *p = (char *)buf;
|
||||||
char *p = (char *)out;
|
size_t total_read = 0;
|
||||||
size_t bytes_to_read = n;
|
|
||||||
|
|
||||||
|
while (1) {
|
||||||
while (bytes_to_read > 0) {
|
int n = recv(sockfd, p, bufsize - total_read, 0);
|
||||||
|
|
||||||
|
if (n > 0) {
|
||||||
if (bytes_read > 0) {
|
p += n;
|
||||||
p += bytes_read;
|
total_read += n;
|
||||||
bytes_to_read -= bytes_read;
|
|
||||||
} else if (bytes_read == 0) {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
if (errno == EINTR) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (total_read == bufsize) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (n == 0) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||||
|
break;
|
||||||
|
} else if (errno == EINTR) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return total_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sock_writeall(int socket, const void* buf, size_t n) {
|
||||||
int sock_writeall(int socket, const void *buf, size_t n) {
|
const char* p = (char*)buf;
|
||||||
size_t bytes_to_write = n;
|
size_t bytes_to_write = n;
|
||||||
int bytes_written;
|
int bytes_written;
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ int sock_platform_init();
|
|||||||
int sock_close(int socket);
|
int sock_close(int socket);
|
||||||
|
|
||||||
/* Read/Write all to socket */
|
/* Read/Write all to socket */
|
||||||
int sock_readall(int socket, void *out, size_t n);
|
int sock_readall(int sockfd, void *buf, size_t bufsize);
|
||||||
int sock_writeall(int socket, const void *buf, size_t n);
|
int sock_writeall(int socket, const void *buf, size_t n);
|
||||||
|
|
||||||
/* Use at exit */
|
/* Use at exit */
|
||||||
|
|||||||
Reference in New Issue
Block a user