From d8f18aee3766f60239868dead863b00fce3fc1c7 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 24 Sep 2015 18:33:07 +0800 Subject: [PATCH] use the right int type for port. --- trunk/src/app/srs_app_edge.cpp | 54 +++++++++------------ trunk/src/app/srs_app_edge.hpp | 6 +-- trunk/src/app/srs_app_encoder.cpp | 2 +- trunk/src/app/srs_app_forward.cpp | 37 ++++++-------- trunk/src/app/srs_app_forward.hpp | 6 +-- trunk/src/app/srs_app_ingest.cpp | 6 +-- trunk/src/app/srs_app_ng_exec.cpp | 4 +- trunk/src/app/srs_app_rtmp_conn.cpp | 33 +++++-------- trunk/src/app/srs_app_rtmp_conn.hpp | 4 +- trunk/src/app/srs_app_utility.cpp | 23 --------- trunk/src/app/srs_app_utility.hpp | 12 ----- trunk/src/kernel/srs_kernel_consts.hpp | 2 +- trunk/src/kernel/srs_kernel_utility.cpp | 22 ++++++++- trunk/src/kernel/srs_kernel_utility.hpp | 8 +++ trunk/src/libs/srs_librtmp.cpp | 6 +-- trunk/src/main/srs_main_ingest_hls.cpp | 8 +-- trunk/src/protocol/srs_protocol_utility.cpp | 12 ++--- trunk/src/protocol/srs_protocol_utility.hpp | 6 +-- trunk/src/protocol/srs_rtmp_stack.cpp | 1 + trunk/src/protocol/srs_rtmp_stack.hpp | 2 +- 20 files changed, 105 insertions(+), 149 deletions(-) diff --git a/trunk/src/app/srs_app_edge.cpp b/trunk/src/app/srs_app_edge.cpp index c347295d6..2ac192bb5 100644 --- a/trunk/src/app/srs_app_edge.cpp +++ b/trunk/src/app/srs_app_edge.cpp @@ -360,19 +360,17 @@ int SrsEdgeIngester::connect_server(string& ep_server, int& ep_port) } // select the origin. - std::string server = lb->select(conf->args); - int port = ::atoi(SRS_CONSTS_RTMP_DEFAULT_PORT); - srs_parse_hostport(server, server, port); - - // output the connected server and port. - ep_server = server; - ep_port = port; + if (true) { + std::string server = lb->select(conf->args); + ep_port = SRS_CONSTS_RTMP_DEFAULT_PORT; + srs_parse_hostport(server, ep_server, ep_port); + } // open socket. int64_t timeout = SRS_EDGE_INGESTER_TIMEOUT_US; - if ((ret = srs_socket_connect(server, port, timeout, &stfd)) != ERROR_SUCCESS) { + if ((ret = srs_socket_connect(ep_server, ep_port, timeout, &stfd)) != ERROR_SUCCESS) { srs_warn("edge pull failed, stream=%s, tcUrl=%s to server=%s, port=%d, timeout=%"PRId64", ret=%d", - _req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port, timeout, ret); + _req->stream.c_str(), _req->tcUrl.c_str(), ep_server.c_str(), ep_port, timeout, ret); return ret; } @@ -386,7 +384,7 @@ int SrsEdgeIngester::connect_server(string& ep_server, int& ep_port) kbps->set_io(io, io); srs_trace("edge pull connected, url=%s/%s, server=%s:%d", - _req->tcUrl.c_str(), _req->stream.c_str(), server.c_str(), port); + _req->tcUrl.c_str(), _req->stream.c_str(), ep_server.c_str(), ep_port); return ret; } @@ -398,7 +396,7 @@ SrsEdgeForwarder::SrsEdgeForwarder() client = NULL; _edge = NULL; _req = NULL; - origin_index = 0; + lb = new SrsLbRoundRobin(); stream_id = 0; stfd = NULL; pthread = new SrsReusableThread2("edge-fwr", this, SRS_EDGE_FORWARDER_SLEEP_US); @@ -410,6 +408,7 @@ SrsEdgeForwarder::~SrsEdgeForwarder() { stop(); + srs_freep(lb); srs_freep(pthread); srs_freep(queue); srs_freep(kbps); @@ -437,7 +436,8 @@ int SrsEdgeForwarder::start() send_error_code = ERROR_SUCCESS; - std::string ep_server, ep_port; + std::string ep_server; + int ep_port; if ((ret = connect_server(ep_server, ep_port)) != ERROR_SUCCESS) { return ret; } @@ -591,7 +591,7 @@ void SrsEdgeForwarder::close_underlayer_socket() srs_close_stfd(stfd); } -int SrsEdgeForwarder::connect_server(string& ep_server, string& ep_port) +int SrsEdgeForwarder::connect_server(string& ep_server, int& ep_port) { int ret = ERROR_SUCCESS; @@ -602,27 +602,17 @@ int SrsEdgeForwarder::connect_server(string& ep_server, string& ep_port) srs_assert(conf); // select the origin. - std::string server = conf->args.at(origin_index % conf->args.size()); - origin_index = (origin_index + 1) % conf->args.size(); - - std::string s_port = SRS_CONSTS_RTMP_DEFAULT_PORT; - int port = ::atoi(SRS_CONSTS_RTMP_DEFAULT_PORT); - size_t pos = server.find(":"); - if (pos != std::string::npos) { - s_port = server.substr(pos + 1); - server = server.substr(0, pos); - port = ::atoi(s_port.c_str()); + if (true) { + std::string server = lb->select(conf->args); + ep_port = SRS_CONSTS_RTMP_DEFAULT_PORT; + srs_parse_hostport(server, ep_server, ep_port); } - // output the connected server and port. - ep_server = server; - ep_port = s_port; - // open socket. int64_t timeout = SRS_EDGE_FORWARDER_TIMEOUT_US; - if ((ret = srs_socket_connect(server, port, timeout, &stfd)) != ERROR_SUCCESS) { + if ((ret = srs_socket_connect(ep_server, ep_port, timeout, &stfd)) != ERROR_SUCCESS) { srs_warn("edge push failed, stream=%s, tcUrl=%s to server=%s, port=%d, timeout=%"PRId64", ret=%d", - _req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port, timeout, ret); + _req->stream.c_str(), _req->tcUrl.c_str(), ep_server.c_str(), ep_port, timeout, ret); return ret; } @@ -637,13 +627,13 @@ int SrsEdgeForwarder::connect_server(string& ep_server, string& ep_port) // open socket. srs_trace("edge push connected, stream=%s, tcUrl=%s to server=%s, port=%d", - _req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port); + _req->stream.c_str(), _req->tcUrl.c_str(), ep_server.c_str(), ep_port); return ret; } // TODO: FIXME: refine the connect_app. -int SrsEdgeForwarder::connect_app(string ep_server, string ep_port) +int SrsEdgeForwarder::connect_app(string ep_server, int ep_port) { int ret = ERROR_SUCCESS; @@ -685,7 +675,7 @@ int SrsEdgeForwarder::connect_app(string ep_server, string ep_port) // generate the tcUrl std::string param = ""; std::string tc_url = srs_generate_tc_url(ep_server, vhost, req->app, ep_port, param); - srs_trace("edge forward to %s:%s at %s", ep_server.c_str(), ep_port.c_str(), tc_url.c_str()); + srs_trace("edge forward to %s:%d at %s", ep_server.c_str(), ep_port, tc_url.c_str()); // replace the tcUrl in request, // which will replace the tc_url in client.connect_app(). diff --git a/trunk/src/app/srs_app_edge.hpp b/trunk/src/app/srs_app_edge.hpp index a8b555eb7..3f05ce925 100644 --- a/trunk/src/app/srs_app_edge.hpp +++ b/trunk/src/app/srs_app_edge.hpp @@ -125,7 +125,7 @@ private: ISrsProtocolReaderWriter* io; SrsKbps* kbps; SrsRtmpClient* client; - int origin_index; + SrsLbRoundRobin* lb; /** * we must ensure one thread one fd principle, * that is, a fd must be write/read by the one thread. @@ -153,8 +153,8 @@ public: virtual int proxy(SrsCommonMessage* msg); private: virtual void close_underlayer_socket(); - virtual int connect_server(std::string& ep_server, std::string& ep_port); - virtual int connect_app(std::string ep_server, std::string ep_port); + virtual int connect_server(std::string& ep_server, int& ep_port); + virtual int connect_app(std::string ep_server, int ep_port); }; /** diff --git a/trunk/src/app/srs_app_encoder.cpp b/trunk/src/app/srs_app_encoder.cpp index fb9730b54..ed25f6e5f 100644 --- a/trunk/src/app/srs_app_encoder.cpp +++ b/trunk/src/app/srs_app_encoder.cpp @@ -281,7 +281,7 @@ int SrsEncoder::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsRequest* req, SrsConfDir // output stream, to other/self server // ie. rtmp://localhost:1935/live/livestream_sd output = srs_string_replace(output, "[vhost]", req->vhost); - output = srs_string_replace(output, "[port]", req->port); + output = srs_string_replace(output, "[port]", srs_int2str(req->port)); output = srs_string_replace(output, "[app]", req->app); output = srs_string_replace(output, "[stream]", req->stream); output = srs_string_replace(output, "[engine]", engine->arg0()); diff --git a/trunk/src/app/srs_app_forward.cpp b/trunk/src/app/srs_app_forward.cpp index b605ef464..21383b570 100644 --- a/trunk/src/app/srs_app_forward.cpp +++ b/trunk/src/app/srs_app_forward.cpp @@ -44,6 +44,7 @@ using namespace std; #include #include #include +#include // when error, forwarder sleep for a while and retry. #define SRS_FORWARDER_SLEEP_US (int64_t)(3*1000*1000LL) @@ -105,7 +106,8 @@ int SrsForwarder::on_publish() SrsRequest* req = _req; // discovery the server port and tcUrl from req and ep_forward. - std::string server, port, tc_url; + int port; + std::string server, tc_url; discovery_ep(server, port, tc_url); // dead loop check @@ -228,7 +230,8 @@ int SrsForwarder::cycle() { int ret = ERROR_SUCCESS; - std::string ep_server, ep_port; + std::string ep_server; + int ep_port; if ((ret = connect_server(ep_server, ep_port)) != ERROR_SUCCESS) { return ret; } @@ -273,25 +276,18 @@ void SrsForwarder::close_underlayer_socket() srs_close_stfd(stfd); } -void SrsForwarder::discovery_ep(string& server, string& port, string& tc_url) +void SrsForwarder::discovery_ep(string& server, int& port, string& tc_url) { SrsRequest* req = _req; - server = _ep_forward; port = SRS_CONSTS_RTMP_DEFAULT_PORT; - - // TODO: FIXME: parse complex params - size_t pos = _ep_forward.find(":"); - if (pos != std::string::npos) { - port = _ep_forward.substr(pos + 1); - server = _ep_forward.substr(0, pos); - } + srs_parse_hostport(_ep_forward, server, port); // generate tcUrl tc_url = srs_generate_tc_url(server, req->vhost, req->app, port, req->param); } -int SrsForwarder::connect_server(string& ep_server, string& ep_port) +int SrsForwarder::connect_server(string& ep_server, int& ep_port) { int ret = ERROR_SUCCESS; @@ -299,19 +295,14 @@ int SrsForwarder::connect_server(string& ep_server, string& ep_port) close_underlayer_socket(); // discovery the server port and tcUrl from req and ep_forward. - std::string server, s_port, tc_url; - discovery_ep(server, s_port, tc_url); - int port = ::atoi(s_port.c_str()); - - // output the connected server and port. - ep_server = server; - ep_port = s_port; + string tc_url; + discovery_ep(ep_server, ep_port, tc_url); // open socket. int64_t timeout = SRS_FORWARDER_SLEEP_US; - if ((ret = srs_socket_connect(ep_server, port, timeout, &stfd)) != ERROR_SUCCESS) { + if ((ret = srs_socket_connect(ep_server, ep_port, timeout, &stfd)) != ERROR_SUCCESS) { srs_warn("forward failed, stream=%s, tcUrl=%s to server=%s, port=%d, timeout=%"PRId64", ret=%d", - _req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port, timeout, ret); + _req->stream.c_str(), _req->tcUrl.c_str(), ep_server.c_str(), ep_port, timeout, ret); return ret; } @@ -325,13 +316,13 @@ int SrsForwarder::connect_server(string& ep_server, string& ep_port) kbps->set_io(io, io); srs_trace("forward connected, stream=%s, tcUrl=%s to server=%s, port=%d", - _req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port); + _req->stream.c_str(), _req->tcUrl.c_str(), ep_server.c_str(), ep_port); return ret; } // TODO: FIXME: refine the connect_app. -int SrsForwarder::connect_app(string ep_server, string ep_port) +int SrsForwarder::connect_app(string ep_server, int ep_port) { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_forward.hpp b/trunk/src/app/srs_app_forward.hpp index 68b4314ab..5842f0ac6 100644 --- a/trunk/src/app/srs_app_forward.hpp +++ b/trunk/src/app/srs_app_forward.hpp @@ -100,9 +100,9 @@ public: virtual int cycle(); private: virtual void close_underlayer_socket(); - virtual void discovery_ep(std::string& server, std::string& port, std::string& tc_url); - virtual int connect_server(std::string& ep_server, std::string& ep_port); - virtual int connect_app(std::string ep_server, std::string ep_port); + virtual void discovery_ep(std::string& server, int& port, std::string& tc_url); + virtual int connect_server(std::string& ep_server, int& ep_port); + virtual int connect_app(std::string ep_server, int ep_port); virtual int forward(); }; diff --git a/trunk/src/app/srs_app_ingest.cpp b/trunk/src/app/srs_app_ingest.cpp index 70736185c..fab6647cf 100644 --- a/trunk/src/app/srs_app_ingest.cpp +++ b/trunk/src/app/srs_app_ingest.cpp @@ -332,13 +332,13 @@ int SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective* vhost, S { int ret = ERROR_SUCCESS; - std::string port; + int port; if (true) { std::vector ip_ports = _srs_config->get_listens(); srs_assert(ip_ports.size() > 0); - std::string ep = ip_ports[0]; std::string ip; + std::string ep = ip_ports[0]; srs_parse_endpoint(ep, ip, port); } @@ -346,7 +346,7 @@ int SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective* vhost, S // output stream, to other/self server // ie. rtmp://localhost:1935/live/livestream_sd output = srs_string_replace(output, "[vhost]", vhost->arg0()); - output = srs_string_replace(output, "[port]", port); + output = srs_string_replace(output, "[port]", srs_int2str(port)); if (output.empty()) { ret = ERROR_ENCODER_NO_OUTPUT; srs_trace("empty output url, ingest=%s. ret=%d", ingest->arg0().c_str(), ret); diff --git a/trunk/src/app/srs_app_ng_exec.cpp b/trunk/src/app/srs_app_ng_exec.cpp index bfa221a0c..88283a890 100644 --- a/trunk/src/app/srs_app_ng_exec.cpp +++ b/trunk/src/app/srs_app_ng_exec.cpp @@ -198,7 +198,7 @@ string SrsNgExec::parse(SrsRequest* req, string tmpl) string output = tmpl; output = srs_string_replace(output, "[vhost]", req->vhost); - output = srs_string_replace(output, "[port]", req->port); + output = srs_string_replace(output, "[port]", srs_int2str(req->port)); output = srs_string_replace(output, "[app]", req->app); output = srs_string_replace(output, "[stream]", req->stream); @@ -207,7 +207,7 @@ string SrsNgExec::parse(SrsRequest* req, string tmpl) output = srs_string_replace(output, "[pageUrl]", req->pageUrl); if (output.find("[url]") != string::npos) { - string url = srs_generate_rtmp_url(req->host, ::atoi(req->port.c_str()), req->vhost, req->app, req->stream); + string url = srs_generate_rtmp_url(req->host, req->port, req->vhost, req->app, req->stream); output = srs_string_replace(output, "[url]", url); } diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index e2f67e843..5bc2d0dd2 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -159,11 +159,11 @@ int SrsRtmpConn::do_cycle() srs_info("discovery app success. schema=%s, vhost=%s, port=%s, app=%s", req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), req->app.c_str()); - if (req->schema.empty() || req->vhost.empty() || req->port.empty() || req->app.empty()) { + if (req->schema.empty() || req->vhost.empty() || req->app.empty()) { ret = ERROR_RTMP_REQ_TCURL; srs_error("discovery tcUrl failed. " - "tcUrl=%s, schema=%s, vhost=%s, port=%s, app=%s, ret=%d", - req->tcUrl.c_str(), req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), req->app.c_str(), ret); + "tcUrl=%s, schema=%s, vhost=%s, port=%d, app=%s, ret=%d", + req->tcUrl.c_str(), req->schema.c_str(), req->vhost.c_str(), req->port, req->app.c_str(), ret); return ret; } @@ -175,9 +175,9 @@ int SrsRtmpConn::do_cycle() srs_verbose("check vhost success."); srs_trace("connect app, " - "tcUrl=%s, pageUrl=%s, swfUrl=%s, schema=%s, vhost=%s, port=%s, app=%s, args=%s", + "tcUrl=%s, pageUrl=%s, swfUrl=%s, schema=%s, vhost=%s, port=%d, app=%s, args=%s", req->tcUrl.c_str(), req->pageUrl.c_str(), req->swfUrl.c_str(), - req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), + req->schema.c_str(), req->vhost.c_str(), req->port, req->app.c_str(), (req->args? "(obj)":"null")); // show client identity @@ -1230,9 +1230,10 @@ int SrsRtmpConn::check_edge_token_traverse_auth() srs_assert(req); st_netfd_t stsock = NULL; - SrsConfDirective* conf = _srs_config->get_vhost_edge_origin(req->vhost); - for (int i = 0; i < (int)conf->args.size(); i++) { - if ((ret = connect_server(i, &stsock)) == ERROR_SUCCESS) { + vector args = _srs_config->get_vhost_edge_origin(req->vhost)->args; + for (int i = 0; i < (int)args.size(); i++) { + string hostport = args.at(i); + if ((ret = connect_server(hostport, &stsock)) == ERROR_SUCCESS) { break; } } @@ -1254,7 +1255,7 @@ int SrsRtmpConn::check_edge_token_traverse_auth() return ret; } -int SrsRtmpConn::connect_server(int origin_index, st_netfd_t* pstsock) +int SrsRtmpConn::connect_server(string hostport, st_netfd_t* pstsock) { int ret = ERROR_SUCCESS; @@ -1262,17 +1263,9 @@ int SrsRtmpConn::connect_server(int origin_index, st_netfd_t* pstsock) srs_assert(conf); // select the origin. - std::string server = conf->args.at(origin_index % conf->args.size()); - origin_index = (origin_index + 1) % conf->args.size(); - - std::string s_port = SRS_CONSTS_RTMP_DEFAULT_PORT; - int port = ::atoi(SRS_CONSTS_RTMP_DEFAULT_PORT); - size_t pos = server.find(":"); - if (pos != std::string::npos) { - s_port = server.substr(pos + 1); - server = server.substr(0, pos); - port = ::atoi(s_port.c_str()); - } + string server; + int port = SRS_CONSTS_RTMP_DEFAULT_PORT; + srs_parse_hostport(hostport, server, port); // open socket. st_netfd_t stsock = NULL; diff --git a/trunk/src/app/srs_app_rtmp_conn.hpp b/trunk/src/app/srs_app_rtmp_conn.hpp index bf9b8e12d..005315aa4 100644 --- a/trunk/src/app/srs_app_rtmp_conn.hpp +++ b/trunk/src/app/srs_app_rtmp_conn.hpp @@ -30,6 +30,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#include + #include #include #include @@ -132,7 +134,7 @@ private: virtual void set_sock_options(); private: virtual int check_edge_token_traverse_auth(); - virtual int connect_server(int origin_index, st_netfd_t* pstsock); + virtual int connect_server(std::string hostport, st_netfd_t* pstsock); virtual int do_token_traverse_auth(SrsRtmpClient* client); /** * when the connection disconnect, call this method. diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index 512661326..490cff7ee 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -205,29 +205,6 @@ string srs_path_build_timestamp(string template_path) return path; } -void srs_parse_endpoint(string ip_port, string& ip, string& port) -{ - ip = "0.0.0.0"; - port = ip_port; - - size_t pos = string::npos; - if ((pos = port.find(":")) != string::npos) { - ip = port.substr(0, pos); - port = port.substr(pos + 1); - } -} - -void srs_parse_endpoint(string ip_port, string& ip, int& port) -{ - std::string the_port; - srs_parse_endpoint(ip_port, ip, the_port); - port = ::atoi(the_port.c_str()); -} - -string srs_bool2switch(bool v) { - return v? "on" : "off"; -} - int srs_kill_forced(int& pid) { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_utility.hpp b/trunk/src/app/srs_app_utility.hpp index 76338ed79..2ef07d330 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -75,18 +75,6 @@ extern std::string srs_path_build_stream(std::string template_path, std::string */ extern std::string srs_path_build_timestamp(std::string template_path); -/** -* parse the endpoint to ip and port. -* @param ip_port the ip and port which formats in <[ip:]port> - */ -extern void srs_parse_endpoint(std::string ip_port, std::string& ip, std::string& port); -extern void srs_parse_endpoint(std::string ip_port, std::string& ip, int& port); - -/** - * convert bool to switch value, true to "on", false to "off". - */ -extern std::string srs_bool2switch(bool v); - /** * kill the pid by SIGINT, then wait to quit, * kill the pid by SIGKILL again when exceed the timeout. diff --git a/trunk/src/kernel/srs_kernel_consts.hpp b/trunk/src/kernel/srs_kernel_consts.hpp index a334f9a04..bc05023b6 100644 --- a/trunk/src/kernel/srs_kernel_consts.hpp +++ b/trunk/src/kernel/srs_kernel_consts.hpp @@ -44,7 +44,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define SRS_CONSTS_RTMP_DEFAULT_VHOST "__defaultVhost__" #define SRS_CONSTS_RTMP_DEFAULT_APP "__defaultApp__" // default port of rtmp -#define SRS_CONSTS_RTMP_DEFAULT_PORT "1935" +#define SRS_CONSTS_RTMP_DEFAULT_PORT 1935 // the default chunk size for system. #define SRS_CONSTS_RTMP_SRS_CHUNK_SIZE 60000 diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index 3e06ac379..550c8d2cd 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -180,13 +180,27 @@ string srs_dns_resolve(string host) void srs_parse_hostport(const string& hostport, string& host, int& port) { - host = hostport; - size_t pos = hostport.find(":"); if (pos != std::string::npos) { string p = hostport.substr(pos + 1); host = hostport.substr(0, pos); port = ::atoi(p.c_str()); + } else { + host = hostport; + } +} + +void srs_parse_endpoint(string hostport, string& ip, int& port) +{ + ip = "0.0.0.0"; + + size_t pos = string::npos; + if ((pos = hostport.find(":")) != string::npos) { + ip = hostport.substr(0, pos); + string sport = hostport.substr(pos + 1); + port = ::atoi(sport.c_str()); + } else { + port = ::atoi(hostport.c_str()); } } @@ -206,6 +220,10 @@ string srs_float2str(double value) return tmp; } +string srs_bool2switch(bool v) { + return v? "on" : "off"; +} + bool srs_is_little_endian() { // convert to network(big-endian) order, if not equals, diff --git a/trunk/src/kernel/srs_kernel_utility.hpp b/trunk/src/kernel/srs_kernel_utility.hpp index 759bc383b..d2bc344ec 100644 --- a/trunk/src/kernel/srs_kernel_utility.hpp +++ b/trunk/src/kernel/srs_kernel_utility.hpp @@ -52,13 +52,21 @@ extern int64_t srs_update_system_time_ms(); // dns resolve utility, return the resolved ip address. extern std::string srs_dns_resolve(std::string host); + // split the host:port to host and port. +// @remark the hostport format in , where port is optional. extern void srs_parse_hostport(const std::string& hostport, std::string& host, int& port); +// parse the endpoint to ip and port. +// @remark hostport format in <[ip:]port>, where ip is default to "0.0.0.0". +extern void srs_parse_endpoint(std::string hostport, std::string& ip, int& port); + // parse the int64 value to string. extern std::string srs_int2str(int64_t value); // parse the float value to string, precise is 2. extern std::string srs_float2str(double value); +// convert bool to switch value, true to "on", false to "off". +extern std::string srs_bool2switch(bool v); // whether system is little endian extern bool srs_is_little_endian(); diff --git a/trunk/src/libs/srs_librtmp.cpp b/trunk/src/libs/srs_librtmp.cpp index 123a4e11f..d8592d482 100644 --- a/trunk/src/libs/srs_librtmp.cpp +++ b/trunk/src/libs/srs_librtmp.cpp @@ -63,7 +63,7 @@ struct Context std::string tcUrl; std::string host; std::string ip; - std::string port; + int port; std::string vhost; std::string app; std::string stream; @@ -509,9 +509,7 @@ int srs_librtmp_context_connect(Context* context) srs_assert(context->skt); std::string ip = context->ip; - int port = ::atoi(context->port.c_str()); - - if ((ret = context->skt->connect(ip.c_str(), port)) != ERROR_SUCCESS) { + if ((ret = context->skt->connect(ip.c_str(), context->port)) != ERROR_SUCCESS) { return ret; } diff --git a/trunk/src/main/srs_main_ingest_hls.cpp b/trunk/src/main/srs_main_ingest_hls.cpp index 878d5a584..8d84e0faa 100644 --- a/trunk/src/main/srs_main_ingest_hls.cpp +++ b/trunk/src/main/srs_main_ingest_hls.cpp @@ -708,7 +708,7 @@ public: */ virtual int flush_message_queue(); private: - virtual int connect_app(std::string ep_server, std::string ep_port); + virtual int connect_app(std::string ep_server, int ep_port); // close the connected io and rtmp to ready to be re-connect. virtual void close(); }; @@ -1235,8 +1235,8 @@ int SrsIngestSrsOutput::connect() } // connect host. - if ((ret = srs_socket_connect(req->host, ::atoi(req->port.c_str()), ST_UTIME_NO_TIMEOUT, &stfd)) != ERROR_SUCCESS) { - srs_error("mpegts: connect server %s:%s failed. ret=%d", req->host.c_str(), req->port.c_str(), ret); + if ((ret = srs_socket_connect(req->host, req->port, ST_UTIME_NO_TIMEOUT, &stfd)) != ERROR_SUCCESS) { + srs_error("mpegts: connect server %s:%d failed. ret=%d", req->host.c_str(), req->port, ret); return ret; } io = new SrsStSocket(stfd); @@ -1270,7 +1270,7 @@ int SrsIngestSrsOutput::connect() } // TODO: FIXME: refine the connect_app. -int SrsIngestSrsOutput::connect_app(string ep_server, string ep_port) +int SrsIngestSrsOutput::connect_app(string ep_server, int ep_port) { int ret = ERROR_SUCCESS; diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index 60be8e561..682f92602 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -44,7 +44,7 @@ using namespace std; void srs_discovery_tc_url( string tcUrl, string& schema, string& host, string& vhost, - string& app, string& port, std::string& param + string& app, int& port, std::string& param ) { size_t pos = std::string::npos; std::string url = tcUrl; @@ -63,8 +63,7 @@ void srs_discovery_tc_url( port = SRS_CONSTS_RTMP_DEFAULT_PORT; if ((pos = host.find(":")) != std::string::npos) { - port = host.substr(pos + 1); - host = host.substr(0, pos); + srs_parse_hostport(host, host, port); srs_info("discovery host=%s, port=%s", host.c_str(), port.c_str()); } @@ -126,11 +125,6 @@ void srs_random_generate(char* bytes, int size) } string srs_generate_tc_url(string ip, string vhost, string app, int port, string param) -{ - return srs_generate_tc_url(ip, vhost, app, srs_int2str(port), param); -} - -string srs_generate_tc_url(string ip, string vhost, string app, string port, string param) { string tcUrl = "rtmp://"; @@ -142,7 +136,7 @@ string srs_generate_tc_url(string ip, string vhost, string app, string port, str if (port != SRS_CONSTS_RTMP_DEFAULT_PORT) { tcUrl += ":"; - tcUrl += port; + tcUrl += srs_int2str(port); } tcUrl += "/"; diff --git a/trunk/src/protocol/srs_protocol_utility.hpp b/trunk/src/protocol/srs_protocol_utility.hpp index beecf51a9..f3650f0a9 100644 --- a/trunk/src/protocol/srs_protocol_utility.hpp +++ b/trunk/src/protocol/srs_protocol_utility.hpp @@ -58,7 +58,7 @@ class ISrsProtocolReaderWriter; extern void srs_discovery_tc_url( std::string tcUrl, std::string& schema, std::string& host, std::string& vhost, - std::string& app, std::string& port, std::string& param + std::string& app, int& port, std::string& param ); /** @@ -85,10 +85,6 @@ extern void srs_random_generate(char* bytes, int size); * @remark when vhost equals to __defaultVhost__, use ip as vhost. * @remark ignore port if port equals to default port 1935. */ -extern std::string srs_generate_tc_url( - std::string ip, std::string vhost, std::string app, std::string port, - std::string param -); extern std::string srs_generate_tc_url( std::string ip, std::string vhost, std::string app, int port, std::string param diff --git a/trunk/src/protocol/srs_rtmp_stack.cpp b/trunk/src/protocol/srs_rtmp_stack.cpp index 107ed7b28..0336f748e 100644 --- a/trunk/src/protocol/srs_rtmp_stack.cpp +++ b/trunk/src/protocol/srs_rtmp_stack.cpp @@ -1664,6 +1664,7 @@ SrsRequest::SrsRequest() { objectEncoding = RTMP_SIG_AMF0_VER; duration = -1; + port = SRS_CONSTS_RTMP_DEFAULT_PORT; args = NULL; } diff --git a/trunk/src/protocol/srs_rtmp_stack.hpp b/trunk/src/protocol/srs_rtmp_stack.hpp index 28b0a3af8..e895fc3f3 100644 --- a/trunk/src/protocol/srs_rtmp_stack.hpp +++ b/trunk/src/protocol/srs_rtmp_stack.hpp @@ -554,7 +554,7 @@ public: // the host in tcUrl. std::string host; // the port in tcUrl. - std::string port; + int port; // the app in tcUrl, without param. std::string app; // the param in tcUrl(app).