Blame view

trunk/src/app/srs_app_server.hpp 4.5 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;
winlin authored
43
44 45
// listener type for server to identify the connection,
// that is, use different type to process the connection.
winlin authored
46 47
enum SrsListenerType 
{
48 49 50 51 52 53
    // RTMP client,
    SrsListenerRtmpStream   = 0,
    // HTTP api,
    SrsListenerHttpApi      = 1,
    // HTTP stream, HDS/HLS/DASH
    SrsListenerHttpStream   = 2
winlin authored
54 55
};
56
class SrsListener : public ISrsThreadHandler
winlin authored
57 58
{
public:
59
    SrsListenerType _type;
winlin authored
60
private:
61 62
    int fd;
    st_netfd_t stfd;
63 64
    int _port;
    SrsServer* _server;
65
    SrsThread* pthread;
winlin authored
66
public:
67
    SrsListener(SrsServer* server, SrsListenerType type);
68
    virtual ~SrsListener();
winlin authored
69
public:
70
    virtual SrsListenerType type();
71
    virtual int listen(int port);
72 73
// interface ISrsThreadHandler.
public:
74
    virtual void on_thread_start();
75
    virtual int cycle();
winlin authored
76 77
};
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
/**
* 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);
};
109
class SrsServer : public ISrsReloadHandler
winlin authored
110 111
{
private:
112
#ifdef SRS_AUTO_HTTP_API
winlin authored
113 114
    SrsHttpHandler* http_api_handler;
#endif
115
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
116 117
    SrsHttpHandler* http_stream_handler;
#endif
118
#ifdef SRS_AUTO_INGEST
119 120
    SrsIngester* ingester;
#endif
winlin authored
121
private:
winlin authored
122
    int pid_fd;
123 124
    std::vector<SrsConnection*> conns;
    std::vector<SrsListener*> listeners;
125
    SrsSignalManager* signal_manager;
126 127
    bool signal_reload;
    bool signal_gmc_stop;
winlin authored
128
public:
129 130
    SrsServer();
    virtual ~SrsServer();
winlin authored
131
public:
132
    virtual int initialize();
133
    virtual int initialize_signal();
winlin authored
134
    virtual int acquire_pid_file();
135
    virtual int initialize_st();
136
    virtual int listen();
137
    virtual int register_signal();
winlin authored
138
    virtual int ingest();
139 140 141
    virtual int cycle();
    virtual void remove(SrsConnection* conn);
    virtual void on_signal(int signo);
winlin authored
142
private:
143 144 145 146
    virtual int listen_rtmp();
    virtual int listen_http_api();
    virtual int listen_http_stream();
    virtual void close_listeners(SrsListenerType type);
147 148
// internal only
public:
149
    virtual int accept_client(SrsListenerType type, st_netfd_t client_stfd);
winlin authored
150
// interface ISrsThreadHandler.
winlin authored
151
public:
152
    virtual int on_reload_listen();
winlin authored
153
    virtual int on_reload_pid();
winlin authored
154 155 156
    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
157 158
    virtual int on_reload_http_api_enabled();
    virtual int on_reload_http_api_disabled();
winlin authored
159 160 161
    virtual int on_reload_http_stream_enabled();
    virtual int on_reload_http_stream_disabled();
    virtual int on_reload_http_stream_updated();
winlin authored
162 163
};
winlin authored
164
#endif