Blame view

trunk/src/app/srs_app_server.hpp 7.3 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24 25
#ifndef SRS_APP_SERVER_HPP
#define SRS_APP_SERVER_HPP
winlin authored
26 27

/*
28
#include <srs_app_server.hpp>
winlin authored
29 30 31 32 33 34
*/

#include <srs_core.hpp>

#include <vector>
35 36 37
#include <srs_app_st.hpp>
#include <srs_app_reload.hpp>
#include <srs_app_thread.hpp>
winlin authored
38 39 40

class SrsServer;
class SrsConnection;
winlin authored
41
class SrsHttpHandler;
42
class SrsIngester;
43
class SrsHttpHeartbeat;
44
class SrsKbps;
winlin authored
45
46 47
// listener type for server to identify the connection,
// that is, use different type to process the connection.
winlin authored
48 49
enum SrsListenerType 
{
50 51 52 53 54 55
    // RTMP client,
    SrsListenerRtmpStream   = 0,
    // HTTP api,
    SrsListenerHttpApi      = 1,
    // HTTP stream, HDS/HLS/DASH
    SrsListenerHttpStream   = 2
winlin authored
56 57
};
58
class SrsListener : public ISrsThreadHandler
winlin authored
59 60
{
public:
61
    SrsListenerType _type;
winlin authored
62
private:
63 64
    int fd;
    st_netfd_t stfd;
65 66
    int _port;
    SrsServer* _server;
67
    SrsThread* pthread;
winlin authored
68
public:
69
    SrsListener(SrsServer* server, SrsListenerType type);
70
    virtual ~SrsListener();
winlin authored
71
public:
72
    virtual SrsListenerType type();
73
    virtual int listen(int port);
74 75
// interface ISrsThreadHandler.
public:
76
    virtual void on_thread_start();
77
    virtual int cycle();
winlin authored
78 79
};
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
* convert signal to io,
* @see: st-1.9/docs/notes.html
*/
class SrsSignalManager : public ISrsThreadHandler
{
private:
    /* Per-process pipe which is used as a signal queue. */
    /* Up to PIPE_BUF/sizeof(int) signals can be queued up. */
    int sig_pipe[2];
    st_netfd_t signal_read_stfd;
private:
    SrsServer* _server;
    SrsThread* pthread;
public:
    SrsSignalManager(SrsServer* server);
    virtual ~SrsSignalManager();
public:
    virtual int initialize();
    virtual int start();
// interface ISrsThreadHandler.
public:
    virtual int cycle();
private:
    // global singleton instance
    static SrsSignalManager* instance;
    /* Signal catching function. */
    /* Converts signal event to I/O event. */
    static void sig_catcher(int signo);
};
111 112 113 114
/**
* SRS RTMP server, initialize and listen, 
* start connection service thread, destroy client.
*/
115
class SrsServer : public ISrsReloadHandler
winlin authored
116 117
{
private:
118
#ifdef SRS_AUTO_HTTP_API
winlin authored
119 120
    SrsHttpHandler* http_api_handler;
#endif
121
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
122 123
    SrsHttpHandler* http_stream_handler;
#endif
124 125 126
#ifdef SRS_AUTO_HTTP_PARSER
    SrsHttpHeartbeat* http_heartbeat;
#endif
127
#ifdef SRS_AUTO_INGEST
128 129
    SrsIngester* ingester;
#endif
winlin authored
130
private:
winlin authored
131 132 133 134 135 136
    /**
    * the pid file fd, lock the file write when server is running.
    * @remark the init.d script should cleanup the pid file, when stop service,
    *       for the server never delete the file; when system startup, the pid in pid file
    *       maybe valid but the process is not SRS, the init.d script will never start server.
    */
winlin authored
137
    int pid_fd;
winlin authored
138 139 140
    /**
    * all connections, connection manager
    */
141
    std::vector<SrsConnection*> conns;
winlin authored
142 143 144
    /**
    * all listners, listener manager.
    */
145
    std::vector<SrsListener*> listeners;
winlin authored
146 147 148
    /**
    * signal manager which convert gignal to io message.
    */
149
    SrsSignalManager* signal_manager;
winlin authored
150 151 152
    /**
    * server total kbps.
    */
153
    SrsKbps* kbps;
winlin authored
154 155 156
    /**
    * user send the signal, convert to variable.
    */
157 158
    bool signal_reload;
    bool signal_gmc_stop;
winlin authored
159
public:
160 161
    SrsServer();
    virtual ~SrsServer();
winlin authored
162 163 164 165 166 167
public:
    /**
    * the destroy is for gmc to analysis the memory leak,
    * if not destroy global/static data, the gmc will warning memory leak.
    * in service, server never destroy, directly exit when restart.
    */
168
    virtual void destroy();
winlin authored
169
// server startup workflow, @see run_master()
winlin authored
170
public:
171
    virtual int initialize();
172
    virtual int initialize_signal();
winlin authored
173
    virtual int acquire_pid_file();
174
    virtual int initialize_st();
175
    virtual int listen();
176
    virtual int register_signal();
winlin authored
177
    virtual int ingest();
178
    virtual int cycle();
winlin authored
179 180 181 182 183 184 185
// server utility
public:
    /**
    * callback for connection to remove itself.
    * when connection thread cycle terminated, callback this to delete connection.
    * @see SrsConnection.on_thread_stop().
    */
186
    virtual void remove(SrsConnection* conn);
winlin authored
187 188 189 190 191 192 193 194 195 196
    /**
    * callback for signal manager got a signal.
    * the signal manager convert signal to io message,
    * whatever, we will got the signo like the orignal signal(int signo) handler.
    * @remark, direclty exit for SIGTERM.
    * @remark, do reload for SIGNAL_RELOAD.
    * @remark, for SIGINT and SIGUSR2:
    *       no gmc, directly exit.
    *       for gmc, set the variable signal_gmc_stop, the cycle will return and cleanup for gmc.
    */
197
    virtual void on_signal(int signo);
winlin authored
198
private:
winlin authored
199 200 201 202 203
    /**
    * the server thread main cycle,
    * update the global static data, for instance, the current time,
    * the cpu/mem/network statistic.
    */
204
    virtual int do_cycle();
winlin authored
205 206 207
    /**
    * listen at specified protocol.
    */
208 209 210
    virtual int listen_rtmp();
    virtual int listen_http_api();
    virtual int listen_http_stream();
winlin authored
211 212 213 214
    /**
    * close the listeners for specified type, 
    * remove the listen object from manager.
    */
215
    virtual void close_listeners(SrsListenerType type);
winlin authored
216 217 218 219 220 221
    /**
    * resample the server kbps. 
    * if conn is NULL, resample all connections delta, then calc the total kbps.
    * @param conn, the connection to do resample the kbps. NULL to resample all connections.
    * @param do_resample, whether resample the server kbps. always false when sample a connection.
    */
222
    virtual void resample_kbps(SrsConnection* conn, bool do_resample = true);
223 224
// internal only
public:
winlin authored
225 226 227 228 229 230
    /**
    * when listener got a fd, notice server to accept it.
    * @param type, the client type, used to create concrete connection, 
    *       for instance RTMP connection to serve client.
    * @param client_stfd, the client fd in st boxed, the underlayer fd.
    */
231
    virtual int accept_client(SrsListenerType type, st_netfd_t client_stfd);
winlin authored
232
// interface ISrsThreadHandler.
winlin authored
233
public:
234
    virtual int on_reload_listen();
winlin authored
235
    virtual int on_reload_pid();
winlin authored
236 237 238
    virtual int on_reload_vhost_added(std::string vhost);
    virtual int on_reload_vhost_removed(std::string vhost);
    virtual int on_reload_vhost_http_updated();
winlin authored
239 240
    virtual int on_reload_http_api_enabled();
    virtual int on_reload_http_api_disabled();
winlin authored
241 242 243
    virtual int on_reload_http_stream_enabled();
    virtual int on_reload_http_stream_disabled();
    virtual int on_reload_http_stream_updated();
winlin authored
244 245
};
246
#endif