Index Source Files Annotated Class List Alphabetical Class List Class Hierarchy Graphical Class Hierarchy

# SocketMonitor.h

Go to the documentation of this file.

    00001 /* -*- C++ -*- */
    00002
    00003 /****************************************************************************
    00004 ** Copyright (c) quickfixengine.org  All rights reserved.
    00005 **
    00006 ** This file is part of the QuickFIX FIX Engine
    00007 **
    00008 ** This file may be distributed under the terms of the quickfixengine.org
    00009 ** license as defined by quickfixengine.org and appearing in the file
    00010 ** LICENSE included in the packaging of this file.
    00011 **
    00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    00014 **
    00015 ** See http://www.quickfixengine.org/LICENSE for licensing information.
    00016 **
    00017 ** Contact ask@quickfixengine.org if any conditions of this licensing are
    00018 ** not clear to you.
    00019 **
    00020 ****************************************************************************/
    00021
    00022 #ifndef FIX_SOCKETMONITOR_H
    00023 #define FIX_SOCKETMONITOR_H
    00024
    00025 #ifdef _MSC_VER
    00026 #pragma warning( disable : 4503 4355 4786 4290 )
    00027 #endif
    00028
    00029 #ifdef _MSC_VER
    00030 #include <Winsock2.h>
    00031 typedef int socklen_t;
    00032 #else
    00033 #include <sys/types.h>
    00034 #include <sys/socket.h>
    00035 #include <sys/time.h>
    00036 #include <netinet/in.h>
    00037 #include <arpa/inet.h>
    00038 #endif
    00039
    00040 #include <set>
    00041 #include <queue>
    00042 #include <time.h>
    00043
    00044 namespace FIX
    00045 {
    00047 class SocketMonitor
    00048 {
    00049 public:
    00050   class Strategy;
    00051
    00052   SocketMonitor( int timeout = 0 );
    00053   virtual ~SocketMonitor();
    00054
    00055   bool addConnect( int socket );
    00056   bool addRead( int socket );
    00057   bool addWrite( int socket );
    00058   bool drop( int socket );
    00059   void signal( int socket );
    00060   void unsignal( int socket );
    00061   void block( Strategy& strategy, bool poll = 0, double timeout = 0.0 );
    00062
    00063   int numSockets()
    00064   { return m_readSockets.size() - 1; }
    00065
    00066 private:
    00067   typedef std::set < int > Sockets;
    00068   typedef std::queue < int > Queue;
    00069
    00070   void setsockopt();
    00071   bool bind();
    00072   bool listen();
    00073   void buildSet( const Sockets&, fd_set& );
    00074   inline timeval* getTimeval( bool poll, double timeout );
    00075   inline bool sleepIfEmpty( bool poll );
    00076
    00077   void processReadSet( Strategy&, fd_set& );
    00078   void processWriteSet( Strategy&, fd_set& );
    00079   void processExceptSet( Strategy&, fd_set& );
    00080
    00081   int m_timeout;
    00082   timeval m_timeval;
    00083 #ifndef SELECT_DECREMENTS_TIME
    00084   clock_t m_ticks;
    00085 #endif
    00086
    00087   int m_signal;
    00088   int m_interrupt;
    00089   Sockets m_connectSockets;
    00090   Sockets m_readSockets;
    00091   Sockets m_writeSockets;
    00092   Queue m_dropped;
    00093
    00094 public:
    00095   class Strategy
    00096   {
    00097   public:
    00098     virtual ~Strategy()
    00099     {}
    00100     virtual void onConnect( SocketMonitor&, int socket ) = 0;
    00101     virtual void onEvent( SocketMonitor&, int socket ) = 0;
    00102     virtual void onWrite( SocketMonitor&, int socket ) = 0;
    00103     virtual void onError( SocketMonitor&, int socket ) = 0;
    00104     virtual void onError( SocketMonitor& ) = 0;
    00105     virtual void onTimeout( SocketMonitor& )
    00106   {}}
    00107   ;
    00108 };
    00109 }
    00110
    00111 #endif //FIX_SOCKETMONITOR_H