diff options
author | cflip <cflip@cflip.net> | 2023-03-26 10:21:47 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-03-26 10:21:47 -0600 |
commit | f8ac67c943244aa57e5f848ca6b1f66eca32e138 (patch) | |
tree | 2bf4f6695393b993ad3a50528304fdb6e6a0b86a /src/ServerConnection.cpp | |
parent | 83fb0b96c94e7f596f81d5bc346150904457ed64 (diff) |
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.
Diffstat (limited to 'src/ServerConnection.cpp')
-rw-r--r-- | src/ServerConnection.cpp | 26 |
1 files changed, 19 insertions, 7 deletions
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 <arpa/inet.h> -#include <csignal> -#include <netdb.h> -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <sys/time.h> #include <sys/types.h> -#include <unistd.h> + +#ifdef _WIN32 + #include <winsock2.h> + #pragma comment(lib, "ws2_32.lib") +#else + #include <arpa/inet.h> + #include <sys/ioctl.h> + #include <sys/socket.h> + #include <sys/time.h> + #include <unistd.h> +#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); |