SocketConnector.cpp
Go to the documentation of this file.
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifdef _MSC_VER
#include "stdafx.h"
#else
#include "config.h"
#endif
#include "SocketConnector.h"
#include "Utility.h"
#ifndef _MSC_VER
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <iostream>
namespace FIX
{
class ConnectorWrapper : public SocketMonitor::Strategy
{
public:
ConnectorWrapper( SocketConnector& connector,
SocketConnector::Strategy& strategy )
: m_connector( connector ), m_strategy( strategy ) {}
private:
void onConnect( SocketMonitor&, int socket )
{
m_strategy.onConnect( m_connector, socket );
}
void onWrite( SocketMonitor&, int socket )
{
m_strategy.onWrite( m_connector, socket );
}
void onEvent( SocketMonitor&, int socket )
{
if( !m_strategy.onData( m_connector, socket ) )
m_strategy.onDisconnect( m_connector, socket );
}
void onError( SocketMonitor&, int socket )
{
m_strategy.onDisconnect( m_connector, socket );
}
void onError( SocketMonitor& )
{
m_strategy.onError( m_connector );
}
void onTimeout( SocketMonitor& )
{
m_strategy.onTimeout( m_connector );
};
SocketConnector& m_connector;
SocketConnector::Strategy& m_strategy;
};
SocketConnector::SocketConnector( int timeout )
: m_monitor( timeout ) {}
int SocketConnector::connect( const std::string& address, int port, bool noDelay,
int sendBufSize, int rcvBufSize,
const std::string& sourceAddress, int sourcePort)
{
int socket = socket_createConnector();
if ( socket != -1 )
{
if( noDelay )
socket_setsockopt( socket, TCP_NODELAY );
if( sendBufSize )
socket_setsockopt( socket, SO_SNDBUF, sendBufSize );
if( rcvBufSize )
socket_setsockopt( socket, SO_RCVBUF, rcvBufSize );
if ( !sourceAddress.empty() || sourcePort )
socket_bind( socket, sourceAddress.c_str(), sourcePort );
m_monitor.addConnect( socket );
socket_connect( socket, address.c_str(), port );
}
return socket;
}
int SocketConnector::connect( const std::string& address, int port, bool noDelay,
int sendBufSize, int rcvBufSize, Strategy& strategy )
{
int socket = connect( address, port, noDelay, sendBufSize, rcvBufSize, "", 0);
return socket;
}
void SocketConnector::block( Strategy& strategy, bool poll, double timeout )
{
ConnectorWrapper wrapper( *this, strategy );
m_monitor.block( wrapper, poll, timeout );
}
}
SocketConnector.h
Utility.h
FIX::ConnectorWrapper
Handles events from SocketMonitor for client connections.
Definition: SocketConnector.cpp:40
FIX::ConnectorWrapper::m_connector
SocketConnector & m_connector
Definition: SocketConnector.cpp:76
FIX::ConnectorWrapper::ConnectorWrapper
ConnectorWrapper(SocketConnector &connector, SocketConnector::Strategy &strategy)
Definition: SocketConnector.cpp:42
FIX::ConnectorWrapper::m_strategy
SocketConnector::Strategy & m_strategy
Definition: SocketConnector.cpp:79
FIX::ConnectorWrapper::onWrite
void onWrite(SocketMonitor &, int socket)
Definition: SocketConnector.cpp:52
FIX::ConnectorWrapper::onTimeout
void onTimeout(SocketMonitor &)
Definition: SocketConnector.cpp:73
FIX::ConnectorWrapper::onConnect
void onConnect(SocketMonitor &, int socket)
Definition: SocketConnector.cpp:47
FIX::ConnectorWrapper::onEvent
void onEvent(SocketMonitor &, int socket)
Definition: SocketConnector.cpp:57
FIX::ConnectorWrapper::onError
void onError(SocketMonitor &)
Definition: SocketConnector.cpp:68
FIX::ConnectorWrapper::onError
void onError(SocketMonitor &, int socket)
Definition: SocketConnector.cpp:63
FIX::SocketConnector::Strategy
Definition: SocketConnector.h:55
FIX::SocketConnector::Strategy::onError
virtual void onError(SocketConnector &)=0
FIX::SocketConnector::Strategy::onData
virtual bool onData(SocketConnector &, int socket)=0
FIX::SocketConnector::Strategy::onDisconnect
virtual void onDisconnect(SocketConnector &, int socket)=0
FIX::SocketConnector::Strategy::onTimeout
virtual void onTimeout(SocketConnector &)
Definition: SocketConnector.h:63
FIX::SocketConnector::Strategy::onWrite
virtual void onWrite(SocketConnector &, int socket)=0
FIX::SocketConnector::Strategy::onConnect
virtual void onConnect(SocketConnector &, int socket)=0
FIX::SocketConnector
Connects sockets to remote ports and addresses.
Definition: SocketConnector.h:36
FIX::SocketConnector::connect
int connect(const std::string &address, int port, bool noDelay, int sendBufSize, int rcvBufSize, const std::string &sourceAddress="", int sourcePort=0)
Definition: SocketConnector.cpp:85
FIX::SocketConnector::block
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketConnector.cpp:114
FIX::SocketConnector::m_monitor
SocketMonitor m_monitor
Definition: SocketConnector.h:51
FIX::SocketConnector::SocketConnector
SocketConnector(int timeout=0)
Definition: SocketConnector.cpp:82
FIX::SocketMonitor::Strategy
Definition: SocketMonitor.h:96
FIX::SocketMonitor
Monitors events on a collection of sockets.
Definition: SocketMonitor.h:48
FIX::SocketMonitor::block
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketMonitor.cpp:181
FIX::SocketMonitor::addConnect
bool addConnect(int socket)
Definition: SocketMonitor.cpp:65
FIX
Definition: Acceptor.cpp:35
FIX::socket_setsockopt
int socket_setsockopt(int s, int opt)
Definition: Utility.cpp:208
FIX::socket_createConnector
int socket_createConnector()
Definition: Utility.cpp:143
FIX::socket_connect
int socket_connect(int socket, const char *address, int port)
Definition: Utility.cpp:148
FIX::socket_bind
int socket_bind(int socket, const char *hostname, int port)
Definition: Utility.cpp:103