Add support for Windows

This commit is contained in:
ReiMerc 2023-02-09 13:37:00 +01:00
parent 2e087254c9
commit 603ab98a14
6 changed files with 71 additions and 19 deletions

19
backend/GNUmakefile Normal file
View File

@ -0,0 +1,19 @@
CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion
HEADERS = $(wildcard *.h)
all: compile_flags.txt server
server: main.o linux.o
gcc $^ -o $@
%.o: %.c $(HEADERS)
gcc $< -c -o $@ $(CFLAGS)
clean:
rm -rf *.o server client
compile_flags.txt:
echo -xc $(CFLAGS) | sed 's/\s\+/\n/g' > compile_flags.txt

View File

@ -1,19 +1,12 @@
CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion OBJS=main.obj windows.obj
HEADERS = $(wildcard *.h) all: $(OBJS)
link /out:server.exe $(OBJS) WS2_32.lib
all: compile_flags.txt server .obj:
cl $*.c
server: main.o
gcc $^ -o $@
%.o: %.c $(HEADERS)
gcc $< -c -o $@ $(CFLAGS)
clean: clean:
rm -rf *.o server client del *.obj server.exe
compile_flags.txt:
echo -xc $(CFLAGS) | sed 's/\s\+/\n/g' > compile_flags.txt

11
backend/linux.c Normal file
View File

@ -0,0 +1,11 @@
#include "native.h"
void init_socket(void)
{
}
void close_socket(int socket)
{
close(socket);
}

View File

@ -1,12 +1,10 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/socket.h>
#include <unistd.h> #include "native.h"
typedef enum { typedef enum {
HttpMethodGet, HttpMethodGet,
@ -47,6 +45,8 @@ const uint16_t port = 8000;
int main(void) int main(void)
{ {
init_sockets();
printf("starting server...\n"); printf("starting server...\n");
int server_socket = socket(AF_INET, SOCK_STREAM, 0); int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) { if (server_socket < 0) {
@ -95,13 +95,13 @@ int main(void)
"<!DOCTYPE><html><head><meta charset=\"utf-8\"><title>hej med " "<!DOCTYPE><html><head><meta charset=\"utf-8\"><title>hej med "
"dig</title></head><body><h1>fuck c hashtag</h1></body></html>\r\n" "dig</title></head><body><h1>fuck c hashtag</h1></body></html>\r\n"
"\r\n"; "\r\n";
ssize_t written = write(client_socket, send_buffer, sizeof(send_buffer)); ssize_t written = send(client_socket, send_buffer, sizeof(send_buffer), 0);
if (written < 0) { if (written < 0) {
printf("error: could not write\n"); printf("error: could not write\n");
continue; continue;
} }
printf("disconnecting client\n"); printf("disconnecting client\n");
close(client_socket); close_socket(client_socket);
} }
} }

16
backend/native.h Normal file
View File

@ -0,0 +1,16 @@
#ifdef _WIN32
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
void init_sockets(void);
void close_socket(int socket);

13
backend/windows.c Normal file
View File

@ -0,0 +1,13 @@
#include "native.h"
void init_sockets(void)
{
WSADATA data;
WSAStartup(0x0202, &data);
}
void close_socket(int socket)
{
closesocket(socket);
}