Overview
Features
Download
Documentation
Community
Add-Ons & Services

Poco Reactor and sending data

A general discussion forum.

Poco Reactor and sending data

Postby codecandy2k » 04 Feb 2012, 03:09

So I was wondering, what is the standard way to handle sending data with the Poco Reactor.

Currently what I'm doing is when I have data that needs to be sent I will register for WritableNotification, send my data in the event handler when it is called, then unregister the handler. Keeping WritableNotification registered the whole time results in 100% CPU usage.

This works fine, except that it results in some latency as we wait for the event handler to be called. On a typical Windows consumer system, the round-trip latency is about 30ms when using this method. Most likely this is because of the use of select in the main reactor thread, and the low timer resolution on these versions of Windows. On Windows Server 2008, there is no real latency if we set the reactor timeout value to 1ms. This is with the client and server on the same machine, or even in the same process.

I think writing to raw sockets is typically thread safe, is it safe to write to Poco sockets in the Reactor framework from worker threads?
codecandy2k
 
Posts: 40
Joined: 03 Dec 2011, 18:48

Re: Poco Reactor and sending data

Postby alex » 16 May 2012, 16:26

This will be addressed in trunk soon, we're working out the details. The solution uses FIFOBuffer with delegates as a "middle man" between user and reactor, so you will be able to do something like this:

Code: Select all
struct ServiceHandler
{
StreamSocket   _socket;
SocketReactor& _reactor;
FIFOBuffer _fifoIn;
FIFOBuffer _fifoOut;

ServiceHandler()
{
   _reactor.addEventHandler(_socket, NObserver<EchoServiceHandler, ReadableNotification>(*this, &ServiceHandler::onSocketReadable));
   _fifoOut.Readable += delegate(this, &ServiceHandler::onFIFOOutReadable);
   _fifoIn.Writable += delegate(this, &ServiceHandler::onFIFOInWritable);
}

void ServiceHandler::onFIFOOutReadable(bool& b)
{
   if (b)
      _reactor.addEventHandler(_socket, NObserver<ServiceHandler, WritableNotification>(*this, &ServiceHandler::onSocketWritable));
   else
      _reactor.removeEventHandler(_socket, NObserver<ServiceHandler, WritableNotification>(*this, &ServiceHandler::onSocketWritable));
}
   
void ServiceHandler::onFIFOInWritable(bool& b)
{
   if (b)
      _reactor.addEventHandler(_socket, NObserver<ServiceHandler, ReadableNotification>(*this, &ServiceHandler::onSocketReadable));
   else
      _reactor.removeEventHandler(_socket, NObserver<ServiceHandler, ReadableNotification>(*this, &ServiceHandler::onSocketReadable));
}
   
void ServiceHandler::onSocketReadable(const AutoPtr<ReadableNotification>& pNf)
{
   int len = _socket.receiveBytes(_fifoIn);
   _fifoIn.drain(_fifoOut.write(_fifoIn.buffer(), len));
}
   
void ServiceHandler::onSocketWritable(const AutoPtr<WritableNotification>& pNf)
{
   int len = _socket.sendBytes(_fifoOut);
   _fifoOut.drain(len);
}
};
alex
 
Posts: 1106
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco Reactor and sending data

Postby alex » 31 May 2012, 05:33

The sample is now available in the trunk.
alex
 
Posts: 1106
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco Reactor and sending data

Postby yuntinghill » 19 May 2013, 08:55

But it seems that The solution uses FIFOBuffer can't reduce the sending latency dueto registering,unregisterint for WritableNotification.
so I want ask too, is it safe to write to Poco sockets in the Reactor framework from worker threads? or send data via another socket directly.
and If I want to receive/send udp datas, how to use FIFOBuffer as the example?
yuntinghill
 
Posts: 3
Joined: 09 Nov 2011, 17:51

Re: Poco Reactor and sending data

Postby alex » 20 May 2013, 02:35

yuntinghill wrote:But it seems that The solution uses FIFOBuffer can't reduce the sending latency dueto registering,unregisterint for WritableNotification.

That was introduced to allow end user to shield himself from spurious notifications.
yuntinghill wrote:so I want ask too, is it safe to write to Poco sockets in the Reactor framework from worker threads?

yes, you don't have to use fifo buffer
yuntinghill wrote:or send data via another socket directly.

i'm not quite clear on what the question is here; reactor will notify you about socket state and you should act accordingly. what is "other socket"?
yuntinghill wrote:and If I want to receive/send udp datas, how to use FIFOBuffer as the example?

There is no support for Reactor or FIFOBuffer with UDP sockets.
alex
 
Posts: 1106
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron