Beispiel #1
0
int on_db_initfinish(lua_State *_){
	printf("on_db_initfinish\n");
	(void)_;
	//启动监听
	kn_sockaddr lgameserver;
	kn_addr_init_in(&lgameserver,kn_to_cstr(g_config->lgameip),g_config->lgameport);
	kn_new_stream_server(t_proactor,&lgameserver,on_new_game);

	kn_sockaddr lgateserver;
	kn_addr_init_in(&lgateserver,kn_to_cstr(g_config->lgateip),g_config->lgateport);
	kn_new_stream_server(t_proactor,&lgateserver,on_new_gate);
	
	return 0;
} 
Beispiel #2
0
static int luasocket_connect(lua_State *L){
	luasocket_t luasock = lua_touserdata(L,1);
	if(luasock->type != _SOCKET){
		lua_pushstring(L,"invaild socket");
		return 1;
	}
	
	if(!luaL_checkstring(L,2)){
		lua_pushstring(L,"invalid ip");
		return 1;
	}
	
	if(!luaL_checkinteger(L,3)){
		lua_pushstring(L,"invalid port");
		return 1;		
	}
	const char *ip = lua_tostring(L,2);
	int port = lua_tointeger(L,3);	
	kn_sockaddr host;
	kn_addr_init_in(&host,ip,port);
	int ret = kn_sock_connect(luasock->sock,&host,NULL);
	if(ret > 0){
		lua_pushnil(L);
		lua_pushinteger(L,ret);
	}else if(ret == 0){
		kn_engine_associate(g_engine,luasock->sock,cb_connect);
		lua_pushnil(L);
		lua_pushinteger(L,ret);		
	}else{
		lua_pushstring(L,"connect error");
		lua_pushnil(L);
	}	
	kn_sock_setud(luasock->sock,luasock);
	return 2;
}
Beispiel #3
0
int main(int argc,char **argv){
	kn_net_open();	
	kn_proactor_t p = kn_new_proactor();
	kn_sockaddr local;
	kn_fd_t l;
	kn_addr_init_in(&local,argv[1],atoi(argv[2]));
	l = kn_dgram_listen(p,SOCK_DGRAM,&local,transfer_finish);	
 	struct dgram_session *session = calloc(1,sizeof(*session));
	session->s = l;  
 	kn_fd_setud(l,session);
 	dgram_session_recv(session);    		
 	uint64_t tick,now;
    tick = now = kn_systemms64();	
	while(1){
		kn_proactor_run(p,50);
        now = kn_systemms64();
		if(now - tick > 1000)
		{
            uint32_t elapse = (uint32_t)(now-tick);
            totalbytes = (totalbytes/elapse)/1024;
			printf("totalbytes:%lldMB/s\n",totalbytes);
			tick = now;
			totalbytes = 0;
		}		
	}
	return 0;
}
Beispiel #4
0
static int luasocket_listen(lua_State *L){
	luasocket_t luasock = lua_touserdata(L,1);
	if(luasock->type == _SOCKET){
		if(!luaL_checkstring(L,2)){
			lua_pushstring(L,"invalid ip");
			return 1;
		}	
		if(!luaL_checkinteger(L,3)){
			lua_pushstring(L,"invalid port");
			return 1;		
		}	
		const char *ip = lua_tostring(L,2);
		int port = lua_tointeger(L,3);	
		kn_sockaddr local;
		kn_addr_init_in(&local,ip,port);
		int ret = listener_listen(luasock,&local);
		if(0 != ret){
			lua_pushstring(L,"listen error");
		}else{
			luasock->listening = 1;
			lua_pushnil(L);
		}
	}else if(luasock->type == _DATAGRAM){
		if(!luaL_checkstring(L,2)){
			lua_pushstring(L,"invalid ip");
			return 1;
		}	
		if(!luaL_checkinteger(L,3)){
			lua_pushstring(L,"invalid port");
			return 1;		
		}	
		const char *ip = lua_tostring(L,2);
		int port = lua_tointeger(L,3);	
		kn_sockaddr local;
		kn_addr_init_in(&local,ip,port);
		if(0 == kn_sock_listen(datagram_gethandle(luasock->datagram),&local)){
			lua_pushnil(L);
		}else{
			lua_pushstring(L,"listen error");
		}		
	}		
	return 1;		
}
Beispiel #5
0
int main(int argc,char **argv){
	signal(SIGPIPE,SIG_IGN);
	engine_t p = kn_new_engine();
	kn_sockaddr local;
	kn_addr_init_in(&local,argv[1],atoi(argv[2]));
	
	handle_t l = kn_new_sock(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	kn_sock_listen(p,l,&local,on_accept,p);
	kn_reg_timer(p,1000,timer_callback,NULL);		
	kn_engine_run(p);
	return 0;
}
Beispiel #6
0
int main(int argc,char **argv){
	signal(SIGPIPE,SIG_IGN);
	kn_addr_init_in(&remote,argv[1],atoi(argv[2]));
	engine_t p = kn_new_engine();
	ut_socket_init(p);
	uscheduler_init(8192);
	int uthread_count = atoi(argv[3]);
	int i =0;
	for(; i < uthread_count; ++i){
		ut_spawn(uthread_worker,NULL);
	}
	ut_socket_run();
	return 0;
}
int main(int argc,char **argv){
	kn_net_open();	
	kn_proactor_t p = kn_new_proactor();
	kn_sockaddr remote;
	kn_addr_init_in(&remote,argv[1],atoi(argv[2]));
	int client_count = atoi(argv[3]);
	int i = 0;
	send_size = atoi(argv[4]);
	for(; i < client_count; ++i){
		kn_asyn_connect(p,IPPROTO_TCP,SOCK_STREAM,NULL,&remote,on_connect,(void*)p,30000);
	} 
	while(1){
		kn_proactor_run(p,50);
	}
	return 0;
}
Beispiel #8
0
static int lua_getaddr(lua_State *L,int idx,kn_sockaddr *addr){
	if(!lua_istable(L,idx))
		return -1;
	lua_rawgeti(L,idx,1);
	int domain = lua_tointeger(L,-1);
	if(domain == AF_INET){
		lua_rawgeti(L,idx,2);
		const char *ip = lua_tostring(L,-1);
		lua_rawgeti(L,idx,3);
		uint32_t port = lua_tointeger(L,-1);
		kn_addr_init_in(addr,ip,port);
	}else if(domain == AF_LOCAL){
		lua_rawgeti(L,idx,2);
		const char *path = lua_tostring(L,-1);
		kn_addr_init_un(addr,path);
	}else{
		return -1;
	}
	return 0;
}
Beispiel #9
0
int main(int argc,char **argv){
	kn_net_open();	
	kn_proactor_t p = kn_new_proactor();
	kn_sockaddr local;
	kn_addr_init_in(&local,argv[1],atoi(argv[2]));
	kn_listen(p,&local,on_accept,(void*)p);
 	uint64_t tick,now;
    tick = now = kn_systemms64();	
	while(1){
		kn_proactor_run(p,50);
        now = kn_systemms64();
		if(now - tick > 1000)
		{
            uint32_t elapse = (uint32_t)(now-tick);
            totalbytes = (totalbytes/elapse)/1024;
			printf("client_count:%d,totalbytes:%lldMB/s\n",client_count,totalbytes);
			tick = now;
			totalbytes = 0;
		}		
	}
	return 0;
}
Beispiel #10
0
int main(int argc,char **argv){
	signal(SIGPIPE,SIG_IGN);	
	//create ioworker and logicprocessor
	logicprocessor_t logic = create_logic(on_new_connection,on_packet,on_disconnected);
	_worker = ioworker_new(logic);
	//run net worker thread
	ioworker_startrun(_worker);
	//run logic process thread
	logic_startrun(logic);

	//start listen
	engine_t p = kn_new_engine();
	kn_sockaddr local;
	kn_addr_init_in(&local,argv[1],atoi(argv[2]));
	
	handle_t l = kn_new_sock(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(0 == kn_sock_listen(l,&local)){
		kn_engine_associate(p,l,on_accept);
		kn_engine_run(p);
	}
	return 0;
}
Beispiel #11
0
int main(int argc,char **argv)
{
	kn_net_open();		
	kn_proactor_t p = kn_new_proactor();
	kn_sockaddr local;
	kn_addr_init_in(&local,argv[1],atoi(argv[2]));		
	kn_stream_server_t s = kn_new_stream_server(p,&local,new_client);
	uint64_t tick,now;
    tick = now = kn_systemms64();	
	while(1){
		kn_proactor_run(p,50);
        now = kn_systemms64();
		if(now - tick > 1000)
		{
            uint32_t elapse = (uint32_t)(now-tick);
            recvcount = recvcount*1000/elapse;
			printf("client_count:%d,recvcount:%d/s,buffer_count:%u\n",client_count,recvcount,buffer_count);
			tick = now;
			recvcount = 0;
		}		
	}	
	return 0;
}