SocketServer.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (c) 2001-2014
3 **
4 ** This file is part of the QuickFIX FIX Engine
5 **
6 ** This file may be distributed under the terms of the quickfixengine.org
7 ** license as defined by quickfixengine.org and appearing in the file
8 ** LICENSE included in the packaging of this file.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 ** See http://www.quickfixengine.org/LICENSE for licensing information.
14 **
15 ** Contact ask@quickfixengine.org if any conditions of this licensing are
16 ** not clear to you.
17 **
18 ****************************************************************************/
19 
20 #ifdef _MSC_VER
21 #include "stdafx.h"
22 #else
23 #include "config.h"
24 #endif
25 
26 #include "SocketServer.h"
27 #include "Utility.h"
28 #include "Exceptions.h"
29 #ifndef _MSC_VER
30 #include <unistd.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #endif
35 #include <exception>
36 
37 namespace FIX
38 {
41 {
42 public:
43  ServerWrapper( std::set<int> sockets, SocketServer& server,
44  SocketServer::Strategy& strategy )
45 : m_sockets( sockets ), m_server( server ), m_strategy( strategy ) {}
46 
47 private:
48  void onConnect( SocketMonitor&, int socket )
49  {
50  }
51 
52  void onEvent( SocketMonitor& monitor, int socket )
53  {
54  if( m_sockets.find(socket) != m_sockets.end() )
55  {
56  m_strategy.onConnect( m_server, socket, m_server.accept(socket) );
57  }
58  else
59  {
60  if( !m_strategy.onData( m_server, socket ) )
61  onError( monitor, socket );
62  }
63  }
64 
65  void onWrite( SocketMonitor&, int socket )
66  {
67  m_strategy.onWrite( m_server, socket );
68  }
69 
70  void onError( SocketMonitor& monitor, int socket )
71  {
72  m_strategy.onDisconnect( m_server, socket );
73  monitor.drop( socket );
74  }
75 
77  {
79  }
80 
82  {
84  };
85 
86  typedef std::set<int>
87  Sockets;
88 
89  Sockets m_sockets;
92 };
93 
95 : m_monitor( timeout ) {}
96 
97 int SocketServer::add( int port, bool reuse, bool noDelay,
98  int sendBufSize, int rcvBufSize )
99  throw( SocketException& )
100 {
101  if( m_portToInfo.find(port) != m_portToInfo.end() )
102  return m_portToInfo[port].m_socket;
103 
104  int socket = socket_createAcceptor( port, reuse );
105  if( socket < 0 )
106  throw SocketException();
107  if( noDelay )
108  socket_setsockopt( socket, TCP_NODELAY );
109  if( sendBufSize )
110  socket_setsockopt( socket, SO_SNDBUF, sendBufSize );
111  if( rcvBufSize )
112  socket_setsockopt( socket, SO_RCVBUF, rcvBufSize );
113  m_monitor.addRead( socket );
114 
115  SocketInfo info( socket, port, noDelay, sendBufSize, rcvBufSize );
116  m_socketToInfo[socket] = info;
117  m_portToInfo[port] = info;
118  return socket;
119 }
120 
121 int SocketServer::accept( int socket )
122 {
123  SocketInfo info = m_socketToInfo[socket];
124 
125  int result = socket_accept( socket );
126  if( info.m_noDelay )
127  socket_setsockopt( result, TCP_NODELAY );
128  if( info.m_sendBufSize )
129  socket_setsockopt( result, SO_SNDBUF, info.m_sendBufSize );
130  if( info.m_rcvBufSize )
131  socket_setsockopt( result, SO_RCVBUF, info.m_rcvBufSize );
132  if ( result >= 0 )
133  m_monitor.addConnect( result );
134  return result;
135 }
136 
138 {
139  SocketToInfo::iterator i = m_socketToInfo.begin();
140  for( ; i != m_socketToInfo.end(); ++i )
141  {
142  int s = i->first;
143  socket_close( s );
144  socket_invalidate( s );
145  }
146 }
147 
148 bool SocketServer::block( Strategy& strategy, bool poll, double timeout )
149 {
150  std::set<int> sockets;
151  SocketToInfo::iterator i = m_socketToInfo.begin();
152  for( ; i != m_socketToInfo.end(); ++i )
153  {
154  if( !socket_isValid(i->first) )
155  return false;
156  sockets.insert( i->first );
157  }
158 
159  ServerWrapper wrapper( sockets, *this, strategy );
160  m_monitor.block( wrapper, poll, timeout );
161  return true;
162 }
163 
164 int SocketServer::socketToPort( int socket )
165 {
166  SocketToInfo::iterator find = m_socketToInfo.find( socket );
167  if( find == m_socketToInfo.end() ) return 0;
168  return find->second.m_port;
169 }
170 
172 {
173  SocketToInfo::iterator find = m_portToInfo.find( port );
174  if( find == m_portToInfo.end() ) return 0;
175  return find->second.m_socket;
176 }
177 }
PortToInfo m_portToInfo
Definition: SocketServer.h:82
void onError(SocketMonitor &monitor, int socket)
virtual void onDisconnect(SocketServer &, int socket)=0
virtual void onTimeout(SocketServer &)
Definition: SocketServer.h:95
Information about listening socket.
Definition: SocketServer.h:38
int socket_accept(int s)
Definition: Utility.cpp:164
bool socket_isValid(int socket)
Definition: Utility.cpp:277
SocketServer & m_server
virtual bool onData(SocketServer &, int socket)=0
bool addRead(int socket)
virtual void onWrite(SocketServer &, int socket)=0
void onTimeout(SocketMonitor &)
void onEvent(SocketMonitor &monitor, int socket)
std::set< int > Sockets
SocketServer::Strategy & m_strategy
virtual void onError(SocketServer &)=0
bool block(Strategy &strategy, bool poll=0, double timeout=0.0)
virtual void onConnect(SocketServer &, int acceptSocket, int socket)=0
int socket_setsockopt(int s, int opt)
Definition: Utility.cpp:208
void socket_invalidate(int &socket)
Definition: Utility.cpp:295
int accept(int socket)
Definition: Acceptor.cpp:34
SocketMonitor m_monitor
Definition: SocketServer.h:83
void onWrite(SocketMonitor &, int socket)
int add(int port, bool reuse=false, bool noDelay=false, int sendBufSize=0, int rcvBufSize=0)
Monitors events on a collection of sockets.
Definition: SocketMonitor.h:47
bool addConnect(int socket)
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
void onError(SocketMonitor &)
Handles events from SocketMonitor for server connections.
bool drop(int socket)
SocketServer(int timeout=0)
Socket Error.
Definition: Exceptions.h:245
int socket_createAcceptor(int port, bool reuse)
Definition: Utility.cpp:120
int socketToPort(int socket)
int portToSocket(int port)
void socket_close(int s)
Definition: Utility.cpp:180
void onConnect(SocketMonitor &, int socket)
SocketToInfo m_socketToInfo
Definition: SocketServer.h:81
Listens for and accepts incoming socket connections on a port.
Definition: SocketServer.h:56
ServerWrapper(std::set< int > sockets, SocketServer &server, SocketServer::Strategy &strategy)

Generated on Wed Aug 28 2019 14:13:46 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001