diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index 2395a0d96..3e0352231 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -23,12 +23,15 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#include + #include #include #include SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd) { + ip = NULL; server = srs_server; stfd = client_stfd; connection_id = 0; @@ -36,6 +39,7 @@ SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd) SrsConnection::~SrsConnection() { + srs_freepa(ip); srs_close_stfd(stfd); } @@ -53,6 +57,41 @@ int SrsConnection::start() return ret; } +int SrsConnection::get_peer_ip() +{ + int ret = ERROR_SUCCESS; + + int fd = st_netfd_fileno(stfd); + + // discovery client information + sockaddr_in addr; + socklen_t addrlen = sizeof(addr); + if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) { + ret = ERROR_SOCKET_GET_PEER_NAME; + srs_error("discovery client information failed. ret=%d", ret); + return ret; + } + srs_verbose("get peer name success."); + + // ip v4 or v6 + char buf[INET6_ADDRSTRLEN]; + memset(buf, 0, sizeof(buf)); + + if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) { + ret = ERROR_SOCKET_GET_PEER_IP; + srs_error("convert client information failed. ret=%d", ret); + return ret; + } + srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd); + + ip = new char[strlen(buf) + 1]; + strcpy(ip, buf); + + srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd); + + return ret; +} + void SrsConnection::cycle() { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_conn.hpp b/trunk/src/app/srs_app_conn.hpp index c2d528329..07b2ad9d2 100644 --- a/trunk/src/app/srs_app_conn.hpp +++ b/trunk/src/app/srs_app_conn.hpp @@ -36,6 +36,7 @@ class SrsServer; class SrsConnection { protected: + char* ip; SrsServer* server; st_netfd_t stfd; int connection_id; @@ -46,6 +47,8 @@ public: virtual int start(); protected: virtual int do_cycle() = 0; +protected: + virtual int get_peer_ip(); private: virtual void cycle(); static void* cycle_thread(void* arg); diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 369ffa04c..71a0c47b1 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -23,8 +23,27 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include -SrsHttpApi::SrsHttpApi() { +#include +#include + +SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd) + : SrsConnection(srs_server, client_stfd) +{ } -SrsHttpApi::~SrsHttpApi() { +SrsHttpApi::~SrsHttpApi() +{ +} + +int SrsHttpApi::do_cycle() +{ + int ret = ERROR_SUCCESS; + + if ((ret = get_peer_ip()) != ERROR_SUCCESS) { + srs_error("get peer ip failed. ret=%d", ret); + return ret; + } + srs_trace("api get peer ip success. ip=%s", ip); + + return ret; } diff --git a/trunk/src/app/srs_app_http_api.hpp b/trunk/src/app/srs_app_http_api.hpp index c77393458..b69e1d0f8 100644 --- a/trunk/src/app/srs_app_http_api.hpp +++ b/trunk/src/app/srs_app_http_api.hpp @@ -30,11 +30,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include -class SrsHttpApi +#include +#include + +class SrsHttpApi : public SrsConnection { public: - SrsHttpApi(); + SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd); virtual ~SrsHttpApi(); +protected: + virtual int do_cycle(); }; #endif diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 256a043c1..529aa1f53 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -22,3 +22,28 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include + +#include +#include + +SrsHttpConn::SrsHttpConn(SrsServer* srs_server, st_netfd_t client_stfd) + : SrsConnection(srs_server, client_stfd) +{ +} + +SrsHttpConn::~SrsHttpConn() +{ +} + +int SrsHttpConn::do_cycle() +{ + int ret = ERROR_SUCCESS; + + if ((ret = get_peer_ip()) != ERROR_SUCCESS) { + srs_error("get peer ip failed. ret=%d", ret); + return ret; + } + srs_trace("http get peer ip success. ip=%s", ip); + + return ret; +} diff --git a/trunk/src/app/srs_app_http_conn.hpp b/trunk/src/app/srs_app_http_conn.hpp index db6a805d1..7ce104d77 100644 --- a/trunk/src/app/srs_app_http_conn.hpp +++ b/trunk/src/app/srs_app_http_conn.hpp @@ -30,11 +30,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include -class SrsHttpConn +#include +#include + +class SrsHttpConn : public SrsConnection { public: - SrsHttpConn(); + SrsHttpConn(SrsServer* srs_server, st_netfd_t client_stfd); virtual ~SrsHttpConn(); +protected: + virtual int do_cycle(); }; #endif diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 89084791c..faf3dafdd 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -23,7 +23,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include -#include #include using namespace std; @@ -46,7 +45,6 @@ using namespace std; SrsRtmpConn::SrsRtmpConn(SrsServer* srs_server, st_netfd_t client_stfd) : SrsConnection(srs_server, client_stfd) { - ip = NULL; req = new SrsRequest(); res = new SrsResponse(); skt = new SrsSocket(client_stfd); @@ -64,7 +62,6 @@ SrsRtmpConn::~SrsRtmpConn() { _srs_config->unsubscribe(this); - srs_freepa(ip); srs_freep(req); srs_freep(res); srs_freep(rtmp); @@ -85,7 +82,7 @@ int SrsRtmpConn::do_cycle() srs_error("get peer ip failed. ret=%d", ret); return ret; } - srs_trace("get peer ip success. ip=%s, send_to=%"PRId64", recv_to=%"PRId64"", + srs_trace("rtmp get peer ip success. ip=%s, send_to=%"PRId64"us, recv_to=%"PRId64"us", ip, SRS_SEND_TIMEOUT_US, SRS_RECV_TIMEOUT_US); rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US); @@ -639,41 +636,6 @@ int SrsRtmpConn::process_publish_message(SrsSource* source, SrsCommonMessage* ms return ret; } -int SrsRtmpConn::get_peer_ip() -{ - int ret = ERROR_SUCCESS; - - int fd = st_netfd_fileno(stfd); - - // discovery client information - sockaddr_in addr; - socklen_t addrlen = sizeof(addr); - if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) { - ret = ERROR_SOCKET_GET_PEER_NAME; - srs_error("discovery client information failed. ret=%d", ret); - return ret; - } - srs_verbose("get peer name success."); - - // ip v4 or v6 - char buf[INET6_ADDRSTRLEN]; - memset(buf, 0, sizeof(buf)); - - if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) { - ret = ERROR_SOCKET_GET_PEER_IP; - srs_error("convert client information failed. ret=%d", ret); - return ret; - } - srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd); - - ip = new char[strlen(buf) + 1]; - strcpy(ip, buf); - - srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd); - - return ret; -} - int SrsRtmpConn::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg) { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_rtmp_conn.hpp b/trunk/src/app/srs_app_rtmp_conn.hpp index 7e93051e9..9285dec78 100644 --- a/trunk/src/app/srs_app_rtmp_conn.hpp +++ b/trunk/src/app/srs_app_rtmp_conn.hpp @@ -53,7 +53,6 @@ class SrsBandwidth; class SrsRtmpConn : public SrsConnection, public ISrsReloadHandler { private: - char* ip; SrsRequest* req; SrsResponse* res; SrsSocket* skt; @@ -81,7 +80,6 @@ private: virtual int fmle_publish(SrsSource* source); virtual int flash_publish(SrsSource* source); virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg); - virtual int get_peer_ip(); virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg); private: virtual int on_connect(); diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 888d72aba..518f0a211 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -38,6 +38,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include +#include +#include #define SERVER_LISTEN_BACKLOG 512 #define SRS_TIME_RESOLUTION_MS 500 @@ -436,7 +438,9 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd) if (type == SrsListenerRtmpStream) { conn = new SrsRtmpConn(this, client_stfd); } else if (type == SrsListenerHttpApi) { + conn = new SrsHttpApi(this, client_stfd); } else if (type == SrsListenerHttpStream) { + conn = new SrsHttpConn(this, client_stfd); } else { // TODO: FIXME: handler others }