SocketServer.h
Go to the documentation of this file.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 "Exceptions.h"
00031 #include <map>
00032 #include <set>
00033 #include <queue>
00034
00035 namespace FIX
00036 {
00038 struct SocketInfo
00039 {
00040 SocketInfo()
00041 : m_socket( -1 ), m_port( 0 ), m_noDelay( false ),
00042 m_sendBufSize( 0 ), m_rcvBufSize( 0 ) {}
00043
00044 SocketInfo( int socket, short port, bool noDelay, int sendBufSize, int rcvBufSize )
00045 : m_socket( socket ), m_port( port ), m_noDelay( noDelay ),
00046 m_sendBufSize( sendBufSize ), m_rcvBufSize( rcvBufSize ) {}
00047
00048 int m_socket;
00049 short m_port;
00050 bool m_noDelay;
00051 int m_sendBufSize;
00052 int m_rcvBufSize;
00053 };
00054
00056 class SocketServer
00057 {
00058 public:
00059 class Strategy;
00060
00061 SocketServer( int timeout = 0 );
00062
00063 int add( int port, bool reuse = false, bool noDelay = false,
00064 int sendBufSize = 0, int rcvBufSize = 0 ) throw( SocketException& );
00065 int accept( int socket );
00066 void close();
00067 bool block( Strategy& strategy, bool poll = 0, double timeout = 0.0 );
00068
00069 int numConnections() { return m_monitor.numSockets() - 1; }
00070 SocketMonitor& getMonitor() { return m_monitor; }
00071
00072 int socketToPort( int socket );
00073 int portToSocket( int port );
00074
00075 private:
00076 typedef std::map<int, SocketInfo>
00077 SocketToInfo;
00078 typedef std::map<int, SocketInfo>
00079 PortToInfo;
00080
00081 SocketToInfo m_socketToInfo;
00082 PortToInfo m_portToInfo;
00083 SocketMonitor m_monitor;
00084
00085 public:
00086 class Strategy
00087 {
00088 public:
00089 virtual ~Strategy() {}
00090 virtual void onConnect( SocketServer&, int acceptSocket, int socket ) = 0;
00091 virtual void onWrite( SocketServer&, int socket ) = 0;
00092 virtual bool onData( SocketServer&, int socket ) = 0;
00093 virtual void onDisconnect( SocketServer&, int socket ) = 0;
00094 virtual void onError( SocketServer& ) = 0;
00095 virtual void onTimeout( SocketServer& ) {};
00096 };
00097 };
00098 }
00099
00100 #endif //FIX_SOCKETSERVER_H