@@ -187,18 +187,23 @@ bool ARTSPConnection::ParseURL(
187187 return true ;
188188}
189189
190- static void MakeSocketBlocking (int s, bool blocking) {
190+ static status_t MakeSocketBlocking (int s, bool blocking) {
191191 // Make socket non-blocking.
192192 int flags = fcntl (s, F_GETFL, 0 );
193- CHECK_NE (flags, -1 );
193+
194+ if (flags == -1 ) {
195+ return UNKNOWN_ERROR;
196+ }
194197
195198 if (blocking) {
196199 flags &= ~O_NONBLOCK;
197200 } else {
198201 flags |= O_NONBLOCK;
199202 }
200203
201- CHECK_NE (fcntl (s, F_SETFL, flags), -1 );
204+ flags = fcntl (s, F_SETFL, flags);
205+
206+ return flags == -1 ? UNKNOWN_ERROR : OK;
202207}
203208
204209void ARTSPConnection::onConnect (const sp<AMessage> &msg) {
@@ -302,27 +307,32 @@ void ARTSPConnection::onConnect(const sp<AMessage> &msg) {
302307 reply->post ();
303308}
304309
310+ void ARTSPConnection::performDisconnect () {
311+ if (mUIDValid ) {
312+ HTTPBase::UnRegisterSocketUserTag (mSocket );
313+ }
314+ close (mSocket );
315+ mSocket = -1 ;
316+
317+ flushPendingRequests ();
318+
319+ mUser .clear ();
320+ mPass .clear ();
321+ mAuthType = NONE;
322+ mNonce .clear ();
323+
324+ mState = DISCONNECTED;
325+ }
326+
305327void ARTSPConnection::onDisconnect (const sp<AMessage> &msg) {
306328 if (mState == CONNECTED || mState == CONNECTING) {
307- if (mUIDValid ) {
308- HTTPBase::UnRegisterSocketUserTag (mSocket );
309- }
310- close (mSocket );
311- mSocket = -1 ;
312-
313- flushPendingRequests ();
329+ performDisconnect ();
314330 }
315331
316332 sp<AMessage> reply;
317333 CHECK (msg->findMessage (" reply" , &reply));
318334
319335 reply->setInt32 (" result" , OK);
320- mState = DISCONNECTED;
321-
322- mUser .clear ();
323- mPass .clear ();
324- mAuthType = NONE;
325- mNonce .clear ();
326336
327337 reply->post ();
328338}
@@ -427,21 +437,25 @@ void ARTSPConnection::onSendRequest(const sp<AMessage> &msg) {
427437 send (mSocket , request.c_str () + numBytesSent,
428438 request.size () - numBytesSent, 0 );
429439
430- if (n == 0 ) {
431- // Server closed the connection.
432- LOGE ( " Server unexpectedly closed the connection. " );
440+ if (n < 0 && errno == EINTR ) {
441+ continue ;
442+ }
433443
434- reply->setInt32 (" result" , ERROR_IO);
435- reply->post ();
436- return ;
437- } else if (n < 0 ) {
438- if (errno == EINTR) {
439- continue ;
444+ if (n <= 0 ) {
445+ performDisconnect ();
446+
447+ if (n == 0 ) {
448+ // Server closed the connection.
449+ LOGE (" Server unexpectedly closed the connection." );
450+
451+ reply->setInt32 (" result" , ERROR_IO);
452+ reply->post ();
453+ } else {
454+ LOGE (" Error sending rtsp request. (%s)" , strerror (errno));
455+ reply->setInt32 (" result" , -errno);
456+ reply->post ();
440457 }
441458
442- LOGE (" Error sending rtsp request." );
443- reply->setInt32 (" result" , -errno);
444- reply->post ();
445459 return ;
446460 }
447461
@@ -512,17 +526,22 @@ status_t ARTSPConnection::receive(void *data, size_t size) {
512526 size_t offset = 0 ;
513527 while (offset < size) {
514528 ssize_t n = recv (mSocket , (uint8_t *)data + offset, size - offset, 0 );
515- if (n == 0 ) {
516- // Server closed the connection.
517- LOGE (" Server unexpectedly closed the connection." );
518- return ERROR_IO;
519- } else if (n < 0 ) {
520- if (errno == EINTR) {
521- continue ;
522- }
523529
524- LOGE (" Error reading rtsp response." );
525- return -errno;
530+ if (n < 0 && errno == EINTR) {
531+ continue ;
532+ }
533+
534+ if (n <= 0 ) {
535+ performDisconnect ();
536+
537+ if (n == 0 ) {
538+ // Server closed the connection.
539+ LOGE (" Server unexpectedly closed the connection." );
540+ return ERROR_IO;
541+ } else {
542+ LOGE (" Error reading rtsp response. (%s)" , strerror (errno));
543+ return -errno;
544+ }
526545 }
527546
528547 offset += (size_t )n;
@@ -681,24 +700,8 @@ bool ARTSPConnection::receiveRTSPReponse() {
681700 if (contentLength > 0 ) {
682701 response->mContent = new ABuffer (contentLength);
683702
684- size_t numBytesRead = 0 ;
685- while (numBytesRead < contentLength) {
686- ssize_t n = recv (
687- mSocket , response->mContent ->data () + numBytesRead,
688- contentLength - numBytesRead, 0 );
689-
690- if (n == 0 ) {
691- // Server closed the connection.
692- TRESPASS ();
693- } else if (n < 0 ) {
694- if (errno == EINTR) {
695- continue ;
696- }
697-
698- TRESPASS ();
699- }
700-
701- numBytesRead += (size_t )n;
703+ if (receive (response->mContent ->data (), contentLength) != OK) {
704+ return false ;
702705 }
703706 }
704707
@@ -765,17 +768,20 @@ bool ARTSPConnection::handleServerRequest(const sp<ARTSPResponse> &request) {
765768 send (mSocket , response.c_str () + numBytesSent,
766769 response.size () - numBytesSent, 0 );
767770
768- if (n == 0 ) {
769- // Server closed the connection.
770- LOGE ( " Server unexpectedly closed the connection. " );
771+ if (n < 0 && errno == EINTR ) {
772+ continue ;
773+ }
771774
772- return false ;
773- } else if (n < 0 ) {
774- if (errno == EINTR) {
775- continue ;
775+ if (n <= 0 ) {
776+ if (n == 0 ) {
777+ // Server closed the connection.
778+ LOGE (" Server unexpectedly closed the connection." );
779+ } else {
780+ LOGE (" Error sending rtsp response (%s)." , strerror (errno));
776781 }
777782
778- LOGE (" Error sending rtsp response." );
783+ performDisconnect ();
784+
779785 return false ;
780786 }
781787
0 commit comments