00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef FIX_SOCKETSERVER_H
00023 #define FIX_SOCKETSERVER_H
00024
00025 #ifdef _MSC_VER
00026 #pragma warning( disable : 4503 4355 4786 4290 )
00027 #endif
00028
00029 #include "SocketMonitor.h"
00030 #include <map>
00031 #include <set>
00032 #include <queue>
00033
00034 namespace FIX
00035 {
00037 struct SocketInfo
00038 {
00039 SocketInfo()
00040 : m_socket( -1 ), m_port( 0 ), m_noDelay( false ) {}
00041 SocketInfo( int socket, short port, bool noDelay )
00042 : m_socket( socket ), m_port( port ), m_noDelay( noDelay ) {}
00043
00044 int m_socket;
00045 short m_port;
00046 bool m_noDelay;
00047 };
00048
00050 class SocketServer
00051 {
00052 public:
00053 class Strategy;
00054
00055 SocketServer( int timeout = 0 );
00056
00057 int add( int port, bool reuse = false, bool noDelay = false );
00058 int accept( int socket );
00059 void close();
00060 bool block( Strategy& strategy, bool poll = 0 );
00061
00062 int numConnections() { return m_monitor.numSockets() - 1; }
00063 SocketMonitor& getMonitor() { return m_monitor; }
00064
00065 int socketToPort( int socket );
00066 int portToSocket( int port );
00067
00068 private:
00069 typedef std::map<int, SocketInfo>
00070 SocketToInfo;
00071 typedef std::map<int, SocketInfo>
00072 PortToInfo;
00073
00074 SocketToInfo m_socketToInfo;
00075 PortToInfo m_portToInfo;
00076 SocketMonitor m_monitor;
00077
00078 public:
00079 class Strategy
00080 {
00081 public:
00082 virtual ~Strategy() {}
00083 virtual void onConnect( SocketServer&, int acceptSocket, int socket ) = 0;
00084 virtual void onWrite( SocketServer&, int socket ) = 0;
00085 virtual void onData( SocketServer&, int socket ) = 0;
00086 virtual void onDisconnect( SocketServer&, int socket ) = 0;
00087 virtual void onError( SocketServer& ) = 0;
00088 virtual void onTimeout( SocketServer& ) {};
00089 };
00090 };
00091 }
00092
00093 #endif //FIX_SOCKETSERVER_H