SocketServer.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 /****************************************************************************
4 ** Copyright (c) 2001-2014
5 **
6 ** This file is part of the QuickFIX FIX Engine
7 **
8 ** This file may be distributed under the terms of the quickfixengine.org
9 ** license as defined by quickfixengine.org and appearing in the file
10 ** LICENSE included in the packaging of this file.
11 **
12 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
13 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 **
15 ** See http://www.quickfixengine.org/LICENSE for licensing information.
16 **
17 ** Contact ask@quickfixengine.org if any conditions of this licensing are
18 ** not clear to you.
19 **
20 ****************************************************************************/
21 
22 #ifndef FIX_SOCKETSERVER_H
23 #define FIX_SOCKETSERVER_H
24 
25 #ifdef _MSC_VER
26 #pragma warning( disable : 4503 4355 4786 4290 )
27 #endif
28 
29 #include "SocketMonitor.h"
30 #include "Exceptions.h"
31 #include <map>
32 #include <set>
33 #include <queue>
34 
35 namespace FIX
36 {
38 struct SocketInfo
39 {
40  SocketInfo()
41  : m_socket( -1 ), m_port( 0 ), m_noDelay( false ),
42  m_sendBufSize( 0 ), m_rcvBufSize( 0 ) {}
43 
44  SocketInfo( int socket, short port, bool noDelay, int sendBufSize, int rcvBufSize )
45  : m_socket( socket ), m_port( port ), m_noDelay( noDelay ),
46  m_sendBufSize( sendBufSize ), m_rcvBufSize( rcvBufSize ) {}
47 
48  int m_socket;
49  short m_port;
50  bool m_noDelay;
51  int m_sendBufSize;
52  int m_rcvBufSize;
53 };
54 
56 class SocketServer
57 {
58 public:
59  class Strategy;
60 
61  SocketServer( int timeout = 0 );
62 
63  int add( int port, bool reuse = false, bool noDelay = false,
64  int sendBufSize = 0, int rcvBufSize = 0 ) throw( SocketException& );
65  int accept( int socket );
66  void close();
67  bool block( Strategy& strategy, bool poll = 0, double timeout = 0.0 );
68 
69  size_t numConnections() { return m_monitor.numSockets() - 1; }
70  SocketMonitor& getMonitor() { return m_monitor; }
71 
72  int socketToPort( int socket );
73  int portToSocket( int port );
74 
75 private:
76  typedef std::map<int, SocketInfo>
78  typedef std::map<int, SocketInfo>
79  PortToInfo;
80 
84 
85 public:
86  class Strategy
87  {
88  public:
89  virtual ~Strategy() {}
90  virtual void onConnect( SocketServer&, int acceptSocket, int socket ) = 0;
91  virtual void onWrite( SocketServer&, int socket ) = 0;
92  virtual bool onData( SocketServer&, int socket ) = 0;
93  virtual void onDisconnect( SocketServer&, int socket ) = 0;
94  virtual void onError( SocketServer& ) = 0;
95  virtual void onTimeout( SocketServer& ) {};
96  };
97 };
98 }
99 
100 #endif //FIX_SOCKETSERVER_H
FIX::SocketServer::m_monitor
SocketMonitor m_monitor
Definition: SocketServer.h:100
FIX::SocketServer::Strategy::onError
virtual void onError(SocketServer &)=0
FIX::SocketServer::Strategy::onData
virtual bool onData(SocketServer &, int socket)=0
FIX::SocketInfo::m_noDelay
bool m_noDelay
Definition: SocketServer.h:84
FIX::SocketServer::SocketServer
SocketServer(int timeout=0)
Definition: SocketServer.cpp:111
FIX::SocketServer::Strategy::onTimeout
virtual void onTimeout(SocketServer &)
Definition: SocketServer.h:112
FIX::SocketMonitor
Monitors events on a collection of sockets.
Definition: SocketMonitor.h:64
FIX::SocketServer::socketToPort
int socketToPort(int socket)
Definition: SocketServer.cpp:181
FIX::SocketServer::block
bool block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketServer.cpp:165
FIX::SocketServer::Strategy::onDisconnect
virtual void onDisconnect(SocketServer &, int socket)=0
FIX::SocketServer::close
void close()
Definition: SocketServer.cpp:154
FIX::SocketServer::getMonitor
SocketMonitor & getMonitor()
Definition: SocketServer.h:87
FIX::SocketServer::portToSocket
int portToSocket(int port)
Definition: SocketServer.cpp:188
FIX::SocketServer::SocketToInfo
std::map< int, SocketInfo > SocketToInfo
Definition: SocketServer.h:94
FIX::SocketInfo::m_rcvBufSize
int m_rcvBufSize
Definition: SocketServer.h:86
FIX::SocketInfo::m_socket
int m_socket
Definition: SocketServer.h:82
FIX::SocketServer::m_portToInfo
PortToInfo m_portToInfo
Definition: SocketServer.h:99
FIX::SocketInfo::m_sendBufSize
int m_sendBufSize
Definition: SocketServer.h:85
FIX::SocketMonitor::numSockets
size_t numSockets()
Definition: SocketMonitor.h:97
FIX::SocketServer::PortToInfo
std::map< int, SocketInfo > PortToInfo
Definition: SocketServer.h:96
FIX::SocketServer::Strategy::onWrite
virtual void onWrite(SocketServer &, int socket)=0
FIX::SocketServer::Strategy::onConnect
virtual void onConnect(SocketServer &, int acceptSocket, int socket)=0
SocketMonitor.h
FIX
Definition: Acceptor.cpp:34
FIX::SocketInfo::m_port
short m_port
Definition: SocketServer.h:83
FIX::SocketServer::m_socketToInfo
SocketToInfo m_socketToInfo
Definition: SocketServer.h:98
FIX::SocketServer::numConnections
size_t numConnections()
Definition: SocketServer.h:86
FIX::SocketInfo::SocketInfo
SocketInfo()
Definition: SocketServer.h:74
FIX::SocketServer::Strategy
Definition: SocketServer.h:103
Exceptions.h
FIX::SocketServer
Listens for and accepts incoming socket connections on a port.
Definition: SocketServer.h:73
FIX::SocketServer::Strategy::~Strategy
virtual ~Strategy()
Definition: SocketServer.h:106
FIX::SocketServer::add
int add(int port, bool reuse=false, bool noDelay=false, int sendBufSize=0, int rcvBufSize=0)
Definition: SocketServer.cpp:114
FIX::SocketServer::accept
int accept(int socket)
Definition: SocketServer.cpp:138

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001