Skip to content

Commit

Permalink
For #1657, refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Nov 5, 2020
1 parent c3d2900 commit 2fedb68
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
59 changes: 36 additions & 23 deletions trunk/src/app/srs_app_http_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,6 @@ srs_error_t SrsHttpConn::do_cycle()
// set the recv timeout, for some clients never disconnect the connection.
// @see https://github.com/ossrs/srs/issues/398
skt->set_recv_timeout(SRS_HTTP_RECV_TIMEOUT);

SrsRequest* last_req = NULL;
SrsAutoFree(SrsRequest, last_req);

// initialize parser
if ((err = parser->initialize(HTTP_REQUEST)) != srs_success) {
Expand All @@ -177,57 +174,73 @@ srs_error_t SrsHttpConn::do_cycle()
if ((err = handler_->on_start()) != srs_success) {
return srs_error_wrap(err, "start");
}

SrsRequest* last_req = NULL;
SrsAutoFree(SrsRequest, last_req);

// process all http messages.
err = process_requests(&last_req);

// process http messages.
for (int req_id = 0; (err = trd->pull()) == srs_success; req_id++) {
srs_error_t r0 = srs_success;
if ((r0 = on_disconnect(last_req)) != srs_success) {
err = srs_error_wrap(err, "on disconnect %s", srs_error_desc(r0).c_str());
srs_freep(r0);
}

return err;
}

srs_error_t SrsHttpConn::process_requests(SrsRequest** preq)
{
srs_error_t err = srs_success;

for (int req_id = 0; ; req_id++) {
if ((err = trd->pull()) != srs_success) {
return srs_error_wrap(err, "pull");
}

// get a http message
ISrsHttpMessage* req = NULL;
if ((err = parser->parse_message(skt, &req)) != srs_success) {
break;
return srs_error_wrap(err, "parse message");
}

// if SUCCESS, always NOT-NULL.
// always free it in this scope.
srs_assert(req);
SrsAutoFree(ISrsHttpMessage, req);

// Attach owner connection to message.
SrsHttpMessage* hreq = (SrsHttpMessage*)req;
hreq->set_connection(this);

// copy request to last request object.
srs_freep(last_req);
last_req = hreq->to_request(hreq->host());
srs_freep(*preq);
*preq = hreq->to_request(hreq->host());

// may should discard the body.
SrsHttpResponseWriter writer(skt);
if ((err = handler_->on_http_message(req, &writer)) != srs_success) {
break;
return srs_error_wrap(err, "on http message");
}

// ok, handle http request.
if ((err = process_request(&writer, req)) != srs_success) {
break;
return srs_error_wrap(err, "process request");
}

// After the request is processed.
if ((err = handler_->on_message_done(req, &writer)) != srs_success) {
break;
return srs_error_wrap(err, "on message done");
}

// donot keep alive, disconnect it.
// @see https://github.com/ossrs/srs/issues/399
if (!req->is_keep_alive()) {
break;
}
}

srs_error_t r0 = srs_success;
if ((r0 = on_disconnect(last_req)) != srs_success) {
err = srs_error_wrap(err, "on disconnect %s", srs_error_desc(r0).c_str());
srs_freep(r0);
}


return err;
}

Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_http_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class SrsHttpConn : virtual public ISrsStartableConneciton, virtual public ISrsC
virtual srs_error_t cycle();
private:
virtual srs_error_t do_cycle();
virtual srs_error_t process_requests(SrsRequest** preq);
private:
virtual srs_error_t process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
// When the connection disconnect, call this method.
Expand Down

0 comments on commit 2fedb68

Please sign in to comment.