implemented socket_setTimeout
This commit is contained in:
@@ -10,20 +10,28 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// include OS-dependent socket headers
|
||||
#if KN_USE_WINSOCK
|
||||
#include <winsock2.h>
|
||||
// There you can see what error codes mean.
|
||||
#include <winerror.h>
|
||||
#define RESULT_ERROR_SOCKET() RESULT_ERROR(sprintf_malloc(64, "Winsock error %i", WSAGetLastError()), true)
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define RESULT_ERROR_SOCKET() RESULT_ERROR(strerror(errno), false)
|
||||
#endif
|
||||
|
||||
#if KN_USE_WINSOCK
|
||||
#define RESULT_ERROR_SOCKET()\
|
||||
RESULT_ERROR(sprintf_malloc(64, "Winsock error %i (look in <winerror.h>)", WSAGetLastError()), true);
|
||||
#else
|
||||
#define RESULT_ERROR_SOCKET()\
|
||||
RESULT_ERROR(strerror(errno), false);
|
||||
#endif
|
||||
|
||||
struct sockaddr_in EndpointIPv4_toSockaddr(EndpointIPv4 end);
|
||||
|
||||
@@ -20,7 +20,30 @@ void socket_close(Socket s){
|
||||
}
|
||||
|
||||
Result(void) socket_shutdown(Socket s, SocketShutdownType direction){
|
||||
if(shutdown(s, (int)direction) == -1)
|
||||
if(shutdown(s, (int)direction) != 0)
|
||||
return RESULT_ERROR_SOCKET();
|
||||
return RESULT_VOID;
|
||||
}
|
||||
|
||||
Result(void) socket_setTimeout(Socket s, u32 ms){
|
||||
void* opt;
|
||||
u32 optlen;
|
||||
|
||||
#if KN_USE_WINSOCK
|
||||
opt = &ms;
|
||||
optlen = sizeof(ms);
|
||||
#else
|
||||
struct timeval tv = {
|
||||
.tv_sec = ms/1000,
|
||||
.tv_usec = (ms%1000)*1000
|
||||
};
|
||||
opt = &tv;
|
||||
optlen = sizeof(tv);
|
||||
#endif
|
||||
|
||||
if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, opt, optlen) != 0)
|
||||
return RESULT_ERROR_SOCKET();
|
||||
if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, opt, optlen) != 0)
|
||||
return RESULT_ERROR_SOCKET();
|
||||
return RESULT_VOID;
|
||||
}
|
||||
@@ -38,18 +61,18 @@ Result(void) socket_listen(Socket s, i32 backlog){
|
||||
return RESULT_VOID;
|
||||
}
|
||||
|
||||
Result(Socket) socket_accept(Socket main_socket, NULLABLE(EndpointIPv4*) remote_end) {
|
||||
Result(Socket) socket_accept(Socket listening_sock, NULLABLE(EndpointIPv4*) remote_end) {
|
||||
struct sockaddr_in remote_addr = {0};
|
||||
i32 sockaddr_size = sizeof(remote_addr);
|
||||
Socket user_connection = accept(main_socket, (void*)&remote_addr, (void*)&sockaddr_size);
|
||||
if(user_connection == -1)
|
||||
Socket accepted_sock = accept(listening_sock, (void*)&remote_addr, (void*)&sockaddr_size);
|
||||
if(accepted_sock == -1)
|
||||
return RESULT_ERROR_SOCKET();
|
||||
|
||||
//TODO: add IPV6 support (struct sockaddr_in6)
|
||||
assert(sockaddr_size == sizeof(remote_addr));
|
||||
if(remote_end)
|
||||
*remote_end = EndpointIPv4_fromSockaddr(remote_addr);
|
||||
return RESULT_VALUE(i, user_connection);
|
||||
return RESULT_VALUE(i, accepted_sock);
|
||||
}
|
||||
|
||||
Result(void) socket_connect(Socket s, EndpointIPv4 remote_end){
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include "endpoint.h"
|
||||
#include "tlibc/errors.h"
|
||||
#include "tlibc/collections/Array.h"
|
||||
#include "tlibc/time.h"
|
||||
|
||||
typedef enum SocketShutdownType {
|
||||
SocketShutdownType_Receive = 0,
|
||||
@@ -18,13 +17,19 @@ typedef enum SocketRecvFlag {
|
||||
|
||||
typedef i64 Socket;
|
||||
|
||||
#define SOCKET_TIMEOUT_MS_DEFAULT 5000
|
||||
#define SOCKET_TIMEOUT_MS_INFINITE 0
|
||||
|
||||
Result(Socket) socket_open_TCP();
|
||||
void socket_close(Socket s);
|
||||
Result(void) socket_shutdown(Socket s, SocketShutdownType direction);
|
||||
Result(void) socket_setTimeout(Socket s, u32 ms);
|
||||
|
||||
Result(void) socket_bind(Socket s, EndpointIPv4 local_end);
|
||||
Result(void) socket_listen(Socket s, i32 backlog);
|
||||
Result(Socket) socket_accept(Socket s, NULLABLE(EndpointIPv4*) remote_end);
|
||||
Result(Socket) socket_accept(Socket listening_sock, NULLABLE(EndpointIPv4*) remote_end);
|
||||
Result(void) socket_connect(Socket s, EndpointIPv4 remote_end);
|
||||
|
||||
Result(void) socket_send(Socket s, Array(u8) buffer);
|
||||
Result(void) socket_sendto(Socket s, Array(u8) buffer, EndpointIPv4 dst);
|
||||
Result(i32) socket_recv(Socket s, Array(u8) buffer, SocketRecvFlag flags);
|
||||
|
||||
Reference in New Issue
Block a user