Blame view

trunk/src/core/srs_core_server.cpp 5.8 KB
winlin authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
The MIT License (MIT)

Copyright (c) 2013 winlin

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 26 27 28 29 30 31
#include <srs_core_server.hpp>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <algorithm>
winlin authored
32 33 34 35
#include <st.h>

#include <srs_core_log.hpp>
#include <srs_core_error.hpp>
36
#include <srs_core_client.hpp>
winlin authored
37
38
#define SERVER_LISTEN_BACKLOG 10
winlin authored
39 40 41 42 43 44 45

SrsServer::SrsServer()
{
}

SrsServer::~SrsServer()
{
46 47 48 49 50
	for (std::vector<SrsConnection*>::iterator it = conns.begin(); it != conns.end(); ++it) {
		SrsConnection* conn = *it;
		delete conn;
	}
	conns.clear();
winlin authored
51 52 53 54 55 56 57 58 59
}

int SrsServer::initialize()
{
	int ret = ERROR_SUCCESS;
    
    // use linux epoll.
    if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) {
        ret = ERROR_ST_SET_EPOLL;
60
        srs_error("st_set_eventsys use linux epoll failed. ret=%d", ret);
winlin authored
61 62
        return ret;
    }
63
    srs_verbose("st_set_eventsys use linux epoll success");
winlin authored
64 65 66
    
    if(st_init() != 0){
        ret = ERROR_ST_INITIALIZE;
67
        srs_error("st_init failed. ret=%d", ret);
winlin authored
68 69
        return ret;
    }
70
    srs_verbose("st_init success");
winlin authored
71 72
	
	// set current log id.
73 74
	log_context->generate_id();
	srs_info("log set id success");
winlin authored
75 76 77
	
	return ret;
}
78
79
int SrsServer::listen(int port)
80 81 82 83 84
{
	int ret = ERROR_SUCCESS;
	
	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        ret = ERROR_SOCKET_CREATE;
85
        srs_error("create linux socket error. ret=%d", ret);
86 87
        return ret;
	}
88
	srs_verbose("create linux socket success. fd=%d", fd);
89 90 91 92
    
    int reuse_socket = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) {
        ret = ERROR_SOCKET_SETREUSE;
93
        srs_error("setsockopt reuse-addr error. ret=%d", ret);
94 95
        return ret;
    }
96
    srs_verbose("setsockopt reuse-addr success. fd=%d", fd);
97 98 99 100 101 102 103
    
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = INADDR_ANY;
    if (bind(fd, (const sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
        ret = ERROR_SOCKET_BIND;
104
        srs_error("bind socket error. ret=%d", ret);
105 106
        return ret;
    }
107
    srs_verbose("bind socket success. fd=%d", fd);
108
    
109
    if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
110
        ret = ERROR_SOCKET_LISTEN;
111
        srs_error("listen socket error. ret=%d", ret);
112 113
        return ret;
    }
114
    srs_verbose("listen socket success. fd=%d", fd);
115 116 117
    
    if ((stfd = st_netfd_open_socket(fd)) == NULL){
        ret = ERROR_ST_OPEN_SOCKET;
118
        srs_error("st_netfd_open_socket open socket failed. ret=%d", ret);
119 120
        return ret;
    }
121
    srs_verbose("st open socket success. fd=%d", fd);
122 123 124
    
    if (st_thread_create(listen_thread, this, 0, 0) == NULL) {
        ret = ERROR_ST_CREATE_LISTEN_THREAD;
125
        srs_error("st_thread_create listen thread error. ret=%d", ret);
126 127
        return ret;
    }
128
    srs_verbose("create st listen thread success.");
129
    
130
    srs_trace("server started, listen at port=%d, fd=%d", port, fd);
131 132 133 134 135 136 137
	
	return ret;
}

int SrsServer::cycle()
{
	int ret = ERROR_SUCCESS;
winlin authored
138
	// TODO: canbe a api thread.
139 140 141 142 143 144 145 146 147 148 149 150
	st_thread_exit(NULL);
	return ret;
}

void SrsServer::remove(SrsConnection* conn)
{
	std::vector<SrsConnection*>::iterator it = std::find(conns.begin(), conns.end(), conn);
	
	if (it != conns.end()) {
		conns.erase(it);
	}
	
151
	srs_info("conn removed. conns=%d", (int)conns.size());
152
	
153 154 155 156 157 158 159 160 161
	// all connections are created by server,
	// so we delete it here.
	delete conn;
}

int SrsServer::accept_client(st_netfd_t client_stfd)
{
	int ret = ERROR_SUCCESS;
	
162
	SrsConnection* conn = new SrsClient(this, client_stfd);
163 164 165
	
	// directly enqueue, the cycle thread will remove the client.
	conns.push_back(conn);
166
	srs_verbose("add conn to vector. conns=%d", (int)conns.size());
167 168 169 170 171
	
	// cycle will start process thread and when finished remove the client.
	if ((ret = conn->start()) != ERROR_SUCCESS) {
		return ret;
	}
172
	srs_verbose("conn start finished. ret=%d", ret);
173 174 175 176 177 178 179 180
    
	return ret;
}

void SrsServer::listen_cycle()
{
	int ret = ERROR_SUCCESS;
	
181 182 183
	log_context->generate_id();
	srs_trace("listen cycle start.");
	
184 185 186 187 188
	while (true) {
	    st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT);
	    
	    if(client_stfd == NULL){
	        // ignore error.
189
	        srs_warn("ignore accept thread stoppped for accept client error");
190 191
	        continue;
	    }
192
	    srs_verbose("get a client. fd=%d", st_netfd_fileno(client_stfd));
193 194
    	
    	if ((ret = accept_client(client_stfd)) != ERROR_SUCCESS) {
195
    		srs_warn("accept client error. ret=%d", ret);
196 197 198
			continue;
    	}
    	
199
    	srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
200 201 202 203 204 205
	}
}

void* SrsServer::listen_thread(void* arg)
{
	SrsServer* server = (SrsServer*)arg;
winlin authored
206
	srs_assert(server != NULL);
207 208 209 210 211 212
	
	server->listen_cycle();
	
	return NULL;
}