SocketConnector.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 "SocketConnector.h"
27#include "Utility.h"
28#ifndef _MSC_VER
29#include <unistd.h>
30#include <sys/ioctl.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#endif
34#include <iostream>
35
36namespace FIX
37{
40{
41public:
44: m_connector( connector ), m_strategy( strategy ) {}
45
46private:
47 void onConnect( SocketMonitor&, int socket )
48 {
50 }
51
52 void onWrite( SocketMonitor&, int socket )
53 {
55 }
56
57 void onEvent( SocketMonitor&, int socket )
58 {
59 if( !m_strategy.onData( m_connector, socket ) )
61 }
62
63 void onError( SocketMonitor&, int socket )
64 {
66 }
67
72
77
80};
81
83: m_monitor( timeout ) {}
84
85int SocketConnector::connect( const std::string& address, int port, bool noDelay,
86 int sendBufSize, int rcvBufSize,
87 const std::string& sourceAddress, int sourcePort)
88{
89 int socket = socket_createConnector();
90
91 if ( socket != -1 )
92 {
93 if( noDelay )
94 socket_setsockopt( socket, TCP_NODELAY );
95 if( sendBufSize )
96 socket_setsockopt( socket, SO_SNDBUF, sendBufSize );
97 if( rcvBufSize )
98 socket_setsockopt( socket, SO_RCVBUF, rcvBufSize );
99 if ( !sourceAddress.empty() || sourcePort )
100 socket_bind( socket, sourceAddress.c_str(), sourcePort );
101 m_monitor.addConnect( socket );
102 socket_connect( socket, address.c_str(), port );
103 }
104 return socket;
105}
106
107int SocketConnector::connect( const std::string& address, int port, bool noDelay,
108 int sendBufSize, int rcvBufSize, Strategy& strategy )
109{
110 int socket = connect( address, port, noDelay, sendBufSize, rcvBufSize, "", 0);
111 return socket;
112}
113
114void SocketConnector::block( Strategy& strategy, bool poll, double timeout )
115{
116 ConnectorWrapper wrapper( *this, strategy );
117 m_monitor.block( wrapper, poll, timeout );
118}
119}
Handles events from SocketMonitor for client connections.
SocketConnector & m_connector
ConnectorWrapper(SocketConnector &connector, SocketConnector::Strategy &strategy)
SocketConnector::Strategy & m_strategy
void onWrite(SocketMonitor &, int socket)
void onTimeout(SocketMonitor &)
void onConnect(SocketMonitor &, int socket)
void onEvent(SocketMonitor &, int socket)
void onError(SocketMonitor &)
void onError(SocketMonitor &, int socket)
virtual void onError(SocketConnector &)=0
virtual bool onData(SocketConnector &, int socket)=0
virtual void onDisconnect(SocketConnector &, int socket)=0
virtual void onTimeout(SocketConnector &)
virtual void onWrite(SocketConnector &, int socket)=0
virtual void onConnect(SocketConnector &, int socket)=0
Connects sockets to remote ports and addresses.
int connect(const std::string &address, int port, bool noDelay, int sendBufSize, int rcvBufSize, const std::string &sourceAddress="", int sourcePort=0)
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
SocketMonitor m_monitor
SocketConnector(int timeout=0)
Monitors events on a collection of sockets.
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
bool addConnect(int socket)
int socket_setsockopt(int s, int opt)
Definition Utility.cpp:208
int socket_createConnector()
Definition Utility.cpp:143
int socket_connect(int socket, const char *address, int port)
Definition Utility.cpp:148
int socket_bind(int socket, const char *hostname, int port)
Definition Utility.cpp:103

Generated on Sat Feb 3 2024 04:23:15 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001