/* * blockingTCPConnector.cpp * * Created on: Jan 4, 2011 * Author: Miha Vitorovic */ #include #include #include #include #include #include #include #include #include namespace epics { namespace pvAccess { BlockingTCPConnector::BlockingTCPConnector( Context::shared_pointer const & context, int receiveBufferSize, float beaconInterval) : _context(context), _namedLocker(), _receiveBufferSize(receiveBufferSize), _beaconInterval(beaconInterval) { } BlockingTCPConnector::~BlockingTCPConnector() { } SOCKET BlockingTCPConnector::tryConnect(osiSockAddr& address, int tries) { char strBuffer[64]; ipAddrToDottedIP(&address.ia, strBuffer, sizeof(strBuffer)); for(int tryCount = 0; tryCount& responseHandler, osiSockAddr& address, short transportRevision, int16 priority) { SOCKET socket = INVALID_SOCKET; char ipAddrStr[64]; ipAddrToDottedIP(&address.ia, ipAddrStr, sizeof(ipAddrStr)); Context::shared_pointer context = _context.lock(); // first try to check cache w/o named lock... Transport::shared_pointer tt = context->getTransportRegistry()->get("TCP", &address, priority); BlockingClientTCPTransport::shared_pointer transport = std::tr1::static_pointer_cast(tt); if(transport.get()) { errlogSevPrintf(errlogInfo, "Reusing existing connection to CA server: %s", ipAddrStr); if (transport->acquire(client)) return transport; } bool lockAcquired = _namedLocker.acquireSynchronizationObject(&address, LOCK_TIMEOUT); if(lockAcquired) { try { // ... transport created during waiting in lock tt = context->getTransportRegistry()->get("TCP", &address, priority); transport = std::tr1::static_pointer_cast(tt); if(transport.get()) { errlogSevPrintf(errlogInfo, "Reusing existing connection to CA server: %s", ipAddrStr); if (transport->acquire(client)) return transport; } errlogSevPrintf(errlogInfo, "Connecting to CA server: %s", ipAddrStr); socket = tryConnect(address, 3); // verify if(socket==INVALID_SOCKET) { errlogSevPrintf(errlogMajor, "Connection to CA server %s failed.", ipAddrStr); ostringstream temp; temp<<"Failed to verify TCP connection to '"<waitUntilVerified(3.0)) { errlogSevPrintf( errlogMinor, "Connection to CA server %s failed to be validated, closing it.", ipAddrStr); ostringstream temp; temp<<"Failed to verify TCP connection to '"<close(true); else if(socket!=INVALID_SOCKET) epicsSocketDestroy(socket); _namedLocker.releaseSynchronizationObject(&address); throw; } catch(...) { if(transport.get()) transport->close(true); else if(socket!=INVALID_SOCKET) epicsSocketDestroy(socket); _namedLocker.releaseSynchronizationObject(&address); throw; } } else { ostringstream temp; temp<<"Failed to obtain synchronization lock for '"<