SocketConnector.cpp
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 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 #include "CallStack.h"
00026
00027 #include "SocketConnector.h"
00028 #include "Utility.h"
00029 #ifndef _MSC_VER
00030 #include <unistd.h>
00031 #include <sys/ioctl.h>
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #endif
00035 #include <iostream>
00036
00037 namespace FIX
00038 {
00040 class ConnectorWrapper : public SocketMonitor::Strategy
00041 {
00042 public:
00043 ConnectorWrapper( SocketConnector& connector,
00044 SocketConnector::Strategy& strategy )
00045 : m_connector( connector ), m_strategy( strategy ) {}
00046
00047 private:
00048 void onConnect( SocketMonitor&, int socket )
00049 { QF_STACK_PUSH(ConnectorWrapper::onConnect)
00050
00051 m_strategy.onConnect( m_connector, socket );
00052
00053 QF_STACK_POP
00054 }
00055
00056 void onWrite( SocketMonitor&, int socket )
00057 { QF_STACK_PUSH(ConnectorWrapper::onWrite)
00058
00059 m_strategy.onWrite( m_connector, socket );
00060
00061 QF_STACK_POP
00062 }
00063
00064 void onEvent( SocketMonitor&, int socket )
00065 { QF_STACK_PUSH(ConnectorWrapper::onEvent)
00066
00067 if( !m_strategy.onData( m_connector, socket ) )
00068 m_strategy.onDisconnect( m_connector, socket );
00069
00070 QF_STACK_POP
00071 }
00072
00073 void onError( SocketMonitor&, int socket )
00074 { QF_STACK_PUSH(ConnectorWrapper::onError)
00075
00076 m_strategy.onDisconnect( m_connector, socket );
00077
00078 QF_STACK_POP
00079 }
00080
00081 void onError( SocketMonitor& )
00082 { QF_STACK_PUSH(ConnectorWrapper::onError)
00083 m_strategy.onError( m_connector );
00084 QF_STACK_POP
00085 }
00086
00087 void onTimeout( SocketMonitor& )
00088 { QF_STACK_PUSH(ConnectorWrapper::onTimeout)
00089 m_strategy.onTimeout( m_connector );
00090 QF_STACK_POP
00091 };
00092
00093 SocketConnector& m_connector;
00094 SocketConnector::Strategy& m_strategy;
00095 };
00096
00097 SocketConnector::SocketConnector( int timeout )
00098 : m_monitor( timeout ) {}
00099
00100 int SocketConnector::connect( const std::string& address, int port, bool noDelay,
00101 int sendBufSize, int rcvBufSize )
00102 { QF_STACK_PUSH(SocketConnector::connect)
00103
00104 int socket = socket_createConnector();
00105
00106 if ( socket != -1 )
00107 {
00108 if( noDelay )
00109 socket_setsockopt( socket, TCP_NODELAY );
00110 if( sendBufSize )
00111 socket_setsockopt( socket, SO_SNDBUF, sendBufSize );
00112 if( rcvBufSize )
00113 socket_setsockopt( socket, SO_RCVBUF, rcvBufSize );
00114
00115 if( socket_connect( socket, address.c_str(), port ) < 0 )
00116 {
00117 socket_close( socket );
00118 socket = -1;
00119 }
00120 else
00121 {
00122 m_monitor.addConnect( socket );
00123 }
00124 }
00125 return socket;
00126
00127 QF_STACK_POP
00128 }
00129
00130 int SocketConnector::connect( const std::string& address, int port, bool noDelay,
00131 int sendBufSize, int rcvBufSize, Strategy& strategy )
00132 { QF_STACK_PUSH(SocketConnector::connect)
00133
00134 int socket = connect( address, port, noDelay, sendBufSize, rcvBufSize );
00135 return socket;
00136
00137 QF_STACK_POP
00138 }
00139
00140 void SocketConnector::block( Strategy& strategy, bool poll, double timeout )
00141 { QF_STACK_PUSH(SocketConnector::block)
00142
00143 ConnectorWrapper wrapper( *this, strategy );
00144 m_monitor.block( wrapper, poll, timeout );
00145
00146 QF_STACK_POP
00147 }
00148 }