Skip to content

Commit

Permalink
feat :
Browse files Browse the repository at this point in the history
1. change api_listener_, apis_listener_, http_listener_, https_listener_ to SrsMultipleTcpListeners
2. for reusing ‘httpmux logic’.  enable listener only when ‘api_listener’ and  ‘http_listener’ have zero intersection, or ‘http_listener’ contains whole ‘api_listener’. same for ‘apis_listener_’ and ‘https_listeners_’. otherwise occurs error.
  • Loading branch information
sangbumlikeagod committed Dec 22, 2024
1 parent e9b4a3f commit 1e848d9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 66 deletions.
42 changes: 16 additions & 26 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2512,8 +2512,6 @@ srs_error_t SrsConfig::check_normal_config()
// Check HTTP API and server.
////////////////////////////////////////////////////////////////////////
if (true) {
string api = get_http_api_listen();
string server = get_http_stream_listen();
vector<string> api_vec = get_http_apis_listens();
vector<string> server_vec = get_http_streams_listens();
if (api_vec.empty()) {
Expand All @@ -2526,50 +2524,42 @@ srs_error_t SrsConfig::check_normal_config()
std::sort(api_vec.begin(), api_vec.end());
std::sort(server_vec.begin(), server_vec.end());

// 교집합 결과를 저장할 벡터
std::vector<string> intersection_result;
std::vector<string> intersections_result;

// set_intersection 사용
std::set_intersection(
api_vec.begin(), api_vec.end(),
server_vec.begin(), server_vec.end(),
std::back_inserter(intersection_result)
);

if (intersection_result.size() != 0 && intersection_result != api_vec.size())
bool isNotSameHttp = intersection_result.size() == 0;
bool isFullyContainedHttp = intersection_result.size() != 0 && intersection_result.size() == api_vec.size();
if (!isNotSameHttp && !isFullyContainedHttp)
{
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "http api and server have a intersection, but http server did not include http api");
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "http api and server have a intersection, but http server doesn't fully contain http api");
}


string apis = get_https_api_listen();
string servers = get_https_stream_listen();
vector<string> apis_vec = get_https_apis_listens();
vector<string> servers_vec = get_https_streams_listens();

std::sort(apis_vec.begin(), apis_vec.end());
std::sort(servers_vec.begin(), servers_vec.end());

// 교집합 결과를 저장할 벡터

// set_intersection 사용
std::set_intersection(
apis_vec.begin(), apis_vec.end(),
servers_vec.begin(), servers_vec.end(),
std::back_inserter(intersections_result)
);

if (intersections_result.size() != 0 && intersections_result != api_vec.size())
bool isNotSameHttps = intersections_result.size() == 0;
bool isFullyContainedHttps = intersections_result.size() != 0 && intersections_result.size() == apis_vec.size();
if (!isNotSameHttps && !isFullyContainedHttps)
{
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "https api and server have a intersection, but https server did not include http api");
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "https api and server have a intersection, but https server doesn't fully contain https api");
}

if (api == server && apis != servers) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "for same http, https api(%s) != server(%s)", apis.c_str(), servers.c_str());
if (!isNotSameHttp && isNotSameHttps) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "for same http, https api != server");
}
if (apis == servers && api != server) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "for same https, http api(%s) != server(%s)", api.c_str(), server.c_str());
if (!isNotSameHttps && isNotSameHttp) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "for same https, http api != server");
}

if (get_https_api_enabled() && !get_http_api_enabled()) {
Expand Down Expand Up @@ -7750,7 +7740,7 @@ std::vector<std::string> SrsConfig::get_http_apis_listens()
ports.push_back(DEFAULT);
return ports;
}

conf = conf->get("listen");
for (int i = 0; i < (int)conf->args.size(); i++) {
ports.push_back(conf->args.at(i));
}
Expand Down Expand Up @@ -7978,7 +7968,7 @@ std::vector<std::string> SrsConfig::get_https_apis_listens()
ports.push_back(DEFAULT);
return ports;
}

conf = conf->get("listen");
for (int i = 0; i < (int)conf->args.size(); i++) {
ports.push_back(conf->args.at(i));
}
Expand Down Expand Up @@ -8414,7 +8404,7 @@ std::vector<std::string> SrsConfig::get_http_streams_listens()
ports.push_back(DEFAULT);
return ports;
}

conf = conf->get("listen");
for (int i = 0; i < (int)conf->args.size(); i++) {
ports.push_back(conf->args.at(i));
}
Expand Down Expand Up @@ -8539,7 +8529,7 @@ std::vector<std::string> SrsConfig::get_https_streams_listens()
ports.push_back(DEFAULT);
return ports;
}

conf = conf->get("listen");
for (int i = 0; i < (int)conf->args.size(); i++) {
ports.push_back(conf->args.at(i));
}
Expand Down
86 changes: 50 additions & 36 deletions trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ SrsServer::SrsServer()
ppid = ::getppid();

rtmp_listener_ = new SrsMultipleTcpListeners(this);
api_listener_ = new SrsTcpListener(this);
apis_listener_ = new SrsTcpListener(this);
http_listener_ = new SrsTcpListener(this);
https_listener_ = new SrsTcpListener(this);
api_listener_ = new SrsMultipleTcpListeners(this);
apis_listener_ = new SrsMultipleTcpListeners(this);
http_listener_ = new SrsMultipleTcpListeners(this);
https_listener_ = new SrsMultipleTcpListeners(this);
webrtc_listener_ = new SrsTcpListener(this);
stream_caster_flv_listener_ = new SrsHttpFlvListener();
stream_caster_mpegts_ = new SrsUdpCasterListener();
Expand Down Expand Up @@ -491,10 +491,8 @@ srs_error_t SrsServer::initialize()
_srs_config->subscribe(this);

bool stream = _srs_config->get_http_stream_enabled();
string http_listen = _srs_config->get_http_stream_listen();
string https_listen = _srs_config->get_https_stream_listen();
vector<string> server_vec = get_http_streams_listens();
vector<string> servers_vec = get_https_streams_listens();
vector<string> server_vec = _srs_config->get_http_streams_listens();
vector<string> servers_vec = _srs_config->get_https_streams_listens();
std::sort(server_vec.begin(), server_vec.end());
std::sort(servers_vec.begin(), servers_vec.end());

Expand All @@ -506,22 +504,26 @@ srs_error_t SrsServer::initialize()
bool rtc_tcp = _srs_config->get_rtc_server_tcp_enabled();
string rtc_listen = srs_int2str(_srs_config->get_rtc_server_tcp_listen());
// If enabled and listen is the same value, resue port for WebRTC over TCP.
if (stream && rtc && rtc_tcp && http_listen == rtc_listen) {
srs_trace("WebRTC tcp=%s reuses http=%s server", rtc_listen.c_str(), http_listen.c_str());
reuse_rtc_over_server_ = true;
for (string http_listen : server_vec)
{
if (stream && rtc && rtc_tcp && http_listen == rtc_listen) {
srs_trace("WebRTC tcp=%s reuses http=%s server", rtc_listen.c_str(), http_listen.c_str());
reuse_rtc_over_server_ = true;
}
}
if (stream && rtc && rtc_tcp && https_listen == rtc_listen) {
srs_trace("WebRTC tcp=%s reuses https=%s server", rtc_listen.c_str(), https_listen.c_str());
reuse_rtc_over_server_ = true;
for (string https_listen : servers_vec)
{
if (stream && rtc && rtc_tcp && https_listen == rtc_listen) {
srs_trace("WebRTC tcp=%s reuses https=%s server", rtc_listen.c_str(), https_listen.c_str());
reuse_rtc_over_server_ = true;
}
}
#endif

// If enabled and the listen is the same value, reuse port.
bool api = _srs_config->get_http_api_enabled();
string api_listen = _srs_config->get_http_api_listen();
string apis_listen = _srs_config->get_https_api_listen();
vector<string> api_vec = get_http_apis_listens();
vector<string> apis_vec = get_https_apis_listens();
vector<string> api_vec = _srs_config->get_http_apis_listens();
vector<string> apis_vec = _srs_config->get_https_apis_listens();

std::sort(api_vec.begin(), api_vec.end());
std::sort(apis_vec.begin(), apis_vec.end());
Expand All @@ -534,25 +536,37 @@ srs_error_t SrsServer::initialize()
server_vec.begin(), server_vec.end(),
std::back_inserter(intersection_result)
);
std::set_intersection(
apis_vec.begin(), apis_vec.end(),
servers_vec.begin(), servers_vec.end(),
std::back_inserter(intersections_result)
);
bool isNotSameHttp = intersection_result.size() == 0;
bool isFullyContainedHttp = intersection_result.size() != 0 && intersection_result.size() == api_vec.size();
bool isNotSameHttps = intersections_result.size() == 0;
bool isFullyContainedHttps = intersections_result.size() != 0 && intersections_result.size() == apis_vec.size();

if (intersection_result.size() == 0)
if ((!isNotSameHttp && !isFullyContainedHttp) || (!isNotSameHttps && !isFullyContainedHttps))
{
reuse_api_over_server_ = false;
return srs_error_wrap(err, "http api and http server have a intersection. but http server doesn't fully contain http api");
}
else if (intersection_result.size() == api_vec.size())

if (stream && api && isFullyContainedHttp && isFullyContainedHttps)
{
srs_trace("API reuses http and https server");
for (string http_listen : intersection_result)
{
srs_trace("API reuses http=%s server", http_listen.c_str());
}
for (string https_listen : intersections_result)
{
srs_trace("API reuses https=%s server", https_listen.c_str());
}
reuse_api_over_server_ = true;
}
else
else if (isNotSameHttp && isNotSameHttps)
{
return srs_error_wrap(err, "http api initialize");
}



if (stream && api && api_listen == http_listen && apis_listen == https_listen) {
srs_trace("API reuses http=%s and https=%s server", http_listen.c_str(), https_listen.c_str());
reuse_api_over_server_ = true;
reuse_api_over_server_ = false;
}

// Only init HTTP API when not reusing HTTP server.
Expand Down Expand Up @@ -622,8 +636,8 @@ srs_error_t SrsServer::listen()
if (reuse_api_over_server_) {
srs_trace("HTTP-API: Reuse listen to http server %s", _srs_config->get_http_stream_listen().c_str());
} else {
api_listener_->set_endpoint(_srs_config->get_http_api_listen())->set_label("HTTP-API");
if ((err = api_listener_->listen()) != srs_success) {
api_listener_->add(_srs_config->get_http_apis_listens())->set_label("HTTP_API");
if ((err = api_listener_->listen()) != srs_success) {
return srs_error_wrap(err, "http api listen");
}
}
Expand All @@ -634,24 +648,24 @@ srs_error_t SrsServer::listen()
if (reuse_api_over_server_) {
srs_trace("HTTPS-API: Reuse listen to http server %s", _srs_config->get_https_stream_listen().c_str());
} else {
apis_listener_->set_endpoint(_srs_config->get_https_api_listen())->set_label("HTTPS-API");
if ((err = apis_listener_->listen()) != srs_success) {
apis_listener_->add(_srs_config->get_https_apis_listens())->set_label("HTTPS_API");
if ((err = apis_listener_->listen()) != srs_success) {
return srs_error_wrap(err, "https api listen");
}
}
}

// Create HTTP server listener.
if (_srs_config->get_http_stream_enabled()) {
http_listener_->set_endpoint(_srs_config->get_http_stream_listen())->set_label("HTTP-Server");
http_listener_->add(_srs_config->get_http_streams_listens())->set_label("HTTP-Server");
if ((err = http_listener_->listen()) != srs_success) {
return srs_error_wrap(err, "http server listen");
}
}

// Create HTTPS server listener.
if (_srs_config->get_https_stream_enabled()) {
https_listener_->set_endpoint(_srs_config->get_https_stream_listen())->set_label("HTTPS-Server");
https_listener_->add(_srs_config->get_https_streams_listens())->set_label("HTTPS-Server");
if ((err = https_listener_->listen()) != srs_success) {
return srs_error_wrap(err, "https server listen");
}
Expand Down
8 changes: 4 additions & 4 deletions trunk/src/app/srs_app_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ class SrsServer : public ISrsReloadHandler, public ISrsLiveSourceHandler, public
// RTMP stream listeners, over TCP.
SrsMultipleTcpListeners* rtmp_listener_;
// HTTP API listener, over TCP. Please note that it might reuse with stream listener.
SrsTcpListener* api_listener_;
SrsMultipleTcpListeners* api_listener_;
// HTTPS API listener, over TCP. Please note that it might reuse with stream listener.
SrsTcpListener* apis_listener_;
SrsMultipleTcpListeners* apis_listener_;
// HTTP server listener, over TCP. Please note that request of both HTTP static and stream are served by this
// listener, and it might be reused by HTTP API and WebRTC TCP.
SrsTcpListener* http_listener_;
SrsMultipleTcpListeners* http_listener_;
// HTTPS server listener, over TCP. Please note that request of both HTTP static and stream are served by this
// listener, and it might be reused by HTTP API and WebRTC TCP.
SrsTcpListener* https_listener_;
SrsMultipleTcpListeners* https_listener_;
// WebRTC over TCP listener. Please note that there is always a UDP listener by RTC server.
SrsTcpListener* webrtc_listener_;
// Stream Caster for push over HTTP-FLV.
Expand Down

0 comments on commit 1e848d9

Please sign in to comment.