Error XMPPStream::respondInit (ChannelPtr channel, const ResultCallback & callback) { Error e = init (channel, false); if (e) return e; e = startOp (XMO_RespondInitialize, callback); if (e) return e; mState = XMS_RespondInitializing; /// wait for opponent... return NoError; }
/* * Wrapper for poll(s, timeout). * Auto restarts with adjusted timeout if interrupted by * signal other than our wakeup signal. */ int NET_Timeout(int s, long timeout) { long prevtime = 0, newtime; struct timeval t; fdEntry_t *fdEntry = getFdEntry(s); /* * Check that fd hasn't been closed. */ if (fdEntry == NULL) { errno = EBADF; return -1; } /* * Pick up current time as may need to adjust timeout */ if (timeout > 0) { gettimeofday(&t, NULL); prevtime = t.tv_sec * 1000 + t.tv_usec / 1000; } for(;;) { struct pollfd pfd; int rv; threadEntry_t self; /* * Poll the fd. If interrupted by our wakeup signal * errno will be set to EBADF. */ pfd.fd = s; pfd.events = POLLIN | POLLERR; startOp(fdEntry, &self); rv = poll(&pfd, 1, timeout); endOp(fdEntry, &self); /* * If interrupted then adjust timeout. If timeout * has expired return 0 (indicating timeout expired). */ if (rv < 0 && errno == EINTR) { if (timeout > 0) { gettimeofday(&t, NULL); newtime = t.tv_sec * 1000 + t.tv_usec / 1000; timeout -= newtime - prevtime; if (timeout <= 0) { return 0; } prevtime = newtime; } } else { return rv; } } }
Error XMPPStream::waitFeatures (const ResultCallback & callback) { if (mReceivedFeatures) { // we have it already. if (callback) xcall (abind (callback, NoError)); return NoError; } Error e = startOp (XMO_WaitFeatures, callback); if (e) return e; return NoError; }
Error XMPPStream::startInit (ChannelPtr channel, const ResultCallback& callback) { mSkipInit = false; Error e = init (channel, false); if (e) return e; e = startOp (XMO_StartInitialize, callback); if (e) return e; mState = XMS_StartInitializing; sendStreamInit(); return NoError; }
Error XMPPStream::requestTls (const ResultCallback & callback) { if (!mReceivedFeatures) { Log (LogWarning) << LOGID << "Cannot request TLS, no stream features" << std::endl; return error::WrongState; } if (!mFeatures.hasChildWithName("starttls")){ Log (LogWarning) << LOGID << "Cannot request TLS, not supported by stream features" << std::endl; return error::NotSupported; } Error e = startOp (XMO_RequestTls, callback); if (e) return e; String tlsRequest ("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"); e = send (tlsRequest); if (e) return e; mNextChunkHandler = memFun (this, &XMPPStream::onStartTlsReply); return NoError; }
Error XMPPStream::authenticate (const String & username, const String & password, const ResultCallback & result) { if (!mReceivedFeatures){ Log (LogWarning) << LOGID << "Cannot authenticate, no stream features" << std::endl; return error::WrongState; } if (!hasMechanism ("PLAIN", mFeatures)){ Log (LogWarning) << LOGID << "Authentication mechanism PLAIN not supported!" << std::endl; return error::NotSupported; } Error e = startOp (XMO_Authenticate, result); if (e) return e; String secret = plainSecret (username, password); String auth = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" + secret + "</auth>"; e = send (auth); if (e) return e; mNextChunkHandler = memFun (this, &XMPPStream::onAuthReply); return NoError; }
/* * Wrapper for select(s, timeout). We are using select() on Mac OS due to Bug 7131399. * Auto restarts with adjusted timeout if interrupted by * signal other than our wakeup signal. */ int NET_Timeout(int s, long timeout) { long prevtime = 0, newtime; struct timeval t, *tp = &t; fd_set fds; fd_set* fdsp = NULL; int allocated = 0; threadEntry_t self; fdEntry_t *fdEntry = getFdEntry(s); /* * Check that fd hasn't been closed. */ if (fdEntry == NULL) { errno = EBADF; return -1; } /* * Pick up current time as may need to adjust timeout */ if (timeout > 0) { /* Timed */ struct timeval now; gettimeofday(&now, NULL); prevtime = now.tv_sec * 1000 + now.tv_usec / 1000; t.tv_sec = timeout / 1000; t.tv_usec = (timeout % 1000) * 1000; } else if (timeout < 0) { /* Blocking */ tp = 0; } else { /* Poll */ t.tv_sec = 0; t.tv_usec = 0; } if (s < FD_SETSIZE) { fdsp = &fds; FD_ZERO(fdsp); } else { int length = (howmany(s+1, NFDBITS)) * sizeof(int); fdsp = (fd_set *) calloc(1, length); if (fdsp == NULL) { return -1; // errno will be set to ENOMEM } allocated = 1; } FD_SET(s, fdsp); for(;;) { int rv; /* * call select on the fd. If interrupted by our wakeup signal * errno will be set to EBADF. */ startOp(fdEntry, &self); rv = select(s+1, fdsp, 0, 0, tp); endOp(fdEntry, &self); /* * If interrupted then adjust timeout. If timeout * has expired return 0 (indicating timeout expired). */ if (rv < 0 && errno == EINTR) { if (timeout > 0) { struct timeval now; gettimeofday(&now, NULL); newtime = now.tv_sec * 1000 + now.tv_usec / 1000; timeout -= newtime - prevtime; if (timeout <= 0) { if (allocated != 0) free(fdsp); return 0; } prevtime = newtime; t.tv_sec = timeout / 1000; t.tv_usec = (timeout % 1000) * 1000; } } else { if (allocated != 0) free(fdsp); return rv; } } }