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

Replace socket select() with poll() #6296

Merged
merged 22 commits into from Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions engine/dlib/src/dlib/file_descriptor.cpp
@@ -0,0 +1,91 @@
// Copyright 2021 The Defold Foundation
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#include "file_descriptor.h"
#include "file_descriptor_private.h"
#include "log.h"
#include <stdint.h>

namespace dmFileDescriptor
britzl marked this conversation as resolved.
Show resolved Hide resolved
{
void PollerSetCapacity(Poller* poller, uint32_t capacity)
{
poller->m_Pollfds.SetCapacity(capacity);
}

void PollerClearEvent(Poller* poller, PollEvent event, int fd)
{
for (uint32_t i = 0; i < poller->m_Pollfds.Size(); ++i)
{
if (poller->m_Pollfds[i].fd == fd)
{
int e = PollEventToNative(event);
poller->m_Pollfds[i].events &= ~e;
return;
}
}
}

void PollerSetEvent(Poller* poller, PollEvent event, int fd)
{
int e = PollEventToNative(event);
for (uint32_t i = 0; i < poller->m_Pollfds.Size(); ++i)
{
if (poller->m_Pollfds[i].fd == fd)
{
poller->m_Pollfds[i].events |= e;
return;
}
}

if (poller->m_Pollfds.Full())
{
poller->m_Pollfds.OffsetCapacity(4);
}

PollFD pfd;
pfd.fd = fd;
pfd.events = e;
poller->m_Pollfds.Push(pfd);
}

bool PollerHasEvent(Poller* poller, PollEvent event, int fd)
{
for (uint32_t i = 0; i < poller->m_Pollfds.Size(); ++i)
britzl marked this conversation as resolved.
Show resolved Hide resolved
{
if (poller->m_Pollfds[i].fd == fd)
{
int e = PollReturnEventToNative(event);
return poller->m_Pollfds[i].revents & e;
}
}
return false;
}

void PollerReset(Poller* poller)
{
while (!poller->m_Pollfds.Empty())
{
poller->m_Pollfds.Pop();
}
}

// for debugging purposes
void PollerDump(Poller* poller)
{
dmLogInfo("poller size = %d ", poller->m_Pollfds.Size());
for (uint32_t i = 0; i < poller->m_Pollfds.Size(); ++i)
{
dmLogInfo("poller i = %d fd = %d events = %d revents = %d", i, poller->m_Pollfds[i].fd, poller->m_Pollfds[i].events, poller->m_Pollfds[i].revents);
}
}
}
18 changes: 18 additions & 0 deletions engine/dlib/src/dlib/file_descriptor.h
@@ -0,0 +1,18 @@
// Copyright 2021 The Defold Foundation
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#ifndef DM_FILE_DESCRIPTOR_H
#define DM_FILE_DESCRIPTOR_H

#include <dmsdk/dlib/file_descriptor.h>

#endif // DM_FILE_DESCRIPTOR_H
50 changes: 50 additions & 0 deletions engine/dlib/src/dlib/file_descriptor_posix.cpp
@@ -0,0 +1,50 @@
// Copyright 2021 The Defold Foundation
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#if !defined(_WIN32)

#include "file_descriptor.h"
#include <stdint.h>
#include <poll.h>
#include <assert.h>

namespace dmFileDescriptor
{
int PollEventToNative(PollEvent event)
{
switch (event)
{
case EVENT_READ: return POLLIN;
case EVENT_WRITE: return POLLOUT;
case EVENT_ERROR: return POLLPRI;
default: assert(false);
}
}
int PollReturnEventToNative(PollEvent event)
{
switch (event)
{
case EVENT_READ: return POLLIN;
case EVENT_WRITE: return POLLOUT;
case EVENT_ERROR: return POLLHUP | POLLERR | POLLNVAL;
default: assert(false);
}
}

int Wait(Poller* poller, int timeout)
{
int r = poll(poller->m_Pollfds.Begin(), poller->m_Pollfds.Size(), timeout);
return r;
}
}

#endif
22 changes: 22 additions & 0 deletions engine/dlib/src/dlib/file_descriptor_private.h
@@ -0,0 +1,22 @@
// Copyright 2021 The Defold Foundation
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#ifndef DM_FILE_DESCRIPTOR_PRIVATE_H
#define DM_FILE_DESCRIPTOR_PRIVATE_H

namespace dmFileDescriptor
{
int PollEventToNative(PollEvent event);
int PollReturnEventToNative(PollEvent event);
}

#endif // DM_FILE_DESCRIPTOR_PRIVATE_H
50 changes: 50 additions & 0 deletions engine/dlib/src/dlib/file_descriptor_win32.cpp
@@ -0,0 +1,50 @@
// Copyright 2021 The Defold Foundation
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#if defined(_WIN32)

#include "file_descriptor.h"
#include <stdint.h>
#include <winsock2.h>
#include <assert.h>

namespace dmFileDescriptor
{
int PollEventToNative(PollEvent event)
{
switch (event)
{
case EVENT_READ: return POLLRDNORM;
case EVENT_WRITE: return POLLWRNORM;
case EVENT_ERROR: return POLLRDBAND;
default: assert(false);
}
}
int PollReturnEventToNative(PollEvent event)
{
switch (event)
{
case EVENT_READ: return POLLRDNORM;
case EVENT_WRITE: return POLLWRNORM;
case EVENT_ERROR: return POLLHUP | POLLERR | POLLNVAL | POLLRDBAND;
default: assert(false);
}
}

int Wait(Poller* poller, int timeout)
{
int r = WSAPoll(poller->m_Pollfds.Begin(), poller->m_Pollfds.Size(), timeout);
return r;
}
}

#endif
1 change: 0 additions & 1 deletion engine/dlib/src/dlib/http_server.cpp
Expand Up @@ -588,7 +588,6 @@ namespace dmHttpServer
server->m_Reconnect = 0;
}
dmSocket::Selector selector;
dmSocket::SelectorZero(&selector);
dmSocket::SelectorSet(&selector, dmSocket::SELECTOR_KIND_READ, server->m_ServerSocket);

dmSocket::Result r = dmSocket::Select(&selector, 0);
Expand Down
46 changes: 28 additions & 18 deletions engine/dlib/src/dlib/socket.cpp
Expand Up @@ -15,6 +15,7 @@
#include "dstrings.h"
#include "log.h"
#include <assert.h>
#include <dlib/file_descriptor.h>

#include <fcntl.h>
#include <string.h>
Expand Down Expand Up @@ -1100,42 +1101,51 @@ namespace dmSocket
SelectorZero(this);
}

dmFileDescriptor::PollEvent SelectorKindToPollEvent(SelectorKind selector_kind)
{
switch (selector_kind)
{
case SELECTOR_KIND_READ:
return dmFileDescriptor::EVENT_READ;
case SELECTOR_KIND_WRITE:
return dmFileDescriptor::EVENT_WRITE;
case SELECTOR_KIND_EXCEPT:
return dmFileDescriptor::EVENT_ERROR;
default:
assert(false);
}
}

void SelectorClear(Selector* selector, SelectorKind selector_kind, Socket socket)
{
FD_CLR(socket, &selector->m_FdSets[selector_kind]);
dmFileDescriptor::PollEvent event = SelectorKindToPollEvent(selector_kind);
dmFileDescriptor::PollerClearEvent(&selector->m_Poller, event, socket);
}

void SelectorSet(Selector* selector, SelectorKind selector_kind, Socket socket)
{
selector->m_Nfds = dmMath::Max(selector->m_Nfds, socket);
FD_SET(socket, &selector->m_FdSets[selector_kind]);
dmFileDescriptor::PollEvent event = SelectorKindToPollEvent(selector_kind);
dmFileDescriptor::PollerSetEvent(&selector->m_Poller, event, socket);
}

bool SelectorIsSet(Selector* selector, SelectorKind selector_kind, Socket socket)
{
return FD_ISSET(socket, &selector->m_FdSets[selector_kind]) != 0;
dmFileDescriptor::PollEvent event = SelectorKindToPollEvent(selector_kind);
return dmFileDescriptor::PollerHasEvent(&selector->m_Poller, event, socket);
}

void SelectorZero(Selector* selector)
{
FD_ZERO(&selector->m_FdSets[SELECTOR_KIND_READ]);
FD_ZERO(&selector->m_FdSets[SELECTOR_KIND_WRITE]);
FD_ZERO(&selector->m_FdSets[SELECTOR_KIND_EXCEPT]);
selector->m_Nfds = 0;
dmFileDescriptor::PollerReset(&selector->m_Poller);
}

Result Select(Selector* selector, int32_t timeout)
{
timeval timeout_val;
timeout_val.tv_sec = timeout / 1000000;
timeout_val.tv_usec = timeout % 1000000;

int r;
if (timeout < 0)
r = select(selector->m_Nfds + 1, &selector->m_FdSets[SELECTOR_KIND_READ], &selector->m_FdSets[SELECTOR_KIND_WRITE], &selector->m_FdSets[SELECTOR_KIND_EXCEPT], 0);
else
r = select(selector->m_Nfds + 1, &selector->m_FdSets[SELECTOR_KIND_READ], &selector->m_FdSets[SELECTOR_KIND_WRITE], &selector->m_FdSets[SELECTOR_KIND_EXCEPT], &timeout_val);

if (timeout > 0)
{
timeout = timeout / 1000;
}
int r = dmFileDescriptor::Wait(&selector->m_Poller, (timeout < 0) ? 0 : timeout);
if (r < 0)
{
return NativeToResult(__FUNCTION__, __LINE__, DM_SOCKET_ERRNO);
Expand Down
15 changes: 5 additions & 10 deletions engine/dlib/src/dlib/sslsocket.cpp
@@ -1,4 +1,3 @@

#include "sslsocket.h"
#include "log.h"
#include "math.h"
Expand All @@ -7,6 +6,8 @@
#include <errno.h>
#include <stdlib.h>

#include <dlib/file_descriptor.h>

#include <mbedtls/certs.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/debug.h>
Expand Down Expand Up @@ -222,26 +223,20 @@ static int TimingGetDelay(void* data)
static int RecvTimeout( void* _ctx, unsigned char *buf, size_t len, uint32_t timeout )
{
int ret;
struct timeval tv;
fd_set read_fds;

CustomNetContext* ctx = (CustomNetContext*)_ctx;
int fd = ctx->m_Context.fd;

if( fd < 0 )
return( MBEDTLS_ERR_NET_INVALID_CONTEXT );

FD_ZERO( &read_fds );
FD_SET( fd, &read_fds );

if (timeout == 0 && ctx->m_Timeout != 0) {
timeout = ctx->m_Timeout / 1000;
}

tv.tv_sec = timeout / 1000;
tv.tv_usec = ( timeout % 1000 ) * 1000;

ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
dmFileDescriptor::Poller poller;
dmFileDescriptor::PollerSetEvent(&poller, dmFileDescriptor::EVENT_READ, fd);
ret = dmFileDescriptor::Wait(&poller, timeout == 0 ? -1 : timeout);

// Zero fds ready means we timed out
if( ret == 0 )
Expand Down