refactor(socket): delete socket

This commit is contained in:
2026-03-16 23:09:26 +01:00
parent d777b9be7f
commit f729463ab3
5 changed files with 4 additions and 179 deletions
+1 -7
View File
@@ -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',
)
-90
View File
@@ -1,90 +0,0 @@
#include "mysocket.h"
#include <errno.h>
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;
}
-36
View File
@@ -1,36 +0,0 @@
#ifndef MYCLIB_SOCKET_H
#define MYCLIB_SOCKET_H
#include <stddef.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
typedef int socket_t;
#else
#include <sys/socket.h>
#include <unistd.h>
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
-43
View File
@@ -1,43 +0,0 @@
#include "../socket/mysocket.h"
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
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();
}
+3 -3
View File
@@ -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("");