Skip to content

Commit

Permalink
More windows tests (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius authored Nov 23, 2024
1 parent b863640 commit 16c8e4b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
19 changes: 15 additions & 4 deletions coroio/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ std::string TAddress::ToString() const {
if (const auto* val = std::get_if<sockaddr_in>(&Addr_)) {
auto* r = inet_ntop(AF_INET, &val->sin_addr, buf, sizeof(buf));
if (r) {
return std::string(r) + ":" + std::to_string(val->sin_port);
return std::string(r) + ":" + std::to_string(ntohs(val->sin_port));
}
} else if (const auto* val = std::get_if<sockaddr_in6>(&Addr_)) {
auto* r = inet_ntop(AF_INET6, &val->sin6_addr, buf, sizeof(buf));
if (r) {
return "[" + std::string(r) + "]:" + std::to_string(val->sin6_port);
return "[" + std::string(r) + "]:" + std::to_string(ntohs(val->sin6_port));
}
}

Expand Down Expand Up @@ -136,16 +136,23 @@ int TSocketOps::Create(int domain, int type) {
int TSocketOps::Setup(int s) {
int value;
socklen_t len = sizeof(value);
[[maybe_unused]] bool isSocket = false;
if (getsockopt(s, SOL_SOCKET, SO_TYPE, (char*) &value, &len) == 0) {
value = 1;
isSocket = true;
// TODO: set for STREAM only
if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char*) &value, len) < 0) {
throw std::system_error(errno, std::generic_category(), "setsockopt");
#ifdef _WIN32
if (WSAGetLastError() != WSAENOPROTOOPT)
#endif
throw std::system_error(errno, std::generic_category(), "setsockopt");
}
}

#ifdef _WIN32
// TODO: This code works only with sockets!
u_long mode = 1;
if (ioctlsocket(s, FIONBIO, &mode) != 0) {
if (isSocket && ioctlsocket(s, FIONBIO, &mode) != 0) {
throw std::system_error(WSAGetLastError(), std::system_category(), "ioctlsocket");
}
#else
Expand Down Expand Up @@ -188,7 +195,11 @@ TSocket::~TSocket()
void TSocket::Close()
{
if (Fd_ >= 0) {
#ifdef _WIN32
closesocket(Fd_);
#else
close(Fd_);
#endif
Poller_->RemoveEvent(Fd_);
}
}
Expand Down
3 changes: 3 additions & 0 deletions coroio/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class TSocketBase: public TSocketOps {
struct TAwaitableRead: public TAwaitable<TAwaitableRead> {
void run() {
this->ret = TSockOps::read(this->fd, this->b, this->s);
// TODO: In windows ret could be < 0 with errno = 0
// Need to process WSAGetLastError
if (this->ret < 0) { process_errno(); }
}

void await_suspend(std::coroutine_handle<> h) {
Expand Down
2 changes: 1 addition & 1 deletion coroio/win32_errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int map_winsock_error_to_errno(int winsock_error) {
} // namespace

int process_errno() {
return errno = map_winsock_error_to_errno(WSAGetLastError());
return errno == 0 ? errno = map_winsock_error_to_errno(WSAGetLastError()) : errno;
}
#else
int process_errno()
Expand Down
3 changes: 2 additions & 1 deletion examples/echoclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ TFuture<void> client(TPoller& poller, TAddress addr)
TByteWriter byteWriter(socket);
TByteReader byteReader(socket);

co_await socket.Connect();
// Windows unable to detect 'connection refused' without setting timeout
co_await socket.Connect(TClock::now()+std::chrono::milliseconds(1000));
while (auto line = co_await lineReader.Read()) {
co_await byteWriter.Write(line);
co_await byteReader.Read(in.data(), line.Size());
Expand Down
1 change: 1 addition & 0 deletions examples/echoserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ TVoidTask server(TPoller& poller, TAddress address, int buffer_size)
typename TPoller::TSocket socket(std::move(address), poller);
socket.Bind();
socket.Listen();
std::cerr << "Listening on: " << socket.Addr().ToString() << std::endl;

while (true) {
auto client = co_await socket.Accept();
Expand Down
16 changes: 12 additions & 4 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ void test_connection_refused_on_write(void**) {
TSocket clientSocket(TAddress{"127.0.0.1", port}, poller);
char buffer[] = "test";
try {
co_await clientSocket.Connect();
// set timeout (windows workaround)
co_await clientSocket.Connect(TClock::now()+std::chrono::milliseconds(100));
co_await clientSocket.WriteSome(buffer, sizeof(buffer));
} catch (const std::system_error& ex) {
*err = ex.code();
Expand All @@ -365,7 +366,8 @@ void test_connection_refused_on_write(void**) {
}

// EPIPE in MacOS
assert_true(err.value() == ECONNREFUSED || err.value() == EPIPE);
// std::errc::timed_out for windows
assert_true(err == std::errc::timed_out || err.value() == ECONNREFUSED || err.value() == EPIPE);
}

template<typename TPoller>
Expand All @@ -381,7 +383,8 @@ void test_connection_refused_on_read(void**) {
TSocket clientSocket(TAddress{"127.0.0.1", port}, poller);
char buffer[] = "test";
try {
co_await clientSocket.Connect();
// set timeout (windows workaround)
co_await clientSocket.Connect(TClock::now()+std::chrono::milliseconds(100));
co_await clientSocket.ReadSome(buffer, sizeof(buffer));
} catch (const std::system_error& ex) {
*err = ex.code();
Expand All @@ -393,7 +396,8 @@ void test_connection_refused_on_read(void**) {
loop.Step();
}

assert_int_equal(err.value(), ECONNREFUSED);
// std::errc::timed_out for windows
assert_true(err == std::errc::timed_out || err.value() == ECONNREFUSED);
}

template<typename TPoller>
Expand Down Expand Up @@ -701,7 +705,11 @@ void test_resolver(void**) {
using TSocket = typename TPoller::TSocket;

TLoop loop;
#ifdef _WIN32
TResolver<TPollerBase> resolver(TAddress{"8.8.8.8", 53}, loop.Poller());
#else
TResolver<TPollerBase> resolver(loop.Poller());
#endif

std::vector<TAddress> addresses;
TFuture<void> h1 = [](auto& resolver, std::vector<TAddress>& addresses) -> TFuture<void> {
Expand Down

0 comments on commit 16c8e4b

Please sign in to comment.