示例#1
0
static int luasocket_new1(lua_State *L){
	luaRef_t obj = toluaRef(L,1);
	int domain = lua_tointeger(L,2);
	int type = lua_tointeger(L,3);
	luasocket_t luasock;
	handle_t sock;
	if(type == SOCK_STREAM){
		sock = kn_new_sock(domain,type,IPPROTO_TCP);	
		if(!sock){
			lua_pushnil(L);
			return 1;
		}
		luasock = calloc(1,sizeof(*luasock));
		luasock->type = _SOCKET;
		kn_sock_setud(sock,luasock);	
	}else if(type == SOCK_DGRAM){
		uint32_t    recvbuf_size = lua_tointeger(L,4);
		decoder*    _decoder = (decoder*)lua_touserdata(L,5);
		luasock = calloc(1,sizeof(*luasock));
		luasock->datagram = new_datagram(domain,recvbuf_size,_decoder);
		luasock->type = _DATAGRAM;
		sock = datagram_gethandle(luasock->datagram);				
		datagram_setud(luasock->datagram,luasock,destroy_luasocket);
		datagram_associate(g_engine,luasock->datagram,on_datagram); 	
	}else{
		lua_pushnil(L);
		return 1;
	}
	luasock->sock = sock;
	luasock->luaObj = obj;	
	lua_pushlightuserdata(L,luasock);
	return 1;
}
示例#2
0
static int luasocket_new2(lua_State *L){
	luaRef_t obj = toluaRef(L,1);	
	handle_t sock =   lua_touserdata(L,2);
	luasocket_t luasock = calloc(1,sizeof(*luasock));
	luasock->type = _SOCKET;
	luasock->sock = sock;
	luasock->luaObj = obj;	
    	kn_sock_setud(sock,luasock);	
	lua_pushlightuserdata(L,luasock);	
	return 1;	
}
示例#3
0
static int lua_create_aoi_obj(lua_State *L){
	luaRef_t *obj = calloc(1,sizeof(*obj));	
	*obj = toluaRef(L,1);
	aoi_object* o = calloc(1,sizeof(*o));
	o->in_myscope = in_myscope;
	o->cb_enter = cb_enter;
	o->cb_leave = cb_leave;
	o->ud = obj;
	o->view_objs = new_bitset(4096);
	lua_pushlightuserdata(L,o);
	return 1;
}
示例#4
0
int lua_redis_connect(lua_State *L){
	const char *ip = lua_tostring(L,1);
	uint16_t    port = (uint16_t)lua_tointeger(L,2);
	luaRef_t   *cbObj = calloc(1,sizeof(*cbObj)); 	
	*cbObj = toluaRef(L,3);	
	int ret = kn_redisAsynConnect(g_engine,ip,port,cb_connect,cb_disconnected,(void*)cbObj);
	if(ret != 0){
		release_luaRef(cbObj);
		free(cbObj);
		lua_pushstring(L,"connect to redis error");
	}else
		lua_pushnil(L);
	return 1; 
}
示例#5
0
int lua_redisCommandSync(lua_State *L){
	redisconn_t conn = lua_touserdata(L,1);
	const char *cmd = lua_tostring(L,2);		
	do{
		if(!cmd || strcmp(cmd,"") == 0){
			lua_pushboolean(L,0);
			break;
		}		
		luaRef_t   *cbObj = calloc(1,sizeof(*cbObj)); 	
		*cbObj = toluaRef(L,3);			
		if(REDIS_OK!= kn_redisCommand(conn,cmd,redis_command_cb,cbObj)){
			release_luaRef(cbObj);
			free(cbObj);			
			lua_pushboolean(L,0);
		}else
			lua_pushboolean(L,1);
	}while(0);	
	return 1;
}
示例#6
0
文件: testlua.c 项目: Oooocean/chuck
int main(int argc,char **argv){
	if(argc < 2){
		printf("usage testlua luafile\n");
		return 0;
	}
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	if (luaL_dofile(L,argv[1])) {
		const char * error = lua_tostring(L, -1);
		lua_pop(L,1);
		printf("%s\n",error);
	}
	const char *err;
	printf("------makeLuaObjByStr---------\n\n");
	//dynamic build a lua table and return
	luaRef tabRef = makeLuaObjByStr(L,"return {a=100}");
	if(tabRef.L){
		int a = 0;
		err = LuaRef_Get(tabRef,"si","a",&a);
		if(err) printf("%s\n",err);
		else{
			printf("a == %d\n",a);	
		}
		release_luaRef(&tabRef);
	}


	printf("------call 0 arg---------\n\n");
	err = LuaCall(L,"fun0",NULL);
	if(err) printf("error on fun0:%s\n",err);
	

	printf("------call 2 arg(integer,str),1 ret(integer)---------\n\n");

	unsigned int iret1;
	err = LuaCall(L,"fun1","is:i",0xfffffffff,"hello",&iret1);
	if(err) printf("error on fun1:%s\n",err);
	else printf("ret1=%u\n",iret1);
	
	printf("------call 2 arg(str),1 ret(str)---------\n\n");
	char *sret1;
	err = LuaCall(L,"fun2","ss:s","hello","kenny",&sret1);
	if(err) printf("error on fun2:%s\n",err);
	else printf("sret1=%s\n",sret1);
	
	printf("------call 3 arg(str,str,lua str),2 ret(lua str)---------\n\n");
	char  *Sret1,*Sret2;
	size_t Lret1,Lret2;
	err = LuaCall(L,"fun3","ssS:SS","1","2","3",sizeof("3"),&Sret1,&Lret1,&Sret2,&Lret2);
	if(err) printf("error on fun3:%s\n",err); 
	else{
		printf("%s %ld\n",Sret1,Lret1);
		printf("%s %ld\n",Sret2,Lret2);		
	}
	
	printf("------call 0 arg, 1 ret(luaobj)---------\n\n");
	luaRef funRef;
	err = LuaCall(L,"fun5",":r",&funRef);
	if(err) printf("error on fun5:%s\n",err);
	int num = -1;
	int num2 = -1;
	LuaCallRefFunc(funRef,":ii",&num,&num2);
	printf("%d,%d\n",num,num2);	
	
	
	printf("------call lua obj function---------\n\n");	
	err = LuaCall(L,"fun6",":ri",&tabRef,&num);
	if(err) printf("error on fun6:%s\n",err);
	printf("%d\n",num);
	err = LuaCallTabFuncS(tabRef,"hello",NULL);
	if(err) printf("%s\n",err);
	
	printf("------set and get luaobj field---------\n\n");	
	err = LuaRef_Set(tabRef,"siss","f1",99,"f2","hello haha");
	if(err) printf("%s\n",err);
	else{
		int f1;
		const char *f2;
		err = LuaRef_Get(tabRef,"siss","f1",&f1,"f2",&f2);
		if(err) printf("%s\n",err);
		else printf("get %d,%s\n",f1,f2);
	}

	err = LuaRef_Set(tabRef,"sp","f1",NULL);
	if(err) printf("%s\n",err);
	else{
		err = LuaCall(L,"fun7",NULL);
		if(err) printf("error on fun7:%s\n",err);		
	}
	
	printf("------iterate luaobj-------------\n\n");
	lua_getglobal(L,"ttab4");
	tabRef = toluaRef(L,-1);
	LuaTabForEach(tabRef){
		const char *key = lua_tostring(L,EnumKey);
		const char *val = lua_tostring(L,EnumVal);
		printf("%s,%s\n",key,val);
	}

	printf("end\n");	
	return 0;	
}