winlin

support listen and accept framework

@@ -40,7 +40,7 @@ clean: @@ -40,7 +40,7 @@ clean:
40 40
41 server: _prepare_dir 41 server: _prepare_dir
42 @echo "build the srs(simple rtmp server) over st(state-threads)" 42 @echo "build the srs(simple rtmp server) over st(state-threads)"
43 - \$(MAKE) -f ${GLOBAL_DIR_OBJS}/${GLOBAL_FILE_MAKEFILE} server 43 + \$(MAKE) -f ${GLOBAL_DIR_OBJS}/${GLOBAL_FILE_MAKEFILE} simple_rtmp_server
44 44
45 # the ./configure will generate it. 45 # the ./configure will generate it.
46 _prepare_dir: 46 _prepare_dir:
@@ -68,7 +68,7 @@ GCC = g++ @@ -68,7 +68,7 @@ GCC = g++
68 LINK = \$(GCC) 68 LINK = \$(GCC)
69 AR = ar 69 AR = ar
70 70
71 -.PHONY: default server 71 +.PHONY: default simple_rtmp_server
72 72
73 default: 73 default:
74 74
@@ -82,7 +82,7 @@ LibSTfile="${LibSTRoot}/libst.a" @@ -82,7 +82,7 @@ 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") 85 +MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server" "srs_core_error" "srs_core_conn")
86 MODULE_DIR="src/core" . auto/modules.sh 86 MODULE_DIR="src/core" . auto/modules.sh
87 CORE_OBJS="${MODULE_OBJS[@]}" 87 CORE_OBJS="${MODULE_OBJS[@]}"
88 88
@@ -100,7 +100,7 @@ MAIN_ENTRANCES=("srs_main_server") @@ -100,7 +100,7 @@ MAIN_ENTRANCES=("srs_main_server")
100 # srs(simple rtmp server) over st(state-threads) 100 # srs(simple rtmp server) over st(state-threads)
101 ModuleLibFiles=(${LibSTfile}) 101 ModuleLibFiles=(${LibSTfile})
102 MODULE_OBJS="${CORE_OBJS[@]} ${CONFIG_OBJS[@]} ${PROTOCOL_OBJS[@]} ${MAIN_OBJS[@]}" 102 MODULE_OBJS="${CORE_OBJS[@]} ${CONFIG_OBJS[@]} ${PROTOCOL_OBJS[@]} ${MAIN_OBJS[@]}"
103 -BUILD_KEY="server" APP_MAIN="srs_main_server" APP_NAME="server" LINK_OPTIONS="-ldl" SO_PATH="" . auto/apps.sh 103 +BUILD_KEY="simple_rtmp_server" APP_MAIN="srs_main_server" APP_NAME="simple_rtmp_server" LINK_OPTIONS="-ldl" SO_PATH="" . auto/apps.sh
104 104
105 echo 'configure ok! ' 105 echo 'configure ok! '
106 106
@@ -35,4 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -35,4 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #define __STDC_FORMAT_MACROS 35 #define __STDC_FORMAT_MACROS
36 #endif 36 #endif
37 37
  38 +#include <assert.h>
  39 +#define SrsAssert(expression) assert(expression)
  40 +
38 #endif 41 #endif
  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_conn.hpp>
  25 +
  26 +#include <srs_core_log.hpp>
  27 +#include <srs_core_error.hpp>
  28 +#include <srs_core_server.hpp>
  29 +
  30 +SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
  31 +{
  32 + server = srs_server;
  33 + stfd = client_stfd;
  34 +}
  35 +
  36 +SrsConnection::~SrsConnection()
  37 +{
  38 +}
  39 +
  40 +int SrsConnection::start()
  41 +{
  42 + int ret = ERROR_SUCCESS;
  43 +
  44 + if (st_thread_create(cycle_thread, this, 0, 0) == NULL) {
  45 + ret = ERROR_ST_CREATE_CYCLE_THREAD;
  46 + SrsError("st_thread_create conn cycle thread error. ret=%d", ret);
  47 + return ret;
  48 + }
  49 + SrsVerbose("create st conn cycle thread success.");
  50 +
  51 + return ret;
  52 +}
  53 +
  54 +int SrsConnection::do_cycle()
  55 +{
  56 + int ret = ERROR_SUCCESS;
  57 + return ret;
  58 +}
  59 +
  60 +void SrsConnection::cycle()
  61 +{
  62 + int ret = ERROR_SUCCESS;
  63 +
  64 + ret = do_cycle();
  65 +
  66 + // success.
  67 + if (ret == ERROR_SUCCESS) {
  68 + SrsInfo("client process normally finished. ret=%d", ret);
  69 + }
  70 +
  71 + // client close peer.
  72 + if (ret == ERROR_SOCKET_CLOSED) {
  73 + SrsTrace("client disconnect peer. ret=%d", ret);
  74 + }
  75 +
  76 + server->remove(this);
  77 +}
  78 +
  79 +void* SrsConnection::cycle_thread(void* arg)
  80 +{
  81 + SrsConnection* conn = (SrsConnection*)arg;
  82 + SrsAssert(conn != NULL);
  83 +
  84 + conn->cycle();
  85 +
  86 + return NULL;
  87 +}
  88 +
  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_CONN_HPP
  25 +#define SRS_CORE_CONN_HPP
  26 +
  27 +/*
  28 +#include <srs_core_conn.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +#include <st.h>
  34 +
  35 +class SrsServer;
  36 +class SrsConnection
  37 +{
  38 +private:
  39 + SrsServer* server;
  40 + st_netfd_t stfd;
  41 +public:
  42 + SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd);
  43 + virtual ~SrsConnection();
  44 +public:
  45 + virtual int start();
  46 +private:
  47 + virtual int do_cycle();
  48 + virtual void cycle();
  49 + static void* cycle_thread(void* arg);
  50 +};
  51 +
  52 +#endif
@@ -28,9 +28,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -28,9 +28,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <srs_core_error.hpp> 28 #include <srs_core_error.hpp>
29 */ 29 */
30 30
31 -#define ERROR_SUCCESS 0 31 +#include <srs_core.hpp>
32 32
33 -#define ERROR_ST_SET_EPOLL 100  
34 -#define ERROR_ST_INITIALIZE 101 33 +#define ERROR_SUCCESS 0
  34 +
  35 +#define ERROR_ST_SET_EPOLL 100
  36 +#define ERROR_ST_INITIALIZE 101
  37 +#define ERROR_ST_OPEN_SOCKET 102
  38 +#define ERROR_ST_CREATE_LISTEN_THREAD 103
  39 +#define ERROR_ST_CREATE_CYCLE_THREAD 104
  40 +
  41 +#define ERROR_SOCKET_CREATE 200
  42 +#define ERROR_SOCKET_SETREUSE 201
  43 +#define ERROR_SOCKET_BIND 202
  44 +#define ERROR_SOCKET_LISTEN 203
  45 +#define ERROR_SOCKET_CLOSED 204
35 46
36 #endif 47 #endif
@@ -21,12 +21,21 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN @@ -21,12 +21,21 @@ 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_server.hpp>
  25 +
  26 +#include <sys/types.h>
  27 +#include <sys/socket.h>
  28 +#include <arpa/inet.h>
  29 +
  30 +#include <algorithm>
  31 +
24 #include <st.h> 32 #include <st.h>
25 33
26 #include <srs_core_log.hpp> 34 #include <srs_core_log.hpp>
27 #include <srs_core_error.hpp> 35 #include <srs_core_error.hpp>
  36 +#include <srs_core_conn.hpp>
28 37
29 -#include <srs_core_server.hpp> 38 +#define SERVER_LISTEN_BACKLOG 10
30 39
31 SrsServer::SrsServer() 40 SrsServer::SrsServer()
32 { 41 {
@@ -34,6 +43,11 @@ SrsServer::SrsServer() @@ -34,6 +43,11 @@ SrsServer::SrsServer()
34 43
35 SrsServer::~SrsServer() 44 SrsServer::~SrsServer()
36 { 45 {
  46 + for (std::vector<SrsConnection*>::iterator it = conns.begin(); it != conns.end(); ++it) {
  47 + SrsConnection* conn = *it;
  48 + delete conn;
  49 + }
  50 + conns.clear();
37 } 51 }
38 52
39 int SrsServer::initialize() 53 int SrsServer::initialize()
@@ -46,14 +60,14 @@ int SrsServer::initialize() @@ -46,14 +60,14 @@ int SrsServer::initialize()
46 SrsError("st_set_eventsys use linux epoll failed. ret=%d", ret); 60 SrsError("st_set_eventsys use linux epoll failed. ret=%d", ret);
47 return ret; 61 return ret;
48 } 62 }
49 - SrsInfo("st_set_eventsys use linux epoll success"); 63 + SrsVerbose("st_set_eventsys use linux epoll success");
50 64
51 if(st_init() != 0){ 65 if(st_init() != 0){
52 ret = ERROR_ST_INITIALIZE; 66 ret = ERROR_ST_INITIALIZE;
53 SrsError("st_init failed. ret=%d", ret); 67 SrsError("st_init failed. ret=%d", ret);
54 return ret; 68 return ret;
55 } 69 }
56 - SrsTrace("st_init success"); 70 + SrsVerbose("st_init success");
57 71
58 // set current log id. 72 // set current log id.
59 log_context->SetId(); 73 log_context->SetId();
@@ -61,3 +75,129 @@ int SrsServer::initialize() @@ -61,3 +75,129 @@ int SrsServer::initialize()
61 75
62 return ret; 76 return ret;
63 } 77 }
  78 +
  79 +int SrsServer::start(int port)
  80 +{
  81 + int ret = ERROR_SUCCESS;
  82 +
  83 + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  84 + ret = ERROR_SOCKET_CREATE;
  85 + SrsError("create linux socket error. ret=%d", ret);
  86 + return ret;
  87 + }
  88 + SrsVerbose("create linux socket success. fd=%d", fd);
  89 +
  90 + int reuse_socket = 1;
  91 + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) {
  92 + ret = ERROR_SOCKET_SETREUSE;
  93 + SrsError("setsockopt reuse-addr error. ret=%d", ret);
  94 + return ret;
  95 + }
  96 + SrsVerbose("setsockopt reuse-addr success. fd=%d", fd);
  97 +
  98 + sockaddr_in addr;
  99 + addr.sin_family = AF_INET;
  100 + addr.sin_port = htons(port);
  101 + addr.sin_addr.s_addr = INADDR_ANY;
  102 + if (bind(fd, (const sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
  103 + ret = ERROR_SOCKET_BIND;
  104 + SrsError("bind socket error. ret=%d", ret);
  105 + return ret;
  106 + }
  107 + SrsVerbose("bind socket success. fd=%d", fd);
  108 +
  109 + if (listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
  110 + ret = ERROR_SOCKET_LISTEN;
  111 + SrsError("listen socket error. ret=%d", ret);
  112 + return ret;
  113 + }
  114 + SrsVerbose("listen socket success. fd=%d", fd);
  115 +
  116 + if ((stfd = st_netfd_open_socket(fd)) == NULL){
  117 + ret = ERROR_ST_OPEN_SOCKET;
  118 + SrsError("st_netfd_open_socket open socket failed. ret=%d", ret);
  119 + return ret;
  120 + }
  121 + SrsVerbose("st open socket success. fd=%d", fd);
  122 +
  123 + if (st_thread_create(listen_thread, this, 0, 0) == NULL) {
  124 + ret = ERROR_ST_CREATE_LISTEN_THREAD;
  125 + SrsError("st_thread_create listen thread error. ret=%d", ret);
  126 + return ret;
  127 + }
  128 + SrsVerbose("create st listen thread success.");
  129 +
  130 + SrsTrace("server started, listen at port=%d, fd=%d", port, fd);
  131 +
  132 + return ret;
  133 +}
  134 +
  135 +int SrsServer::cycle()
  136 +{
  137 + int ret = ERROR_SUCCESS;
  138 + st_thread_exit(NULL);
  139 + return ret;
  140 +}
  141 +
  142 +void SrsServer::remove(SrsConnection* conn)
  143 +{
  144 + std::vector<SrsConnection*>::iterator it = std::find(conns.begin(), conns.end(), conn);
  145 +
  146 + if (it != conns.end()) {
  147 + conns.erase(it);
  148 + }
  149 +
  150 + // all connections are created by server,
  151 + // so we delete it here.
  152 + delete conn;
  153 +}
  154 +
  155 +int SrsServer::accept_client(st_netfd_t client_stfd)
  156 +{
  157 + int ret = ERROR_SUCCESS;
  158 +
  159 + SrsConnection* conn = new SrsConnection(this, client_stfd);
  160 +
  161 + // directly enqueue, the cycle thread will remove the client.
  162 + conns.push_back(conn);
  163 +
  164 + // cycle will start process thread and when finished remove the client.
  165 + if ((ret = conn->start()) != ERROR_SUCCESS) {
  166 + return ret;
  167 + }
  168 +
  169 + return ret;
  170 +}
  171 +
  172 +void SrsServer::listen_cycle()
  173 +{
  174 + int ret = ERROR_SUCCESS;
  175 +
  176 + while (true) {
  177 + st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT);
  178 +
  179 + if(client_stfd == NULL){
  180 + // ignore error.
  181 + SrsWarn("ignore accept thread stoppped for accept client error");
  182 + continue;
  183 + }
  184 +
  185 + if ((ret = accept_client(client_stfd)) != ERROR_SUCCESS) {
  186 + SrsWarn("accept client error. ret=%d", ret);
  187 + continue;
  188 + }
  189 +
  190 + SrsVerbose("accept client finished. ret=%d", ret);
  191 + }
  192 +}
  193 +
  194 +void* SrsServer::listen_thread(void* arg)
  195 +{
  196 + SrsServer* server = (SrsServer*)arg;
  197 + SrsAssert(server != NULL);
  198 +
  199 + server->listen_cycle();
  200 +
  201 + return NULL;
  202 +}
  203 +
@@ -28,13 +28,31 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -28,13 +28,31 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <srs_core_server.hpp> 28 #include <srs_core_server.hpp>
29 */ 29 */
30 30
  31 +#include <srs_core.hpp>
  32 +
  33 +#include <vector>
  34 +
  35 +#include <st.h>
  36 +
  37 +class SrsConnection;
31 class SrsServer 38 class SrsServer
32 { 39 {
  40 +private:
  41 + int fd;
  42 + st_netfd_t stfd;
  43 + std::vector<SrsConnection*> conns;
33 public: 44 public:
34 SrsServer(); 45 SrsServer();
35 virtual ~SrsServer(); 46 virtual ~SrsServer();
36 public: 47 public:
37 virtual int initialize(); 48 virtual int initialize();
  49 + virtual int start(int port);
  50 + virtual int cycle();
  51 + virtual void remove(SrsConnection* conn);
  52 +private:
  53 + virtual int accept_client(st_netfd_t client_stfd);
  54 + virtual void listen_cycle();
  55 + static void* listen_thread(void* arg);
38 }; 56 };
39 57
40 #endif 58 #endif
@@ -36,5 +36,13 @@ int main(int /*argc*/, char** /*argv*/){ @@ -36,5 +36,13 @@ int main(int /*argc*/, char** /*argv*/){
36 return ret; 36 return ret;
37 } 37 }
38 38
  39 + if ((ret = server.start(19350)) != ERROR_SUCCESS) {
  40 + return ret;
  41 + }
  42 +
  43 + if ((ret = server.cycle()) != ERROR_SUCCESS) {
  44 + return ret;
  45 + }
  46 +
39 return 0; 47 return 0;
40 } 48 }
@@ -8,6 +8,8 @@ file @@ -8,6 +8,8 @@ file
8 ..\core\srs_core_error.cpp, 8 ..\core\srs_core_error.cpp,
9 ..\core\srs_core_server.hpp, 9 ..\core\srs_core_server.hpp,
10 ..\core\srs_core_server.cpp, 10 ..\core\srs_core_server.cpp,
  11 + ..\core\srs_core_conn.hpp,
  12 + ..\core\srs_core_conn.cpp,
11 ..\core\srs_core_log.hpp, 13 ..\core\srs_core_log.hpp,
12 ..\core\srs_core_log.cpp; 14 ..\core\srs_core_log.cpp;
13 mainconfig 15 mainconfig