winlin

get peer ip and add rtmp class

@@ -82,7 +82,9 @@ LibSTfile="${LibSTRoot}/libst.a" @@ -82,7 +82,9 @@ LibSTfile="${LibSTRoot}/libst.a"
82 MODULE_ID="CORE" 82 MODULE_ID="CORE"
83 MODULE_DEPENDS=() 83 MODULE_DEPENDS=()
84 ModuleLibIncs=(${LibSTRoot}) 84 ModuleLibIncs=(${LibSTRoot})
85 -MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server" "srs_core_error" "srs_core_conn" "srs_core_conn_rtmp") 85 +MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server"
  86 + "srs_core_error" "srs_core_conn" "srs_core_client"
  87 + "srs_core_rtmp")
86 MODULE_DIR="src/core" . auto/modules.sh 88 MODULE_DIR="src/core" . auto/modules.sh
87 CORE_OBJS="${MODULE_OBJS[@]}" 89 CORE_OBJS="${MODULE_OBJS[@]}"
88 90
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#include <srs_core_client.hpp>
  25 +
  26 +#include <arpa/inet.h>
  27 +
  28 +#include <srs_core_error.hpp>
  29 +#include <srs_core_log.hpp>
  30 +#include <srs_core_rtmp.hpp>
  31 +
  32 +SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd)
  33 + : SrsConnection(srs_server, client_stfd)
  34 +{
  35 + ip = NULL;
  36 + rtmp = new SrsRtmp(client_stfd);
  37 +}
  38 +
  39 +SrsClient::~SrsClient()
  40 +{
  41 + if (ip) {
  42 + delete[] ip;
  43 + ip = NULL;
  44 + }
  45 +
  46 + if (rtmp) {
  47 + delete rtmp;
  48 + rtmp = NULL;
  49 + }
  50 +}
  51 +
  52 +int SrsClient::do_cycle()
  53 +{
  54 + int ret = ERROR_SUCCESS;
  55 +
  56 + if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
  57 + return ret;
  58 + }
  59 +
  60 + return ret;
  61 +}
  62 +
  63 +int SrsClient::get_peer_ip()
  64 +{
  65 + int ret = ERROR_SUCCESS;
  66 +
  67 + int fd = st_netfd_fileno(stfd);
  68 +
  69 + // discovery client information
  70 + sockaddr_in addr;
  71 + socklen_t addrlen = sizeof(addr);
  72 + if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
  73 + ret = ERROR_SOCKET_GET_PEER_NAME;
  74 + SrsError("discovery client information failed. ret=%d", ret);
  75 + return ret;
  76 + }
  77 + SrsVerbose("get peer name success.");
  78 +
  79 + // ip v4 or v6
  80 + char buf[INET6_ADDRSTRLEN];
  81 + memset(buf, 0, sizeof(buf));
  82 +
  83 + if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
  84 + ret = ERROR_SOCKET_GET_PEER_IP;
  85 + SrsError("convert client information failed. ret=%d", ret);
  86 + return ret;
  87 + }
  88 + SrsVerbose("get peer ip of client ip=%s, fd=%d", buf, fd);
  89 +
  90 + ip = new char[strlen(buf) + 1];
  91 + strcpy(ip, buf);
  92 +
  93 + SrsInfo("get peer ip success. ip=%s, fd=%d", ip, fd);
  94 +
  95 + return ret;
  96 +}
  97 +
@@ -21,22 +21,30 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN @@ -21,22 +21,30 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */ 22 */
23 23
24 -#ifndef SRS_CORE_CONN_RTMP_HPP  
25 -#define SRS_CORE_CONN_RTMP_HPP 24 +#ifndef SRS_CORE_CLIENT_HPP
  25 +#define SRS_CORE_CLIENT_HPP
26 26
27 /* 27 /*
28 -#include <srs_core_conn_rtmp.hpp> 28 +#include <srs_core_client.hpp>
29 */ 29 */
30 30
  31 +#include <srs_core.hpp>
  32 +
31 #include <srs_core_conn.hpp> 33 #include <srs_core_conn.hpp>
32 34
33 -class SrsRtmpConnection : public SrsConnection 35 +class SrsRtmp;
  36 +class SrsClient : public SrsConnection
34 { 37 {
  38 +private:
  39 + char* ip;
  40 + SrsRtmp* rtmp;
35 public: 41 public:
36 - SrsRtmpConnection(SrsServer* srs_server, st_netfd_t client_stfd);  
37 - virtual ~SrsRtmpConnection(); 42 + SrsClient(SrsServer* srs_server, st_netfd_t client_stfd);
  43 + virtual ~SrsClient();
38 protected: 44 protected:
39 virtual int do_cycle(); 45 virtual int do_cycle();
  46 +private:
  47 + virtual int get_peer_ip();
40 }; 48 };
41 49
42 #endif 50 #endif
@@ -35,7 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -35,7 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 class SrsServer; 35 class SrsServer;
36 class SrsConnection 36 class SrsConnection
37 { 37 {
38 -private: 38 +protected:
39 SrsServer* server; 39 SrsServer* server;
40 st_netfd_t stfd; 40 st_netfd_t stfd;
41 public: 41 public:
@@ -43,5 +43,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -43,5 +43,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 #define ERROR_SOCKET_BIND 202 43 #define ERROR_SOCKET_BIND 202
44 #define ERROR_SOCKET_LISTEN 203 44 #define ERROR_SOCKET_LISTEN 203
45 #define ERROR_SOCKET_CLOSED 204 45 #define ERROR_SOCKET_CLOSED 204
  46 +#define ERROR_SOCKET_GET_PEER_NAME 205
  47 +#define ERROR_SOCKET_GET_PEER_IP 206
46 48
47 #endif 49 #endif
@@ -21,22 +21,13 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN @@ -21,22 +21,13 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */ 22 */
23 23
24 -#include <srs_core_conn_rtmp.hpp> 24 +#include <srs_core_rtmp.hpp>
25 25
26 -#include <srs_core_error.hpp>  
27 -  
28 -SrsRtmpConnection::SrsRtmpConnection(SrsServer* srs_server, st_netfd_t client_stfd)  
29 - : SrsConnection(srs_server, client_stfd) 26 +SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
30 { 27 {
  28 + stfd = client_stfd;
31 } 29 }
32 30
33 -SrsRtmpConnection::~SrsRtmpConnection() 31 +SrsRtmp::~SrsRtmp()
34 { 32 {
35 } 33 }
36 -  
37 -int SrsRtmpConnection::do_cycle()  
38 -{  
39 - int ret = ERROR_SUCCESS;  
40 - return ret;  
41 -}  
42 -  
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#ifndef SRS_CORE_RTMP_HPP
  25 +#define SRS_CORE_RTMP_HPP
  26 +
  27 +/*
  28 +#include <srs_core_rtmp.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +#include <st.h>
  34 +
  35 +class SrsRtmp
  36 +{
  37 +private:
  38 + st_netfd_t stfd;
  39 +public:
  40 + SrsRtmp(st_netfd_t client_stfd);
  41 + virtual ~SrsRtmp();
  42 +};
  43 +
  44 +#endif
@@ -33,7 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -33,7 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 33
34 #include <srs_core_log.hpp> 34 #include <srs_core_log.hpp>
35 #include <srs_core_error.hpp> 35 #include <srs_core_error.hpp>
36 -#include <srs_core_conn_rtmp.hpp> 36 +#include <srs_core_client.hpp>
37 37
38 #define SERVER_LISTEN_BACKLOG 10 38 #define SERVER_LISTEN_BACKLOG 10
39 39
@@ -76,7 +76,7 @@ int SrsServer::initialize() @@ -76,7 +76,7 @@ int SrsServer::initialize()
76 return ret; 76 return ret;
77 } 77 }
78 78
79 -int SrsServer::start(int port) 79 +int SrsServer::listen(int port)
80 { 80 {
81 int ret = ERROR_SUCCESS; 81 int ret = ERROR_SUCCESS;
82 82
@@ -106,7 +106,7 @@ int SrsServer::start(int port) @@ -106,7 +106,7 @@ int SrsServer::start(int port)
106 } 106 }
107 SrsVerbose("bind socket success. fd=%d", fd); 107 SrsVerbose("bind socket success. fd=%d", fd);
108 108
109 - if (listen(fd, SERVER_LISTEN_BACKLOG) == -1) { 109 + if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
110 ret = ERROR_SOCKET_LISTEN; 110 ret = ERROR_SOCKET_LISTEN;
111 SrsError("listen socket error. ret=%d", ret); 111 SrsError("listen socket error. ret=%d", ret);
112 return ret; 112 return ret;
@@ -159,7 +159,7 @@ int SrsServer::accept_client(st_netfd_t client_stfd) @@ -159,7 +159,7 @@ int SrsServer::accept_client(st_netfd_t client_stfd)
159 { 159 {
160 int ret = ERROR_SUCCESS; 160 int ret = ERROR_SUCCESS;
161 161
162 - SrsConnection* conn = new SrsRtmpConnection(this, client_stfd); 162 + SrsConnection* conn = new SrsClient(this, client_stfd);
163 163
164 // directly enqueue, the cycle thread will remove the client. 164 // directly enqueue, the cycle thread will remove the client.
165 conns.push_back(conn); 165 conns.push_back(conn);
@@ -46,7 +46,7 @@ public: @@ -46,7 +46,7 @@ public:
46 virtual ~SrsServer(); 46 virtual ~SrsServer();
47 public: 47 public:
48 virtual int initialize(); 48 virtual int initialize();
49 - virtual int start(int port); 49 + virtual int listen(int port);
50 virtual int cycle(); 50 virtual int cycle();
51 virtual void remove(SrsConnection* conn); 51 virtual void remove(SrsConnection* conn);
52 private: 52 private:
@@ -25,8 +25,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -25,8 +25,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include <srs_core_error.hpp> 25 #include <srs_core_error.hpp>
26 #include <srs_core_server.hpp> 26 #include <srs_core_server.hpp>
27 27
28 -#include <unistd.h>  
29 -  
30 int main(int /*argc*/, char** /*argv*/){ 28 int main(int /*argc*/, char** /*argv*/){
31 int ret = ERROR_SUCCESS; 29 int ret = ERROR_SUCCESS;
32 30
@@ -36,7 +34,7 @@ int main(int /*argc*/, char** /*argv*/){ @@ -36,7 +34,7 @@ int main(int /*argc*/, char** /*argv*/){
36 return ret; 34 return ret;
37 } 35 }
38 36
39 - if ((ret = server.start(19350)) != ERROR_SUCCESS) { 37 + if ((ret = server.listen(19350)) != ERROR_SUCCESS) {
40 return ret; 38 return ret;
41 } 39 }
42 40
@@ -10,8 +10,10 @@ file @@ -10,8 +10,10 @@ file
10 ..\core\srs_core_server.cpp, 10 ..\core\srs_core_server.cpp,
11 ..\core\srs_core_conn.hpp, 11 ..\core\srs_core_conn.hpp,
12 ..\core\srs_core_conn.cpp, 12 ..\core\srs_core_conn.cpp,
13 - ..\core\srs_core_conn_rtmp.hpp,  
14 - ..\core\srs_core_conn_rtmp.cpp, 13 + ..\core\srs_core_client.hpp,
  14 + ..\core\srs_core_client.cpp,
  15 + ..\core\srs_core_rtmp.hpp,
  16 + ..\core\srs_core_rtmp.cpp,
15 ..\core\srs_core_log.hpp, 17 ..\core\srs_core_log.hpp,
16 ..\core\srs_core_log.cpp; 18 ..\core\srs_core_log.cpp;
17 mainconfig 19 mainconfig