status_t ARTSPConnection::receive(void *data, size_t size) { size_t offset = 0; while (offset < size) { ssize_t n = recv(mSocket, (uint8_t *)data + offset, size - offset, 0); if (n < 0 && errno == EINTR) { continue; } if (n <= 0) { performDisconnect(); if (n == 0) { // Server closed the connection. ALOGE("Server unexpectedly closed the connection."); return ERROR_IO; } else { ALOGE("Error reading rtsp response. (%s)", strerror(errno)); return -errno; } } offset += (size_t)n; } return OK; }
status_t ARTSPConnection::receive(void *data, size_t size) { size_t offset = 0; while (offset < size) { ssize_t n = recv(mSocket, (uint8_t *)data + offset, size - offset, 0); if (n < 0 && errno == EINTR) { continue; } if (n <= 0) { #ifndef ANDROID_DEFAULT_CODE ALOGW("receive %d errno %d trying %d, force %d", n, errno, mKeepTCPTrying, mForceQuitTCP); if (errno == EAGAIN && mKeepTCPTrying && !mForceQuitTCP) { continue; } #endif // #ifndef ANDROID_DEFAULT_CODE performDisconnect(); if (n == 0) { // Server closed the connection. ALOGE("Server unexpectedly closed the connection."); return ERROR_IO; } else { ALOGE("Error reading rtsp response. (%s)", strerror(errno)); return -errno; } } offset += (size_t)n; } return OK; }
bool ARTSPConnection::handleServerRequest(const sp<ARTSPResponse> &request) { // Implementation of server->client requests is optional for all methods // but we do need to respond, even if it's just to say that we don't // support the method. ssize_t space1 = request->mStatusLine.find(" "); CHECK_GE(space1, 0); AString response; response.append("RTSP/1.0 501 Not Implemented\r\n"); ssize_t i = request->mHeaders.indexOfKey("cseq"); if (i >= 0) { AString value = request->mHeaders.valueAt(i); unsigned long cseq; if (!ParseSingleUnsignedLong(value.c_str(), &cseq)) { return false; } response.append("CSeq: "); response.append(cseq); response.append("\r\n"); } response.append("\r\n"); size_t numBytesSent = 0; while (numBytesSent < response.size()) { ssize_t n = send(mSocket, response.c_str() + numBytesSent, response.size() - numBytesSent, 0); if (n < 0 && errno == EINTR) { continue; } if (n <= 0) { if (n == 0) { // Server closed the connection. ALOGE("Server unexpectedly closed the connection."); } else { ALOGE("Error sending rtsp response (%s).", strerror(errno)); } performDisconnect(); return false; } numBytesSent += (size_t)n; } return true; }
void ARTSPConnection::onDisconnect(const sp<AMessage> &msg) { if (mState == CONNECTED || mState == CONNECTING) { performDisconnect(); } sp<AMessage> reply; CHECK(msg->findMessage("reply", &reply)); reply->setInt32("result", OK); reply->post(); }
/* Performs command according to the user input line. If the user command is EXIT then it returns FINISH. Otherwise, it returns UNFINISH. */ int twitClient::performCommand(const std::vector<std::string>& tokens) { std::string command = upperCopy(tokens.at(0)); if (command == TWIT) { performTwit(tokens); } else if (command == DM) { performDirectMessage(tokens); } else if (tokens.size() == 1) { if (command == EXIT) { performDisconnect(tokens); return FINISH; } else if (command == WHO) { performWho(tokens); } else { std::cout << USER_ILLEGAL_COMMAND << std::endl; } } else if (tokens.size() == 2) { if (command == FOLLOW) { performFollow(tokens); } else if (command == UNFOLLOW) { performUnfollow(tokens); } else if (command == BLOCK) { performBlock(tokens); } else { std::cout << USER_ILLEGAL_COMMAND << std::endl; } } else { std::cout << USER_ILLEGAL_COMMAND << std::endl; } return UNFINISH; }
void ARTSPConnection::onSendRequest(const sp<AMessage> &msg) { sp<AMessage> reply; CHECK(msg->findMessage("reply", &reply)); if (mState != CONNECTED) { reply->setInt32("result", -ENOTCONN); reply->post(); return; } AString request; CHECK(msg->findString("request", &request)); // Just in case we need to re-issue the request with proper authentication // later, stash it away. reply->setString("original-request", request.c_str(), request.size()); addAuthentication(&request); addUserAgent(&request); // Find the boundary between headers and the body. ssize_t i = request.find("\r\n\r\n"); CHECK_GE(i, 0); int32_t cseq = mNextCSeq++; AString cseqHeader = "CSeq: "; cseqHeader.append(cseq); cseqHeader.append("\r\n"); request.insert(cseqHeader, i + 2); ALOGV("request: '%s'", request.c_str()); size_t numBytesSent = 0; while (numBytesSent < request.size()) { ssize_t n = send(mSocket, request.c_str() + numBytesSent, request.size() - numBytesSent, 0); if (n < 0 && errno == EINTR) { continue; } if (n <= 0) { performDisconnect(); if (n == 0) { // Server closed the connection. ALOGE("Server unexpectedly closed the connection."); reply->setInt32("result", ERROR_IO); reply->post(); } else { ALOGE("Error sending rtsp request. (%s)", strerror(errno)); reply->setInt32("result", -errno); reply->post(); } return; } numBytesSent += (size_t)n; } mPendingRequests.add(cseq, reply); }
void ARTSPConnection::onSendRequest(const sp<AMessage> &msg) { #ifndef ANDROID_DEFAULT_CODE int32_t backup; if (msg->findInt32("backup-keep-tcp", &backup)) { mForceQuitTCP = backup; } #endif sp<AMessage> reply; CHECK(msg->findMessage("reply", &reply)); if (mState != CONNECTED) { reply->setInt32("result", -ENOTCONN); reply->post(); return; } AString request; CHECK(msg->findString("request", &request)); // Just in case we need to re-issue the request with proper authentication // later, stash it away. reply->setString("original-request", request.c_str(), request.size()); #ifndef ANDROID_DEFAULT_CODE // mtk80902: ALPS01139972 - SETUP resp with a wrong Session "xxx\0\0\0.." // so the followed \r\n is ignored by find's strstr operation if (request.find("\r\n\r\n") < 0) { ALOGW("what the hell with this req?"); // seems print str is useless.. reply->setInt32("result", -EBADMSG); reply->post(); return; } #endif addAuthentication(&request); addUserAgent(&request); // Find the boundary between headers and the body. ssize_t i = request.find("\r\n\r\n"); CHECK_GE(i, 0); int32_t cseq = mNextCSeq++; AString cseqHeader = "CSeq: "; cseqHeader.append(cseq); cseqHeader.append("\r\n"); request.insert(cseqHeader, i + 2); #ifndef ANDROID_DEFAULT_CODE ALOGI("request: '%s'", request.c_str()); #else ALOGV("request: '%s'", request.c_str()); #endif size_t numBytesSent = 0; while (numBytesSent < request.size()) { ssize_t n = #ifndef ANDROID_DEFAULT_CODE send(mSocket, request.c_str() + numBytesSent, request.size() - numBytesSent, MTK_SEND_FLAG); #else send(mSocket, request.c_str() + numBytesSent, request.size() - numBytesSent, 0); #endif if (n < 0 && errno == EINTR) { continue; } if (n <= 0) { performDisconnect(); if (n == 0) { // Server closed the connection. ALOGE("Server unexpectedly closed the connection."); reply->setInt32("result", ERROR_IO); reply->post(); } else { ALOGE("Error sending rtsp request. (%s)", strerror(errno)); reply->setInt32("result", -errno); reply->post(); } return; } numBytesSent += (size_t)n; } mPendingRequests.add(cseq, reply); #ifndef ANDROID_DEFAULT_CODE sp<AMessage> timeout = new AMessage(kWhatTimeout, id()); timeout->setInt32("cseq", cseq); int64_t t; if (reply->findInt64("timeout", &t)) { timeout->post(t); } else { timeout->post(kRequestTimeout); } #endif // #ifndef ANDROID_DEFAULT_CODE }