Ejemplo n.º 1
0
int main() {
    set_default_logif_level(LOG_WARN);
    evt_loop *loop = evt_loop_init();
    netaddr_init_v4(&self_addr, "0.0.0.0", 8322);
    netaddr_init_v4(&serv_addr, "107.170.203.188", 8002);

    tcp_server *server = tcp_server_init(&self_addr, loop, 0);

    tcp_connection_set_on_accept(server, on_accept);
    tcp_connection_set_on_close(server, on_close);
    tcp_connection_set_on_read(server, on_read);

    tcp_server_start(server);
    evt_loop_run(loop);
    return 0;
}
Ejemplo n.º 2
0
int
rpc_start(struct rpc *rpc) {
	tcp_server_t *tcpserver = NULL;
	struct timeval timeout;

	/* タイムアウト値の設定 */
	timeout.tv_sec = rpc->rpc_timeout;
	timeout.tv_usec = 0;
	/* TCPサーバーの生成 */
	if (tcp_server_create(
	    &tcpserver,
	    NULL,
	    rpc->bind_port,
	    RECV_BUFF,
	    EV_READ,
	    &timeout,
	    rpc_accept_init,
	    rpc_accept_main,
	    rpc_accept_finish,
	    rpc_listen_init,
	    rpc_listen_finish,
	    rpc,
	    rpc->event_base)) {
		fprintf(stderr, "failed in create tcp server instance.\n");
		return 1;
	}
	rpc->tcpserver = tcpserver;
	/*
         * TCPサーバーの開始
         * イベントループで回る
         */ 
	if (tcp_server_start(rpc->tcpserver)) {
		fprintf(stderr, "failed in start up tcp server instance.\n");
		rpc->tcpserver = NULL;
	}

        /* 
         * TCPサーバーの削除
         */
	tcp_server_destroy(rpc->tcpserver);
	rpc->tcpserver = NULL;
        printf("tcp server end.\n");

	return 0;
}
Ejemplo n.º 3
0
int main(void)
{
    
    tcp_server_data_t * server;
    tcp_client_data_t * client;
    int i;
    char message[MAX_BUFFER_LEN];
    
    i= 0;
    
    server = tcp_server_data_new();
    strcpy(server->port,"8675");
    
    client = tcp_client_data_new();
    strcpy(client->dest_ip_address,"localhost");
    strcpy(client->port,"8675");
    
    tcp_server_start(server);
    tcp_client_start(client);

    printf("testing server and client with 5 messages\n");
    while(i<5)
    {
        sleep(1);
        sprintf(message, "ack from client %d\n",i);
        thread_copy_to_buffer(client->send_buffer, message);
        
        sprintf(message, "boo form server %d\n",i);
        thread_copy_to_buffer(server->send_buffer, message);
        i++;
    }
    
    printf("shutting down! this isnt pretty yet, but its ok :) \n");
    tcp_client_stop(client);
    tcp_client_data_free(client);
    
    tcp_server_stop(server);
    tcp_server_data_free(server);
    
    printf("goodbye!\n");
    return 0;
    
}
Ejemplo n.º 4
0
Archivo: main.c Proyecto: gc87/ctools
int 
main(int argc, char** argv) {
	char path[50] = { 0x00 };
	if (2 <= argc) {
		strncpy(path, argv[1], strlen(argv[1]));
	} else {
		strncpy(path, "server.cfg", 10);
	}

	//读取配置文件
	TinyConf* config; 
	char* run;
	for(;;) {
		config = conf_open(path, '=');
		if(NULL == config) {
			sleep(5);
			continue;
		}

		run = conf_get(config, "run");
		if(0 >= strlen(run)) { 
			sleep(5);
			continue;
		} 

		if(0 != strcmp(run, "on")) {
			sleep(5);
			continue;
		}

		break; //一切ok,启动正常处理
	}

	//初始化mysql配置
	char* mysql_host = conf_get(config, "mysql_host");
	char* mysql_port_str = conf_get(config, "mysql_port");
	char* mysql_user = conf_get(config, "mysql_user");
	char* mysql_password = conf_get(config, "mysql_password");
	char* mysql_database = conf_get(config, "mysql_database");

	//将数据库设置存入到map中
	TinyMap* store_map = map_create();
	map_add(store_map, "host", mysql_host, NULL);
	map_add(store_map, "port", mysql_port_str, NULL);
	map_add(store_map, "user", mysql_user, NULL);
	map_add(store_map, "password", mysql_password, NULL);
	map_add(store_map, "database", mysql_database, NULL);

	//初始化tcpserver的相关代码
	int tcp_port = 7000;
	char* tcp_port_str = conf_get(config, "port");
	if(0 < strlen(tcp_port_str)) {
		tcp_port = atoi(tcp_port_str);
	}

	TCPServer* tcp_server = tcp_server_create(tcp_port); //生成tcp服务器实例
	if(NULL == tcp_server) {
		fprintf(stderr, "Create server falied.\n");
	}

	tcp_server_set_proc_callback(tcp_server, store_map, analyze_proc); //注册数据处理的回调函数
	tcp_server_set_timeout_callback(tcp_server, store_map, analyze_timeout); //注册客户端连接超时的回调函数

	int ret = tcp_server_start(tcp_server); //启动tcp服务器
	if(-1 == ret) {
 		fprintf(stderr, "Start tcpserver falied.\n");
 		return 0;
	}


	//创建存储实例
	Store* store = store_create(mysql_host, atoi(mysql_port_str), mysql_user, mysql_password, mysql_database);

	//创建网络检测线程
	pthread_t thread_id;
	int p_ret = pthread_create(&thread_id, NULL, (void*)check_networks, store);
	if(0 != p_ret) {
		fprintf(stderr, "%s\n", "create networks check thread err.");
	}

	for(;;) { //阻塞
		sleep(1000);
	}

	pthread_cancel(thread_id); //退出线程
	store_destroy(store); //销毁存储实例
	tcp_server_stop(tcp_server); //停止tcp服务器实例
	tcp_server_destroy(tcp_server); //销毁tcp服务器实例
	map_destroy(store_map); //销毁store_map实例
	conf_close(config); //关闭配置信息读取实例
	return 0;
}