diff --git a/meson.build b/meson.build index ff622e5..fd99b11 100644 --- a/meson.build +++ b/meson.build @@ -14,7 +14,6 @@ add_global_arguments('-D_POSIX_C_SOURCE=200112L', language: 'c') lib_src = files( 'hashmap/myhashmap.c', 'queue/myqueue.c', - 'socket/mysocket.c', 'set/myset.c', 'stack/mystack.c', 'string/mystring.c', @@ -25,7 +24,6 @@ lib_src = files( test_src = files( 'test/hashmap/hm1.c', 'test/queue/queue1.c', - 'test/socket/socket1.c', 'test/set/set1.c', 'test/stack/stack1.c', 'test/string/str1.c', @@ -35,11 +33,8 @@ test_src = files( 'test/vector/vec1.c', ) -# Windows Socket lib -winsock_dep = cc.find_library('ws2_32', required: false) - # Include directories -inc_dir = include_directories('string', 'queue', 'hashmap', 'vector', 'stack', 'socket', 'set') +inc_dir = include_directories('string', 'queue', 'hashmap', 'vector', 'stack', 'set') if host_machine.system() == 'windows' win_inc_dir = include_directories('c:/include/') else @@ -64,7 +59,6 @@ install_headers( 'vector/myvector.h', 'set/myset.h', 'stack/mystack.h', - 'socket/mysocket.h', ], subdir: 'myclib', ) diff --git a/socket/mysocket.c b/socket/mysocket.c deleted file mode 100644 index afe6b02..0000000 --- a/socket/mysocket.c +++ /dev/null @@ -1,90 +0,0 @@ -#include "mysocket.h" -#include - -int sock_platform_init(void) { -#ifdef _WIN32 - WSADATA wsaData; - if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { - return -1; - } - if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { - WSACleanup(); - return -1; - } -#endif - return 0; -} - -int sock_close(socket_t socket) { - int ret = 0; -#ifdef _WIN32 - ret = closesocket(socket); -#else - ret = close(socket); -#endif - return ret; -} - -int sock_platform_shutdown(void) { -#ifdef _WIN32 - WSACleanup(); -#endif - return 0; -} - -int sock_readall(socket_t sockfd, void *buf, size_t bufsize) { - char *p = (char *)buf; - size_t total_read = 0; - - while (total_read < bufsize) { - int n = recv(sockfd, p, bufsize - total_read, 0); - - if (n > 0) { - /* Data received */ - p += n; - total_read += n; - } else if (n == 0) { - /* Connection closed */ - break; - } else { - /* Error */ - if (errno == EINTR) { - /* Try again */ - continue; - } else if (errno == EAGAIN || errno == EWOULDBLOCK) { - /* Socket non-blocking, no data right now */ - /* Returns what has read */ - return total_read; - } else { - return -1; - } - } - } - - return total_read; -} - -int sock_writeall(socket_t socket, const void *buf, size_t n) { - const char *p = (const char *)buf; - size_t bytes_to_write = n; - int bytes_written; - - while (bytes_to_write > 0) { - bytes_written = send(socket, p, bytes_to_write, 0); - - if (bytes_written >= 0) { - p += bytes_written; - bytes_to_write -= bytes_written; - } else { - if (errno == EINTR) { - continue; - } else if (errno == EAGAIN || errno == EWOULDBLOCK) { - return n - bytes_to_write; - } else { - return -1; - } - } - } - - return n; -} diff --git a/socket/mysocket.h b/socket/mysocket.h deleted file mode 100644 index ab4e64c..0000000 --- a/socket/mysocket.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef MYCLIB_SOCKET_H -#define MYCLIB_SOCKET_H - -#include - -#ifdef _WIN32 -#include -#include -typedef int socket_t; -#else -#include -#include -typedef int socket_t; -#endif - -/* Initialize the socket system */ -int sock_platform_init(void); - -/* Close a socket */ -int sock_close(socket_t socket); - -/* Clean the socket system */ -int sock_platform_shutdown(void); - -/* - * Read 'bufsize' bytes from socket - * Returns the read bytes, -1 on failure, 0 connection closed without issues - */ -int sock_readall(socket_t sockfd, void *buf, size_t bufsize); - -/* - * Writes 'n' bytes to socket - */ -int sock_writeall(socket_t socket, const void *buf, size_t n); - -#endif diff --git a/test/socket/socket1.c b/test/socket/socket1.c deleted file mode 100644 index 50e44c0..0000000 --- a/test/socket/socket1.c +++ /dev/null @@ -1,43 +0,0 @@ -#include "../socket/mysocket.h" -#include -#include -#include -#include -#include -#include - -void test_socket1(void) { - sock_platform_init(); - struct addrinfo hints, *res, *p; - char ipstr[INET6_ADDRSTRLEN]; - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - char hostname[1024]; - printf("hostname> "); - fscanf(stdin, "%s", hostname); - - if (getaddrinfo(hostname, NULL, &hints, &res) != 0) { - fprintf(stderr, "getaddrinfo() failed\n"); - sock_platform_shutdown(); - return; - } - - for (p = res; p != NULL; p = p->ai_next) { - void *addr = 0; - char *ipver = 0; - struct sockaddr_in *ipv4; - if (p->ai_family == AF_INET) { - ipv4 = (struct sockaddr_in *)p->ai_addr; - addr = &(ipv4->sin_addr); - ipver = "IPv4"; - } else { - continue; - } - inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); - printf("%s: %s\n", ipver, ipstr); - } - - freeaddrinfo(res); - sock_platform_shutdown(); -} diff --git a/test/test.c b/test/test.c index 7359600..c26fc65 100644 --- a/test/test.c +++ b/test/test.c @@ -17,7 +17,7 @@ void test_vec1(void); void test_stack1(void); -void test_socket1(void); +void test_set1(void); int main(void) { puts("==== [Running Hashmap tests] ===="); @@ -47,8 +47,8 @@ int main(void) { puts("OK!"); puts(""); - puts("==== [Running Socket tests] ===="); - test_socket1(); + puts("==== [Running Set tests] ===="); + test_set1(); puts("OK!"); puts("");