Network: Add SocketPoller class
Former-commit-id: 86b9266d904551d19e7ec8e8d6bbe5f137e8d29f [formerly 34575937d84f796cfe36a5ab97ea1f787c1506ba] [formerly 6c8c621523800958ad38eef118fcb3687c34b367 [formerly a1f1330fbe6f990a21bbe6dccfe727edec0e2b44]] Former-commit-id: 6635c463a753f744b835267181b51ed89620b627 [formerly 14e84c7155cd669ac20a5492e67704059a442925] Former-commit-id: 7becccdf2d8aac7cdf526d855215d7d144be284c
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
// 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_IPADDRESSIMPL_HPP
|
||||
#define NAZARA_IPADDRESSIMPL_HPP
|
||||
|
||||
#include <Nazara/Network/IpAddress.hpp>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
@@ -30,3 +35,5 @@ namespace Nz
|
||||
static ResolveError TranslateEAIErrorToResolveError(int error);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_IPADDRESSIMPL_HPP
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <poll.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <Nazara/Network/Debug.hpp>
|
||||
@@ -397,6 +398,23 @@ namespace Nz
|
||||
return IpAddressImpl::FromSockAddr(reinterpret_cast<sockaddr*>(nameBuffer.data()));
|
||||
}
|
||||
|
||||
int SocketImpl::Poll(PollSocket* fdarray, unsigned long nfds, int timeout, SocketError* error)
|
||||
{
|
||||
NazaraAssert(fdarray && nfds > 0, "Invalid fdarray");
|
||||
|
||||
static_assert(sizeof(PollSocket) == sizeof(pollfd), "PollSocket size must match WSAPOLLFD size");
|
||||
|
||||
int result = poll(reinterpret_cast<pollfd*>(fdarray), static_cast<nfds_t>(nfds), timeout);
|
||||
if (result < 0)
|
||||
{
|
||||
if (error)
|
||||
*error = TranslateErrnoToResolveError(GetLastErrorCode());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool SocketImpl::Receive(SocketHandle handle, void* buffer, int length, int* read, SocketError* error)
|
||||
{
|
||||
|
||||
@@ -2,12 +2,26 @@
|
||||
// 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_SOCKETIMPL_HPP
|
||||
#define NAZARA_SOCKETIMPL_HPP
|
||||
|
||||
#include <Nazara/Network/SocketHandle.hpp>
|
||||
#include <Nazara/Network/Enums.hpp>
|
||||
#include <Nazara/Network/IpAddress.hpp>
|
||||
|
||||
#define NAZARA_NETWORK_POLL_SUPPORT 1
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
struct PollSocket
|
||||
{
|
||||
SocketHandle fd;
|
||||
short events;
|
||||
short revents;
|
||||
};
|
||||
|
||||
class SocketImpl
|
||||
{
|
||||
public:
|
||||
@@ -42,6 +56,8 @@ namespace Nz
|
||||
static IpAddress QueryPeerAddress(SocketHandle handle, SocketError* error = nullptr);
|
||||
static IpAddress QuerySocketAddress(SocketHandle handle, SocketError* error = nullptr);
|
||||
|
||||
static int Poll(PollSocket* fdarray, std::size_t nfds, int timeout, SocketError* error);
|
||||
|
||||
static bool Receive(SocketHandle handle, void* buffer, int length, int* read, SocketError* error);
|
||||
static bool ReceiveFrom(SocketHandle handle, void* buffer, int length, IpAddress* from, int* read, SocketError* error);
|
||||
|
||||
@@ -66,3 +82,5 @@ namespace Nz
|
||||
static socketID s_socket;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_SOCKETIMPL_HPP
|
||||
92
src/Nazara/Network/Posix/SocketPollerImpl.cpp
Normal file
92
src/Nazara/Network/Posix/SocketPollerImpl.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
// 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/Win32/SocketPollerImpl.hpp>
|
||||
#include <poll.h>
|
||||
#include <Nazara/Network/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
void SocketPollerImpl::Clear()
|
||||
{
|
||||
m_activeSockets.clear();
|
||||
m_allSockets.clear();
|
||||
m_sockets.clear();
|
||||
}
|
||||
|
||||
bool SocketPollerImpl::IsReady(SocketHandle socket) const
|
||||
{
|
||||
return m_activeSockets.count(socket) != 0;
|
||||
}
|
||||
|
||||
bool SocketPollerImpl::IsRegistered(SocketHandle socket) const
|
||||
{
|
||||
return m_allSockets.count(socket) != 0;
|
||||
}
|
||||
|
||||
bool SocketPollerImpl::RegisterSocket(SocketHandle socket)
|
||||
{
|
||||
NazaraAssert(!IsRegistered(socket), "Socket is already registered");
|
||||
|
||||
PollSocket entry = {
|
||||
socket,
|
||||
POLLRDNORM,
|
||||
0
|
||||
};
|
||||
|
||||
m_allSockets[socket] = m_sockets.size();
|
||||
m_sockets.emplace_back(entry);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SocketPollerImpl::UnregisterSocket(SocketHandle socket)
|
||||
{
|
||||
NazaraAssert(IsRegistered(socket), "Socket is not registered");
|
||||
|
||||
if (m_sockets.size() > 1U)
|
||||
{
|
||||
// Instead of using vector::erase, let's move the last element to the now unoccupied position
|
||||
std::size_t entry = m_allSockets[socket];
|
||||
|
||||
// Get the last element and update it's position
|
||||
const PollSocket& lastElement = m_sockets.back();
|
||||
m_allSockets[lastElement.fd] = entry;
|
||||
|
||||
// Now move it properly (lastElement is invalid after the following line) and pop it
|
||||
m_sockets[entry] = std::move(m_sockets.back());
|
||||
}
|
||||
|
||||
m_sockets.pop_back();
|
||||
m_activeSockets.erase(socket);
|
||||
m_allSockets.erase(socket);
|
||||
}
|
||||
|
||||
int SocketPollerImpl::Wait(UInt64 msTimeout, SocketError* error)
|
||||
{
|
||||
int activeSockets;
|
||||
|
||||
// Reset status of sockets
|
||||
for (PollSocket& entry : m_sockets)
|
||||
entry.revents = 0;
|
||||
|
||||
activeSockets = SocketImpl::Poll(m_sockets.data(), m_sockets.size(), static_cast<int>(msTimeout), error);
|
||||
|
||||
m_activeSockets.clear();
|
||||
if (activeSockets > 0U)
|
||||
{
|
||||
for (PollSocket& entry : m_sockets)
|
||||
{
|
||||
if (entry.revents & POLLRDNORM)
|
||||
{
|
||||
m_activeSockets.insert(entry.fd);
|
||||
if (--activeSockets == 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return activeSockets;
|
||||
}
|
||||
}
|
||||
42
src/Nazara/Network/Posix/SocketPollerImpl.hpp
Normal file
42
src/Nazara/Network/Posix/SocketPollerImpl.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
// 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/Win32/SocketImpl.hpp>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class SocketPollerImpl
|
||||
{
|
||||
public:
|
||||
SocketPollerImpl() = default;
|
||||
~SocketPollerImpl() = default;
|
||||
|
||||
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_map<SocketHandle, std::size_t> m_allSockets;
|
||||
std::vector<PollSocket> m_sockets;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_SOCKETPOLLERIMPL_HPP
|
||||
Reference in New Issue
Block a user