SocketConnector.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #include "SocketConnector.h"
00027 #include "Utility.h"
00028 #ifndef _MSC_VER
00029 #include <unistd.h>
00030 #include <sys/ioctl.h>
00031 #include <sys/types.h>
00032 #include <sys/stat.h>
00033 #endif
00034 #include <iostream>
00035 
00036 namespace FIX
00037 {
00039 class ConnectorWrapper : public SocketMonitor::Strategy
00040 {
00041 public:
00042   ConnectorWrapper( SocketConnector& connector,
00043                     SocketConnector::Strategy& strategy )
00044 : m_connector( connector ), m_strategy( strategy ) {}
00045 
00046 private:
00047   void onConnect( SocketMonitor&, int socket )
00048   {    
00049     m_strategy.onConnect( m_connector, socket );
00050   }
00051 
00052   void onWrite( SocketMonitor&, int socket )
00053   {
00054     m_strategy.onWrite( m_connector, socket );
00055   }
00056 
00057   void onEvent( SocketMonitor&, int socket )
00058   {
00059     if( !m_strategy.onData( m_connector, socket ) )
00060       m_strategy.onDisconnect( m_connector, socket );
00061   }
00062 
00063   void onError( SocketMonitor&, int socket )
00064   {
00065     m_strategy.onDisconnect( m_connector, socket );
00066   }
00067 
00068   void onError( SocketMonitor& )
00069   {
00070     m_strategy.onError( m_connector );
00071   }
00072 
00073   void onTimeout( SocketMonitor& )
00074   {
00075     m_strategy.onTimeout( m_connector );
00076   };
00077 
00078   SocketConnector& m_connector;
00079   SocketConnector::Strategy& m_strategy;
00080 };
00081 
00082 SocketConnector::SocketConnector( int timeout )
00083 : m_monitor( timeout ) {}
00084 
00085 int SocketConnector::connect( const std::string& address, int port, bool noDelay,
00086                               int sendBufSize, int rcvBufSize )
00087 {
00088   int socket = socket_createConnector();
00089 
00090   if ( socket != -1 )
00091   {
00092     if( noDelay )
00093       socket_setsockopt( socket, TCP_NODELAY );
00094     if( sendBufSize )
00095       socket_setsockopt( socket, SO_SNDBUF, sendBufSize );
00096     if( rcvBufSize )
00097       socket_setsockopt( socket, SO_RCVBUF, rcvBufSize );
00098     m_monitor.addConnect( socket );
00099     socket_connect( socket, address.c_str(), port );
00100   }
00101   return socket;
00102 }
00103 
00104 int SocketConnector::connect( const std::string& address, int port, bool noDelay, 
00105                               int sendBufSize, int rcvBufSize, Strategy& strategy )
00106 {
00107   int socket = connect( address, port, noDelay, sendBufSize, rcvBufSize );
00108   return socket;
00109 }
00110 
00111 void SocketConnector::block( Strategy& strategy, bool poll, double timeout )
00112 {
00113   ConnectorWrapper wrapper( *this, strategy );
00114   m_monitor.block( wrapper, poll, timeout );
00115 }
00116 }

Generated on Mon Sep 15 2014 01:23:54 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001