eliminate Connector interface

no code generically creates UDP or TCP connections,
so this abstraction results only in unnecessary
virtual calls and casts.
This commit is contained in:
Michael Davidsaver
2017-11-28 15:28:59 -06:00
parent c4fe7150c9
commit a10534d74d
6 changed files with 22 additions and 67 deletions

View File

@@ -33,9 +33,6 @@ BlockingTCPConnector::BlockingTCPConnector(
{
}
BlockingTCPConnector::~BlockingTCPConnector() {
}
SOCKET BlockingTCPConnector::tryConnect(osiSockAddr& address, int tries) {
char strBuffer[64];

View File

@@ -18,9 +18,9 @@ using namespace epics::pvData;
namespace {
struct closer {
epics::pvAccess::Transport::shared_pointer P;
closer(const epics::pvAccess::Transport::shared_pointer& P) :P(P) {}
void operator()(epics::pvAccess::Transport*) {
epics::pvAccess::BlockingUDPTransport::shared_pointer P;
closer(const epics::pvAccess::BlockingUDPTransport::shared_pointer& P) :P(P) {}
void operator()(epics::pvAccess::BlockingUDPTransport*) {
try{
P->close();
}catch(...){
@@ -35,7 +35,7 @@ struct closer {
namespace epics {
namespace pvAccess {
Transport::shared_pointer BlockingUDPConnector::connect(std::tr1::shared_ptr<ClientChannelImpl> const & /*client*/,
BlockingUDPTransport::shared_pointer BlockingUDPConnector::connect(std::tr1::shared_ptr<ClientChannelImpl> const & /*client*/,
ResponseHandler::shared_pointer const & responseHandler, osiSockAddr& bindAddress,
int8 transportRevision, int16 /*priority*/) {
@@ -47,7 +47,7 @@ Transport::shared_pointer BlockingUDPConnector::connect(std::tr1::shared_ptr<Cli
char errStr[64];
epicsSocketConvertErrnoToString(errStr, sizeof(errStr));
LOG(logLevelError, "Error creating socket: %s.", errStr);
return Transport::shared_pointer();
return BlockingUDPTransport::shared_pointer();
}
int optval = _broadcast ? 1 : 0;
@@ -58,7 +58,7 @@ Transport::shared_pointer BlockingUDPConnector::connect(std::tr1::shared_ptr<Cli
epicsSocketConvertErrnoToString(errStr, sizeof(errStr));
LOG(logLevelError, "Error setting SO_BROADCAST: %s.", errStr);
epicsSocketDestroy (socket);
return Transport::shared_pointer();
return BlockingUDPTransport::shared_pointer();
}
/*
@@ -83,17 +83,16 @@ Transport::shared_pointer BlockingUDPConnector::connect(std::tr1::shared_ptr<Cli
epicsSocketConvertErrnoToString(errStr, sizeof(errStr));
LOG(logLevelError, "Error binding socket %s: %s.", ip, errStr);
epicsSocketDestroy (socket);
return Transport::shared_pointer();
return BlockingUDPTransport::shared_pointer();
}
// sockets are blocking by default
BlockingUDPTransport::shared_pointer transport(new BlockingUDPTransport(_serverFlag, responseHandler,
socket, bindAddress, transportRevision));
transport->internal_this = transport;
// the worker thread holds a strong ref, which is released by transport->close()
// note: casting to Transport* to prevent iOS version of shared_ptr from trying (and failing)
// to setup shared_from_this() using the wrapped pointer
Transport::shared_pointer ret(static_cast<Transport*>(transport.get()), closer(transport));
BlockingUDPTransport::shared_pointer ret(transport.get(), closer(transport));
return ret;
}

View File

@@ -207,7 +207,7 @@ void BlockingUDPTransport::run() {
osiSockAddr fromAddress;
osiSocklen_t addrStructSize = sizeof(sockaddr);
Transport::shared_pointer thisTransport = shared_from_this();
Transport::shared_pointer thisTransport(internal_this);
try {
@@ -573,10 +573,10 @@ void initializeUDPTransports(bool serverFlag,
anyAddress.ia.sin_port = htons(0);
anyAddress.ia.sin_addr.s_addr = htonl(INADDR_ANY);
sendTransport = static_pointer_cast<BlockingUDPTransport>(connector->connect(
sendTransport = connector->connect(
nullTransportClient, responseHandler,
anyAddress, PVA_PROTOCOL_REVISION,
PVA_DEFAULT_PRIORITY));
PVA_DEFAULT_PRIORITY);
if (!sendTransport)
{
THROW_BASE_EXCEPTION("Failed to initialize UDP transport.");
@@ -693,10 +693,10 @@ void initializeUDPTransports(bool serverFlag,
listenLocalAddress.ia.sin_port = htons(listenPort);
listenLocalAddress.ia.sin_addr.s_addr = node.ifaceAddr.ia.sin_addr.s_addr;
BlockingUDPTransport::shared_pointer transport = static_pointer_cast<BlockingUDPTransport>(connector->connect(
BlockingUDPTransport::shared_pointer transport = connector->connect(
nullTransportClient, responseHandler,
listenLocalAddress, PVA_PROTOCOL_REVISION,
PVA_DEFAULT_PRIORITY));
PVA_DEFAULT_PRIORITY);
if (!transport)
continue;
listenLocalAddress = *transport->getRemoteAddress();
@@ -730,10 +730,10 @@ void initializeUDPTransports(bool serverFlag,
bcastAddress.ia.sin_port = htons(listenPort);
bcastAddress.ia.sin_addr.s_addr = node.ifaceBCast.ia.sin_addr.s_addr;
transport2 = static_pointer_cast<BlockingUDPTransport>(connector->connect(
transport2 = connector->connect(
nullTransportClient, responseHandler,
bcastAddress, PVA_PROTOCOL_REVISION,
PVA_DEFAULT_PRIORITY));
PVA_DEFAULT_PRIORITY);
if (transport2)
{
/* The other wrinkle is that nothing should be sent from this second
@@ -786,7 +786,7 @@ void initializeUDPTransports(bool serverFlag,
try
{
// NOTE: multicast receiver socket must be "bound" to INADDR_ANY or multicast address
localMulticastTransport = static_pointer_cast<BlockingUDPTransport>(connector->connect(
localMulticastTransport = connector->connect(
nullTransportClient, responseHandler,
#if !defined(_WIN32)
group,
@@ -794,7 +794,7 @@ void initializeUDPTransports(bool serverFlag,
anyAddress,
#endif
PVA_PROTOCOL_REVISION,
PVA_DEFAULT_PRIORITY));
PVA_DEFAULT_PRIORITY);
if (!localMulticastTransport)
throw std::runtime_error("Failed to bind UDP socket.");

View File

@@ -49,16 +49,14 @@ class ClientChannelImpl;
* @author <a href="mailto:matej.sekoranjaATcosylab.com">Matej Sekoranja</a>
* @version $Id: BlockingTCPConnector.java,v 1.1 2010/05/03 14:45:47 mrkraimer Exp $
*/
class BlockingTCPConnector : public Connector {
class BlockingTCPConnector {
public:
POINTER_DEFINITIONS(BlockingTCPConnector);
BlockingTCPConnector(Context::shared_pointer const & context, int receiveBufferSize,
float beaconInterval);
virtual ~BlockingTCPConnector();
virtual Transport::shared_pointer connect(std::tr1::shared_ptr<ClientChannelImpl> const & client,
Transport::shared_pointer connect(std::tr1::shared_ptr<ClientChannelImpl> const & client,
ResponseHandler::shared_pointer const & responseHandler, osiSockAddr& address,
epics::pvData::int8 transportRevision, epics::pvData::int16 priority);
private:

View File

@@ -44,13 +44,13 @@ enum InetAddressType { inetAddressType_all, inetAddressType_unicast, inetAddress
class BlockingUDPTransport : public epics::pvData::NoDefaultMethods,
public Transport,
public TransportSendControl,
public std::tr1::enable_shared_from_this<BlockingUDPTransport>,
public epicsThreadRunable
{
public:
POINTER_DEFINITIONS(BlockingUDPTransport);
private:
std::tr1::weak_ptr<BlockingUDPTransport> internal_this;
friend class BlockingUDPConnector;
BlockingUDPTransport(bool serverFlag,
ResponseHandler::shared_pointer const & responseHandler,
@@ -58,19 +58,6 @@ private:
short remoteTransportRevision);
public:
static shared_pointer create(bool serverFlag,
ResponseHandler::shared_pointer const & responseHandler,
SOCKET channel, osiSockAddr& bindAddress,
short remoteTransportRevision) EPICS_DEPRECATED
{
shared_pointer thisPointer(
new BlockingUDPTransport(serverFlag, responseHandler,
channel, bindAddress,
remoteTransportRevision)
);
return thisPointer;
}
virtual ~BlockingUDPTransport();
virtual bool isClosed() {
@@ -428,7 +415,6 @@ private:
};
class BlockingUDPConnector :
public Connector,
private epics::pvData::NoDefaultMethods {
public:
POINTER_DEFINITIONS(BlockingUDPConnector);
@@ -442,13 +428,10 @@ public:
_broadcast(broadcast) {
}
virtual ~BlockingUDPConnector() {
}
/**
* NOTE: transport client is ignored for broadcast (UDP).
*/
virtual Transport::shared_pointer connect(std::tr1::shared_ptr<ClientChannelImpl> const & client,
BlockingUDPTransport::shared_pointer connect(std::tr1::shared_ptr<ClientChannelImpl> const & client,
ResponseHandler::shared_pointer const & responseHandler, osiSockAddr& bindAddress,
epics::pvData::int8 transportRevision, epics::pvData::int16 priority);

View File

@@ -377,28 +377,6 @@ protected:
epics::pvData::int32 _debugLevel;
};
/**
* Interface defining socket connector (Connector-Transport pattern).
*/
class Connector {
public:
virtual ~Connector() {}
/**
* Connect.
* @param[in] client client requesting connection (transport).
* @param[in] address address of the server.
* @param[in] responseHandler reponse handler.
* @param[in] transportRevision transport revision to be used.
* @param[in] priority process priority.
* @return transport instance.
*/
virtual Transport::shared_pointer connect(std::tr1::shared_ptr<ClientChannelImpl> const & client,
ResponseHandler::shared_pointer const & responseHandler, osiSockAddr& address,
epics::pvData::int8 transportRevision, epics::pvData::int16 priority) = 0;
};
/**
* A request that expects an response.
* Responses identified by its I/O ID.