Exemplo n.º 1
0
void *Routine4(void *arg)
{
	msgque_open_read(mq1);
	uint64_t count = 0;
	uint64_t total_count = 0;
	uint32_t tick = GetSystemMs();
	for( ; ; )
	{
		list_node *n;
		if(0 != msgque_get(mq1,&n,50))
            break;
		if(n)
		{
			++count;
			++total_count;
		}
		uint32_t now = GetSystemMs();
		if(now - tick > 1000)
		{
			printf("recv:%lld\n",(count*1000)/(now-tick));
			tick = now;
			count = 0;
		}
	}
    printf("Routine4 end\n");
    return NULL;
}
Exemplo n.º 2
0
int main(int argc,char **argv)
{
	init_net_service();
	ip = argv[1];
	port = atoi(argv[2]);
	uint32_t thread_count = atoi(argv[3]);
	init_clients();	
	netservice_t n = create_net_service(thread_count);
	net_add_listener(n,ip,port);
	msg_loop_t m = create_msg_loop(server_process_packet,process_new_connection,process_connection_disconnect,process_send_block);
	uint32_t now;
	uint32_t tick = GetSystemMs();
	while(1)
	{
		msg_loop_once(m,n,100);	
		now = GetSystemMs();
		if(now - tick > 1000)
		{
			printf("total send:%u,total_recv:%u\n",isusecount,total_bytes_recv/1024/1024);
			//printf("w:%d,r:%d,b:%d\n",wpacket_count,rpacket_count,buf_count);
			tick = now;
			total_bytes_recv = 0;
			send_count = 0;
		}
	}

	return 0;
}
Exemplo n.º 3
0
int main()
{
   init_log_system();
   log_t l = create_log("log.log");
   int i = 0; 
   uint32_t tick = GetSystemMs();
   for( ;i < 20000000; ++i)
   {
	   //buffer_t b = buffer_create_and_acquire(0,NULL,16);
	   //buffer_t c = buffer_acquire(NULL,b);
	   //buffer_release(&b);
	   //buffer_release(&c);
	   //int size = GetSize_of_pow2(16);
	   //wpacket_t w = wpacket_create(0,NULL,4096,0);
	   //wpacket_destroy(&w);
	   //void *ptr = malloc(512);
	   //free(ptr);
		log_write(l,"hello kenny",0);
   }
	//log_write(l,"hello kenny",0);
	 /*log_write(l,"hello kennyhello kennyhello kennyhello kennyhello kennyhello kenny"
	 "hello kennyhello kennyhello kennyhello kennyhello kennyhello kennyhello kenny"
	 "hello kennyhello kennyhello kennyhello kennyhello kennyhello kennyhello kenny"
	 "hello kennyhello kennyhello kennyhello kennyhello kennyhello kennyhello kenny"
	 "hello kennyhello kennyhello kennyhello kennyhello kennyhello kennyhello kenny"
	 "hello kennyhello kennyhello kennyhello kennyhello kennyhello kennyhello kenny"
	 "hello kennyhello kennyvhello kennyhello kennyhello kennyhello kennyhello kenny",0);*/
   printf("write finish:%u\n",GetSystemMs()-tick);
   getchar();
   close_log_system(); 			
};
Exemplo n.º 4
0
int main(int argc,char **argv)
{
    mutil_thread = 0;
    setup_signal_handler();
    init_clients();
    InitNetSystem();
    ENGINE engine = CreateEngine();
    int csize = atoi(argv[3]);
    int i = 0;
    for(; i < csize; ++i)
        EConnect(engine,argv[1],atoi(argv[2]),(void*)engine,on_connect,1000);
    uint32_t tick,now;
    tick = now = GetSystemMs();
    while(!stop) {
        EngineRun(engine,50);
        sendpacket();
        now = GetSystemMs();
        if(now - tick > 1000)
        {
            printf("ava_interval:%d\n",ava_interval);
            tick = now;
        }
    }
    CleanNetSystem();
    return 0;
}
Exemplo n.º 5
0
void epoll_loop(engine_t n) {
    assert(n);
    struct epoll_event events[MAX_SOCKET];
    memset(events,0,sizeof(events));
    uint32_t tick = GetSystemMs();
    uint32_t last_sync = tick;
    while(n->status) {
        int nfds = TEMP_FAILURE_RETRY(epoll_wait(n->poller_fd,events,MAX_SOCKET,10));
        if(nfds < 0) {
            printf("error:%d\n",errno);
            break;
        }
        int i;
        for(i = 0 ; i < nfds ; ++i) {
            socket_t sock = (socket_t)events[i].data.ptr;
            if(sock) {
                if(events[i].events & EPOLLIN)
                    on_read_active(sock);
                if(events[i].events & EPOLLOUT)
                    on_write_active(sock);

                if(events[i].events & EPOLLERR) {
                }
            }
        }
        mq_flush(n->event_queue);
        uint32_t now = GetSystemMs();
        if(now - tick > 1000) {
            printf("recv:%dmb\n",total_bytes_recv/1024/1024);
            tick = now;
            total_bytes_recv = 0;
        }
    }
    close(n->poller_fd);
}
Exemplo n.º 6
0
int main(){
	
	int i,j;
	const char *words[100001];
	for(i = 0; i < 100000; ++i){
		char *word = calloc(1,21);
		for(j = 0; j < 20; ++j){
			word[j] = rand()%26;
			word[j] += 'a';
		}
		words[i] = word;
	}
	words[100000] = NULL;
	wordfilter_t f = wordfilter_new(words);
	
	uint32_t begin = GetSystemMs();
	
	for(j = 0; j < 10;++j){	
		for(i = 0; i < 100000; ++i){
			isvaildword(f,"aaaaaaaaaaaaaaa");
			//isvaildword(f,words[i]);
		}
	}
	
	printf("%d\n",GetSystemMs()-begin);
		
	return 0;
}
Exemplo n.º 7
0
int main()
{		
	struct point p;
	p.x = p.y = p.z = 1;
	int i = 0;
	for(; i < 1000; ++i)
	{
		g_points[i] = create_atomic_type(sizeof(p));
		SetPoint(g_points[i],p);
	}
	thread_t t1 = CREATE_THREAD_RUN(1,SetRotine,NULL);
	thread_t t2 = CREATE_THREAD_RUN(1,GetRoutine,(void*)1);	
	thread_t t3 = CREATE_THREAD_RUN(1,GetRoutine,(void*)2);	
	thread_t t4 = CREATE_THREAD_RUN(1,GetRoutine,(void*)3);	
	uint32_t tick = GetSystemMs();
	while(1)
	{
		uint32_t new_tick = GetSystemMs();
		if(new_tick - tick >= 1000)
		{
			printf("get:%d,set:%d,miss:%d\n",get_count,set_count,miss_count);
			get_count = set_count = miss_count = 0;
			tick = new_tick;
		}
		sleepms(50);
	}
}
Exemplo n.º 8
0
int main(int argc,char **argv)
{

	HANDLE engine;
	uint32_t n;
	
	ip = argv[1];
	port = atoi(argv[2]);
	signal(SIGPIPE,SIG_IGN);
	if(InitNetSystem() != 0)
	{
		printf("Init error\n");
		return 0;
	}
	wpacket_allocator = (allocator_t)create_block_obj_allocator(0,sizeof(struct wpacket));	

	uint32_t i = 0;
	//getchar();
	//init_wpacket_pool(100000);
	//init_rpacket_pool(50000);
	//buffer_init_maxbuffer_size(2000);
	//buffer_init_64(2000);
	init_clients();

	engine = CreateEngine();
	thread_run(_Listen,&engine);
	tick = GetSystemMs();
	while(1)
	{
		EngineRun(engine,15);
		now = GetSystemMs();
		if(now - tick > 1000)
		{
			printf("recv:%u,send:%u,s_req:%u\n",packet_recv,packet_send,send_request);
			tick = now;
			packet_recv = 0;
			packet_send = 0;
			send_request = 0;
			s_p = 0;
			iocp_count = 0;
			recv_count = 0;
		}
		/*if(now - last_send_tick > 50)
		{
			//心跳,每50ms集中发一次包
			last_send_tick = now;
			for(i=0; i < MAX_CLIENT; ++i)
			{
				if(clients[i])
				{
					//++send_request;
					connection_send(clients[i],0,0);
				}
			}
		}*/
		
	}
	return 0;
}
Exemplo n.º 9
0
int main(int argc,char **argv)
{
    //msgque_flush_time = 5;
    setup_signal_handler();
    InitNetSystem();

    //共用网络层,两个线程各运行一个echo服务
    wpacket_allocator = new_obj_allocator(sizeof(struct wpacket),8192);
    rpacket_allocator = new_obj_allocator(sizeof(struct rpacket),8192);
    asynnet_t asynet = asynnet_new(2);
    msgdisp_t  disp1 = new_msgdisp(asynet,5,
                                   CB_CONNECT(asynconnect),
                                   CB_CONNECTED(asynconnected),
                                   CB_DISCNT(asyndisconnected),
                                   CB_PROCESSPACKET(asynprocesspacket),
                                   CB_CONNECTFAILED(asynconnectfailed));

    /*msgdisp_t  disp2 = new_msgdisp(asynet,
                                  asynconnect,
                                  asynconnected,
                                  asyndisconnected,
                                  asynprocesspacket,
                                  asynconnectfailed);
    */
    thread_t service1 = create_thread(THREAD_JOINABLE);
    //thread_t service2 = create_thread(THREAD_JOINABLE);

    ip = argv[1];
    port = atoi(argv[2]);

    thread_start_run(service1,service_main,(void*)disp1);
    //sleepms(1000);
    //thread_start_run(service2,service_main,(void*)disp2);

    uint32_t tick,now;
    tick = now = GetSystemMs();
    while(!stop){
        sleepms(1000);
        now = GetSystemMs();
        //if(now - tick > 1000)
        {
            uint32_t elapse = now-tick;
            recvsize = (recvsize/elapse)/1000;
            printf("client_count:%d,recvsize:%d,recvcount:%d\n",client_count,recvsize,recvcount);
            tick = now;
            packet_send_count = 0;
            recvcount = 0;
            recvsize = 0;
        }
    }

    thread_join(service1);
    //thread_join(service2);

    CleanNetSystem();
    return 0;
}
Exemplo n.º 10
0
int main(int argc,char **argv)
{

	HANDLE engine;
	uint32_t n;
	init_system_time(10);
	ip = argv[1];
	port = atoi(argv[2]);
	signal(SIGPIPE,SIG_IGN);
	if(InitNetSystem() != 0)
	{
		printf("Init error\n");
		return 0;
	}
	wpacket_allocator = (allocator_t)create_block_obj_allocator(SINGLE_THREAD,sizeof(struct wpacket));	

	uint32_t i = 0;
	init_clients();

	engine = CreateEngine();
	thread_run(_Listen,&engine);
	tick = GetSystemMs();
	while(1)
	{
		EngineRun(engine,100);
		now = GetSystemMs();
		if(now - tick > 1000)
		{
			printf("recv:%u,send:%u,s_req:%u,total_recv:%u\n",packet_recv,packet_send,send_request,total_bytes_recv/1024/1024);
			tick = now;
			packet_recv = 0;
			packet_send = 0;
			send_request = 0;
			iocp_count = 0;
			total_bytes_recv = 0;
			//printf("w:%d,r:%d,b:%d\n",wpacket_count,rpacket_count,buf_count);
		}
		/*
		if(now - last_send_tick > 10)
		{
			last_send_tick = now;
			for(i=0; i < MAX_CLIENT; ++i)
			{
				if(clients[i])
				{
					connection_send(clients[i],NULL,NULL);
				}
			}
		}*/
		
	}
	return 0;
}
Exemplo n.º 11
0
void *test_coro_fun(void *arg)
{
	printf("test_coro_fun\n");
	int i = 0;
	uint32_t tick = GetSystemMs();
	coro_t co = get_current_coro();
	for( ; i < 20000000; ++i)
	{
		//coro_sleep(get_current_coro(),500);
		coro_yield(co);
		//printf("i back:%x\n",get_current_coro());
	}
	printf("%u\n",GetSystemMs()-tick);
}
Exemplo n.º 12
0
static void write_to_file(log_t l,int32_t is_close)
{
	mutex_lock(l->mtx);
	if(!list_is_empty(l->log_queue))
		link_list_swap(l->pending_log,l->log_queue);
	mutex_unlock(l->mtx);
	if(is_close)
	{
		//日志系统关闭,写入关闭消息
		char buf[4096];
		time_t t = time(NULL);
		struct tm re;
		struct tm *_tm = localtime_r(&t,&re);
		snprintf(buf,4096,"[%d-%d-%d %d:%d:%d]close log sucessful\n",_tm->tm_year+1900,_tm->tm_mon+1,_tm->tm_mday,_tm->tm_hour,_tm->tm_min,_tm->tm_sec);
		int32_t str_len = strlen(buf);
		wpacket_t w = wpacket_create(0,NULL,str_len,1);
		wpacket_write_binary(w,buf,str_len);	
		LINK_LIST_PUSH_BACK(l->pending_log,w);
	}
	while(!list_is_empty(l->pending_log))
	{
		int32_t wbuf_count = prepare_write(l);
		int32_t bytetransfer = TEMP_FAILURE_RETRY(writev(l->file_descriptor,l->wbuf,wbuf_count));
		if(bytetransfer > 0)
		{
			l->file_size += bytetransfer;
			on_write_finish(l,bytetransfer);
			g_log_system->bytes += bytetransfer;
		}
		if(bytetransfer <= 0)
		{
			printf("errno: %d wbuf_count: %d\n",errno,wbuf_count);
		}
		if(last_tick +1000 <= GetSystemMs())
		{
			printf("log/ms:%u\n",log_count);
			last_tick = GetSystemMs();
			log_count = 0;
		}
	}
	
	if(!is_close)
	{
		/*
		* if(l->file_size > max_log_filse_size)
		* 文件超过一定大小,将原文件重命名,开一个新的文件
		*/
	}
}
Exemplo n.º 13
0
int32_t epoll_loop(engine_t n,int32_t timeout)
{

	assert(n);	
	uint32_t ms;
	uint32_t tick = GetSystemMs();
	uint32_t _timeout = tick + timeout;
	do{
		while(!LINK_LIST_IS_EMPTY(n->actived))
		{
			socket_t s = LINK_LIST_POP(socket_t,n->actived);
			s->isactived = 0;
			if(Process(s) && s->isactived == 0)
			{
				s->isactived = 1;
				LINK_LIST_PUSH_BACK(n->actived,s);
			}
		}		
		ms = _timeout - tick;
		int32_t nfds = TEMP_FAILURE_RETRY(epoll_wait(n->poller_fd,n->events,MAX_SOCKET,ms));
		if(nfds < 0)
			return -1;
		int32_t i;
		for(i = 0 ; i < nfds ; ++i)
		{	
			socket_t sock = (socket_t)n->events[i].data.ptr;
			if(sock)
			{
				//套接口可读
				if(n->events[i].events & EPOLLIN)
				{	
					on_read_active(sock);
				}
				//套接口可写
				if(n->events[i].events & EPOLLOUT)
					on_write_active(sock);	
			
				if(n->events[i].events & EPOLLERR)
				{
					//套接口异常
				}
			}
		}	
		tick = GetSystemMs();
	}while(tick < _timeout);
	return 0;
}
Exemplo n.º 14
0
void* ufun1(void *arg)
{
    uthread_t self = (uthread_t)arg;
    uthread_t u = uthread_create(self,stack2,4096,ufun2);
    char* _arg[2];
    _arg[0] = (char*)u;
    _arg[1] = (char*)self;
    int i = 0;
    uint32_t tick = GetSystemMs();
    for( ; i < 30000000; ++i)
    {
        uthread_switch(self,u,&_arg[0]);
    }
    printf("%d\n",GetSystemMs()-tick);
    uthread_switch(self,u,NULL);
    return arg;
}
Exemplo n.º 15
0
int32_t epoll_loop(engine_t n,int32_t ms)
{
	assert(n);	
	uint32_t sleep_ms;
	uint32_t timeout = GetSystemMs() + ms;
	uint32_t current_tick;
	do{		
		while(!double_link_empty(n->actived))
		{
			socket_t s = (socket_t)double_link_pop(n->actived);
			s->isactived = 0;
			if(Process(s))
			{
				s->isactived = 1;
				double_link_push(n->actived,(struct double_link_node*)s);
			}
			//if(GetCurrentMs() >= timeout)
			//	break;
		}
		
		current_tick = GetSystemMs();
		sleep_ms = timeout > current_tick ? timeout - current_tick:0;	
		int32_t nfds = TEMP_FAILURE_RETRY(epoll_wait(n->poller_fd,n->events,MAX_SOCKET,sleep_ms));
		if(nfds < 0)
			return -1;
		int32_t i;
		for(i = 0 ; i < nfds ; ++i)
		{	
			socket_t sock = (socket_t)n->events[i].data.ptr;
			if(sock)
			{
				if(n->events[i].events & EPOLLIN)
					on_read_active(sock);
				if(n->events[i].events & EPOLLOUT)
					on_write_active(sock);	
				if(n->events[i].events & EPOLLERR)
					on_read_active(sock);
			}
		}	
		current_tick = GetSystemMs();
	}while(timeout > current_tick);
	return 0;
}
Exemplo n.º 16
0
int main()
{
    {
        uint64_t begin = GetSystemMs();
        int i = 0;
        for(; i < 100000000;++i)
            GetSystemMs();
        printf("100000000 call GetSystemMs use %lld ms\n",GetSystemMs()-begin);
    }

    {
        uint64_t begin = GetSystemMs();
        int i = 0;
        for(; i < 100000000;++i)
            GetSystemSec();
        printf("100000000 call GetSystemSec() use %lld ms\n",GetSystemMs()-begin);
    }
    return 0;
}
Exemplo n.º 17
0
static void *worker_routine(void *arg)
{
	while(!g_log_system->is_close)
	{
		int32_t tick = GetSystemMs(); 
		write_all_log_file(0);
		tick = GetSystemMs() - tick;
		if(tick < 50)
			usleep(50-tick);
		if(last_tick +1000 <= GetSystemMs())
		{
			printf("log/ms:%u\n",log_count);
			last_tick = GetSystemMs();
			log_count = 0;
		}				
			
	}
	write_all_log_file(1);
	return 0;
}
Exemplo n.º 18
0
void msg_loop(msgdisp_t disp,uint32_t ms)
{
    uint64_t nowtick = GetSystemMs64();
    uint64_t timeout = nowtick+(uint64_t)ms;
    do{
        msg_t _msg = NULL;
        uint32_t sleeptime = (uint32_t)(timeout - nowtick);
        msgque_get(disp->mq,(lnode**)&_msg,sleeptime);
        if(_msg) dispatch_msg(disp,_msg);
        nowtick = GetSystemMs();
    }while(nowtick < timeout);
}
Exemplo n.º 19
0
void on_process_packet(struct connection *c,rpacket_t r)
{
    uint32_t s = rpk_read_uint32(r);
    uint32_t t;
    if(s == (uint32_t)c)
    {
        t = rpk_read_uint32(r);
        uint32_t sys_t = GetSystemMs();
        ava_interval += (sys_t - t);
        ava_interval /= 2;
    }
}
Exemplo n.º 20
0
int main(int argc,char **argv)
{
    setup_signal_handler();
    msgdisp_t disp1 = new_msgdisp(NULL,0);

    thread_t service1 = create_thread(THREAD_JOINABLE);

    msgdisp_t disp2 = new_msgdisp(NULL,0);

    thread_t service2 = create_thread(THREAD_JOINABLE);    
    asydb = new_asyndb();
    asydb->connectdb(asydb,"127.0.0.1",6379);
    asydb->connectdb(asydb,"127.0.0.1",6379);
    //发出第一个请求uu
    char req[256];
    snprintf(req,256,"set key%d %d",g,g);
    
    asydb->request(asydb,new_dbrequest(db_set,req,db_setcallback,disp1,disp1));
    thread_start_run(service1,service_main,(void*)disp1);

    asydb->request(asydb,new_dbrequest(db_set,req,db_setcallback,disp2,disp2));
    thread_start_run(service2,service_main,(void*)disp2);    
    
    uint32_t tick,now;
    tick = now = GetSystemMs();
    while(!stop){
        sleepms(100);
        now = GetSystemMs();
        if(now - tick > 1000)
        {
            printf("count:%d\n",count);
            tick = now;
            count = 0;
        }
    }
    thread_join(service1);
    thread_join(service2);
    return 0;
}
Exemplo n.º 21
0
static void _update()
{
	int32_t index = (stm->current_index + 1)%2;
	
	stm->ms[index] = GetSystemMs();
	time_t _now = time(NULL);
	stm->sec[index] = _now;
	
	struct tm _tm;
	//gmtime_r(&_now, &_tm);
	localtime_r(&_now, &_tm);
	snprintf(stm->str[index],64,"[%04d-%02d-%02d %02d:%02d:%02d]",_tm.tm_year+1900,_tm.tm_mon+1,_tm.tm_mday,_tm.tm_hour,_tm.tm_min,_tm.tm_sec);	
	
	stm->current_index  = index;
}
Exemplo n.º 22
0
int main(int argc,char **argv)
{
    mutil_thread = 0;
	setup_signal_handler();
	init_clients();
    InitNetSystem();
    struct netservice *tcpserver = new_service();
	tcpserver->listen(tcpserver,argv[1],atoi(argv[2]),(void*)tcpserver,accept_client);
	uint32_t tick,now;
    tick = now = GetSystemMs();
	while(!stop){
		tcpserver->loop(tcpserver,50);
        now = GetSystemMs();
		if(now - tick > 1000)
		{
			printf("client_count:%d,send_count:%d\n",client_count,(packet_send_count*1000)/(now-tick));
			tick = now;
			packet_send_count = 0;
		}
	}
	destroy_service(&tcpserver);
    CleanNetSystem();
    return 0;
}
Exemplo n.º 23
0
int main()
{
	
	mq1 = rque_int32_new(65536/2);
	
	thread_t t4 = create_thread(0);
	thread_start_run(t4,Routine4,NULL);

	thread_t t1 = create_thread(0);
	thread_start_run(t1,Routine1,NULL);
	uint32_t tick = GetSystemMs();
	for(;;){
		uint32_t now = GetSystemMs();
		if(now - tick > 1000)
		{
			printf("recv:%d\n",(uint32_t)((count*1000)/(now-tick)));
			tick = now;
			count = 0;
		}
		sleepms(1);
	}
	getchar();
	return 0;
}
Exemplo n.º 24
0
int32_t	init_log_system()
{
	if(!g_log_system)
	{
		g_log_system = calloc(1,sizeof(*g_log_system));
		g_log_system->mtx = mutex_create();
		g_log_system->is_close = 0;
		g_log_system->log_files = create_link_list();
		g_log_system->worker_thread = CREATE_THREAD_RUN(1,worker_routine,0);
		g_log_system->last_tick = GetSystemMs();
		g_log_system->bytes = 0;
		g_log_system->_wpacket_allocator = NULL;
		return 0;
	}
	return -1;
}
Exemplo n.º 25
0
sche_t sche_create(int32_t max_coro,int32_t stack_size,void (*idel)(void*),void *idel_arg)
{
	init_system_time(10);
	sche_t s = calloc(1,sizeof(*s));
	s->stack_size = stack_size;
	s->max_coro = max_coro;
	s->active_list_1 = LINK_LIST_CREATE();
	s->active_list_2 = LINK_LIST_CREATE();
	s->_minheap = minheap_create(max_coro,_less);
	s->next_check_timeout = GetSystemMs() + 200;
	s->co = coro_create(s,0,NULL);
	s->idel = idel;
	s->idel_arg = idel_arg;
	double_link_clear(&s->coros);
	set_current_coro(s->co);
	return s;
}
Exemplo n.º 26
0
int32_t connector_connect(connector_t c,const char *ip,uint32_t port,on_connect call_back,void *ud,uint32_t ms)
{
	struct sockaddr_in remote;
	HANDLE sock;
	struct pending_connect *pc;	
	sock = OpenSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if(sock < 0)
		return -1;
	
	remote.sin_family = AF_INET;
	remote.sin_port = htons(port);
	if(inet_pton(INET,ip,&remote.sin_addr) < 0)
	{

		printf("%s\n",strerror(errno));
		return -1;
	}
	if(ms>0)
	{
		if(setNonblock(sock)!=0)
			return -1;
	}
	if(Connect(sock, (struct sockaddr *)&remote, sizeof(remote)) == 0)
	{
		//连接成功,无需要后续处理了,直接调用回调函数
		call_back(sock,ip,port,ud);
		return 0;
	}
	
	socket_t s = GetSocketByHandle(sock);

	pc = malloc(sizeof(*pc));
	pc->lnode.next = NULL;
	pc->sock = sock;
	pc->ip = ip;
	pc->port = port;
	pc->call_back = call_back;
	pc->timeout = GetSystemMs() + ms;
	pc->ud = ud;
	pc->real_fd = s->fd;
	mutex_lock(c->lock);
	LINK_LIST_PUSH_BACK(c->extern_pending_connect,pc);
	mutex_unlock(c->lock);
	return 0;
}
Exemplo n.º 27
0
static inline void tick_super_object(struct map *m,struct aoi_object *o)
{
	uint32_t now = GetSystemMs();
	if(now - o->last_update_tick >= UPDATE_INTERVAL)
	{ 
		//remove out of view object first
		uint32_t i = 0;
		for( ; i < MAX_BITS; ++i)
		{
			if(o->self_view_objs.bits[i] > 0)
			{
				uint32_t j = 0;
				for( ; j < sizeof(uint32_t); ++j)
				{
					if(o->self_view_objs.bits[i] & (1 << j))
					{
						uint32_t aoi_object_id = i*sizeof(uint32_t) + j;
						if(aoi_object_id != o->aoi_object_id)
						{
							struct aoi_object *other = m->all_aoi_objects[aoi_object_id];
							if(other->is_leave_map)
								leave_me(m,o,other);
							else
							{
								uint64_t distance = cal_distance_2D(&o->current_pos,&other->current_pos);
								if(distance > o->view_radius)
									leave_me(m,o,other);
							}
						}
					}
				}
			}
		}
		//process enter view
		uint32_t x1,y1,x2,y2;
		x1 = y1 = x2 = y2 = 0;
		cal_blocks(m,&o->current_pos,o->view_radius,&x1,&y1,&x2,&y2);
		uint32_t y = y1;
		uint32_t x;
		for( ; y <= y2; ++y)
		{
			for( x=x1; x <= x2; ++x)
			{
				struct map_block *bl = get_block(m,y,x);
				struct aoi_object *cur = (struct aoi_object*)bl->aoi_objs.head.next;
				while(cur != (struct aoi_object*)&bl->aoi_objs.tail)
				{
					if(is_set(&o->self_view_objs,cur->aoi_object_id) == 0)
					{
						uint64_t distance = cal_distance_2D(&o->current_pos,&cur->current_pos);
						if(o->view_radius >= distance)
							enter_me(m,o,cur);
					}
					cur = (struct aoi_object *)cur->block_node.next;
				}
			}		
		}		
		o->last_update_tick = now;	
	}
	
}
Exemplo n.º 28
0
int main()
{
	init_system_time(10);
	global_table_t gtb = global_table_create(65536);
	
	char key[64];
	uint32_t now = GetSystemMs();
	int32_t i = 0;
	for( ; i < 1000000; ++i)
	{
		basetype_t a = basetype_create_int32(i);
		snprintf(key,64,"test%d",i);
		a = global_table_insert(gtb,key,a,global_hash(key));
		if(!a)
			printf("error 1\n");
		basetype_release(&a);		
	}
	now = GetSystemMs() - now;
	printf("%u\n",now);
	now = GetSystemMs();
	int j = 0;
	for( ; j < 2; ++j)
		for(i=0 ; i < 1000000; ++i)
		{
			basetype_t a;
			snprintf(key,64,"test%d",i);
			a = global_table_find(gtb,key,global_hash(key));
			if(!a)
				printf("error 2\n");
			if(i != basetype_get_int32(a))
				printf("error 3\n");
			basetype_release(&a);		
		}
	now = GetSystemMs() - now;
	printf("%u\n",now);
	printf("table_size:%u\n",global_table_size(gtb));
	now = GetSystemMs();
	for(i=0; i < 1000000; ++i)
	{
		snprintf(key,64,"test%d",i);
		basetype_t a = global_table_remove(gtb,key,global_hash(key));
		if(!a)
			printf("error 1\n");
		basetype_release(&a);		
	}
	now = GetSystemMs() - now;
	printf("%u\n",now);	
	printf("table_size:%u\n",global_table_size(gtb));
	
	
	{
		printf("test1\n");
		//测试简单类型
		basetype_t a = basetype_create_int32(10);
		
		a = global_table_insert(gtb,"a",a,global_hash("a"));
		if(!a)
			printf("error 1\n");
		basetype_release(&a);	
			
		a = global_table_remove(gtb,"a",global_hash("a"));
		if(!a)
			printf("error 2\n");
		printf("%d\n",basetype_get_int32(a));	
		basetype_release(&a);	
	}
	
	{
		printf("test2\n");
		//测试array类型
		basetype_t _array = db_array_create(10);
		int i = 0;
		for( ; i < 10; ++i)
		{
			basetype_t a = basetype_create_int32(i);
			db_array_set((db_array_t)_array,i,a);
			basetype_release(&a);
		}
		
		_array = global_table_insert(gtb,"a",_array,global_hash("a"));
		if(!_array)
			printf("error 1\n");
		basetype_release(&_array);	
			
		_array = global_table_remove(gtb,"a",global_hash("a"));
		if(!_array)
			printf("error 2\n");
		i = 0;
		for( ; i < 10; ++i)
		{
			basetype_t a = db_array_get((db_array_t)_array,i);
			printf("%d\n",basetype_get_int32(a));
			basetype_release(&a);
		}
		basetype_release(&_array);	
	}
	
	{
		printf("test3\n");
		//测试list类型
		basetype_t _list = db_list_create();
		int i = 0;
		for( ; i < 10; ++i)
		{
			basetype_t a = basetype_create_int32(i);
			db_list_append((db_list_t)_list,a);
			basetype_release(&a);
		}
		_list = global_table_insert(gtb,"a",_list,global_hash("a"));
		if(!_list)
			printf("error 1\n");
		basetype_release(&_list);	
			
		_list = global_table_remove(gtb,"a",global_hash("a"));
		if(!_list)
			printf("error 2\n");
		basetype_t a;
		while(a = db_list_pop((db_list_t)_list))
		{
			printf("%d\n",basetype_get_int32(a));
			basetype_release(&a);
		}
		basetype_release(&_list);	
	}
	global_table_destroy(&gtb);
	
	return 0;
}
Exemplo n.º 29
0
void connector_run(connector_t c,uint32_t ms)
{
	int32_t i = 0;
	uint32_t tick,_timeout,_ms;
	int32_t size;
	int32_t total;
	struct pending_connect *pc;
	struct timeval timeout;
	tick = GetSystemMs();
	_timeout = tick + ms;
	
	struct link_list *_l = LINK_LIST_CREATE();
	mutex_lock(c->lock);
	link_list_swap(_l,c->extern_pending_connect);
	mutex_unlock(c->lock);
	while(pc = LINK_LIST_POP(struct pending_connect*,_l))
	{
		if(c->fd_seisize >= FD_SETSIZE)
		{
			pc->call_back(-1,pc->ip,pc->port,pc->ud);
			free(pc);
		}
		else
		{
			FD_SET(pc->real_fd,&c->Set);
			LINK_LIST_PUSH_BACK(c->_pending_connect,pc);
			++c->fd_seisize;
		}
	}
	LINK_LIST_DESTROY(&_l);
	
	do{
		_ms = _timeout - tick;
		timeout.tv_sec = 0;
		timeout.tv_usec = 1000*_ms;
		size = list_size(c->_pending_connect);
		if(size == 0)
			return;
		if((total = select(1024,0,&c->Set,0, &timeout)) >0 )
		{
			for(; i < size; ++i)
			{
				pc = LINK_LIST_POP(struct pending_connect*,c->_pending_connect);
				if(pc)
				{
					if(FD_ISSET(pc->real_fd, &c->Set))
					{
						pc->call_back(pc->sock,pc->ip,pc->port,pc->ud);
						free(pc);
						--c->fd_seisize;
					}
					else
						LINK_LIST_PUSH_BACK(c->_pending_connect,pc);
				}
			}
		}
		FD_ZERO(&c->Set);
		tick = GetSystemMs();
		size = list_size(c->_pending_connect);
		i = 0;
		for(; i < (int32_t)size; ++i)
		{
			pc = LINK_LIST_POP(struct pending_connect*,c->_pending_connect);
			if(tick >= pc->timeout)
			{
				pc->call_back(-1,pc->ip,pc->port,pc->ud);
				free(pc);
				--c->fd_seisize;
			}
			else
			{
				LINK_LIST_PUSH_BACK(c->_pending_connect,pc);
				FD_SET(pc->real_fd,&c->Set);
			}
		}
		tick = GetSystemMs();
	}while(tick < _timeout);
}