Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,12 @@ ipch/
# TFS 2012 Local Workspace
$tf/

# cmake
*.cmake
CMakeFiles/
CMakeCache.txt
Makefile

*.tcl
internal/Config.h
src/main/web/Embedded.cpp
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/main/c/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -439,7 +439,7 @@ bool Connection::flush() {
}

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

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

void Connection::setLinger() {
if (_fd == -1) {
if (_fd == (NativeSocketType) -1) {
return;
}
const int secondsToLinger = 1;
Expand Down
16 changes: 8 additions & 8 deletions src/main/c/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 ("
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/c/StringUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/c/seasocks/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/main/c/seasocks/win32/wepoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/main/c/seasocks/win32/win_getopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

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

#define __GETOPT_H__

Expand Down Expand Up @@ -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 ""
Expand Down
7 changes: 7 additions & 0 deletions src/main/c/seasocks/win32/winsock_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
#ifdef _WIN32
#define NOMINMAX 1
#include <winsock2.h>

#ifndef TCP_KEEPALIVE
#define TCP_KEEPALIVE 3
#define TCP_KEEPIDLE TCP_KEEPALIVE
#define TCP_KEEPCNT 16
#define TCP_KEEPINTVL 17
#endif
#endif