Skip to content

Async servers (UDP and TCP) #4501

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Net/include/Poco/Net/DatagramSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Net_API DatagramSocket: public Socket
/// Creates the DatagramSocket with the SocketImpl
/// from another socket.

~DatagramSocket();
~DatagramSocket() override;
/// Destroys the DatagramSocket.

DatagramSocket& operator = (const Socket& socket);
Expand Down
23 changes: 12 additions & 11 deletions Net/include/Poco/Net/MultiSocketPoller.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@


#include "Poco/Net/Net.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/Socket.h"
#include "Poco/Net/PollSet.h"
#include "Poco/Net/UDPHandler.h"
#include "Poco/Net/UDPServerParams.h"
#include "Poco/Net/UDPSocketReader.h"


namespace Poco {
Expand All @@ -34,7 +38,7 @@ class MultiSocketPoller
/// state, the reading/error handling actions are delegated to the reader.
{
public:
MultiSocketPoller(typename UDPHandlerImpl<S>::List& handlers, const Poco::Net::SocketAddress& sa, int nSockets = 10, Poco::Timespan timeout = 250000):
MultiSocketPoller(typename UDPHandlerImpl<S>::List& handlers, const Poco::Net::SocketAddress& sa, int nSockets = 10, const Poco::Timespan& timeout = 250000):
_address(sa),
_timeout(timeout),
_reader(handlers)
Expand Down Expand Up @@ -74,23 +78,20 @@ class MultiSocketPoller

void poll()
{
if (_reader.handlerStopped()) return;
if (_reader.handlerStopped())
return;
PollSet::SocketModeMap sm;
PollSet::SocketModeMap::iterator it;
PollSet::SocketModeMap::iterator end;
sm = _pollSet.poll(_timeout);
it = sm.begin();
end = sm.end();
for (; it != end; ++it)
for (auto& se: sm)
{
if (it->second & PollSet::POLL_READ)
if (se.second & PollSet::POLL_READ)
{
DatagramSocket ds(it->first);
DatagramSocket ds(se.first);
_reader.read(ds);
}
else if (it->second & PollSet::POLL_ERROR)
else if (se.second & PollSet::POLL_ERROR)
{
_reader.setError(it->first.impl()->sockfd());
_reader.setError(se.first.impl()->sockfd());
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Net/include/Poco/Net/SingleSocketPoller.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/UDPHandler.h"
#include "Poco/Net/PollSet.h"
#include "Poco/Net/UDPServerParams.h"
#include "Poco/Net/UDPSocketReader.h"


namespace Poco {
Expand Down
3 changes: 2 additions & 1 deletion Net/include/Poco/Net/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,12 @@ class FDCompare
{
public:
FDCompare(int fd): _fd(fd) { }
FDCompare() = delete;

inline bool operator()(const Socket& socket) const
{ return socket.sockfd() == _fd; }

private:
FDCompare();
int _fd;
};
#endif
Expand Down
8 changes: 4 additions & 4 deletions Net/include/Poco/Net/SocketDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
#define POCO_NO_DATA NO_DATA
#elif defined(POCO_OS_FAMILY_UNIX)
#include <unistd.h>
#include <errno.h>
#include <cerrno>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
Expand Down Expand Up @@ -369,12 +369,12 @@ namespace Net {


#if defined(POCO_OS_FAMILY_WINDOWS)
typedef WSABUF SocketBuf;
using SocketBuf = WSABUF;
#elif defined(POCO_OS_FAMILY_UNIX) // TODO: may need more refinement
typedef iovec SocketBuf;
using SocketBuf = iovec;
#endif

typedef std::vector<SocketBuf> SocketBufVec;
using SocketBufVec = std::vector<SocketBuf>;

inline int SocketBufVecSize(const SocketBufVec& sbv)
/// Returns total length of all SocketBufs in the vector.
Expand Down
2 changes: 1 addition & 1 deletion Net/include/Poco/Net/SocketImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ class Net_API SocketImpl: public Poco::RefCountedObject
SocketImpl(poco_socket_t sockfd);
/// Creates a SocketImpl using the given native socket.

virtual ~SocketImpl();
~SocketImpl() override;
/// Destroys the SocketImpl.
/// Closes the socket if it is still open.

Expand Down
2 changes: 1 addition & 1 deletion Net/include/Poco/Net/StreamSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Net_API StreamSocket: public Socket
/// Creates the StreamSocket with the SocketImpl
/// from another socket.

virtual ~StreamSocket();
~StreamSocket() override;
/// Destroys the StreamSocket.

StreamSocket& operator = (const Socket& socket);
Expand Down
10 changes: 5 additions & 5 deletions Net/include/Poco/Net/UDPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Timespan.h"
#include "Poco/Runnable.h"
#include "Poco/RefCountedObject.h"
#include "Poco/Thread.h"


namespace Poco {
namespace Net {


class Net_API UDPClient : public Poco::Runnable
class Net_API UDPClient : public Poco::Runnable, public RefCountedObject
/// UDP client can either send, or send/receive UDP packets.
/// The mode of operation is specified at construction time.
/// If receiving functionality is enabled, it will run in a
Expand All @@ -43,12 +43,12 @@ class Net_API UDPClient : public Poco::Runnable
UDPClient(const std::string& address, Poco::UInt16 port, bool listen = false);
/// Creates UDP client and connects it to specified address/port.
/// If listen is true, a thread is launched where client can receive
/// responses rom the server.
/// responses from the server.

virtual ~UDPClient();
~UDPClient() override;
/// Destroys UDPClient.

void run();
void run() override;
/// Runs listener (typically invoked internally, in separate thread).

SocketAddress address() const;
Expand Down
Loading
Loading