Esempio n. 1
0
int main(int argc,char** argv){

	evbase = event_base_new(); 
	if(!evbase){ 
		fprintf(stderr, "create evbase error!\n");
		exit(0); 
	}

	// 创建http server实例
	ev_ssl = evhttp_new(evbase);
	if(!ev_ssl){ 
		exit(0); 
	}

	// openssl 初始化 
	SSL_library_init(); 
	ERR_load_crypto_strings(); 
	SSL_load_error_strings(); 
	OpenSSL_add_all_algorithms();

	if (SSLeay() != OPENSSL_VERSION_NUMBER){ 
	
	}

	ev_ssl->ctx = get_ssl_ctx(certfile_url.c_str(),keyfile_url.c_str()); 
	ev_ssl->ssl_cb = bufferevent_openssl_socket_new;

	std::string ev_ssl_ip="192.168.1.10"; int ev_ssl_port = 8080;

	// evhttp_bind_socket_with_handle这个函数在原基础上追加一个参数,标示http server知否支持ssl(0:不支持 1:支持) 
	struct evhttp_bound_socket *ssl_handle = evhttp_bind_socket_with_handle(ev_ssl, ev_ssl_ip.c_str(), ev_ssl_port,1); 
	if(!ssl_handle){ 
		exit(0); 
	}
	
	struct evconnlistener *ssl_listener = evhttp_bound_socket_get_listener(ssl_handle); 
	evconnlistener_set_error_cb(ssl_listener, ssl_accept_error_cb);

	evhttp_set_cb(ev_ssl, "/ping", ping_handler, NULL);

	event_base_dispatch(evbase);

	evhttp_free(ev_ssl); event_base_free(evbase);

	return 0; 
}
Esempio n. 2
0
int main(int argc, char* argv[])
{
	welcome();
	init(argc, argv);

	ServerConfig::max_channels = conf->get_num("front.max_channels");
	ServerConfig::max_subscribers_per_channel = conf->get_num("front.max_subscribers_per_channel");
	ServerConfig::channel_buffer_size = conf->get_num("front.channel_buffer_size");
	ServerConfig::channel_timeout = conf->get_num("front.channel_timeout");
	ServerConfig::polling_timeout = conf->get_num("front.polling_timeout");
	if (ServerConfig::polling_timeout <= 0){
		log_fatal("Invalid polling_timeout!");
		exit(0);
	}
	if (ServerConfig::channel_timeout <= 0){
		ServerConfig::channel_timeout = (int)(0.5 * ServerConfig::polling_timeout);
	}

	ServerConfig::polling_idles = ServerConfig::polling_timeout / CHANNEL_CHECK_INTERVAL;
	ServerConfig::channel_idles = ServerConfig::channel_timeout / CHANNEL_CHECK_INTERVAL;

	log_info("ServerConfig::max_channels =%d", ServerConfig::max_channels);
	log_info("ServerConfig::max_subscribers_per_channel=%d", ServerConfig::max_subscribers_per_channel);
	log_info("ServerConfig::polling_timeout=%d", ServerConfig::polling_timeout);
	log_info("ServerConfig::polling_idles =%d", ServerConfig::polling_idles);
	log_info("ServerConfig::channel_buffer_size=%d", ServerConfig::channel_buffer_size);
	log_info("ServerConfig::channel_timeout=%d", ServerConfig::channel_timeout);
	log_info("ServerConfig::channel_idles =%d", ServerConfig::channel_idles);
	serv = new Server();
	ip_filter = new IpFilter();


	{
		// /pub?cname=abc&content=hi
		// content must be json encoded string without leading quote and trailing quote
		// TODO: multi_pub
		evhttp_set_cb(admin_http, "/pub", pub_handler, NULL);
		// pub raw content(not json encoded)
		evhttp_set_cb(admin_http, "/push", push_handler, NULL);
		// 分配通道, 返回通道的id和token
		// /sign?cname=abc[&expires=60]
		// wait 60 seconds to expire before any sub
		evhttp_set_cb(admin_http, "/sign", sign_handler, NULL);
		// 销毁通道
		// /close?cname=abc
		evhttp_set_cb(admin_http, "/close", close_handler, NULL);
		// 销毁通道
		// /clear?cname=abc
		evhttp_set_cb(admin_http, "/clear", clear_handler, NULL);
		// 获取通道的信息
		// /info?[cname=abc], or TODO: /info?cname=a,b,c
		evhttp_set_cb(admin_http, "/info", info_handler, NULL);
		// 判断通道是否处于被订阅状态(所有订阅者断开连接一定时间后, 通道才转为空闲状态)
		// /check?cname=abc, or TODO: /check?cname=a,b,c
		evhttp_set_cb(admin_http, "/check", check_handler, NULL);

		// 订阅通道的状态变化信息, 如创建通道(第一个订阅者连接时), 关闭通道.
		// 通过 endless chunk 返回.
		evhttp_set_cb(admin_http, "/psub", psub_handler, NULL);

		std::string admin_ip;
		int admin_port = 0;
		parse_ip_port(conf->get_str("admin.listen"), &admin_ip, &admin_port);
		struct evhttp_bound_socket *handle;
		handle = evhttp_bind_socket_with_handle(admin_http, admin_ip.c_str(), admin_port);
		if (!handle){
			log_fatal("bind admin_port %d error! %s", admin_port, strerror(errno));
			exit(0);
		}
		log_info("admin server listen on %s:%d", admin_ip.c_str(), admin_port);

		struct evconnlistener *listener = evhttp_bound_socket_get_listener(handle);
		evconnlistener_set_error_cb(listener, accept_error_cb);
		// TODO: modify libevent, add evhttp_set_accept_cb()
	}

	// init admin ip_filter
	{
		Config *cc = (Config *)conf->get("admin");
		if (cc != NULL){
			std::vector<Config *> *children = &cc->children;
			std::vector<Config *>::iterator it;
			for (it = children->begin(); it != children->end(); it++){
				if ((*it)->key != "allow"){
					continue;
				}
				const char *ip = (*it)->str();
				log_info("    allow %s", ip);
				ip_filter->add_allow(ip);
			}
		}
	}

	{
		Config *cc = (Config *)conf->get("admin");
		if (cc != NULL){
			std::vector<Config *> *children = &cc->children;
			std::vector<Config *>::iterator it;
			for (it = children->begin(); it != children->end(); it++){
				if ((*it)->key != "deny"){
					continue;
				}
				const char *ip = (*it)->str();
				log_info("    deny %s", ip);
				ip_filter->add_deny(ip);
			}
		}
	}

	{
		// /sub?cname=abc&cb=jsonp&token=&seq=123&noop=123
		evhttp_set_cb(front_http, "/sub", poll_handler, NULL);
		evhttp_set_cb(front_http, "/poll", poll_handler, NULL);
		// forever iframe
		evhttp_set_cb(front_http, "/iframe", iframe_handler, NULL);
		// http endless chunk
		evhttp_set_cb(front_http, "/stream", stream_handler, NULL);
		// /ping?cb=jsonp
		evhttp_set_cb(front_http, "/ping", ping_handler, NULL);

		std::string front_ip;
		int front_port = 0;
		parse_ip_port(conf->get_str("front.listen"), &front_ip, &front_port);
		for (int i = 0; i < MAX_BIND_PORTS; i++){
			int port = front_port + i;

			struct evhttp_bound_socket *handle;
			handle = evhttp_bind_socket_with_handle(front_http, front_ip.c_str(), port);
			if (!handle){
				log_fatal("bind front_port %d error! %s", port, strerror(errno));
				exit(0);
			}
			log_info("front server listen on %s:%d", front_ip.c_str(), port);

			struct evconnlistener *listener = evhttp_bound_socket_get_listener(handle);
			evconnlistener_set_error_cb(listener, accept_error_cb);
		}

		std::string auth = conf->get_str("front.auth");
		log_info("    auth %s", auth.c_str());
		log_info("    max_channels %d", ServerConfig::max_channels);
		log_info("    max_subscribers_per_channel %d", ServerConfig::max_subscribers_per_channel);
		log_info("    channel_buffer_size %d", ServerConfig::channel_buffer_size);
		log_info("    channel_timeout %d", ServerConfig::channel_timeout);
		log_info("    polling_timeout %d", ServerConfig::polling_timeout);
		if (auth == "token"){
			serv->auth = Server::AUTH_TOKEN;
		}
	}

	//write_pidfile();
	log_info("chatserv started");
	event_base_dispatch(evbase);
	//remove_pidfile();

	event_free(timer_event);
	event_free(sigint_event);
	event_free(sigterm_event);
	evhttp_free(admin_http);
	evhttp_free(front_http);
	event_base_free(evbase);

	delete serv;
	delete conf;
	log_info("chatserv exit");

	return 0;
}