SocketServer.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 "SocketServer.h"
#include "Utility.h"
#include "Exceptions.h"
#ifndef _MSC_VER
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <exception>
namespace FIX
{
class ServerWrapper : public SocketMonitor::Strategy
{
public:
ServerWrapper( std::set<int> sockets, SocketServer& server,
SocketServer::Strategy& strategy )
: m_sockets( sockets ), m_server( server ), m_strategy( strategy ) {}
private:
void onConnect( SocketMonitor&, int socket )
{
}
void onEvent( SocketMonitor& monitor, int socket )
{
if( m_sockets.find(socket) != m_sockets.end() )
{
m_strategy.onConnect( m_server, socket, m_server.accept(socket) );
}
else
{
if( !m_strategy.onData( m_server, socket ) )
onError( monitor, socket );
}
}
void onWrite( SocketMonitor&, int socket )
{
m_strategy.onWrite( m_server, socket );
}
void onError( SocketMonitor& monitor, int socket )
{
m_strategy.onDisconnect( m_server, socket );
monitor.drop( socket );
}
void onError( SocketMonitor& )
{
m_strategy.onError( m_server );
}
void onTimeout( SocketMonitor& )
{
m_strategy.onTimeout( m_server );
};
typedef std::set<int>
Sockets;
Sockets m_sockets;
SocketServer& m_server;
SocketServer::Strategy& m_strategy;
};
SocketServer::SocketServer( int timeout )
: m_monitor( timeout ) {}
int SocketServer::add( int port, bool reuse, bool noDelay,
int sendBufSize, int rcvBufSize )
throw( SocketException& )
{
if( m_portToInfo.find(port) != m_portToInfo.end() )
return m_portToInfo[port].m_socket;
int socket = socket_createAcceptor( port, reuse );
if( socket < 0 )
throw SocketException();
if( noDelay )
socket_setsockopt( socket, TCP_NODELAY );
if( sendBufSize )
socket_setsockopt( socket, SO_SNDBUF, sendBufSize );
if( rcvBufSize )
socket_setsockopt( socket, SO_RCVBUF, rcvBufSize );
m_monitor.addRead( socket );
SocketInfo info( socket, port, noDelay, sendBufSize, rcvBufSize );
m_socketToInfo[socket] = info;
m_portToInfo[port] = info;
return socket;
}
int SocketServer::accept( int socket )
{
SocketInfo info = m_socketToInfo[socket];
int result = socket_accept( socket );
if( info.m_noDelay )
socket_setsockopt( result, TCP_NODELAY );
if( info.m_sendBufSize )
socket_setsockopt( result, SO_SNDBUF, info.m_sendBufSize );
if( info.m_rcvBufSize )
socket_setsockopt( result, SO_RCVBUF, info.m_rcvBufSize );
if ( result >= 0 )
m_monitor.addConnect( result );
return result;
}
void SocketServer::close()
{
SocketToInfo::iterator i = m_socketToInfo.begin();
for( ; i != m_socketToInfo.end(); ++i )
{
int s = i->first;
socket_close( s );
socket_invalidate( s );
}
}
bool SocketServer::block( Strategy& strategy, bool poll, double timeout )
{
std::set<int> sockets;
SocketToInfo::iterator i = m_socketToInfo.begin();
for( ; i != m_socketToInfo.end(); ++i )
{
if( !socket_isValid(i->first) )
return false;
sockets.insert( i->first );
}
ServerWrapper wrapper( sockets, *this, strategy );
m_monitor.block( wrapper, poll, timeout );
return true;
}
int SocketServer::socketToPort( int socket )
{
SocketToInfo::iterator find = m_socketToInfo.find( socket );
if( find == m_socketToInfo.end() ) return 0;
return find->second.m_port;
}
int SocketServer::portToSocket( int port )
{
SocketToInfo::iterator find = m_portToInfo.find( port );
if( find == m_portToInfo.end() ) return 0;
return find->second.m_socket;
}
}
FIX::SocketServer::m_monitor
SocketMonitor m_monitor
Definition: SocketServer.h:100
FIX::SocketMonitor::block
void block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketMonitor.cpp:198
FIX::SocketServer::Strategy::onError
virtual void onError(SocketServer &)=0
FIX::SocketServer::Strategy::onData
virtual bool onData(SocketServer &, int socket)=0
FIX::SocketInfo::m_noDelay
bool m_noDelay
Definition: SocketServer.h:84
FIX::ServerWrapper
Handles events from SocketMonitor for server connections.
Definition: SocketServer.cpp:57
FIX::SocketInfo
Information about listening socket.
Definition: SocketServer.h:55
FIX::ServerWrapper::onConnect
void onConnect(SocketMonitor &, int socket)
Definition: SocketServer.cpp:82
FIX::SocketServer::SocketServer
SocketServer(int timeout=0)
Definition: SocketServer.cpp:111
FIX::SocketException
Socket Error.
Definition: Exceptions.h:262
FIX::socket_setsockopt
int socket_setsockopt(int s, int opt)
Definition: Utility.cpp:225
FIX::ServerWrapper::onTimeout
void onTimeout(SocketMonitor &)
Definition: SocketServer.cpp:115
FIX::ServerWrapper::onWrite
void onWrite(SocketMonitor &, int socket)
Definition: SocketServer.cpp:99
FIX::SocketServer::Strategy::onTimeout
virtual void onTimeout(SocketServer &)
Definition: SocketServer.h:112
FIX::SocketMonitor
Monitors events on a collection of sockets.
Definition: SocketMonitor.h:64
FIX::ServerWrapper::m_strategy
SocketServer::Strategy & m_strategy
Definition: SocketServer.cpp:125
FIX::SocketServer::socketToPort
int socketToPort(int socket)
Definition: SocketServer.cpp:181
FIX::ServerWrapper::Sockets
std::set< int > Sockets
Definition: SocketServer.cpp:118
FIX::SocketServer::block
bool block(Strategy &strategy, bool poll=0, double timeout=0.0)
Definition: SocketServer.cpp:165
FIX::SocketMonitor::addConnect
bool addConnect(int socket)
Definition: SocketMonitor.cpp:82
FIX::SocketServer::Strategy::onDisconnect
virtual void onDisconnect(SocketServer &, int socket)=0
FIX::SocketServer::close
void close()
Definition: SocketServer.cpp:154
FIX::socket_isValid
bool socket_isValid(int socket)
Definition: Utility.cpp:294
FIX::ServerWrapper::m_sockets
Sockets m_sockets
Definition: SocketServer.cpp:123
FIX::SocketServer::portToSocket
int portToSocket(int port)
Definition: SocketServer.cpp:188
FIX::SocketInfo::m_rcvBufSize
int m_rcvBufSize
Definition: SocketServer.h:86
FIX::SocketServer::m_portToInfo
PortToInfo m_portToInfo
Definition: SocketServer.h:99
FIX::SocketInfo::m_sendBufSize
int m_sendBufSize
Definition: SocketServer.h:85
FIX::socket_accept
int socket_accept(int s)
Definition: Utility.cpp:181
FIX::ServerWrapper::onError
void onError(SocketMonitor &monitor, int socket)
Definition: SocketServer.cpp:104
FIX::SocketServer::Strategy::onWrite
virtual void onWrite(SocketServer &, int socket)=0
FIX::ServerWrapper::onEvent
void onEvent(SocketMonitor &monitor, int socket)
Definition: SocketServer.cpp:86
FIX::SocketServer::Strategy::onConnect
virtual void onConnect(SocketServer &, int acceptSocket, int socket)=0
FIX
Definition: Acceptor.cpp:34
FIX::SocketServer::m_socketToInfo
SocketToInfo m_socketToInfo
Definition: SocketServer.h:98
FIX::socket_createAcceptor
int socket_createAcceptor(int port, bool reuse)
Definition: Utility.cpp:137
FIX::socket_invalidate
void socket_invalidate(int &socket)
Definition: Utility.cpp:312
SocketServer.h
FIX::ServerWrapper::ServerWrapper
ServerWrapper(std::set< int > sockets, SocketServer &server, SocketServer::Strategy &strategy)
Definition: SocketServer.cpp:77
FIX::socket_close
void socket_close(int s)
Definition: Utility.cpp:197
FIX::ServerWrapper::m_server
SocketServer & m_server
Definition: SocketServer.cpp:124
FIX::SocketServer::Strategy
Definition: SocketServer.h:103
Exceptions.h
FIX::SocketServer
Listens for and accepts incoming socket connections on a port.
Definition: SocketServer.h:73
Utility.h
FIX::SocketServer::add
int add(int port, bool reuse=false, bool noDelay=false, int sendBufSize=0, int rcvBufSize=0)
Definition: SocketServer.cpp:114
FIX::SocketServer::accept
int accept(int socket)
Definition: SocketServer.cpp:138