void test_client_session::send_command( const char* cmd, const char* data, int data_length /*= 0*/ )
{
	static char s_send_buffer[_MAX_PACKET_LENGTH];

	int cmd_length = (int)strlen(cmd);
	int packet_data_length = cmd_length;
	memcpy(s_send_buffer, cmd, cmd_length);
	if (data) {
		if (data_length) {
			memcpy(s_send_buffer + cmd_length, data, data_length);
			packet_data_length += data_length;
		}
		else {
			memcpy(s_send_buffer + cmd_length, data, strlen(data));
			packet_data_length += (int)strlen(data);
		}
	}

	iod_packet* packet = create_packet();
	packet->set_data(s_send_buffer, packet_data_length);
	send(packet);
	destroy_packet(packet);

	last_send_command_time = iod_utility::get_time_msec();

	if (strcmp(cmd, SESSION_CMD_LOGIN) == 0 && get_login_state() == LOGIN_STATE_NONE) {
		login_stat = LOGIN_STATE_LOGINING;
	}
}
Esempio n. 2
0
static void make_multi_packet(SimplyMsg *self, SimplyPacket *packet) {
  if (!packet) {
    return;
  }
  size_t length = 0;
  SimplyPacket *last;
  for (SimplyPacket *walk = packet;;) {
    length += walk->length;
    SimplyPacket *next = (SimplyPacket*) walk->node.next;
    if (!next || length + next->length > APP_MSG_SIZE_OUTBOUND - 2 * sizeof(Tuple)) {
      last = next;
      break;
    }
    walk = next;
  }
  uint8_t *buffer = malloc(length);
  if (!buffer) {
    return;
  }
  uint8_t *cursor = buffer;
  for (SimplyPacket *walk = packet; walk && walk != last;) {
    memcpy(cursor, walk->buffer, walk->length);
    cursor += walk->length;
    SimplyPacket *next = (SimplyPacket*) walk->node.next;
    destroy_packet(self, walk);
    walk = next;
  }
  self->send_buffer = buffer;
  self->send_length = length;
}
Esempio n. 3
0
static int destroy_luapacket(lua_State *L) {
	lua_packet_t p = lua_getluapacket(L,1);
	if(p->_packet){ 
		destroy_packet(p->_packet);
		p->_packet = NULL;
	}
    return 0;
}
Esempio n. 4
0
int parse_gesture(const unsigned char *raw) {

    Packet *p = init_packet(raw);

    printf("%s\n", get_gesture(p));
    destroy_packet(p);

    return 0;

}
Esempio n. 5
0
int parse_packet(const unsigned char *raw) {

    Packet *p = init_packet(raw);

    printf("x=%f\ty=%f\tz=%f\tval=%f\n", p->acc_x, p->acc_y, p->acc_z, p->w);

    printf("gesture=%s\narm=%s\n", get_gesture(p), get_arm(p));
    destroy_packet(p);

    return 0;

}
Esempio n. 6
0
void uthread_worker(void *arg){
	ut_socket_t sock = ut_connect(&remote,4096,new_rpk_decoder(4096));
	if(!is_empty_ident(sock)){
		wpacket_t wpk = wpk_create(64);
		wpk_write_uint64(wpk,100);
		ut_send(sock,(packet_t)wpk);
		packet_t packet = NULL;
		int err = 0;
		while(ut_recv(sock,&packet,&err) == 0){
			packet_t wpk = make_writepacket(packet);
			destroy_packet(packet);
			ut_send(sock,wpk);
		}		
	}else{
		printf("connect error\n");
	}
}
Esempio n. 7
0
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");
}