feat(socket): add socket helpers
This commit is contained in:
10
include/utils/socket.h
Normal file
10
include/utils/socket.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef CWS_SOCKET_H
|
||||||
|
#define CWS_SOCKET_H
|
||||||
|
|
||||||
|
#include <myclib/mystring.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
ssize_t cws_read_data(int sockfd, string_s *str);
|
||||||
|
ssize_t cws_send_data(int sockfd, char *buffer, int len, int flags);
|
||||||
|
|
||||||
|
#endif
|
||||||
53
src/utils/socket.c
Normal file
53
src/utils/socket.c
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
#include "utils/socket.h"
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
ssize_t cws_read_data(int sockfd, string_s *str) {
|
||||||
|
char tmp[4096] = {0};
|
||||||
|
|
||||||
|
ssize_t n = recv(sockfd, tmp, sizeof tmp, 0);
|
||||||
|
if (n < 0) {
|
||||||
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||||
|
/* No data available right now */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n == 0) {
|
||||||
|
/* Connection closed */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp[n] = '\0';
|
||||||
|
string_append(str, tmp);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t cws_send_data(int sockfd, char *buffer, int len, int flags) {
|
||||||
|
ssize_t total_sent = 0;
|
||||||
|
ssize_t n;
|
||||||
|
|
||||||
|
while (total_sent < len) {
|
||||||
|
n = send(sockfd, buffer + total_sent, len - total_sent, flags);
|
||||||
|
|
||||||
|
if (n < 0) {
|
||||||
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||||
|
/* Buffer full, return bytes sent */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
total_sent += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total_sent;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user