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 0a3a387 commit b492d59
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 83 deletions.
140 changes: 70 additions & 70 deletions trunk/src/app/srs_app_http_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,55 @@ void SrsHttpConn::remark(int64_t* in, int64_t* out)
kbps->remark(in, out);
}

srs_error_t SrsHttpConn::start()
{
srs_error_t err = srs_success;

if ((err = skt->initialize()) != srs_success) {
return srs_error_wrap(err, "init socket");
}

if ((err = trd->start()) != srs_success) {
return srs_error_wrap(err, "coroutine");
}

return err;
}

srs_error_t SrsHttpConn::cycle()
{
srs_error_t err = do_cycle();

// Notify handler to handle it.
handler_->on_conn_done();

// success.
if (err == srs_success) {
srs_trace("client finished.");
return err;
}

// It maybe success with message.
if (srs_error_code(err) == ERROR_SUCCESS) {
srs_trace("client finished%s.", srs_error_summary(err).c_str());
srs_freep(err);
return err;
}

// client close peer.
// TODO: FIXME: Only reset the error when client closed it.
if (srs_is_client_gracefully_close(err)) {
srs_warn("client disconnect peer. ret=%d", srs_error_code(err));
} else if (srs_is_server_gracefully_close(err)) {
srs_warn("server disconnect. ret=%d", srs_error_code(err));
} else {
srs_error("serve error %s", srs_error_desc(err).c_str());
}

srs_freep(err);
return srs_success;
}

srs_error_t SrsHttpConn::do_cycle()
{
srs_error_t err = srs_success;
Expand Down Expand Up @@ -169,6 +218,27 @@ srs_error_t SrsHttpConn::do_cycle()
return err;
}

srs_error_t SrsHttpConn::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
srs_error_t err = srs_success;

srs_trace("HTTP %s:%d %s %s, content-length=%" PRId64 "",
ip.c_str(), port, r->method_str().c_str(), r->url().c_str(), r->content_length());

// use cors server mux to serve http request, which will proxy to http_remux.
if ((err = cors->serve_http(w, r)) != srs_success) {
return srs_error_wrap(err, "mux serve");
}

return err;
}

srs_error_t SrsHttpConn::on_disconnect(SrsRequest* req)
{
// TODO: FIXME: Implements it.
return srs_success;
}

ISrsHttpConnOwner* SrsHttpConn::handler()
{
return handler_;
Expand Down Expand Up @@ -203,27 +273,6 @@ srs_error_t SrsHttpConn::set_jsonp(bool v)
return err;
}

srs_error_t SrsHttpConn::process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
srs_error_t err = srs_success;

srs_trace("HTTP %s:%d %s %s, content-length=%" PRId64 "",
ip.c_str(), port, r->method_str().c_str(), r->url().c_str(), r->content_length());

// use cors server mux to serve http request, which will proxy to http_remux.
if ((err = cors->serve_http(w, r)) != srs_success) {
return srs_error_wrap(err, "mux serve");
}

return err;
}

srs_error_t SrsHttpConn::on_disconnect(SrsRequest* req)
{
// TODO: FIXME: Implements it.
return srs_success;
}

srs_error_t SrsHttpConn::set_tcp_nodelay(bool v)
{
return skt->set_tcp_nodelay(v);
Expand All @@ -234,55 +283,6 @@ srs_error_t SrsHttpConn::set_socket_buffer(srs_utime_t buffer_v)
return skt->set_socket_buffer(buffer_v);
}

srs_error_t SrsHttpConn::start()
{
srs_error_t err = srs_success;

if ((err = skt->initialize()) != srs_success) {
return srs_error_wrap(err, "init socket");
}

if ((err = trd->start()) != srs_success) {
return srs_error_wrap(err, "coroutine");
}

return err;
}

srs_error_t SrsHttpConn::cycle()
{
srs_error_t err = do_cycle();

// Notify handler to handle it.
handler_->on_conn_done();

// success.
if (err == srs_success) {
srs_trace("client finished.");
return err;
}

// It maybe success with message.
if (srs_error_code(err) == ERROR_SUCCESS) {
srs_trace("client finished%s.", srs_error_summary(err).c_str());
srs_freep(err);
return err;
}

// client close peer.
// TODO: FIXME: Only reset the error when client closed it.
if (srs_is_client_gracefully_close(err)) {
srs_warn("client disconnect peer. ret=%d", srs_error_code(err));
} else if (srs_is_server_gracefully_close(err)) {
srs_warn("server disconnect. ret=%d", srs_error_code(err));
} else {
srs_error("serve error %s", srs_error_desc(err).c_str());
}

srs_freep(err);
return srs_success;
}

string SrsHttpConn::remote_ip()
{
return ip;
Expand Down
25 changes: 12 additions & 13 deletions trunk/src/app/srs_app_http_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,20 @@ class SrsHttpConn : virtual public ISrsStartableConneciton, virtual public ISrsC
// Interface ISrsKbpsDelta
public:
virtual void remark(int64_t* in, int64_t* out);
// Interface ISrsStartable
public:
virtual srs_error_t start();
// Interface ISrsOneCycleThreadHandler
public:
virtual srs_error_t cycle();
private:
virtual srs_error_t do_cycle();
private:
virtual srs_error_t process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
// When the connection disconnect, call this method.
// e.g. log msg of connection and report to other system.
// @param request: request which is converted by the last http message.
virtual srs_error_t on_disconnect(SrsRequest* req);
public:
// Get the HTTP message handler.
virtual ISrsHttpConnOwner* handler();
Expand All @@ -116,24 +128,11 @@ class SrsHttpConn : virtual public ISrsStartableConneciton, virtual public ISrsC
virtual srs_error_t set_crossdomain_enabled(bool v);
// Whether enable the JSONP.
virtual srs_error_t set_jsonp(bool v);
private:
virtual srs_error_t process_request(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
// When the connection disconnect, call this method.
// e.g. log msg of connection and report to other system.
// @param request: request which is converted by the last http message.
virtual srs_error_t on_disconnect(SrsRequest* req);
// Extract APIs from SrsTcpConnection.
public:
// Set socket option TCP_NODELAY.
virtual srs_error_t set_tcp_nodelay(bool v);
// Set socket option SO_SNDBUF in srs_utime_t.
virtual srs_error_t set_socket_buffer(srs_utime_t buffer_v);
// Interface ISrsStartable
public:
virtual srs_error_t start();
// Interface ISrsOneCycleThreadHandler
public:
virtual srs_error_t cycle();
// Interface ISrsConnection.
public:
virtual std::string remote_ip();
Expand Down

0 comments on commit b492d59

Please sign in to comment.