Error BoshXMPPConnection::startConnect (bool withLogin, const XMPPStreamPtr & stream, int timeOutMs, const ResultCallback & callback) { if (mConnecting){ Log (LogError) << LOGID << "There is already a connection process" << std::endl; return error::ExistsAlready; } ConnectingOp * op = new ConnectingOp (sf::regTimeOutMs (timeOutMs)); op->setId (genFreeId()); op->resultCallback = callback; op->withLogin = withLogin; op->transport = BoshTransportPtr (new BoshTransport()); op->stream = stream; op->boss = this; if (mDetails.port == 0) mDetails.port = 443; // Starting connect Url url = Url (String ("https://") + mDetails.server + ":" + toString (mDetails.port) + "/http-bind/"); BoshTransport::StringMap addArgs; addArgs["to"] = mDetails.server; addArgs["xmpp:version"] = "1.0"; addArgs["xmlns:xmpp"] = "urn:xmpp:xbosh"; // is important for ejabberd op->transport->connect(url, addArgs, op->lastingTimeMs(), abind (dMemFun (this, &BoshXMPPConnection::onBoshConnect), op->id())); op->setState (ConnectingOp::WAIT_BOSH_CONNECT); setState (IMClient::CS_CONNECTING); addAsyncOp (op); return NoError; }
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 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); }