正在显示
5 个修改的文件
包含
130 行增加
和
6 行删除
@@ -34,7 +34,7 @@ SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd) | @@ -34,7 +34,7 @@ SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd) | ||
34 | { | 34 | { |
35 | server = srs_server; | 35 | server = srs_server; |
36 | stfd = client_stfd; | 36 | stfd = client_stfd; |
37 | - connection_id = 0; | 37 | + |
38 | // the client thread should reap itself, | 38 | // the client thread should reap itself, |
39 | // so we never use joinable. | 39 | // so we never use joinable. |
40 | // TODO: FIXME: maybe other thread need to stop it. | 40 | // TODO: FIXME: maybe other thread need to stop it. |
@@ -57,7 +57,6 @@ int SrsConnection::cycle() | @@ -57,7 +57,6 @@ int SrsConnection::cycle() | ||
57 | int ret = ERROR_SUCCESS; | 57 | int ret = ERROR_SUCCESS; |
58 | 58 | ||
59 | _srs_context->generate_id(); | 59 | _srs_context->generate_id(); |
60 | - connection_id = _srs_context->get_id(); | ||
61 | ip = srs_get_peer_ip(st_netfd_fileno(stfd)); | 60 | ip = srs_get_peer_ip(st_netfd_fileno(stfd)); |
62 | 61 | ||
63 | ret = do_cycle(); | 62 | ret = do_cycle(); |
@@ -37,26 +37,78 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -37,26 +37,78 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
37 | #include <srs_app_kbps.hpp> | 37 | #include <srs_app_kbps.hpp> |
38 | 38 | ||
39 | class SrsServer; | 39 | class SrsServer; |
40 | + | ||
41 | +/** | ||
42 | +* the basic connection of SRS, | ||
43 | +* all connections accept from listener must extends from this base class, | ||
44 | +* server will add the connection to manager, and delete it when remove. | ||
45 | +*/ | ||
40 | class SrsConnection : public virtual ISrsThreadHandler, public virtual IKbpsDelta | 46 | class SrsConnection : public virtual ISrsThreadHandler, public virtual IKbpsDelta |
41 | { | 47 | { |
42 | private: | 48 | private: |
49 | + /** | ||
50 | + * each connection start a green thread, | ||
51 | + * when thread stop, the connection will be delete by server. | ||
52 | + */ | ||
43 | SrsThread* pthread; | 53 | SrsThread* pthread; |
44 | protected: | 54 | protected: |
55 | + /** | ||
56 | + * the server object to manage the connection. | ||
57 | + */ | ||
45 | SrsServer* server; | 58 | SrsServer* server; |
59 | + /** | ||
60 | + * the underlayer st fd handler. | ||
61 | + */ | ||
46 | st_netfd_t stfd; | 62 | st_netfd_t stfd; |
47 | - int connection_id; | 63 | + /** |
64 | + * the ip of client. | ||
65 | + */ | ||
48 | std::string ip; | 66 | std::string ip; |
49 | public: | 67 | public: |
50 | SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd); | 68 | SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd); |
51 | virtual ~SrsConnection(); | 69 | virtual ~SrsConnection(); |
52 | public: | 70 | public: |
71 | + /** | ||
72 | + * start the client green thread. | ||
73 | + * when server get a client from listener, | ||
74 | + * 1. server will create an concrete connection(for instance, RTMP connection), | ||
75 | + * 2. then add connection to its connection manager, | ||
76 | + * 3. start the client thread by invoke this start() | ||
77 | + * when client cycle thread stop, invoke the on_thread_stop(), which will use server | ||
78 | + * to remove the client by server->remove(this). | ||
79 | + */ | ||
53 | virtual int start(); | 80 | virtual int start(); |
81 | + /** | ||
82 | + * the thread cycle function, | ||
83 | + * when serve connection completed, terminate the loop which will terminate the thread, | ||
84 | + * thread will invoke the on_thread_stop() when it terminated. | ||
85 | + */ | ||
54 | virtual int cycle(); | 86 | virtual int cycle(); |
87 | + /** | ||
88 | + * when the thread cycle finished, thread will invoke the on_thread_stop(), | ||
89 | + * which will remove self from server, server will remove the connection from manager | ||
90 | + * then delete the connection. | ||
91 | + */ | ||
55 | virtual void on_thread_stop(); | 92 | virtual void on_thread_stop(); |
56 | public: | 93 | public: |
94 | + /** | ||
95 | + * when server to get the kbps of connection, | ||
96 | + * it cannot wait the connection terminated then get the kbps, | ||
97 | + * it must sample the kbps every some interval, for instance, 9s to sample all connections kbps, | ||
98 | + * all connections will extends from IKbpsDelta which provides the bytes delta, | ||
99 | + * while the delta must be update by the sample which invoke by the kbps_resample(). | ||
100 | + */ | ||
57 | virtual void kbps_resample() = 0; | 101 | virtual void kbps_resample() = 0; |
58 | protected: | 102 | protected: |
103 | + /** | ||
104 | + * for concrete connection to do the cycle. | ||
105 | + */ | ||
59 | virtual int do_cycle() = 0; | 106 | virtual int do_cycle() = 0; |
107 | +private: | ||
108 | + /** | ||
109 | + * when delete the connection, stop the connection, | ||
110 | + * close the underlayer socket, delete the thread. | ||
111 | + */ | ||
60 | virtual void stop(); | 112 | virtual void stop(); |
61 | }; | 113 | }; |
62 | 114 |
@@ -94,8 +94,6 @@ SrsRtmpConn::~SrsRtmpConn() | @@ -94,8 +94,6 @@ SrsRtmpConn::~SrsRtmpConn() | ||
94 | { | 94 | { |
95 | _srs_config->unsubscribe(this); | 95 | _srs_config->unsubscribe(this); |
96 | 96 | ||
97 | - stop(); | ||
98 | - | ||
99 | srs_freep(req); | 97 | srs_freep(req); |
100 | srs_freep(res); | 98 | srs_freep(res); |
101 | srs_freep(rtmp); | 99 | srs_freep(rtmp); |
@@ -993,6 +991,7 @@ int SrsRtmpConn::http_hooks_on_connect() | @@ -993,6 +991,7 @@ int SrsRtmpConn::http_hooks_on_connect() | ||
993 | return ret; | 991 | return ret; |
994 | } | 992 | } |
995 | 993 | ||
994 | + int connection_id = _srs_context->get_id(); | ||
996 | for (int i = 0; i < (int)on_connect->args.size(); i++) { | 995 | for (int i = 0; i < (int)on_connect->args.size(); i++) { |
997 | std::string url = on_connect->args.at(i); | 996 | std::string url = on_connect->args.at(i); |
998 | if ((ret = SrsHttpHooks::on_connect(url, connection_id, ip, req)) != ERROR_SUCCESS) { | 997 | if ((ret = SrsHttpHooks::on_connect(url, connection_id, ip, req)) != ERROR_SUCCESS) { |
@@ -1016,6 +1015,7 @@ void SrsRtmpConn::http_hooks_on_close() | @@ -1016,6 +1015,7 @@ void SrsRtmpConn::http_hooks_on_close() | ||
1016 | return; | 1015 | return; |
1017 | } | 1016 | } |
1018 | 1017 | ||
1018 | + int connection_id = _srs_context->get_id(); | ||
1019 | for (int i = 0; i < (int)on_close->args.size(); i++) { | 1019 | for (int i = 0; i < (int)on_close->args.size(); i++) { |
1020 | std::string url = on_close->args.at(i); | 1020 | std::string url = on_close->args.at(i); |
1021 | SrsHttpHooks::on_close(url, connection_id, ip, req); | 1021 | SrsHttpHooks::on_close(url, connection_id, ip, req); |
@@ -1035,6 +1035,7 @@ int SrsRtmpConn::http_hooks_on_publish() | @@ -1035,6 +1035,7 @@ int SrsRtmpConn::http_hooks_on_publish() | ||
1035 | return ret; | 1035 | return ret; |
1036 | } | 1036 | } |
1037 | 1037 | ||
1038 | + int connection_id = _srs_context->get_id(); | ||
1038 | for (int i = 0; i < (int)on_publish->args.size(); i++) { | 1039 | for (int i = 0; i < (int)on_publish->args.size(); i++) { |
1039 | std::string url = on_publish->args.at(i); | 1040 | std::string url = on_publish->args.at(i); |
1040 | if ((ret = SrsHttpHooks::on_publish(url, connection_id, ip, req)) != ERROR_SUCCESS) { | 1041 | if ((ret = SrsHttpHooks::on_publish(url, connection_id, ip, req)) != ERROR_SUCCESS) { |
@@ -1058,6 +1059,7 @@ void SrsRtmpConn::http_hooks_on_unpublish() | @@ -1058,6 +1059,7 @@ void SrsRtmpConn::http_hooks_on_unpublish() | ||
1058 | return; | 1059 | return; |
1059 | } | 1060 | } |
1060 | 1061 | ||
1062 | + int connection_id = _srs_context->get_id(); | ||
1061 | for (int i = 0; i < (int)on_unpublish->args.size(); i++) { | 1063 | for (int i = 0; i < (int)on_unpublish->args.size(); i++) { |
1062 | std::string url = on_unpublish->args.at(i); | 1064 | std::string url = on_unpublish->args.at(i); |
1063 | SrsHttpHooks::on_unpublish(url, connection_id, ip, req); | 1065 | SrsHttpHooks::on_unpublish(url, connection_id, ip, req); |
@@ -1077,6 +1079,7 @@ int SrsRtmpConn::http_hooks_on_play() | @@ -1077,6 +1079,7 @@ int SrsRtmpConn::http_hooks_on_play() | ||
1077 | return ret; | 1079 | return ret; |
1078 | } | 1080 | } |
1079 | 1081 | ||
1082 | + int connection_id = _srs_context->get_id(); | ||
1080 | for (int i = 0; i < (int)on_play->args.size(); i++) { | 1083 | for (int i = 0; i < (int)on_play->args.size(); i++) { |
1081 | std::string url = on_play->args.at(i); | 1084 | std::string url = on_play->args.at(i); |
1082 | if ((ret = SrsHttpHooks::on_play(url, connection_id, ip, req)) != ERROR_SUCCESS) { | 1085 | if ((ret = SrsHttpHooks::on_play(url, connection_id, ip, req)) != ERROR_SUCCESS) { |
@@ -1100,6 +1103,7 @@ void SrsRtmpConn::http_hooks_on_stop() | @@ -1100,6 +1103,7 @@ void SrsRtmpConn::http_hooks_on_stop() | ||
1100 | return; | 1103 | return; |
1101 | } | 1104 | } |
1102 | 1105 | ||
1106 | + int connection_id = _srs_context->get_id(); | ||
1103 | for (int i = 0; i < (int)on_stop->args.size(); i++) { | 1107 | for (int i = 0; i < (int)on_stop->args.size(); i++) { |
1104 | std::string url = on_stop->args.at(i); | 1108 | std::string url = on_stop->args.at(i); |
1105 | SrsHttpHooks::on_stop(url, connection_id, ip, req); | 1109 | SrsHttpHooks::on_stop(url, connection_id, ip, req); |
@@ -857,6 +857,7 @@ void SrsServer::resample_kbps(SrsConnection* conn, bool do_resample) | @@ -857,6 +857,7 @@ void SrsServer::resample_kbps(SrsConnection* conn, bool do_resample) | ||
857 | 857 | ||
858 | kbps->add_delta(conn); | 858 | kbps->add_delta(conn); |
859 | 859 | ||
860 | + // resample for server. | ||
860 | if (do_resample) { | 861 | if (do_resample) { |
861 | kbps->sample(); | 862 | kbps->sample(); |
862 | } | 863 | } |
@@ -128,17 +128,45 @@ private: | @@ -128,17 +128,45 @@ private: | ||
128 | SrsIngester* ingester; | 128 | SrsIngester* ingester; |
129 | #endif | 129 | #endif |
130 | private: | 130 | private: |
131 | + /** | ||
132 | + * the pid file fd, lock the file write when server is running. | ||
133 | + * @remark the init.d script should cleanup the pid file, when stop service, | ||
134 | + * for the server never delete the file; when system startup, the pid in pid file | ||
135 | + * maybe valid but the process is not SRS, the init.d script will never start server. | ||
136 | + */ | ||
131 | int pid_fd; | 137 | int pid_fd; |
138 | + /** | ||
139 | + * all connections, connection manager | ||
140 | + */ | ||
132 | std::vector<SrsConnection*> conns; | 141 | std::vector<SrsConnection*> conns; |
142 | + /** | ||
143 | + * all listners, listener manager. | ||
144 | + */ | ||
133 | std::vector<SrsListener*> listeners; | 145 | std::vector<SrsListener*> listeners; |
146 | + /** | ||
147 | + * signal manager which convert gignal to io message. | ||
148 | + */ | ||
134 | SrsSignalManager* signal_manager; | 149 | SrsSignalManager* signal_manager; |
150 | + /** | ||
151 | + * server total kbps. | ||
152 | + */ | ||
135 | SrsKbps* kbps; | 153 | SrsKbps* kbps; |
154 | + /** | ||
155 | + * user send the signal, convert to variable. | ||
156 | + */ | ||
136 | bool signal_reload; | 157 | bool signal_reload; |
137 | bool signal_gmc_stop; | 158 | bool signal_gmc_stop; |
138 | public: | 159 | public: |
139 | SrsServer(); | 160 | SrsServer(); |
140 | virtual ~SrsServer(); | 161 | virtual ~SrsServer(); |
162 | +public: | ||
163 | + /** | ||
164 | + * the destroy is for gmc to analysis the memory leak, | ||
165 | + * if not destroy global/static data, the gmc will warning memory leak. | ||
166 | + * in service, server never destroy, directly exit when restart. | ||
167 | + */ | ||
141 | virtual void destroy(); | 168 | virtual void destroy(); |
169 | +// server startup workflow, @see run_master() | ||
142 | public: | 170 | public: |
143 | virtual int initialize(); | 171 | virtual int initialize(); |
144 | virtual int initialize_signal(); | 172 | virtual int initialize_signal(); |
@@ -148,18 +176,58 @@ public: | @@ -148,18 +176,58 @@ public: | ||
148 | virtual int register_signal(); | 176 | virtual int register_signal(); |
149 | virtual int ingest(); | 177 | virtual int ingest(); |
150 | virtual int cycle(); | 178 | virtual int cycle(); |
179 | +// server utility | ||
180 | +public: | ||
181 | + /** | ||
182 | + * callback for connection to remove itself. | ||
183 | + * when connection thread cycle terminated, callback this to delete connection. | ||
184 | + * @see SrsConnection.on_thread_stop(). | ||
185 | + */ | ||
151 | virtual void remove(SrsConnection* conn); | 186 | virtual void remove(SrsConnection* conn); |
187 | + /** | ||
188 | + * callback for signal manager got a signal. | ||
189 | + * the signal manager convert signal to io message, | ||
190 | + * whatever, we will got the signo like the orignal signal(int signo) handler. | ||
191 | + * @remark, direclty exit for SIGTERM. | ||
192 | + * @remark, do reload for SIGNAL_RELOAD. | ||
193 | + * @remark, for SIGINT and SIGUSR2: | ||
194 | + * no gmc, directly exit. | ||
195 | + * for gmc, set the variable signal_gmc_stop, the cycle will return and cleanup for gmc. | ||
196 | + */ | ||
152 | virtual void on_signal(int signo); | 197 | virtual void on_signal(int signo); |
153 | private: | 198 | private: |
199 | + /** | ||
200 | + * the server thread main cycle, | ||
201 | + * update the global static data, for instance, the current time, | ||
202 | + * the cpu/mem/network statistic. | ||
203 | + */ | ||
154 | virtual int do_cycle(); | 204 | virtual int do_cycle(); |
205 | + /** | ||
206 | + * listen at specified protocol. | ||
207 | + */ | ||
155 | virtual int listen_rtmp(); | 208 | virtual int listen_rtmp(); |
156 | virtual int listen_http_api(); | 209 | virtual int listen_http_api(); |
157 | virtual int listen_http_stream(); | 210 | virtual int listen_http_stream(); |
211 | + /** | ||
212 | + * close the listeners for specified type, | ||
213 | + * remove the listen object from manager. | ||
214 | + */ | ||
158 | virtual void close_listeners(SrsListenerType type); | 215 | virtual void close_listeners(SrsListenerType type); |
159 | - // resample the server kbps. resample all when conn is NULL. | 216 | + /** |
217 | + * resample the server kbps. | ||
218 | + * if conn is NULL, resample all connections delta, then calc the total kbps. | ||
219 | + * @param conn, the connection to do resample the kbps. NULL to resample all connections. | ||
220 | + * @param do_resample, whether resample the server kbps. always false when sample a connection. | ||
221 | + */ | ||
160 | virtual void resample_kbps(SrsConnection* conn, bool do_resample = true); | 222 | virtual void resample_kbps(SrsConnection* conn, bool do_resample = true); |
161 | // internal only | 223 | // internal only |
162 | public: | 224 | public: |
225 | + /** | ||
226 | + * when listener got a fd, notice server to accept it. | ||
227 | + * @param type, the client type, used to create concrete connection, | ||
228 | + * for instance RTMP connection to serve client. | ||
229 | + * @param client_stfd, the client fd in st boxed, the underlayer fd. | ||
230 | + */ | ||
163 | virtual int accept_client(SrsListenerType type, st_netfd_t client_stfd); | 231 | virtual int accept_client(SrsListenerType type, st_netfd_t client_stfd); |
164 | // interface ISrsThreadHandler. | 232 | // interface ISrsThreadHandler. |
165 | public: | 233 | public: |
-
请 注册 或 登录 后发表评论