SocketMonitor.h
Go to the documentation of this file.
/* -*- C++ -*- */
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifndef FIX_SOCKETMONITOR_H
#define FIX_SOCKETMONITOR_H
#ifdef _MSC_VER
#pragma warning( disable : 4503 4355 4786 4290 )
#endif
#ifdef _MSC_VER
#include <Winsock2.h>
typedef int socklen_t;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <set>
#include <queue>
#include <time.h>
namespace FIX
{
class SocketMonitor
{
public:
class Strategy;
SocketMonitor( int timeout = 0 );
virtual ~SocketMonitor();
bool addConnect( int socket );
bool addRead( int socket );
bool addWrite( int socket );
bool drop( int socket );
void signal( int socket );
void unsignal( int socket );
void block( Strategy& strategy, bool poll = 0, double timeout = 0.0 );
size_t numSockets()
{ return m_readSockets.size() - 1; }
private:
typedef std::set < int > Sockets;
typedef std::queue < int > Queue;
void setsockopt();
bool bind();
bool listen();
void buildSet( const Sockets&, fd_set& );
inline timeval* getTimeval( bool poll, double timeout );
inline bool sleepIfEmpty( bool poll );
void processReadSet( Strategy&, fd_set& );
void processWriteSet( Strategy&, fd_set& );
void processExceptSet( Strategy&, fd_set& );
int m_timeout;
timeval m_timeval;
#ifndef SELECT_DECREMENTS_TIME
clock_t m_ticks;
#endif
int m_signal;
int m_interrupt;
Sockets m_connectSockets;
Sockets m_readSockets;
Sockets m_writeSockets;
Queue m_dropped;
public:
class Strategy
{
public:
virtual ~Strategy()
{}
virtual void onConnect( SocketMonitor&, int socket ) = 0;
virtual void onEvent( SocketMonitor&, int socket ) = 0;
virtual void onWrite( SocketMonitor&, int socket ) = 0;
virtual void onError( SocketMonitor&, int socket ) = 0;
virtual void onError( SocketMonitor& ) = 0;
virtual void onTimeout( SocketMonitor& )
{}}
;
};
}
#endif //FIX_SOCKETMONITOR_H
FIX::SocketMonitor::m_timeout
int m_timeout
Definition: SocketMonitor.h:115
FIX::SocketMonitor::unsignal
void unsignal(int socket)
Definition: SocketMonitor.cpp:190
FIX::SocketMonitor::m_signal
int m_signal
Definition: SocketMonitor.h:121
FIX::SocketMonitor::block
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketMonitor.cpp:198
FIX::SocketMonitor::addRead
bool addRead(int socket)
Definition: SocketMonitor.cpp:92
FIX::SocketMonitor::m_connectSockets
Sockets m_connectSockets
Definition: SocketMonitor.h:123
FIX::SocketMonitor::Queue
std::queue< int > Queue
Definition: SocketMonitor.h:102
FIX::SocketMonitor::addWrite
bool addWrite(int socket)
Definition: SocketMonitor.cpp:102
FIX::SocketMonitor::Strategy::onWrite
virtual void onWrite(SocketMonitor &, int socket)=0
FIX::SocketMonitor::m_interrupt
int m_interrupt
Definition: SocketMonitor.h:122
FIX::SocketMonitor::processWriteSet
void processWriteSet(Strategy &, fd_set &)
Definition: SocketMonitor.cpp:283
FIX::SocketMonitor::m_readSockets
Sockets m_readSockets
Definition: SocketMonitor.h:124
FIX::SocketMonitor
Monitors events on a collection of sockets.
Definition: SocketMonitor.h:64
FIX::SocketMonitor::signal
void signal(int socket)
Definition: SocketMonitor.cpp:185
FIX::SocketMonitor::buildSet
void buildSet(const Sockets &, fd_set &)
Definition: SocketMonitor.cpp:345
FIX::SocketMonitor::addConnect
bool addConnect(int socket)
Definition: SocketMonitor.cpp:82
FIX::SocketMonitor::sleepIfEmpty
bool sleepIfEmpty(bool poll)
Definition: SocketMonitor.cpp:169
FIX::SocketMonitor::numSockets
size_t numSockets()
Definition: SocketMonitor.h:97
FIX::SocketMonitor::Strategy::onEvent
virtual void onEvent(SocketMonitor &, int socket)=0
FIX::SocketMonitor::bind
bool bind()
FIX::SocketMonitor::m_writeSockets
Sockets m_writeSockets
Definition: SocketMonitor.h:125
FIX::SocketMonitor::Strategy::onConnect
virtual void onConnect(SocketMonitor &, int socket)=0
FIX::SocketMonitor::getTimeval
timeval * getTimeval(bool poll, double timeout)
Definition: SocketMonitor.cpp:135
FIX::SocketMonitor::m_ticks
clock_t m_ticks
Definition: SocketMonitor.h:118
FIX
Definition: Acceptor.cpp:34
FIX::SocketMonitor::setsockopt
void setsockopt()
FIX::Queue
A thread safe monitored queue.
Definition: Queue.h:50
FIX::SocketMonitor::drop
bool drop(int socket)
Definition: SocketMonitor.cpp:115
FIX::SocketMonitor::Strategy::onTimeout
virtual void onTimeout(SocketMonitor &)
Definition: SocketMonitor.h:139
FIX::SocketMonitor::processExceptSet
void processExceptSet(Strategy &, fd_set &)
Definition: SocketMonitor.cpp:324
FIX::SocketMonitor::listen
bool listen()
FIX::SocketMonitor::m_timeval
timeval m_timeval
Definition: SocketMonitor.h:116
FIX::SocketMonitor::SocketMonitor
SocketMonitor(int timeout=0)
Definition: SocketMonitor.cpp:52
FIX::SocketMonitor::processReadSet
void processReadSet(Strategy &, fd_set &)
Definition: SocketMonitor.cpp:244
FIX::SocketMonitor::Sockets
std::set< int > Sockets
Definition: SocketMonitor.h:101
FIX::SocketMonitor::Strategy::onError
virtual void onError(SocketMonitor &, int socket)=0
FIX::SocketMonitor::Strategy::~Strategy
virtual ~Strategy()
Definition: SocketMonitor.h:132
FIX::SocketMonitor::m_dropped
Queue m_dropped
Definition: SocketMonitor.h:126
FIX::SocketMonitor::~SocketMonitor
virtual ~SocketMonitor()
Definition: SocketMonitor.cpp:71