From f8ac67c943244aa57e5f848ca6b1f66eca32e138 Mon Sep 17 00:00:00 2001 From: cflip Date: Sun, 26 Mar 2023 10:21:47 -0600 Subject: Add support for building on Windows This makes it possible to compile cfws with Visual Studio. Since winsock and POSIX use very similar APIs, porting is mostly just a matter of placing ifdefs around #includes and functions with slightly different names. However, CGI scripts and command line arguments are not available in this port yet, since they used the Unix-exclusive getopt.h and popen. --- src/ServerConnection.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'src/ServerConnection.cpp') diff --git a/src/ServerConnection.cpp b/src/ServerConnection.cpp index 64692b6..0a47dc3 100644 --- a/src/ServerConnection.cpp +++ b/src/ServerConnection.cpp @@ -1,13 +1,17 @@ #include "ServerConnection.h" -#include -#include -#include -#include -#include -#include #include -#include + +#ifdef _WIN32 + #include + #pragma comment(lib, "ws2_32.lib") +#else + #include + #include + #include + #include + #include +#endif #include "ClientConnection.h" @@ -19,14 +23,22 @@ static void error_and_die(const char* message) ServerConnection::ServerConnection(int port) { +#ifdef _WIN32 + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) + error_and_die("Failed to initialize Winsock"); +#endif + sockaddr_in address {}; int socket_options = 1; if ((m_socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) error_and_die("Failed to create socket"); +#ifndef _WIN32 if (setsockopt(m_socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &socket_options, sizeof(socket_options)) != 0) error_and_die("setsockopt"); +#endif address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(INADDR_ANY); -- cgit v1.2.3