winlin

fix bugs

@@ -410,7 +410,7 @@ int SrsRtmpConn::do_cycle() @@ -410,7 +410,7 @@ int SrsRtmpConn::do_cycle()
410 srs_info("discovery app success. schema=%s, vhost=%s, port=%s, app=%s", 410 srs_info("discovery app success. schema=%s, vhost=%s, port=%s, app=%s",
411 req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), req->app.c_str()); 411 req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), req->app.c_str());
412 412
413 - if (req->schema.empty() || req->vhost.empty() || req->app.empty()) { 413 + if (req->schema.empty() || req->vhost.empty() || req->port == 0 || req->app.empty()) {
414 ret = ERROR_RTMP_REQ_TCURL; 414 ret = ERROR_RTMP_REQ_TCURL;
415 srs_error("discovery tcUrl failed. " 415 srs_error("discovery tcUrl failed. "
416 "tcUrl=%s, schema=%s, vhost=%s, port=%d, app=%s, ret=%d", 416 "tcUrl=%s, schema=%s, vhost=%s, port=%d, app=%s, ret=%d",
@@ -968,11 +968,11 @@ int SrsServer::do_cycle() @@ -968,11 +968,11 @@ int SrsServer::do_cycle()
968 968
969 // the deamon thread, update the time cache 969 // the deamon thread, update the time cache
970 while (true) { 970 while (true) {
971 - if(handler && (ret = handler->on_cycle((int)conns.size())) != ERROR_SUCCESS){ 971 + if (handler && (ret = handler->on_cycle()) != ERROR_SUCCESS) {
972 srs_error("cycle handle failed. ret=%d", ret); 972 srs_error("cycle handle failed. ret=%d", ret);
973 return ret; 973 return ret;
974 } 974 }
975 - 975 +
976 // the interval in config. 976 // the interval in config.
977 int heartbeat_max_resolution = (int)(_srs_config->get_heartbeat_interval() / SRS_SYS_CYCLE_INTERVAL); 977 int heartbeat_max_resolution = (int)(_srs_config->get_heartbeat_interval() / SRS_SYS_CYCLE_INTERVAL);
978 978
@@ -1254,15 +1254,20 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd) @@ -1254,15 +1254,20 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
1254 int ret = ERROR_SUCCESS; 1254 int ret = ERROR_SUCCESS;
1255 1255
1256 int fd = st_netfd_fileno(client_stfd); 1256 int fd = st_netfd_fileno(client_stfd);
1257 - 1257 +
  1258 + // check connection limitation.
1258 int max_connections = _srs_config->get_max_connections(); 1259 int max_connections = _srs_config->get_max_connections();
  1260 + if (handler && (ret = handler->on_accept_client(max_connections, (int)conns.size()) != ERROR_SUCCESS)) {
  1261 + srs_error("handle accept client failed, drop client: "
  1262 + "clients=%d, max=%d, fd=%d. ret=%d", (int)conns.size(), max_connections, fd, ret);
  1263 + srs_close_stfd(client_stfd);
  1264 + return ERROR_SUCCESS;
  1265 + }
1259 if ((int)conns.size() >= max_connections) { 1266 if ((int)conns.size() >= max_connections) {
1260 srs_error("exceed the max connections, drop client: " 1267 srs_error("exceed the max connections, drop client: "
1261 "clients=%d, max=%d, fd=%d", (int)conns.size(), max_connections, fd); 1268 "clients=%d, max=%d, fd=%d", (int)conns.size(), max_connections, fd);
1262 -  
1263 srs_close_stfd(client_stfd); 1269 srs_close_stfd(client_stfd);
1264 -  
1265 - return ret; 1270 + return ERROR_SUCCESS;
1266 } 1271 }
1267 1272
1268 // avoid fd leak when fork. 1273 // avoid fd leak when fork.
@@ -225,7 +225,11 @@ public: @@ -225,7 +225,11 @@ public:
225 /** 225 /**
226 * do on_cycle while server doing cycle. 226 * do on_cycle while server doing cycle.
227 */ 227 */
228 - virtual int on_cycle(int connections) = 0; 228 + virtual int on_cycle() = 0;
  229 + /**
  230 + * callback the handler when got client.
  231 + */
  232 + virtual int on_accept_client(int conf_conns, int curr_conns) = 0;
229 }; 233 };
230 234
231 /** 235 /**
@@ -440,7 +440,7 @@ int SrsTcpClient::connect(string host, int port, int64_t timeout) @@ -440,7 +440,7 @@ int SrsTcpClient::connect(string host, int port, int64_t timeout)
440 440
441 // connect host. 441 // connect host.
442 if ((ret = srs_socket_connect(host, port, timeout, &stfd)) != ERROR_SUCCESS) { 442 if ((ret = srs_socket_connect(host, port, timeout, &stfd)) != ERROR_SUCCESS) {
443 - srs_error("mpegts: connect server %s:%d failed. ret=%d", host.c_str(), port, ret); 443 + srs_error("connect server %s:%d failed. ret=%d", host.c_str(), port, ret);
444 return ret; 444 return ret;
445 } 445 }
446 446
@@ -356,7 +356,9 @@ vector<string> srs_string_split(string str, string flag) @@ -356,7 +356,9 @@ vector<string> srs_string_split(string str, string flag)
356 string s = str; 356 string s = str;
357 357
358 while ((pos = s.find(flag)) != string::npos) { 358 while ((pos = s.find(flag)) != string::npos) {
359 - arr.push_back(s.substr(0, pos)); 359 + if (pos != 0) {
  360 + arr.push_back(s.substr(0, pos));
  361 + }
360 s = s.substr(pos + flag.length()); 362 s = s.substr(pos + flag.length());
361 } 363 }
362 364
@@ -406,7 +408,9 @@ vector<string> srs_string_split(string str, vector<string> flags) @@ -406,7 +408,9 @@ vector<string> srs_string_split(string str, vector<string> flags)
406 break; 408 break;
407 } 409 }
408 410
409 - arr.push_back(s.substr(0, pos)); 411 + if (pos != 0) {
  412 + arr.push_back(s.substr(0, pos));
  413 + }
410 s = s.substr(pos + flag.length()); 414 s = s.substr(pos + flag.length());
411 } 415 }
412 416
@@ -88,6 +88,8 @@ extern bool srs_string_starts_with(std::string str, std::string flag0, std::stri @@ -88,6 +88,8 @@ extern bool srs_string_starts_with(std::string str, std::string flag0, std::stri
88 extern bool srs_string_starts_with(std::string str, std::string flag0, std::string flag1, std::string flag2, std::string flag3); 88 extern bool srs_string_starts_with(std::string str, std::string flag0, std::string flag1, std::string flag2, std::string flag3);
89 // whether string contains with 89 // whether string contains with
90 extern bool srs_string_contains(std::string str, std::string flag); 90 extern bool srs_string_contains(std::string str, std::string flag);
  91 +// find the min match in str for flags.
  92 +extern std::string srs_string_min_match(std::string str, std::vector<std::string> flags);
91 // split the string by flag to array. 93 // split the string by flag to array.
92 extern std::vector<std::string> srs_string_split(std::string str, std::string flag); 94 extern std::vector<std::string> srs_string_split(std::string str, std::string flag);
93 extern std::vector<std::string> srs_string_split(std::string str, std::vector<std::string> flags); 95 extern std::vector<std::string> srs_string_split(std::string str, std::vector<std::string> flags);
@@ -97,9 +99,9 @@ extern int srs_create_dir_recursively(std::string dir); @@ -97,9 +99,9 @@ extern int srs_create_dir_recursively(std::string dir);
97 99
98 // whether path exists. 100 // whether path exists.
99 extern bool srs_path_exists(std::string path); 101 extern bool srs_path_exists(std::string path);
100 -// get the dirname of path, for instance, filename("/live/livestream")="/live" 102 +// get the dirname of path, for instance, dirname("/live/livestream")="/live"
101 extern std::string srs_path_dirname(std::string path); 103 extern std::string srs_path_dirname(std::string path);
102 -// get the basename of path, for instance, filename("/live/livestream")="livestream" 104 +// get the basename of path, for instance, basename("/live/livestream")="livestream"
103 extern std::string srs_path_basename(std::string path); 105 extern std::string srs_path_basename(std::string path);
104 // get the filename of path, for instance, filename("livestream.flv")="livestream" 106 // get the filename of path, for instance, filename("livestream.flv")="livestream"
105 extern std::string srs_path_filename(std::string path); 107 extern std::string srs_path_filename(std::string path);