Network/SocketPoller: Switch to epoll implementation on Linux
Former-commit-id: 1a4b998bff35b5aac411b053fe3dee48f1f6985c [formerly b7a50753347b629f708f21d85efc9e76e4b1bfc6] [formerly 7d59f9ff3d2173657cc5873209753fe64b59e2f2 [formerly 4c38f94a4a366ed290e605870e6f3c87e6decd7f]] Former-commit-id: af5cc261c162ca3eebe5885acd5e2adfbd817984 [formerly 26e7b701e8dcafb7fb9c3537107729b2d0bfe354] Former-commit-id: 00bd2c62ecdb5c493c4ec117dd2033d272f7143a
This commit is contained in:
parent
72680be701
commit
a13b17573e
|
|
@ -14,6 +14,11 @@ MODULE.OsFiles.Posix = {
|
|||
"../src/Nazara/Network/Posix/**.cpp"
|
||||
}
|
||||
|
||||
MODULE.OsFiles.Linux = {
|
||||
"../src/Nazara/Network/Linux/**.hpp",
|
||||
"../src/Nazara/Network/Linux/**.cpp"
|
||||
}
|
||||
|
||||
MODULE.OsLibraries.Windows = {
|
||||
"ws2_32"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,99 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Network module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Network/Linux/SocketPollerImpl.hpp>
|
||||
#include <cstring>
|
||||
#include <Nazara/Network/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
SocketPollerImpl::SocketPollerImpl()
|
||||
{
|
||||
m_handle = epoll_create1(0);
|
||||
}
|
||||
|
||||
SocketPollerImpl::~SocketPollerImpl()
|
||||
{
|
||||
close(m_handle);
|
||||
}
|
||||
|
||||
void SocketPollerImpl::Clear()
|
||||
{
|
||||
m_activeSockets.clear();
|
||||
m_sockets.clear();
|
||||
}
|
||||
|
||||
bool SocketPollerImpl::IsReady(SocketHandle socket) const
|
||||
{
|
||||
return m_activeSockets.count(socket) != 0;
|
||||
}
|
||||
|
||||
bool SocketPollerImpl::IsRegistered(SocketHandle socket) const
|
||||
{
|
||||
return m_sockets.count(socket) != 0;
|
||||
}
|
||||
|
||||
bool SocketPollerImpl::RegisterSocket(SocketHandle socket)
|
||||
{
|
||||
NazaraAssert(!IsRegistered(socket), "Socket is already registered");
|
||||
|
||||
epoll_event event;
|
||||
event.events = EPOLLIN;
|
||||
event.data.fd = socket;
|
||||
|
||||
if (epoll_ctl(m_handle, EPOLL_CTL_ADD, socket, &event) != 0)
|
||||
{
|
||||
NazaraError("Failed to add socket to epoll structure (errno " String::Number(errno) + ": " + Error::GetLastSystemError() + ')');
|
||||
return false;
|
||||
}
|
||||
|
||||
m_sockets.insert(socket);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SocketPollerImpl::UnregisterSocket(SocketHandle socket)
|
||||
{
|
||||
NazaraAssert(IsRegistered(socket), "Socket is not registered");
|
||||
|
||||
m_activeSockets.erase(socket);
|
||||
m_sockets.erase(socket);
|
||||
|
||||
if (epoll_ctl(m_handle, EPOLL_CTL_DEL, socket, nullptr) != 0)
|
||||
NazaraWarning("An error occured while removing socket from epoll structure (errno " String::Number(errno) + ": " + Error::GetLastSystemError() + ')');
|
||||
}
|
||||
|
||||
int SocketPollerImpl::Wait(UInt64 msTimeout, SocketError* error)
|
||||
{
|
||||
int activeSockets;
|
||||
|
||||
// Reset status of sockets
|
||||
m_events.resize(m_sockets.size());
|
||||
std::memset(m_events.data(), 0, m_events.size() * sizeof(epoll_event));
|
||||
|
||||
activeSockets = epoll_wait(m_handle, m_events.data(), static_cast<int>(m_events.size()), static_cast<int>(msTimeout));
|
||||
|
||||
m_activeSockets.clear();
|
||||
if (activeSockets > 0U)
|
||||
{
|
||||
int socketCount = activeSockets;
|
||||
for (int i = 0; i < socketCount; ++i)
|
||||
{
|
||||
if (m_events[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR))
|
||||
{
|
||||
m_activeSockets.insert(m_events[i].data.fd);
|
||||
if (m_events[i].events & EPOLLERR)
|
||||
NazaraWarning("Descriptor " + String::Number(m_events[i].data.fd) + " was returned by epoll with EPOLLERR status");
|
||||
}
|
||||
else
|
||||
{
|
||||
NazaraWarning("Descriptor " + String::Number(m_events[i].data.fd) + " was returned by epoll without EPOLLIN (events: 0x" + String::Number(m_events[i].events, 16) + ')');
|
||||
activeSockets--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return activeSockets;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Network module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SOCKETPOLLERIMPL_HPP
|
||||
#define NAZARA_SOCKETPOLLERIMPL_HPP
|
||||
|
||||
#include <Nazara/Network/IpAddress.hpp>
|
||||
#include <Nazara/Network/SocketHandle.hpp>
|
||||
#include <Nazara/Network/Posix/SocketImpl.hpp>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
#include <sys/epoll.h>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class SocketPollerImpl
|
||||
{
|
||||
public:
|
||||
SocketPollerImpl();
|
||||
~SocketPollerImpl();
|
||||
|
||||
void Clear();
|
||||
|
||||
bool IsReady(SocketHandle socket) const;
|
||||
bool IsRegistered(SocketHandle socket) const;
|
||||
|
||||
bool RegisterSocket(SocketHandle socket);
|
||||
void UnregisterSocket(SocketHandle socket);
|
||||
|
||||
int Wait(UInt64 msTimeout, SocketError* error);
|
||||
|
||||
private:
|
||||
std::unordered_set<SocketHandle> m_activeSockets;
|
||||
std::unordered_set<SocketHandle> m_sockets;
|
||||
std::vector<epoll_event> m_events;
|
||||
int m_handle;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_SOCKETPOLLERIMPL_HPP
|
||||
Loading…
Reference in New Issue