mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
support http heartbeat, 0.9.107
This commit is contained in:
parent
cb95b5909c
commit
ef0f7f4da8
14 changed files with 459 additions and 185 deletions
|
@ -42,13 +42,25 @@ max_connections 1000;
|
||||||
# default: on
|
# default: on
|
||||||
daemon on;
|
daemon on;
|
||||||
# heartbeat to api server
|
# heartbeat to api server
|
||||||
# when startup, srs will heartbeat to this api.
|
heartbeat {
|
||||||
# @remark: must be a restful http api url, where SRS will POST with following data:
|
# whether heartbeat is enalbed.
|
||||||
# {
|
# default: off
|
||||||
# "device_id": "my-srs-device",
|
enabled off;
|
||||||
# "ip": "192.168.1.100"
|
# 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,....
|
||||||
heartbeat http://127.0.0.1:8085/api/v1/servers;
|
# 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
|
# HTTP sections
|
||||||
|
|
|
@ -1422,17 +1422,6 @@ string SrsConfig::get_pid_file()
|
||||||
return conf->arg0();
|
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()
|
int SrsConfig::get_pithy_print_publish()
|
||||||
{
|
{
|
||||||
SrsConfDirective* pithy = root->get("pithy_print");
|
SrsConfDirective* pithy = root->get("pithy_print");
|
||||||
|
@ -2787,6 +2776,74 @@ string SrsConfig::get_vhost_http_dir(string vhost)
|
||||||
return conf->arg0();
|
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)
|
bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b)
|
||||||
{
|
{
|
||||||
// both NULL, equal.
|
// both NULL, equal.
|
||||||
|
|
|
@ -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_STREAM_PORT 8080
|
||||||
#define SRS_CONF_DEFAULT_HTTP_API_PORT 1985
|
#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_PLAY_USER_INTERVAL_MS 10000
|
||||||
#define SRS_STAGE_PUBLISH_USER_INTERVAL_MS 10000
|
#define SRS_STAGE_PUBLISH_USER_INTERVAL_MS 10000
|
||||||
#define SRS_STAGE_FORWARDER_INTERVAL_MS 10000
|
#define SRS_STAGE_FORWARDER_INTERVAL_MS 10000
|
||||||
|
@ -173,7 +177,6 @@ public:
|
||||||
virtual int get_max_connections();
|
virtual int get_max_connections();
|
||||||
virtual SrsConfDirective* get_listen();
|
virtual SrsConfDirective* get_listen();
|
||||||
virtual std::string get_pid_file();
|
virtual std::string get_pid_file();
|
||||||
virtual std::string get_heartbeat_url();
|
|
||||||
virtual int get_pithy_print_publish();
|
virtual int get_pithy_print_publish();
|
||||||
virtual int get_pithy_print_forwarder();
|
virtual int get_pithy_print_forwarder();
|
||||||
virtual int get_pithy_print_encoder();
|
virtual int get_pithy_print_encoder();
|
||||||
|
@ -286,6 +289,14 @@ public:
|
||||||
virtual bool get_vhost_http_enabled(std::string vhost);
|
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_mount(std::string vhost);
|
||||||
virtual std::string get_vhost_http_dir(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();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -23,6 +23,68 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_app_heartbeat.hpp>
|
#include <srs_app_heartbeat.hpp>
|
||||||
|
|
||||||
|
#ifdef SRS_AUTO_HTTP_PARSER
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
#include <srs_kernel_error.hpp>
|
#include <srs_kernel_error.hpp>
|
||||||
#include <srs_kernel_log.hpp>
|
#include <srs_kernel_log.hpp>
|
||||||
|
#include <srs_app_config.hpp>
|
||||||
|
#include <srs_app_http_client.hpp>
|
||||||
|
#include <srs_app_json.hpp>
|
||||||
|
#include <srs_app_http.hpp>
|
||||||
|
#include <srs_app_utility.hpp>
|
||||||
|
|
||||||
|
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<string>& 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
|
||||||
|
|
|
@ -29,4 +29,23 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include <srs_core.hpp>
|
#include <srs_core.hpp>
|
||||||
|
|
||||||
|
#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
|
#endif
|
||||||
|
|
|
@ -23,3 +23,148 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_app_http_client.hpp>
|
#include <srs_app_http_client.hpp>
|
||||||
|
|
||||||
|
#ifdef SRS_AUTO_HTTP_PARSER
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
#include <srs_app_http.hpp>
|
||||||
|
#include <srs_kernel_error.hpp>
|
||||||
|
#include <srs_kernel_log.hpp>
|
||||||
|
#include <srs_app_socket.hpp>
|
||||||
|
|
||||||
|
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
|
||||||
|
|
|
@ -29,4 +29,37 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include <srs_core.hpp>
|
#include <srs_core.hpp>
|
||||||
|
|
||||||
|
#ifdef SRS_AUTO_HTTP_PARSER
|
||||||
|
|
||||||
|
#include <srs_app_st.hpp>
|
||||||
|
|
||||||
|
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
|
#endif
|
||||||
|
|
|
@ -28,8 +28,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
|
|
||||||
#include <srs_kernel_error.hpp>
|
#include <srs_kernel_error.hpp>
|
||||||
#include <srs_protocol_rtmp.hpp>
|
#include <srs_protocol_rtmp.hpp>
|
||||||
#include <srs_kernel_log.hpp>
|
#include <srs_kernel_log.hpp>
|
||||||
|
@ -38,145 +36,13 @@ using namespace std;
|
||||||
#include <srs_app_json.hpp>
|
#include <srs_app_json.hpp>
|
||||||
#include <srs_app_dvr.hpp>
|
#include <srs_app_dvr.hpp>
|
||||||
#include <srs_app_config.hpp>
|
#include <srs_app_config.hpp>
|
||||||
|
#include <srs_app_http_client.hpp>
|
||||||
|
|
||||||
#define SRS_HTTP_RESPONSE_OK "0"
|
#define SRS_HTTP_RESPONSE_OK "0"
|
||||||
|
|
||||||
#define SRS_HTTP_HEADER_BUFFER 1024
|
#define SRS_HTTP_HEADER_BUFFER 1024
|
||||||
#define SRS_HTTP_BODY_BUFFER 32 * 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()
|
SrsHttpHooks::SrsHttpHooks()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,32 +39,6 @@ class SrsRequest;
|
||||||
class SrsHttpParser;
|
class SrsHttpParser;
|
||||||
class SrsFlvSegment;
|
class SrsFlvSegment;
|
||||||
|
|
||||||
#include <srs_app_st.hpp>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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,
|
* the http hooks, http callback api,
|
||||||
* for some event, such as on_connect, call
|
* for some event, such as on_connect, call
|
||||||
|
|
|
@ -41,11 +41,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#include <srs_app_http_api.hpp>
|
#include <srs_app_http_api.hpp>
|
||||||
#include <srs_app_http_conn.hpp>
|
#include <srs_app_http_conn.hpp>
|
||||||
#include <srs_app_http.hpp>
|
#include <srs_app_http.hpp>
|
||||||
#ifdef SRS_AUTO_INGEST
|
|
||||||
#include <srs_app_ingest.hpp>
|
#include <srs_app_ingest.hpp>
|
||||||
#endif
|
|
||||||
#include <srs_app_source.hpp>
|
#include <srs_app_source.hpp>
|
||||||
#include <srs_app_utility.hpp>
|
#include <srs_app_utility.hpp>
|
||||||
|
#include <srs_app_heartbeat.hpp>
|
||||||
|
|
||||||
#define SERVER_LISTEN_BACKLOG 512
|
#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:
|
// update platform info interval:
|
||||||
// SRS_SYS_CYCLE_INTERVAL * SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES
|
// 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)
|
SrsListener::SrsListener(SrsServer* server, SrsListenerType type)
|
||||||
{
|
{
|
||||||
|
@ -317,6 +316,9 @@ SrsServer::SrsServer()
|
||||||
#ifdef SRS_AUTO_HTTP_SERVER
|
#ifdef SRS_AUTO_HTTP_SERVER
|
||||||
http_stream_handler = NULL;
|
http_stream_handler = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef SRS_AUTO_HTTP_PARSER
|
||||||
|
http_heartbeat = NULL;
|
||||||
|
#endif
|
||||||
#ifdef SRS_AUTO_INGEST
|
#ifdef SRS_AUTO_INGEST
|
||||||
ingester = NULL;
|
ingester = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
@ -348,6 +350,11 @@ void SrsServer::destroy()
|
||||||
#ifdef SRS_AUTO_HTTP_SERVER
|
#ifdef SRS_AUTO_HTTP_SERVER
|
||||||
srs_freep(http_stream_handler);
|
srs_freep(http_stream_handler);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef SRS_AUTO_HTTP_PARSER
|
||||||
|
srs_freep(http_heartbeat);
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef SRS_AUTO_INGEST
|
#ifdef SRS_AUTO_INGEST
|
||||||
srs_freep(ingester);
|
srs_freep(ingester);
|
||||||
#endif
|
#endif
|
||||||
|
@ -392,6 +399,10 @@ int SrsServer::initialize()
|
||||||
srs_assert(!http_stream_handler);
|
srs_assert(!http_stream_handler);
|
||||||
http_stream_handler = SrsHttpHandler::create_http_stream();
|
http_stream_handler = SrsHttpHandler::create_http_stream();
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef SRS_AUTO_HTTP_PARSER
|
||||||
|
srs_assert(!http_heartbeat);
|
||||||
|
http_heartbeat = new SrsHttpHeartbeat();
|
||||||
|
#endif
|
||||||
#ifdef SRS_AUTO_INGEST
|
#ifdef SRS_AUTO_INGEST
|
||||||
srs_assert(!ingester);
|
srs_assert(!ingester);
|
||||||
ingester = new SrsIngester();
|
ingester = new SrsIngester();
|
||||||
|
@ -635,7 +646,14 @@ int SrsServer::do_cycle()
|
||||||
|
|
||||||
// the deamon thread, update the time cache
|
// the deamon thread, update the time cache
|
||||||
while (true) {
|
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);
|
st_usleep(SRS_SYS_CYCLE_INTERVAL * 1000);
|
||||||
|
|
||||||
// for gperf heap checker,
|
// for gperf heap checker,
|
||||||
|
@ -677,6 +695,13 @@ int SrsServer::do_cycle()
|
||||||
if ((i % SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES) == 0) {
|
if ((i % SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES) == 0) {
|
||||||
srs_update_platform_info();
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@ class SrsServer;
|
||||||
class SrsConnection;
|
class SrsConnection;
|
||||||
class SrsHttpHandler;
|
class SrsHttpHandler;
|
||||||
class SrsIngester;
|
class SrsIngester;
|
||||||
|
class SrsHttpHeartbeat;
|
||||||
|
|
||||||
// listener type for server to identify the connection,
|
// listener type for server to identify the connection,
|
||||||
// that is, use different type to process the connection.
|
// that is, use different type to process the connection.
|
||||||
|
@ -115,6 +116,9 @@ private:
|
||||||
#ifdef SRS_AUTO_HTTP_SERVER
|
#ifdef SRS_AUTO_HTTP_SERVER
|
||||||
SrsHttpHandler* http_stream_handler;
|
SrsHttpHandler* http_stream_handler;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef SRS_AUTO_HTTP_PARSER
|
||||||
|
SrsHttpHeartbeat* http_heartbeat;
|
||||||
|
#endif
|
||||||
#ifdef SRS_AUTO_INGEST
|
#ifdef SRS_AUTO_INGEST
|
||||||
SrsIngester* ingester;
|
SrsIngester* ingester;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -23,12 +23,19 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_app_utility.hpp>
|
#include <srs_app_utility.hpp>
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
#include <srs_kernel_log.hpp>
|
#include <srs_kernel_log.hpp>
|
||||||
#include <srs_app_config.hpp>
|
#include <srs_app_config.hpp>
|
||||||
#include <srs_kernel_utility.hpp>
|
#include <srs_kernel_utility.hpp>
|
||||||
|
|
||||||
|
#define SRS_LOCAL_LOOP_IP "127.0.0.1"
|
||||||
|
|
||||||
int srs_get_log_level(std::string level)
|
int srs_get_log_level(std::string level)
|
||||||
{
|
{
|
||||||
if ("verbose" == _srs_config->get_log_level()) {
|
if ("verbose" == _srs_config->get_log_level()) {
|
||||||
|
@ -435,3 +442,55 @@ void srs_update_platform_info()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<string> _srs_system_ipv4_ips;
|
||||||
|
|
||||||
|
void retrieve_local_ipv4_ips()
|
||||||
|
{
|
||||||
|
vector<string>& 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<string>& srs_get_local_ipv4_ips()
|
||||||
|
{
|
||||||
|
if (_srs_system_ipv4_ips.empty()) {
|
||||||
|
retrieve_local_ipv4_ips();
|
||||||
|
}
|
||||||
|
|
||||||
|
return _srs_system_ipv4_ips;
|
||||||
|
}
|
||||||
|
|
|
@ -30,6 +30,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_core.hpp>
|
#include <srs_core.hpp>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <sys/resource.h>
|
#include <sys/resource.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -346,4 +349,8 @@ extern SrsPlatformInfo* srs_get_platform_info();
|
||||||
// the deamon st-thread will update it.
|
// the deamon st-thread will update it.
|
||||||
extern void srs_update_platform_info();
|
extern void srs_update_platform_info();
|
||||||
|
|
||||||
|
// get local ip, fill to @param ips
|
||||||
|
extern void srs_retrieve_local_ipv4_ips();
|
||||||
|
extern std::vector<std::string>& srs_get_local_ipv4_ips();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
// current release version
|
// current release version
|
||||||
#define VERSION_MAJOR "0"
|
#define VERSION_MAJOR "0"
|
||||||
#define VERSION_MINOR "9"
|
#define VERSION_MINOR "9"
|
||||||
#define VERSION_REVISION "106"
|
#define VERSION_REVISION "107"
|
||||||
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
|
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
|
||||||
// server info.
|
// server info.
|
||||||
#define RTMP_SIG_SRS_KEY "SRS"
|
#define RTMP_SIG_SRS_KEY "SRS"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue