minheap* minheap_new(int32_t size, int32_t (*less)(minheap_element*,minheap_element*)) { size = size_of_pow2(size); minheap *m = calloc(1,sizeof(*m)); m->data = (minheap_element**)calloc(size,sizeof(minheap_element*)); m->size = 0; m->max_size = size; m->less = less; return m; }
rawpacket* rawpacket_new(uint32_t size) { size = size_of_pow2(size); if(size < MIN_BUFFER_SIZE) size = MIN_BUFFER_SIZE; bytebuffer *b = bytebuffer_new(size); rawpacket *raw = (rawpacket*)CALLOC(g_rawpk_allocator,1,sizeof(*raw)); ((packet*)raw)->type = RAWPACKET; ((packet*)raw)->head = b; buffer_writer_init(&raw->writer,b,0); INIT_CONSTROUCTOR(raw); return raw; }
rawpacket *rawpacket_new(uint32_t size) { bytebuffer *b; rawpacket *raw; size = size_of_pow2(size); if(size < MIN_BUFFER_SIZE) size = MIN_BUFFER_SIZE; b = bytebuffer_new(size); raw = calloc(1,sizeof(*raw)); cast(packet*,raw)->type = RAWPACKET; cast(packet*,raw)->head = b; buffer_writer_init(&raw->writer,b,0); INIT_CONSTROUCTOR(raw); return raw; }
datagram *datagram_new(int32_t fd,uint32_t buffersize,decoder *d) { buffersize = size_of_pow2(buffersize); if(buffersize < MIN_RECV_BUFSIZE) buffersize = MIN_RECV_BUFSIZE; datagram *dgarm = calloc(1,sizeof(*dgarm)); datagram_socket_init((dgram_socket_*)dgarm,fd); dgarm->recv_bufsize = buffersize; dgarm->next_recv_buf = bytebuffer_new(buffersize); //save socket_ imp_engine_add,and replace with self if(!base_engine_add) base_engine_add = ((handle*)dgarm)->imp_engine_add; dgarm->imp_engine_add = imp_engine_add; dgarm->dctor = datagram_dctor; dgarm->decoder_ = d ? d:dgram_raw_decoder_new(); return dgarm; }
wpacket* wpacket_new(uint16_t size) { size = size_of_pow2(size); if(size < MIN_BUFFER_SIZE) size = MIN_BUFFER_SIZE; bytebuffer *b = bytebuffer_new(size); wpacket *w = (wpacket*)CALLOC(g_wpk_allocator,1,sizeof(*w)); ((packet*)w)->type = WPACKET; ((packet*)w)->head = b; buffer_writer_init(&w->writer,b,sizeof(*w->len)); w->len = (TYPE_HEAD*)b->data; ((packet*)w)->len_packet = SIZE_HEAD; ((packet*)w)->head->size = SIZE_HEAD; INIT_CONSTROUCTOR(w); return w; }
static int luasocket_establish(lua_State *L){ luasocket_t luasock = (luasocket_t)lua_touserdata(L,1); uint32_t recvbuf_size = lua_tointeger(L,2); decoder* _decoder = (decoder*)lua_touserdata(L,3); recvbuf_size = size_of_pow2(recvbuf_size); if(recvbuf_size < 1024) recvbuf_size = 1024; connection_t conn = new_connection(luasock->sock,recvbuf_size,_decoder); if(_decoder && _decoder->mask == mask_http_decode) connection_associate(g_engine,conn,on_http_cb,on_disconnected); else connection_associate(g_engine,conn,on_packet,on_disconnected); refobj_inc((refobj*)conn); luasock->type = _STREAM_CONN; luasock->streamconn = conn; connection_setud(conn,luasock,destroy_luasocket); return 0; }
wpacket_t wpk_create(uint32_t size) { size = size_of_pow2(size); if(size < 64) size = 64; wpacket_t w = (wpacket_t)CALLOC(g_wpk_allocator,1,sizeof(*w));//calloc(1,sizeof(*w)); packet_buf(w) = buffer_create(size); w->writebuf = packet_buf(w);//buffer_acquire(NULL,packet_buf(w)); //packet_begpos(w)= 0; //packet_next(w) = NULL; packet_type(w) = WPACKET; w->wpos = sizeof(*(w->len)); w->len = (uint32_t*)packet_buf(w)->buf; *(w->len) = 0; packet_buf(w)->size = sizeof(*(w->len)); packet_datasize(w) = sizeof(*(w->len)); packet_clone(w) = wpk_clone; packet_makeforwrite(w) = wpk_clone; packet_makeforread(w) = wpk_makeforread; return w; }
wpacket_t wpk_create_by_bin(int8_t *addr,uint32_t size) { uint32_t datasize = size; size = size_of_pow2(size); if(size < 64) size = 64; wpacket_t w = (wpacket_t)CALLOC(g_wpk_allocator,1,sizeof(*w));//calloc(1,sizeof(*w)); packet_buf(w) = buffer_create(size); w->writebuf = packet_buf(w);//buffer_acquire(NULL,packet_buf(w)); //packet_begpos(w)= 0; //packet_next(w) = NULL; packet_type(w) = WPACKET; memcpy(&w->writebuf->buf[0],addr,datasize); w->len = (uint32_t*)packet_buf(w)->buf; w->wpos = datasize; packet_buf(w)->size = datasize; packet_datasize(w) = datasize; packet_clone(w) = wpk_clone; packet_makeforwrite(w) = wpk_clone; packet_makeforread(w) = wpk_makeforread; return w; }
static int lua_new_packet(lua_State *L,int packettype){ int argtype = lua_type(L,1); if(packettype == WPACKET){ if(argtype == LUA_TNUMBER || argtype == LUA_TNIL || argtype == LUA_TNONE){ //参数为数字,构造一个初始大小为len的wpacket size_t len = 0; if(argtype == LUA_TNUMBER) len = size_of_pow2(lua_tointeger(L,1)); if(len < 64) len = 64; lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = (packet_t)wpk_create(len); return 1; }else if(argtype == LUA_TSTRING){ size_t len; char *data = (char*)lua_tolstring(L,1,&len); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = (packet_t)wpk_create_by_bin((int8_t*)data,len); return 1; }else if(argtype == LUA_TUSERDATA){ lua_packet_t other = lua_getluapacket(L,1); if(!other) return luaL_error(L,"invaild opration for arg1"); if(other->_packet->type == RAWPACKET) return luaL_error(L,"invaild opration for arg1"); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = make_writepacket(other->_packet);//(packet_t)wpk_copy_create(other->_packet); return 1; }else if(argtype == LUA_TTABLE){ wpacket_t wpk = wpk_create(512); if(0 != lua_pack_table(wpk,L,-1)){ destroy_packet((packet_t)wpk); return luaL_error(L,"table should not hava metatable"); //lua_pushnil(L); }else{ lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = (packet_t)wpk; } return 1; } else return luaL_error(L,"invaild opration for arg1"); }else if(packettype == RAWPACKET){ if(argtype == LUA_TSTRING){ //参数为string,构造一个函数数据data的rawpacket size_t len; char *data = (char*)lua_tolstring(L,1,&len); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = (packet_t)rawpacket_create2(data,len); return 1; }else if(argtype == LUA_TUSERDATA){ lua_packet_t other = lua_getluapacket(L,1); if(!other) return luaL_error(L,"invaild opration for arg1"); if(other->_packet->type != RAWPACKET) return luaL_error(L,"invaild opration for arg1"); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = clone_packet(other->_packet); return 1; }else return luaL_error(L,"invaild opration for arg1"); }else if(packettype == RPACKET){ if(argtype == LUA_TUSERDATA){ lua_packet_t other = lua_getluapacket(L,1); if(!other) return luaL_error(L,"invaild opration for arg1"); if(other->_packet->type == RAWPACKET) return luaL_error(L,"invaild opration for arg1"); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = make_readpacket(other->_packet); return 1; }else return luaL_error(L,"invaild opration for arg1"); }else return luaL_error(L,"invaild packet type"); }