Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support via MSYS mingw-w64-ucrt-x86_64 #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -4,6 +4,11 @@ add_subdirectory(main/web)
macro(add_seasocks_executable _name)
add_executable(${_name} ${ARGN})
target_link_libraries(${_name} PRIVATE seasocks)

if(MINGW)
message(STATUS "Building in MinGW environment, linking with ws2_32")
target_link_libraries(${_name} PRIVATE ws2_32)
endif()
endmacro()

if (SEASOCKS_EXAMPLE_APP)
10 changes: 5 additions & 5 deletions src/main/c/Connection.cpp
Original file line number Diff line number Diff line change
@@ -278,7 +278,7 @@ void Connection::closeInternal() {
// It only actually only calls shutdown on the socket,
// leaving the close of the FD and the cleanup until the destructor runs.
_server.checkThread();
if (_fd != -1 && !_shutdown && ::shutdown(_fd, SHUT_RDWR) == -1) {
if (_fd != (NativeSocketType) -1 && !_shutdown && ::shutdown(_fd, SHUT_RDWR) == -1) {
LS_WARNING(_logger, "Unable to shutdown socket : " << getLastError());
}
_shutdown = true;
@@ -296,7 +296,7 @@ void Connection::finalise() {
_webSocketHandler->onDisconnect(this);
_webSocketHandler.reset();
}
if (_fd != -1) {
if (_fd != (NativeSocketType) -1) {
_server.remove(this);
LS_DEBUG(_logger, "Closing socket");
#ifndef _WIN32
@@ -309,7 +309,7 @@ void Connection::finalise() {
}

ssize_t Connection::safeSend(const void* data, size_t size) {
if (_fd == -1 || _hadSendError || _shutdown) {
if ((_fd == (NativeSocketType) -1) || _hadSendError || _shutdown) {
// Ignore further writes to the socket, it's already closed or has been shutdown
return -1;
}
@@ -439,7 +439,7 @@ bool Connection::flush() {
}

bool Connection::closed() const {
return _fd == -1 || _shutdown;
return _fd == (NativeSocketType) -1 || _shutdown;
}

void Connection::handleNewData() {
@@ -1302,7 +1302,7 @@ void Connection::bufferResponseAndCommonHeaders(ResponseCode code) {
}

void Connection::setLinger() {
if (_fd == -1) {
if (_fd == (NativeSocketType) -1) {
return;
}
const int secondsToLinger = 1;
16 changes: 8 additions & 8 deletions src/main/c/Server.cpp
Original file line number Diff line number Diff line change
@@ -170,8 +170,8 @@ Server::Server(std::shared_ptr<Logger> logger)
return;
}

epoll_event eventWake = {EPOLLIN, {&_eventFd}};
#ifndef _WIN32
epoll_event eventWake = {EPOLLIN, {&_eventFd}};
if (epoll_ctl(_epollFd, EPOLL_CTL_ADD, _eventFd, &eventWake) == -1) {
LS_ERROR(_logger, "Unable to add wake socket to epoll: " << getLastError());
return;
@@ -201,7 +201,7 @@ Server::~Server() {

void Server::shutdown() {
// Stop listening to any further incoming connections.
if (_listenSock != -1) {
if (_listenSock != (NativeSocketType) -1) {
#ifdef _WIN32
::closesocket(_listenSock);
#else
@@ -309,7 +309,7 @@ bool Server::startListening(uint32_t ipInHostOrder, int port) {
return false;
}
_listenSock = socket(AF_INET, SOCK_STREAM, 0);
if (_listenSock == -1) {
if (_listenSock == (NativeSocketType) -1) {
LS_ERROR(_logger, "Unable to create listen socket: " << getLastError());
return false;
}
@@ -346,7 +346,7 @@ bool Server::startListeningUnix(const char* socketPath) {
struct sockaddr_un sock;

_listenSock = socket(AF_UNIX, SOCK_STREAM, 0);
if (_listenSock == -1) {
if (_listenSock == (NativeSocketType) -1) {
LS_ERROR(_logger, "Unable to create unix listen socket: " << getLastError());
return false;
}
@@ -452,7 +452,7 @@ void Server::checkAndDispatchEpoll(int epollMillis) {
} else if (events[i].data.ptr == &_eventFd) {
// This is never true in windows
#ifdef _WIN32
throw std::exception("Win32 uses a seperate, native wake-up HANDLE as an event");
throw std::runtime_error("Win32 uses a seperate, native wake-up HANDLE as an event");
#else
if (events[i].events & ~EPOLLIN) {
LS_SEVERE(_logger, "Got unexpected event on management pipe ("
@@ -500,7 +500,7 @@ bool Server::serve(const char* staticPath, int port) {
}

bool Server::loop() {
if (_listenSock == -1) {
if (_listenSock == (NativeSocketType) -1) {
LS_ERROR(_logger, "Server not initialised");
return false;
}
@@ -528,7 +528,7 @@ Server::PollResult Server::poll(int millis) {
LS_ERROR(_logger, "poll() called from the wrong thread");
return PollResult::Error;
}
if (_listenSock == -1) {
if (_listenSock == (NativeSocketType) -1) {
LS_ERROR(_logger, "Server not initialised");
return PollResult::Error;
}
@@ -581,7 +581,7 @@ void Server::handleAccept() {
NativeSocketType fd = ::accept(_listenSock,
reinterpret_cast<sockaddr*>(&address),
&addrLen);
if (fd == -1) {
if (fd == (NativeSocketType) -1) {
LS_ERROR(_logger, "Unable to accept: " << getLastError());
return;
}
8 changes: 4 additions & 4 deletions src/main/c/StringUtil.cpp
Original file line number Diff line number Diff line change
@@ -95,10 +95,10 @@ std::string formatAddress(const sockaddr_in& address) {
std::snprintf(ipBuffer,
bufferSize,
"%d.%d.%d.%d:%d",
(address.sin_addr.s_addr >> 0) & 0xff,
(address.sin_addr.s_addr >> 8) & 0xff,
(address.sin_addr.s_addr >> 16) & 0xff,
(address.sin_addr.s_addr >> 24) & 0xff,
(int) (address.sin_addr.s_addr >> 0) & 0xff,
(int) (address.sin_addr.s_addr >> 8) & 0xff,
(int) (address.sin_addr.s_addr >> 16) & 0xff,
(int) (address.sin_addr.s_addr >> 24) & 0xff,
htons(address.sin_port));
return ipBuffer;
}
2 changes: 2 additions & 0 deletions src/main/c/seasocks/Server.h
Original file line number Diff line number Diff line change
@@ -42,7 +42,9 @@
#ifdef _WIN32
#include "seasocks/win32/winsock_includes.h"
#define ioctl ioctlsocket
#ifndef _INC_TYPES
using pid_t = DWORD;
#endif
#include <afunix.h> // windows has unix sockets -- who'da guessed?
#endif

8 changes: 8 additions & 0 deletions src/main/c/seasocks/win32/wepoll.c
Original file line number Diff line number Diff line change
@@ -895,13 +895,21 @@ int nt_global_init(void) {
ntdll = GetModuleHandleW(L"ntdll.dll");
if (ntdll == NULL)
return -1;
#if _WIN32
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#endif

#define X(return_type, attributes, name, parameters) \
fn_ptr = GetProcAddress(ntdll, #name); \
if (fn_ptr == NULL) \
return -1; \
name = (return_type(attributes*) parameters)(nt__fn_ptr_cast_t) fn_ptr;
NT_NTDLL_IMPORT_LIST(X)

#if _WIN32
#pragma GCC diagnostic pop
#endif
#undef X

return 0;
4 changes: 3 additions & 1 deletion src/main/c/seasocks/win32/win_getopt.h
Original file line number Diff line number Diff line change
@@ -57,7 +57,9 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#if defined(_MSC_VER)
#pragma warning(disable : 4996)
#endif

#define __GETOPT_H__

@@ -113,7 +115,7 @@ static inline char* optarg = nullptr; /* argument associated with option */
extern char __declspec(dllimport) * __progname;
#endif

#ifdef __CYGWIN__
#if defined(__CYGWIN__) || defined(__MINGW64__)
static char EMSG[] = "";
#else
#define EMSG ""