diff --git a/trunk/configure b/trunk/configure index 732f2f6d8..9d4e4b5cc 100755 --- a/trunk/configure +++ b/trunk/configure @@ -82,7 +82,9 @@ LibSTfile="${LibSTRoot}/libst.a" MODULE_ID="CORE" MODULE_DEPENDS=() ModuleLibIncs=(${LibSTRoot}) -MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server" "srs_core_error" "srs_core_conn" "srs_core_conn_rtmp") +MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server" + "srs_core_error" "srs_core_conn" "srs_core_client" + "srs_core_rtmp") MODULE_DIR="src/core" . auto/modules.sh CORE_OBJS="${MODULE_OBJS[@]}" diff --git a/trunk/src/core/srs_core_client.cpp b/trunk/src/core/srs_core_client.cpp new file mode 100755 index 000000000..a162c20d8 --- /dev/null +++ b/trunk/src/core/srs_core_client.cpp @@ -0,0 +1,97 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013 winlin + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include + +#include + +#include +#include +#include + +SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd) + : SrsConnection(srs_server, client_stfd) +{ + ip = NULL; + rtmp = new SrsRtmp(client_stfd); +} + +SrsClient::~SrsClient() +{ + if (ip) { + delete[] ip; + ip = NULL; + } + + if (rtmp) { + delete rtmp; + rtmp = NULL; + } +} + +int SrsClient::do_cycle() +{ + int ret = ERROR_SUCCESS; + + if ((ret = get_peer_ip()) != ERROR_SUCCESS) { + return ret; + } + + return ret; +} + +int SrsClient::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; + SrsError("discovery client information failed. ret=%d", ret); + return ret; + } + SrsVerbose("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; + SrsError("convert client information failed. ret=%d", ret); + return ret; + } + SrsVerbose("get peer ip of client ip=%s, fd=%d", buf, fd); + + ip = new char[strlen(buf) + 1]; + strcpy(ip, buf); + + SrsInfo("get peer ip success. ip=%s, fd=%d", ip, fd); + + return ret; +} + diff --git a/trunk/src/core/srs_core_client.hpp b/trunk/src/core/srs_core_client.hpp new file mode 100755 index 000000000..c4d9fce10 --- /dev/null +++ b/trunk/src/core/srs_core_client.hpp @@ -0,0 +1,50 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013 winlin + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef SRS_CORE_CLIENT_HPP +#define SRS_CORE_CLIENT_HPP + +/* +#include +*/ + +#include + +#include + +class SrsRtmp; +class SrsClient : public SrsConnection +{ +private: + char* ip; + SrsRtmp* rtmp; +public: + SrsClient(SrsServer* srs_server, st_netfd_t client_stfd); + virtual ~SrsClient(); +protected: + virtual int do_cycle(); +private: + virtual int get_peer_ip(); +}; + +#endif \ No newline at end of file diff --git a/trunk/src/core/srs_core_conn.hpp b/trunk/src/core/srs_core_conn.hpp index 8cf2b1a20..03c47a2a6 100755 --- a/trunk/src/core/srs_core_conn.hpp +++ b/trunk/src/core/srs_core_conn.hpp @@ -35,7 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. class SrsServer; class SrsConnection { -private: +protected: SrsServer* server; st_netfd_t stfd; public: diff --git a/trunk/src/core/srs_core_error.hpp b/trunk/src/core/srs_core_error.hpp index a6d78fa2e..15d2b2142 100755 --- a/trunk/src/core/srs_core_error.hpp +++ b/trunk/src/core/srs_core_error.hpp @@ -43,5 +43,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define ERROR_SOCKET_BIND 202 #define ERROR_SOCKET_LISTEN 203 #define ERROR_SOCKET_CLOSED 204 +#define ERROR_SOCKET_GET_PEER_NAME 205 +#define ERROR_SOCKET_GET_PEER_IP 206 #endif \ No newline at end of file diff --git a/trunk/src/core/srs_core_conn_rtmp.cpp b/trunk/src/core/srs_core_rtmp.cpp similarity index 75% rename from trunk/src/core/srs_core_conn_rtmp.cpp rename to trunk/src/core/srs_core_rtmp.cpp index 0c15f0dff..352f0baf9 100755 --- a/trunk/src/core/srs_core_conn_rtmp.cpp +++ b/trunk/src/core/srs_core_rtmp.cpp @@ -21,22 +21,13 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include -#include - -SrsRtmpConnection::SrsRtmpConnection(SrsServer* srs_server, st_netfd_t client_stfd) - : SrsConnection(srs_server, client_stfd) +SrsRtmp::SrsRtmp(st_netfd_t client_stfd) { + stfd = client_stfd; } -SrsRtmpConnection::~SrsRtmpConnection() +SrsRtmp::~SrsRtmp() { } - -int SrsRtmpConnection::do_cycle() -{ - int ret = ERROR_SUCCESS; - return ret; -} - diff --git a/trunk/src/core/srs_core_conn_rtmp.hpp b/trunk/src/core/srs_core_rtmp.hpp similarity index 75% rename from trunk/src/core/srs_core_conn_rtmp.hpp rename to trunk/src/core/srs_core_rtmp.hpp index 6e5330012..0702e33cd 100755 --- a/trunk/src/core/srs_core_conn_rtmp.hpp +++ b/trunk/src/core/srs_core_rtmp.hpp @@ -21,22 +21,24 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef SRS_CORE_CONN_RTMP_HPP -#define SRS_CORE_CONN_RTMP_HPP +#ifndef SRS_CORE_RTMP_HPP +#define SRS_CORE_RTMP_HPP /* -#include +#include */ -#include +#include -class SrsRtmpConnection : public SrsConnection +#include + +class SrsRtmp { +private: + st_netfd_t stfd; public: - SrsRtmpConnection(SrsServer* srs_server, st_netfd_t client_stfd); - virtual ~SrsRtmpConnection(); -protected: - virtual int do_cycle(); + SrsRtmp(st_netfd_t client_stfd); + virtual ~SrsRtmp(); }; #endif \ No newline at end of file diff --git a/trunk/src/core/srs_core_server.cpp b/trunk/src/core/srs_core_server.cpp index 8546a503b..46867665c 100755 --- a/trunk/src/core/srs_core_server.cpp +++ b/trunk/src/core/srs_core_server.cpp @@ -33,7 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include -#include +#include #define SERVER_LISTEN_BACKLOG 10 @@ -76,7 +76,7 @@ int SrsServer::initialize() return ret; } -int SrsServer::start(int port) +int SrsServer::listen(int port) { int ret = ERROR_SUCCESS; @@ -106,7 +106,7 @@ int SrsServer::start(int port) } SrsVerbose("bind socket success. fd=%d", fd); - if (listen(fd, SERVER_LISTEN_BACKLOG) == -1) { + if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) { ret = ERROR_SOCKET_LISTEN; SrsError("listen socket error. ret=%d", ret); return ret; @@ -159,7 +159,7 @@ int SrsServer::accept_client(st_netfd_t client_stfd) { int ret = ERROR_SUCCESS; - SrsConnection* conn = new SrsRtmpConnection(this, client_stfd); + SrsConnection* conn = new SrsClient(this, client_stfd); // directly enqueue, the cycle thread will remove the client. conns.push_back(conn); diff --git a/trunk/src/core/srs_core_server.hpp b/trunk/src/core/srs_core_server.hpp index 599775765..e5007b46f 100755 --- a/trunk/src/core/srs_core_server.hpp +++ b/trunk/src/core/srs_core_server.hpp @@ -46,7 +46,7 @@ public: virtual ~SrsServer(); public: virtual int initialize(); - virtual int start(int port); + virtual int listen(int port); virtual int cycle(); virtual void remove(SrsConnection* conn); private: diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index 1e7524a22..5a1954077 100755 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -25,8 +25,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include -#include - int main(int /*argc*/, char** /*argv*/){ int ret = ERROR_SUCCESS; @@ -36,7 +34,7 @@ int main(int /*argc*/, char** /*argv*/){ return ret; } - if ((ret = server.start(19350)) != ERROR_SUCCESS) { + if ((ret = server.listen(19350)) != ERROR_SUCCESS) { return ret; } diff --git a/trunk/src/srs/srs.upp b/trunk/src/srs/srs.upp index 2badd3b79..bff184306 100755 --- a/trunk/src/srs/srs.upp +++ b/trunk/src/srs/srs.upp @@ -10,8 +10,10 @@ file ..\core\srs_core_server.cpp, ..\core\srs_core_conn.hpp, ..\core\srs_core_conn.cpp, - ..\core\srs_core_conn_rtmp.hpp, - ..\core\srs_core_conn_rtmp.cpp, + ..\core\srs_core_client.hpp, + ..\core\srs_core_client.cpp, + ..\core\srs_core_rtmp.hpp, + ..\core\srs_core_rtmp.cpp, ..\core\srs_core_log.hpp, ..\core\srs_core_log.cpp; mainconfig