Error TCPConnectProtocol::requestDetails (const HostId & target, const RequestConnectDetailsCallback & callback, int timeOutMs){ OpId id = genFreeId (); RequestConnectDetails req; req.id = id; Error e = mCommunicationDelegate->send (target, Datagram::fromCmd(req)); if (e) return e; RequestOp * op = new RequestOp (regTimeOutMs(timeOutMs)); op->cb = callback; op->setId(id); addAsyncOp (op); return NoError; }
void UDPEchoClient::start (String echoServer, int echoPort, int timeOutMs) { mToken = sf::genRandomToken80 (); mSocket.readyRead() = dMemFun (this, &UDPEchoClient::onReadyRead); mTimeoutHandle = xcallTimed (dMemFun (this, &UDPEchoClient::onTimeOut), regTimeOutMs(timeOutMs)); char request [256]; snprintf (request, 256, "condata %s", mToken.c_str()); Error e = mSocket.sendTo(echoServer, echoPort, sf::createByteArrayPtr (request)); if (e) { xcall (abind (mResultDelegate, e)); return; } mState = WAIT; }
void HttpConnectionManager::giveBack (Error lastResult, const HttpConnectionPtr & connection) { if (lastResult) { // throw away, asynchronous // we do not trust this anymore... xcall (abind (&throwAway, connection)); return; } PendingConnectionOp * op = new PendingConnectionOp (regTimeOutMs (mGeneralTimeoutMs)); AsyncOpId id = genFreeId(); op->setId(id); op->connection = connection; op->boss = this; op->connection->channel->changed() = abind (dMemFun (this, &HttpConnectionManager::onChannelChange), id); addToPendingConnections (op); Log (LogInfo) << LOGID << "Storing pending connection to " << connection->host << " id=" << id << std::endl; addAsyncOp (op); }
void HttpConnectionManager::requestConnection (const Url & url, int timeOutMs, const RequestConnectionCallback & callback) { assert (callback); { // Check the pool PendingConnectionMap::iterator i = mPendingConnections.find(ConId(url.protocol(), url.host())); if (i != mPendingConnections.end()){ while (!i->second.empty()){ AsyncOpId id = i->second.front(); i->second.pop_front(); PendingConnectionOp * op; getReadyAsyncOp (id, PendingConnection, &op); mPendingConnectionsCount--; if (op) { Log (LogInfo) << LOGID << "Reusing connection to " << url.protocol() << "/" << url.host() << " id=" << op->id() << std::endl; xcall (abind(callback, NoError, op->connection)); delete op; return; } } // not existant anymore mPendingConnections.erase (i); } } EstablishConnectionOp * op = new EstablishConnectionOp (regTimeOutMs (timeOutMs)); const String & protocol = url.protocol(); if (protocol != "http" && protocol != "https") { Log (LogWarning) << LOGID << "Unsupported protocol: " << protocol << std::endl; return xcall(abind(callback, error::NotSupported, HttpConnectionPtr())); } op->callback = callback; op->connection = HttpConnectionPtr (new HttpConnection()); op->connection->host = url.host(); op->connection->pureHost = url.pureHost(); op->connection->protocol = url.protocol(); TCPSocketPtr sock = TCPSocketPtr (new TCPSocket ()); op->connection->channel = sock; op->setId(genFreeId()); op->setState(EstablishConnectionOp::WaitTcpConnect); Error e = sock->connectToHost(url.pureHost(), url.port(), timeOutMs, abind(dMemFun(this, &HttpConnectionManager::onTcpConnect), op->id())); if (e) { delete op; return xcall (abind(callback, e, HttpConnectionPtr())); } addAsyncOp (op); }