From ef0f7f4da884bb93fc7060673fa4c4aa7b30fe4d Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 19 May 2014 17:39:01 +0800 Subject: [PATCH] support http heartbeat, 0.9.107 --- trunk/conf/full.conf | 26 +++-- trunk/src/app/srs_app_config.cpp | 79 ++++++++++++-- trunk/src/app/srs_app_config.hpp | 13 ++- trunk/src/app/srs_app_heartbeat.cpp | 62 +++++++++++ trunk/src/app/srs_app_heartbeat.hpp | 19 ++++ trunk/src/app/srs_app_http_client.cpp | 145 ++++++++++++++++++++++++++ trunk/src/app/srs_app_http_client.hpp | 33 ++++++ trunk/src/app/srs_app_http_hooks.cpp | 136 +----------------------- trunk/src/app/srs_app_http_hooks.hpp | 26 ----- trunk/src/app/srs_app_server.cpp | 33 +++++- trunk/src/app/srs_app_server.hpp | 4 + trunk/src/app/srs_app_utility.cpp | 59 +++++++++++ trunk/src/app/srs_app_utility.hpp | 7 ++ trunk/src/core/srs_core.hpp | 2 +- 14 files changed, 459 insertions(+), 185 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 75b4b78ca..3510ae8e8 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -42,13 +42,25 @@ max_connections 1000; # default: on daemon on; # heartbeat to api server -# when startup, srs will heartbeat to this api. -# @remark: must be a restful http api url, where SRS will POST with following data: -# { -# "device_id": "my-srs-device", -# "ip": "192.168.1.100" -# } -heartbeat http://127.0.0.1:8085/api/v1/servers; +heartbeat { + # whether heartbeat is enalbed. + # default: off + enabled off; + # the interval seconds for heartbeat, + # recommend 0.3,0.6,0.9,1.2,1.5,1.8,2.1,2.4,2.7,3,...,6,9,12,.... + # default: 9.9 + interval 9.3; + # when startup, srs will heartbeat to this api. + # @remark: must be a restful http api url, where SRS will POST with following data: + # { + # "device_id": "my-srs-device", + # "ip": "192.168.1.100" + # } + # default: http://127.0.0.1:8085/api/v1/servers + url http://127.0.0.1:8085/api/v1/servers; + # the id of devide. + device_id "my-srs-device"; +} ############################################################################################# # HTTP sections diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 8382958e2..48260285c 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -1422,17 +1422,6 @@ string SrsConfig::get_pid_file() return conf->arg0(); } -string SrsConfig::get_heartbeat_url() -{ - SrsConfDirective* conf = root->get("heartbeat"); - - if (!conf) { - return ""; - } - - return conf->arg0(); -} - int SrsConfig::get_pithy_print_publish() { SrsConfDirective* pithy = root->get("pithy_print"); @@ -2787,6 +2776,74 @@ string SrsConfig::get_vhost_http_dir(string vhost) return conf->arg0(); } +SrsConfDirective* SrsConfig::get_heartbeart() +{ + return root->get("heartbeat"); +} + +bool SrsConfig::get_heartbeat_enabled() +{ + SrsConfDirective* conf = get_heartbeart(); + + if (!conf) { + return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_ENABLED; + } + + conf = conf->get("enabled"); + if (!conf || conf->arg0() != "on") { + return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_ENABLED; + } + + return true; +} + +int64_t SrsConfig::get_heartbeat_interval() +{ + SrsConfDirective* conf = get_heartbeart(); + + if (!conf) { + return (int64_t)(SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INTERVAL * 1000); + } + conf = conf->get("interval"); + if (!conf || conf->arg0().empty()) { + return (int64_t)(SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INTERVAL * 1000); + } + + return (int64_t)(::atof(conf->arg0().c_str()) * 1000); +} + +string SrsConfig::get_heartbeat_url() +{ + SrsConfDirective* conf = get_heartbeart(); + + if (!conf) { + return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_URL; + } + + conf = conf->get("url"); + if (!conf || conf->arg0().empty()) { + return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_URL; + } + + return conf->arg0(); +} + +string SrsConfig::get_heartbeat_device_id() +{ + SrsConfDirective* conf = get_heartbeart(); + + if (!conf) { + return ""; + } + + conf = conf->get("device_id"); + if (!conf || conf->arg0().empty()) { + return ""; + } + + return conf->arg0(); +} + bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b) { // both NULL, equal. diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 05fa9851b..bdaa056f6 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -70,6 +70,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define SRS_CONF_DEFAULT_HTTP_STREAM_PORT 8080 #define SRS_CONF_DEFAULT_HTTP_API_PORT 1985 +#define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_ENABLED false +#define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INTERVAL 9.9 +#define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_URL "http://127.0.0.1:8085/api/v1/servers" + #define SRS_STAGE_PLAY_USER_INTERVAL_MS 10000 #define SRS_STAGE_PUBLISH_USER_INTERVAL_MS 10000 #define SRS_STAGE_FORWARDER_INTERVAL_MS 10000 @@ -173,7 +177,6 @@ public: virtual int get_max_connections(); virtual SrsConfDirective* get_listen(); virtual std::string get_pid_file(); - virtual std::string get_heartbeat_url(); virtual int get_pithy_print_publish(); virtual int get_pithy_print_forwarder(); virtual int get_pithy_print_encoder(); @@ -286,6 +289,14 @@ public: virtual bool get_vhost_http_enabled(std::string vhost); virtual std::string get_vhost_http_mount(std::string vhost); virtual std::string get_vhost_http_dir(std::string vhost); +// http heartbeart section +private: + virtual SrsConfDirective* get_heartbeart(); +public: + virtual bool get_heartbeat_enabled(); + virtual int64_t get_heartbeat_interval(); + virtual std::string get_heartbeat_url(); + virtual std::string get_heartbeat_device_id(); }; /** diff --git a/trunk/src/app/srs_app_heartbeat.cpp b/trunk/src/app/srs_app_heartbeat.cpp index 544fd3ea5..05464e3e6 100644 --- a/trunk/src/app/srs_app_heartbeat.cpp +++ b/trunk/src/app/srs_app_heartbeat.cpp @@ -23,6 +23,68 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#ifdef SRS_AUTO_HTTP_PARSER + +#include +using namespace std; + #include #include +#include +#include +#include +#include +#include +SrsHttpHeartbeat::SrsHttpHeartbeat() +{ +} + +SrsHttpHeartbeat::~SrsHttpHeartbeat() +{ +} + +void SrsHttpHeartbeat::heartbeat() +{ + int ret = ERROR_SUCCESS; + + std::string url = _srs_config->get_heartbeat_url(); + + SrsHttpUri uri; + if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { + srs_error("http uri parse hartbeart url failed. url=%s, ret=%d", url.c_str(), ret); + return; + } + + std::string ip = ""; + std::string device_id = _srs_config->get_heartbeat_device_id(); + + vector& ips = srs_get_local_ipv4_ips(); + if (!ips.empty()) { + ip = ips[0]; // TODO: FIXME: maybe need to config it. + } + + std::stringstream ss; + ss << JOBJECT_START + << JFIELD_STR("device_id", device_id) << JFIELD_CONT + << JFIELD_STR("ip", ip) + << JOBJECT_END; + std::string data = ss.str(); + std::string res; + + SrsHttpClient http; + if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) { + srs_error("http post hartbeart uri failed. " + "url=%s, request=%s, response=%s, ret=%d", + url.c_str(), data.c_str(), res.c_str(), ret); + return; + } + + srs_trace("http hook hartbeart success. " + "url=%s, request=%s, response=%s, ret=%d", + url.c_str(), data.c_str(), res.c_str(), ret); + + return; +} + +#endif diff --git a/trunk/src/app/srs_app_heartbeat.hpp b/trunk/src/app/srs_app_heartbeat.hpp index c71e4f932..2a3f036a4 100644 --- a/trunk/src/app/srs_app_heartbeat.hpp +++ b/trunk/src/app/srs_app_heartbeat.hpp @@ -29,4 +29,23 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include +#ifdef SRS_AUTO_HTTP_PARSER + +/** +* the http heartbeat to api-server to notice api +* that the information of SRS. +*/ +class SrsHttpHeartbeat +{ +public: + SrsHttpHeartbeat(); + virtual ~SrsHttpHeartbeat(); +public: + virtual void heartbeat(); +public: + static void update_local_ipv4_ips(); +}; + +#endif + #endif diff --git a/trunk/src/app/srs_app_http_client.cpp b/trunk/src/app/srs_app_http_client.cpp index d377b3454..f3250dd1f 100644 --- a/trunk/src/app/srs_app_http_client.cpp +++ b/trunk/src/app/srs_app_http_client.cpp @@ -23,3 +23,148 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#ifdef SRS_AUTO_HTTP_PARSER + +#include + +using namespace std; + +#include +#include +#include +#include + +SrsHttpClient::SrsHttpClient() +{ + connected = false; + stfd = NULL; + parser = NULL; +} + +SrsHttpClient::~SrsHttpClient() +{ + disconnect(); + srs_freep(parser); +} + +int SrsHttpClient::post(SrsHttpUri* uri, string req, string& res) +{ + res = ""; + + int ret = ERROR_SUCCESS; + + if (!parser) { + parser = new SrsHttpParser(); + + if ((ret = parser->initialize(HTTP_RESPONSE)) != ERROR_SUCCESS) { + srs_error("initialize parser failed. ret=%d", ret); + return ret; + } + } + + if ((ret = connect(uri)) != ERROR_SUCCESS) { + srs_error("http connect server failed. ret=%d", ret); + return ret; + } + + // send POST request to uri + // POST %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s + std::stringstream ss; + ss << "POST " << uri->get_path() << " " + << "HTTP/1.1" << __CRLF + << "Host: " << uri->get_host() << __CRLF + << "Connection: Keep-Alive" << __CRLF + << "Content-Length: " << std::dec << req.length() << __CRLF + << "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << __CRLF + << "Content-Type: text/html" << __CRLF + << __CRLF + << req; + + SrsSocket skt(stfd); + + std::string data = ss.str(); + if ((ret = skt.write(data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) { + // disconnect when error. + disconnect(); + + srs_error("write http post failed. ret=%d", ret); + return ret; + } + + SrsHttpMessage* msg = NULL; + if ((ret = parser->parse_message(&skt, &msg)) != ERROR_SUCCESS) { + srs_error("parse http post response failed. ret=%d", ret); + return ret; + } + + srs_assert(msg); + srs_assert(msg->is_complete()); + + // get response body. + if (msg->body_size() > 0) { + res = msg->body(); + } + srs_info("parse http post response success."); + + return ret; +} + +void SrsHttpClient::disconnect() +{ + connected = false; + + srs_close_stfd(stfd); +} + +int SrsHttpClient::connect(SrsHttpUri* uri) +{ + int ret = ERROR_SUCCESS; + + if (connected) { + return ret; + } + + disconnect(); + + std::string ip = srs_dns_resolve(uri->get_host()); + if (ip.empty()) { + ret = ERROR_SYSTEM_IP_INVALID; + srs_error("dns resolve server error, ip empty. ret=%d", ret); + return ret; + } + + int sock = socket(AF_INET, SOCK_STREAM, 0); + if(sock == -1){ + ret = ERROR_SOCKET_CREATE; + srs_error("create socket error. ret=%d", ret); + return ret; + } + + stfd = st_netfd_open_socket(sock); + if(stfd == NULL){ + ret = ERROR_ST_OPEN_SOCKET; + srs_error("st_netfd_open_socket failed. ret=%d", ret); + return ret; + } + + sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(uri->get_port()); + addr.sin_addr.s_addr = inet_addr(ip.c_str()); + + if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1){ + ret = ERROR_ST_CONNECT; + srs_error("connect to server error. " + "ip=%s, port=%d, ret=%d", ip.c_str(), uri->get_port(), ret); + return ret; + } + srs_info("connect to server success. " + "http url=%s, server=%s, ip=%s, port=%d", + uri->get_url(), uri->get_host(), ip.c_str(), uri->get_port()); + + connected = true; + + return ret; +} + +#endif diff --git a/trunk/src/app/srs_app_http_client.hpp b/trunk/src/app/srs_app_http_client.hpp index 8a40aefa4..92e7629b6 100644 --- a/trunk/src/app/srs_app_http_client.hpp +++ b/trunk/src/app/srs_app_http_client.hpp @@ -29,4 +29,37 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include +#ifdef SRS_AUTO_HTTP_PARSER + +#include + +class SrsHttpUri; +class SrsHttpParser; + +/** +* http client to GET/POST/PUT/DELETE uri +*/ +class SrsHttpClient +{ +private: + bool connected; + st_netfd_t stfd; + SrsHttpParser* parser; +public: + SrsHttpClient(); + virtual ~SrsHttpClient(); +public: + /** + * to post data to the uri. + * @param req the data post to uri. + * @param res the response data from server. + */ + virtual int post(SrsHttpUri* uri, std::string req, std::string& res); +private: + virtual void disconnect(); + virtual int connect(SrsHttpUri* uri); +}; + +#endif + #endif diff --git a/trunk/src/app/srs_app_http_hooks.cpp b/trunk/src/app/srs_app_http_hooks.cpp index 536bed356..12c3ce5a8 100644 --- a/trunk/src/app/srs_app_http_hooks.cpp +++ b/trunk/src/app/srs_app_http_hooks.cpp @@ -28,8 +28,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include using namespace std; -#include - #include #include #include @@ -38,145 +36,13 @@ using namespace std; #include #include #include +#include #define SRS_HTTP_RESPONSE_OK "0" #define SRS_HTTP_HEADER_BUFFER 1024 #define SRS_HTTP_BODY_BUFFER 32 * 1024 -SrsHttpClient::SrsHttpClient() -{ - connected = false; - stfd = NULL; - parser = NULL; -} - -SrsHttpClient::~SrsHttpClient() -{ - disconnect(); - srs_freep(parser); -} - -int SrsHttpClient::post(SrsHttpUri* uri, string req, string& res) -{ - res = ""; - - int ret = ERROR_SUCCESS; - - if (!parser) { - parser = new SrsHttpParser(); - - if ((ret = parser->initialize(HTTP_RESPONSE)) != ERROR_SUCCESS) { - srs_error("initialize parser failed. ret=%d", ret); - return ret; - } - } - - if ((ret = connect(uri)) != ERROR_SUCCESS) { - srs_error("http connect server failed. ret=%d", ret); - return ret; - } - - // send POST request to uri - // POST %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n%s - std::stringstream ss; - ss << "POST " << uri->get_path() << " " - << "HTTP/1.1" << __CRLF - << "Host: " << uri->get_host() << __CRLF - << "Connection: Keep-Alive" << __CRLF - << "Content-Length: " << std::dec << req.length() << __CRLF - << "User-Agent: " << RTMP_SIG_SRS_NAME << RTMP_SIG_SRS_VERSION << __CRLF - << "Content-Type: text/html" << __CRLF - << __CRLF - << req; - - SrsSocket skt(stfd); - - std::string data = ss.str(); - if ((ret = skt.write(data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) { - // disconnect when error. - disconnect(); - - srs_error("write http post failed. ret=%d", ret); - return ret; - } - - SrsHttpMessage* msg = NULL; - if ((ret = parser->parse_message(&skt, &msg)) != ERROR_SUCCESS) { - srs_error("parse http post response failed. ret=%d", ret); - return ret; - } - - srs_assert(msg); - srs_assert(msg->is_complete()); - - // get response body. - if (msg->body_size() > 0) { - res = msg->body(); - } - srs_info("parse http post response success."); - - return ret; -} - -void SrsHttpClient::disconnect() -{ - connected = false; - - srs_close_stfd(stfd); -} - -int SrsHttpClient::connect(SrsHttpUri* uri) -{ - int ret = ERROR_SUCCESS; - - if (connected) { - return ret; - } - - disconnect(); - - std::string ip = srs_dns_resolve(uri->get_host()); - if (ip.empty()) { - ret = ERROR_SYSTEM_IP_INVALID; - srs_error("dns resolve server error, ip empty. ret=%d", ret); - return ret; - } - - int sock = socket(AF_INET, SOCK_STREAM, 0); - if(sock == -1){ - ret = ERROR_SOCKET_CREATE; - srs_error("create socket error. ret=%d", ret); - return ret; - } - - stfd = st_netfd_open_socket(sock); - if(stfd == NULL){ - ret = ERROR_ST_OPEN_SOCKET; - srs_error("st_netfd_open_socket failed. ret=%d", ret); - return ret; - } - - sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_port = htons(uri->get_port()); - addr.sin_addr.s_addr = inet_addr(ip.c_str()); - - if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1){ - ret = ERROR_ST_CONNECT; - srs_error("connect to server error. " - "ip=%s, port=%d, ret=%d", ip.c_str(), uri->get_port(), ret); - return ret; - } - srs_info("connect to server success. " - "http url=%s, server=%s, ip=%s, port=%d", - uri->get_url(), uri->get_host(), ip.c_str(), uri->get_port()); - - connected = true; - - return ret; -} - SrsHttpHooks::SrsHttpHooks() { } diff --git a/trunk/src/app/srs_app_http_hooks.hpp b/trunk/src/app/srs_app_http_hooks.hpp index f5fb27676..7785be436 100644 --- a/trunk/src/app/srs_app_http_hooks.hpp +++ b/trunk/src/app/srs_app_http_hooks.hpp @@ -39,32 +39,6 @@ class SrsRequest; class SrsHttpParser; class SrsFlvSegment; -#include - -/** -* http client to GET/POST/PUT/DELETE uri -*/ -class SrsHttpClient -{ -private: - bool connected; - st_netfd_t stfd; - SrsHttpParser* parser; -public: - SrsHttpClient(); - virtual ~SrsHttpClient(); -public: - /** - * to post data to the uri. - * @param req the data post to uri. - * @param res the response data from server. - */ - virtual int post(SrsHttpUri* uri, std::string req, std::string& res); -private: - virtual void disconnect(); - virtual int connect(SrsHttpUri* uri); -}; - /** * the http hooks, http callback api, * for some event, such as on_connect, call diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 9eabe29fc..4d63f9fcd 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -41,11 +41,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include -#ifdef SRS_AUTO_INGEST #include -#endif #include #include +#include #define SERVER_LISTEN_BACKLOG 512 @@ -74,7 +73,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // update platform info interval: // SRS_SYS_CYCLE_INTERVAL * SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES -#define SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES 80 +#define SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES 90 SrsListener::SrsListener(SrsServer* server, SrsListenerType type) { @@ -317,6 +316,9 @@ SrsServer::SrsServer() #ifdef SRS_AUTO_HTTP_SERVER http_stream_handler = NULL; #endif +#ifdef SRS_AUTO_HTTP_PARSER + http_heartbeat = NULL; +#endif #ifdef SRS_AUTO_INGEST ingester = NULL; #endif @@ -348,6 +350,11 @@ void SrsServer::destroy() #ifdef SRS_AUTO_HTTP_SERVER srs_freep(http_stream_handler); #endif + +#ifdef SRS_AUTO_HTTP_PARSER + srs_freep(http_heartbeat); +#endif + #ifdef SRS_AUTO_INGEST srs_freep(ingester); #endif @@ -392,6 +399,10 @@ int SrsServer::initialize() srs_assert(!http_stream_handler); http_stream_handler = SrsHttpHandler::create_http_stream(); #endif +#ifdef SRS_AUTO_HTTP_PARSER + srs_assert(!http_heartbeat); + http_heartbeat = new SrsHttpHeartbeat(); +#endif #ifdef SRS_AUTO_INGEST srs_assert(!ingester); ingester = new SrsIngester(); @@ -635,7 +646,14 @@ int SrsServer::do_cycle() // the deamon thread, update the time cache while (true) { - for (int i = 1; i < max + 1; i++) { + // the interval in config. + int64_t heartbeat_max_resolution = _srs_config->get_heartbeat_interval() / 100; + + // dynamic fetch the max. + int __max = max; + __max = srs_max(__max, heartbeat_max_resolution); + + for (int i = 1; i < __max + 1; i++) { st_usleep(SRS_SYS_CYCLE_INTERVAL * 1000); // for gperf heap checker, @@ -677,6 +695,13 @@ int SrsServer::do_cycle() if ((i % SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES) == 0) { srs_update_platform_info(); } +#ifdef SRS_AUTO_HTTP_PARSER + if (_srs_config->get_heartbeat_enabled()) { + if ((i % heartbeat_max_resolution) == 0) { + http_heartbeat->heartbeat(); + } + } +#endif } } diff --git a/trunk/src/app/srs_app_server.hpp b/trunk/src/app/srs_app_server.hpp index 5c8d7005a..a9bc4a662 100644 --- a/trunk/src/app/srs_app_server.hpp +++ b/trunk/src/app/srs_app_server.hpp @@ -40,6 +40,7 @@ class SrsServer; class SrsConnection; class SrsHttpHandler; class SrsIngester; +class SrsHttpHeartbeat; // listener type for server to identify the connection, // that is, use different type to process the connection. @@ -115,6 +116,9 @@ private: #ifdef SRS_AUTO_HTTP_SERVER SrsHttpHandler* http_stream_handler; #endif +#ifdef SRS_AUTO_HTTP_PARSER + SrsHttpHeartbeat* http_heartbeat; +#endif #ifdef SRS_AUTO_INGEST SrsIngester* ingester; #endif diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index deb0f1239..9cb626cc5 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -23,12 +23,19 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#include #include +#include +#include + +using namespace std; #include #include #include +#define SRS_LOCAL_LOOP_IP "127.0.0.1" + int srs_get_log_level(std::string level) { if ("verbose" == _srs_config->get_log_level()) { @@ -435,3 +442,55 @@ void srs_update_platform_info() } } } + +vector _srs_system_ipv4_ips; + +void retrieve_local_ipv4_ips() +{ + vector& ips = _srs_system_ipv4_ips; + + ips.clear(); + + ifaddrs* ifap; + if (getifaddrs(&ifap) == -1) { + srs_warn("retrieve local ips, ini ifaddrs failed."); + return; + } + + ifaddrs* p = ifap; + while (p != NULL) { + sockaddr* addr = p->ifa_addr; + + // retrieve ipv4 addr + if (addr->sa_family == AF_INET) { + in_addr* inaddr = &((sockaddr_in*)addr)->sin_addr; + + char buf[16]; + memset(buf, 0, sizeof(buf)); + + if ((inet_ntop(addr->sa_family, inaddr, buf, sizeof(buf))) == NULL) { + srs_warn("convert local ip failed"); + break; + } + + std::string ip = buf; + if (ip != SRS_LOCAL_LOOP_IP) { + srs_trace("retrieve local ipv4 addresses: %s", ip.c_str()); + ips.push_back(ip); + } + } + + p = p->ifa_next; + } + + freeifaddrs(ifap); +} + +vector& srs_get_local_ipv4_ips() +{ + if (_srs_system_ipv4_ips.empty()) { + retrieve_local_ipv4_ips(); + } + + return _srs_system_ipv4_ips; +} diff --git a/trunk/src/app/srs_app_utility.hpp b/trunk/src/app/srs_app_utility.hpp index 2a221076d..0a36bc027 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -30,6 +30,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#include +#include + #include /** @@ -346,4 +349,8 @@ extern SrsPlatformInfo* srs_get_platform_info(); // the deamon st-thread will update it. extern void srs_update_platform_info(); +// get local ip, fill to @param ips +extern void srs_retrieve_local_ipv4_ips(); +extern std::vector& srs_get_local_ipv4_ips(); + #endif diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 7e5a0be10..48f91b210 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR "0" #define VERSION_MINOR "9" -#define VERSION_REVISION "106" +#define VERSION_REVISION "107" #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION // server info. #define RTMP_SIG_SRS_KEY "SRS"