Exemplo n.º 1
0
Arquivo: friend.c Projeto: a1406/txgg
int handle_friend_del_blacklist_request(uint32_t session, const uint8_t *data, uint16_t len)
{
	int ret = 0;
	ROLE *role;	
	FriendDelBlacklistRequest *req = NULL;
	FriendDelBlacklistResponse resp;
	size_t size;
	uint64_t id;
	PROTO_HEAD_CONN *head = (PROTO_HEAD_CONN *)toclient_send_buf;	

	role = get_role_by_session(session);
	if (unlikely(!role)) {
		log4c_category_log(mycat, LOG4C_PRIORITY_INFO,
			"%s: get role obj failed[%u]", __FUNCTION__, session);				
		return (-1);
	}
	friend_del_blacklist_response__init(&resp);
	req = friend_del_blacklist_request__unpack(NULL, len, data);
	if (unlikely(!req)) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: unpack failed by role[%u][%lu] failed", __FUNCTION__, session, role->role_id);
		ret = -10;
		goto done;
	}
	resp.role_id = req->role_id;
	resp.area_id = req->area_id;

	id = ((uint64_t)req->area_id << 32) | req->role_id;
	ret = del_role_blacklist(role, id);
	
done:
	if (req)
		friend_del_blacklist_request__free_unpacked(req, NULL);
	size = friend_del_blacklist_response__pack(&resp, &(toclient_send_buf[sizeof(PROTO_HEAD_CONN)]));
	size += sizeof(PROTO_HEAD_CONN);
	resp.result = ret;
	head->msg_id = htons(CS__MESSAGE__ID__FRIEND_DEL_BLACKLIST_RESPONSE);
	head->len = htons(size);
	head->fd = ROLE_FD(role);

	if (size != send_to_role(role, (PROTO_HEAD *)head)) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: send to %lu failed", __FUNCTION__, role->role_id);		
	}
	return (ret);
}
Exemplo n.º 2
0
int main(int argc, char** argv)
{
  struct timeval start_time;
  struct timeval now_time;
  int looptime = 0;
  log4c_category_t* mycat = NULL;
  int i = 0;
  
  if (argc < 2)
  {
    printf("usage: %s loop_time_in_seconds\n",argv[0]);
    exit (1);
  }
  if (sscanf(argv[1],"%d",&looptime) != 1)
  {
    printf("could not convert %s to number of seconds to loop\n",argv[1]);
    exit(1);
  }
  
  /* You could put your category class into a file with a wrapper macro
  * to make calling it easier and more consistent
  */
  
  log4c_init();
  mycat = log4c_category_get("six13log.log.app.application1");
  
  i = 0;
  while ( (now_time.tv_sec - start_time.tv_sec) < looptime)
  {
    log4c_category_log(mycat, LOG4C_PRIORITY_DEBUG, "Debugging app 1 - loop %d", i);
    log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
      "some error from app1 at line %d in file %s - loop %d",
      __LINE__, __FILE__, i);
    
    sleep(3);
    
    i++;
  }
  
  /* Explicitly call the log4c cleanup routine */
  if ( log4c_fini()){
    printf("log4c_fini() failed");
  }
  
  return 0;
}
Exemplo n.º 3
0
static int kick_role(CLIENT_MAP *client)
{
	CLIENT_MAP *kick_role;
	PROTO_HEAD_CONN *head_conn;
	head_conn = (PROTO_HEAD_CONN *)(client->buf);

	log4c_category_log(mycat, LOG4C_PRIORITY_INFO,
		"%s: kick role from fd [%u]", __FUNCTION__, (head_conn->fd));	
		//todo send msg to client

	kick_role = get_client_map_by_fd(head_conn->fd, &client_map_head);
	if (kick_role)
		remove_listen_callback_event(kick_role);			
	else
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: can not find client_map from fd %u", __FUNCTION__, head_conn->fd);
	return (0);
}
Exemplo n.º 4
0
Arquivo: util.c Projeto: peplin/spade
int check_error(int result, const char* function) {
    if(result < 0) {
        log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_WARN,
                           "ERROR: %s failed with error %d: %s\n", function, errno,
                           strerror(errno));
        return -1;
    }
    return 0;
}
Exemplo n.º 5
0
int game_add_listen_event(uint16_t port, LISTEN_CALLBACKS *callback)
{
	struct event *event_accept = &callback->event_accept;
	int fd = 0;
	struct sockaddr_in addr;

	if (!callback || !callback->cb_get_client_map)
		return (-1);
	
	fd = create_new_socket(1);
	if (fd < 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s: create new socket failed[%d]", __FUNCTION__, errno);
		return (-2);		
	}
	evutil_make_listen_socket_reuseable(fd);	
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = htonl(0);
	addr.sin_port = htons(port);	
	if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s: bind failed[%d]", __FUNCTION__, errno);
		return (-10);
	}
	if (listen(fd, 128) != 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s: listen failed[%d]", __FUNCTION__, errno);
		return (-20);		
	}
	if (0 != event_assign(event_accept, base, fd, EV_READ|EV_PERSIST, cb_listen, (void *)callback)) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s: event_new failed[%d]", __FUNCTION__, errno);
		goto fail;
	}
	event_add(event_accept, NULL);
	return (0);
fail:
	if (fd > 0) {
		evutil_closesocket(fd);				
	}
	if (event_accept) {
		event_del(event_accept);				
	}
	return (-1);
}
Exemplo n.º 6
0
int create_new_socket(int set_opt)
{
	int fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd <= 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: socket failed[%d]", __FUNCTION__, __LINE__, errno);		
		return fd;
	}

	if (likely(set_opt))
		game_set_socket_opt(fd);
	return fd;
}
Exemplo n.º 7
0
void AudioStream::print_stats()
{
	//log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "Audio Frame Stats");

	for(auto it = pkt_list.begin(), ite = pkt_list.end(); it != ite; it++)
	{
		if((*it)->PUSI_flag)
		{
			log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "Audio pkt %d offset %d PTS %llu Duration %d", (*it)->pkt_count, (*it)->byte_offset, (*it)->pts, (*it)->duration_pts_ms);
		}
	}
}
Exemplo n.º 8
0
void startPerfTunerPthread(DecoderMetadata decoderMetadata) {
    printf("Printing accuracy - %d", printAccuracyLog);
    fflush(stdout);
    log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO, "\nStarting Performance Tuning For CPU\n");
    log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO, "===================================\n\n");
    int i, j;
    time_t start_t, end_t;
    double diff_t;
    struct timespec tstart = {0, 0}, tend = {0, 0};
    time(&start_t);
    clock_gettime(CLOCK_MONOTONIC, &tstart);
    log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO,
                       "Number of packets being decoded - %d\n\n", decoderMetadata.size);
    for (i = 0; i < tunerLoopCount; i++) {
        DecodedContext *pStruct = decodePthread(decoderMetadata);
        if(printAccuracyLog) {
         printf("Printing accuracy logs - %d", decoderMetadata.size);
         fflush(stdout);
         int j;
         for(j = 0; j < decoderMetadata.size; j++){
           DecodedContext decodedContext = pStruct[j];
           BaseSmReq baseSmReq = *(BaseSmReq *) decodedContext.pduStruct;
           log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO, "source-address-value - %s|", baseSmReq.sourceAddress->addressValue);
           log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO, "destination-address-value - %s|", baseSmReq.destinationAddress->addressValue);
           log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO, "message - %s\n", baseSmReq.shortMessage);
         }
        }
        free(pStruct);
    }
    time(&end_t);
    clock_gettime(CLOCK_MONOTONIC, &tend);
    diff_t = (((double) tend.tv_sec + 1.0e-9 * tend.tv_nsec) - ((double) tstart.tv_sec + 1.0e-9 * tstart.tv_nsec)) /
             tunerLoopCount;
    log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO, "TimeTaken - %.5f \n", diff_t);
    log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO, "Throughput - %.5f \n", (decoderMetadata.size) / diff_t);
    log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO, "Time for a single packet (Micro-Seconds) - %.5f \n\n",
                       (diff_t * 1000000) / (decoderMetadata.size));
    log4c_category_log(cpuTunerCategory, LOG4C_PRIORITY_INFO, " =====================\n\n");
}
Exemplo n.º 9
0
static int transfer_to_client(CLIENT_MAP *client)
{
	uint16_t old_len, old_msgid;
	PROTO_HEAD *head;	
	PROTO_HEAD_CONN *head_conn;
	uint16_t fd;
	assert(client);
	head = (PROTO_HEAD *)(client->buf + sizeof(uint16_t));
	head_conn = (PROTO_HEAD_CONN *)(client->buf);

	if (head_conn->msg_id == SERVER_PROTO_BROADCAST)
		return broadcast_to_client(client);
	else if (htons(head_conn->msg_id) == SERVER_PROTO_KICK_ROLE_NOTIFY)
		return kick_role(client);
	
	fd = head_conn->fd;

	old_len = htons(head_conn->len);
	old_msgid = htons(head_conn->msg_id);
	head->msg_id = head_conn->msg_id;
	head->len = htons(old_len - sizeof(uint16_t));

	if (old_len == 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: len wrong drop all msg", __FUNCTION__);
		return (-1);
	}

//	int ret = send(fd, head, htons(head->len), 0);
	int ret = send_one_msg(fd, head, 1);
	if (ret != htons(head->len)) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: send to client failed err[%d]", __FUNCTION__, errno);
		return (0);
	}
	head_conn->len = htons(old_len);
	head_conn->msg_id = htons(old_msgid);	
	return (0);
}
Exemplo n.º 10
0
int send_one_msg(evutil_socket_t fd, PROTO_HEAD *head, uint8_t force)
{
	int ret;
	char *p = (char *)head;
	int len = htons(head->len);
	for (;;) {
		ret = send(fd, p, len, 0);
		assert(ret <= len);
		if (ret == len)
			goto done;
		if (ret < 0) {
			if (errno != EAGAIN || force)
				goto fail;
				//ignore EINPROGRESS
			usleep(100);			
		} else if (ret < len) {
			len -= ret;
			p += ret;
			usleep(10);
		}
	}
done:	
	log4c_category_log(mycat, LOG4C_PRIORITY_DEBUG, "%s %d: send msg[%d] len[%d], ret [%d]",
		__FUNCTION__, fd, htons(head->msg_id), htons(head->len), ret);
#ifdef CALC_NET_MSG
	uint16_t id = htons(head->msg_id);
	if (id < CS__MESSAGE__ID__MAX_MSG_ID) {
		send_buf_size[id] += len;
		++send_buf_times[id];
	}
#endif	
	
	return (htons(head->len));
fail:
	log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: msg[%d] len[%d] ret[%d] errno[%d]",
		__FUNCTION__, fd, htons(head->msg_id), htons(head->len), ret, errno);			
	return ret;
}
Exemplo n.º 11
0
int conn_cb_server_recv(evutil_socket_t fd)
{
	CLIENT_MAP *client;
	int ret;
	client = get_client_map_by_fd(fd, &server_map_head);
	if (client == NULL) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: can not find client_map from fd %u", __FUNCTION__, fd);
//		del_client_map_by_fd(fd, &server_maps[0], (int *)&num_server_map);		
		return (-1);
	}
	ret = get_one_buf(fd, client);
	if (ret < 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: connect closed from fd %u", __FUNCTION__, fd);
//		del_client_map_by_fd(fd, &server_maps[0], (int *)&num_server_map);		
		return (-10);		
	}
	else if (ret > 0) {
		return (0);
	}

	for (;;) {
		if (transfer_to_client(client) != 0) {
			//todo close connect and return
			//remove_one_buf(client);
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
				"%s: transfer_to_client fail", __FUNCTION__);
				//client->pos_end = client->pos_begin;
				//break;
		}
		ret = remove_one_buf(client);
		assert(ret >= 0);
		if (ret == 0)
			break;
	}
	return (0);
}
Exemplo n.º 12
0
static CLIENT_MAP *get_server_client_map(evutil_socket_t fd)
{
	CLIENT_MAP *ret;
	static CLIENT_MAP client;

	ret = get_first_client_map(&server_map_head);
	if (ret) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s: a server already connected", __FUNCTION__);
		return NULL;
	}
	
	client.fd = fd;
	client.server_id = 0;
	client.pos_end = client.pos_begin = 0;
	client.cb_recv = conn_cb_server_recv;
	client.cb_release = release_server_client_map;

	ret = add_client_map(&client, &server_map_head);
	if (!ret)
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: add_client_map failed[%d]", __FUNCTION__, __LINE__, errno);
	assert(ret == &client);
	return (ret);
}
Exemplo n.º 13
0
int manage_packet(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data2)
{
    struct nfqnl_msg_packet_hdr *packetHeader;
    uint32_t id;

    packetHeader = nfq_get_msg_packet_hdr(packet);
    if(packetHeader != NULL ) {
        id = packetHeader->packet_id;

        /* We should only be inspecting packets on the INPUT hook;
        * any others, return NF_ACCEPT immediately.
        */
        if(packetHeader->hook != NF_IP_LOCAL_IN) {
            return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
        }

        log4c_category_log(log4c_cat, LOG4C_PRIORITY_NOTICE, "manage_packet: Received packet.");
    } else {
        log4c_category_log(log4c_cat, LOG4C_PRIORITY_DEBUG, "Error in nfq_get_msg_packet_hdr");
    }

    return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
Exemplo n.º 14
0
static void cb_log_object_timer(evutil_socket_t fd, short events, void *arg)
{
	static struct timeval t1 = {5, 0};
	int num_container, num_thing_obj, num_role, num_scene, num_instance;
	add_timer(t1, &log_object_timer);
	log_object_timer.ev_callback = cb_log_object_timer;
	num_container = get_container_pool_num();	
	num_thing_obj = get_thing_obj_pool_num();
	num_role = get_role_pool_num();
	num_scene = get_scene_pool_num();
	num_instance = get_instance_pool_num();
	log4c_category_log(obj_count_loger, LOG4C_PRIORITY_INFO, "game_srv count obj: container[%d] thing_obj[%d] role[%d] scene[%d] instance[%d]",
		num_container, num_thing_obj, num_role, num_scene, num_instance);
}
void readFromSlave(aeEventLoop *el, int fd, void *privdata, int mask){
	REDIS_NOTUSED(el);
	REDIS_NOTUSED(mask);
	char rdata[MAX_READ];
	int nread = readData(el, fd, rdata, MAX_READ);
	if(nread <= 0 )
	{
		return;
	}
	if(nread == 6){
		rdata[6]= 0;
		char *sync = "SYNC\r\n";
		if(strcmp(sync, rdata) == 0){
			log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "sync start");
			state = SYNC_START_STATE;
		}		
	}
	int wr = writeData(el, master_fd, rdata, nread);
	if(-1 == wr)
	{
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "write to master failed error %d, at line %d in file %s",  errno,  __LINE__, __FILE__);
	}
}
Exemplo n.º 16
0
void configure_dirt_handlers(spade_server* server, config_t* configuration) {
    config_setting_t* handler_settings = config_lookup(configuration,
            "dirt.handlers");
    if (!handler_settings) {
        log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                "No Dirt handlers registered");
        return;
    }
    int dirt_handler_count = config_setting_length(handler_settings);

    for (int n = 0; n < dirt_handler_count; n++) {
        config_setting_t* handler_setting = config_setting_get_elem(
                handler_settings, n);
        const char* library = NULL;
        config_setting_lookup_string(handler_setting, "library", &library);

        const char* handler = NULL;
        config_setting_lookup_string(handler_setting, "handler", &handler);

        const char* url = NULL;
        config_setting_lookup_string(handler_setting, "url", &url);

        if(!register_dirt_handler(server, url, handler, library)) {
            log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                    "Registered Dirt handler '%s' for URL prefix '%s'",
                    handler, url);
        }
    }
    if (server->dirt_handler_count == 0) {
        log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                "No Dirt handlers registered");
    } else {
        log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                "Registered a total of %d Dirt handlers",
                server->dirt_handler_count);
    }
}
Exemplo n.º 17
0
static CLIENT_MAP *get_client_client_map(evutil_socket_t fd)
{
	CLIENT_MAP *ret;
	CLIENT_MAP *client;
	client = create_client_map();
	if (!client) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s: create_client_map failed[%u]", __FUNCTION__, fd);		
		return NULL;
	}
	client->fd = fd;
	client->server_id = 0;
	client->pos_end = client->pos_begin = sizeof(uint16_t);  // 预留两个字节为  conn_head和proto_head的fd字段
	client->cb_recv = conn_cb_client_recv;
	client->cb_release = release_client_client_map;
	
	ret = add_client_map(client, &client_map_head);
	if (!ret) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s: add_client_map failed[%u]", __FUNCTION__, fd);
		destory_client_map(client);
		return NULL;
	}
	assert(ret == client);
	return (client);
}
Exemplo n.º 18
0
int add_signal(int signum, struct event *event)
{
	if (!event) {
		event = evsignal_new(base, signum, cb_signal, NULL);
		if (!event) {
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: evsignal_new failed[%d]", __FUNCTION__, __LINE__, errno);					
			return (-1);
		}
		event->ev_arg = event;
	} else {
		evsignal_assign(event, base, signum, event->ev_callback, NULL);
	}

	return evsignal_add(event, NULL);
}
Exemplo n.º 19
0
int add_timer(struct timeval t, struct event *event_timer)
{
	if (!event_timer) {
		event_timer = evtimer_new(base, cb_timer, NULL);
		if (!event_timer) {
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: evtimer_new failed[%d]", __FUNCTION__, __LINE__, errno);					
			return (-1);
		}
		event_timer->ev_arg = event_timer;
	} else {
		evtimer_assign(event_timer, base, event_timer->ev_callback, NULL);
	}

	return evtimer_add(event_timer, &t);
}
Exemplo n.º 20
0
static int transfer_to_gameserver(CLIENT_MAP *client)
{
	uint16_t old_len;
	PROTO_HEAD *head;
	PROTO_HEAD_CONN *head_conn;
	CLIENT_MAP *server;	
	assert(client);
	head = (PROTO_HEAD *)CLIENT_MAP_BUF_HEAD(client);
	head_conn = (PROTO_HEAD_CONN *)(client->buf);
	old_len = htons(head->len);
	
	head_conn->len = htons(old_len + sizeof(uint16_t));
	head_conn->msg_id = head->msg_id;	
	head_conn->fd = client->fd;

	server = get_first_client_map(&server_map_head);
	
	if (unlikely(!server)) {
		log4c_category_log(mycat, LOG4C_PRIORITY_INFO,
			"%s: do not have game server connected, cache it", __FUNCTION__);
		if (cache_to_gamesrv_buf((PROTO_HEAD *)head_conn) != 0) {
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
				"%s: cache_to_gamesrv_buf failed", __FUNCTION__);			
		}
		goto done;
	}
	
	if (send_one_msg(server->fd, (PROTO_HEAD *)head_conn, 0) != htons(head_conn->len)) {		
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: send to gameserver failed err[%d]", __FUNCTION__, errno);
		goto done;
	}
done:
	head->len = htons(old_len);
	return (0);
}
Exemplo n.º 21
0
void cb_listen(evutil_socket_t fd, short events, void *arg)
{
	assert(arg);
	CLIENT_MAP *client_map = NULL;
	int new_fd;
	LISTEN_CALLBACKS *callback = (LISTEN_CALLBACKS *)arg;
	if (callback->cb_listen_pre && callback->cb_listen_pre() < 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "%s: connect pre refused", __FUNCTION__);		
		goto fail;
	}
	
	log4c_category_log(mycat, LOG4C_PRIORITY_DEBUG, "%s: fd = %d, events = %d, arg = %p", __FUNCTION__, fd, events, arg);
	struct sockaddr_storage ss;
	size_t socklen = sizeof(ss);
	new_fd = accept(fd, (struct sockaddr*)&ss, (socklen_t *)&socklen);
	if (new_fd < 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: accept failed[%d]", __FUNCTION__, __LINE__, errno);
		goto fail;
	}
	game_set_socket_opt(new_fd);	

	client_map = callback->cb_get_client_map(new_fd);
	if (!client_map) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s: cb_get_client_map with fd[%u] failed", __FUNCTION__, __LINE__, new_fd);		
		goto fail;
	}
	
	if (0 != event_assign(&client_map->event_recv, base, new_fd, EV_READ | EV_PERSIST, cb_recv, client_map)) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: event_assign failed[%d]", __FUNCTION__, __LINE__, errno);
		goto fail;
	}

	if (event_add(&client_map->event_recv, NULL) != 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: event_assign failed[%d]", __FUNCTION__, __LINE__, errno);
		goto fail;			
	}

	if (callback->cb_listen_after && callback->cb_listen_after(new_fd) < 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "%s: connect after refused", __FUNCTION__);
		goto fail;
	}
	return;
fail:
	if (client_map) {
		remove_listen_callback_event(client_map);
	}
	if (new_fd > 0)
		evutil_closesocket(new_fd);			
}
Exemplo n.º 22
0
/**
	parse response from slave. There are two connections between proxy and slave, one to transfer 
	slave to master data, the other for proxy to query slave and get responses. This function 
	deals with the latter situation.
**/
void parseReadData(aeEventLoop *el, int fd, int mask, struct WData *wdata){
    int index = 0;
    int flag = 0;
    struct WData *processData = wdata;
    if(buf_slave_answer.len > 0){
		if(buf_slave_answer.len + wdata->len < MAX_READ * 2){
			struct WData merge_buf_cmd;
			flag = 1;
    		merge_buf_cmd.len=buf_slave_answer.len + wdata->len ;
			merge_buf_cmd.data=(char*) calloc(merge_buf_cmd.len, sizeof(char));
			strncpy(merge_buf_cmd.data, buf_slave_answer.data, buf_slave_answer.len);
			strncpy(merge_buf_cmd.data + buf_slave_answer.len, wdata->data , wdata->len );
			processData = &merge_buf_cmd;
		}
		buf_slave_answer.len=0;
     }
     while(index >= 0 && index < processData->len){
		if(processData->data[index] == '*'){
			index = parseArray(processData, index);
		}
		else if(processData->data[index] == '$'){
			index = parseBulkString(processData, index);
		}
		else if(processData->data[index] == '-' || processData->data[index] == '+' || processData->data[index] == ':'){
			index = parseSimple(processData, index);
		}
		else{
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "query data unexpected  index %d, total %d  at line %d in file %s, data %s",   index, processData->len,  __LINE__, __FILE__, processData->data);
			printf("query data unexpected index %d, total %d\n", index, processData->len);
			int i;
			for(i = 0; i < index; i++)
				printf("%c", processData->data[i]);
			printf("\n index %d\n", processData->data[index]);
			for(i = index + 1; i < processData->len; i++){
				printf("%c", processData->data[i]);
			}
			printf(" end\n");
			break;
		}
    }
 	if(flag){
    	free(processData->data);
    }
}
Exemplo n.º 23
0
static int test_big(sd_test_t *a_test, int argc, char *argv[])
{
    char *buf;
    size_t i;

    buf = malloc(1500);

    for (i = 0; i < 1500 - 1; i++) {
        buf[i] = 'A';
    }

    buf[i] = 0;

    log4c_category_log(root, LOG4C_PRIORITY_INFO, "%s", buf);

    free(buf);

    return 1;
}
Exemplo n.º 24
0
void TsPacket::parse_data_byte(Bitstream & bitstream, bool parse_data)
{
//	level3_timer->start();
	if(adaptation_field_control == 0x1 || adaptation_field_control == 0x3)
	{
		switch(pid)
		{
		case 1:
		case 2:
			//conditional access & transport stream description table
		case 0x1fff:
			//null packet
			skip_to_end(bitstream);
			break;


		default:
			if(payload_unit_start_indicator && parse_data)
			{
				pes_packet->input_bitstream(bitstream);
				skip_to_end(bitstream);
				log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "PTS %llu DTS %llu", pes_packet->get_PTS(), pes_packet->get_DTS());
				//std::cout << "PTS " << pes_packet->get_PTS() << std::endl;
				//std::cout << "DTS " << pes_packet->get_DTS() << std::endl;
			}
			else
			{
				//bitstream.skip_bytes(data_packet_start_byte_offset, TS_PKT_SIZE_BYTES);
				//throw TS_exception();
				skip_to_end(bitstream);
			}
		}
	}
	else
	{
		//throw TS_exception();
		skip_to_end(bitstream);
	}
//	level3_timer->stop();
}
Exemplo n.º 25
0
int create_role_handle(PROTO_HEAD *head, uint16_t fd, int len)
{
	uint64_t effect = 0;
	CREATE_ROLE_REQUEST *req = (CREATE_ROLE_REQUEST *)&head->data;
	CREATE_ROLE_RESPONSE *resp;
	
	char sendbuf[sizeof(PROTO_HEAD) + sizeof(*resp)];
	char sql[256];
	char *p = sql;

	resp = (CREATE_ROLE_RESPONSE *)(&sendbuf[sizeof(PROTO_HEAD)]);
	head = (PROTO_HEAD *)(&sendbuf[0]);
	
	p += sprintf(sql, "insert into DBRoleInfo set id = %lu, profession = %u, scene_id = 1, pos_x = 1000, pos_y = 500, name = \'",
		req->id, req->profession);
	p += escape_string(p, req->name, strlen(req->name));
	*p++ = '\'';
	*p++ = '\0';
	query(sql, 1, &effect);	
	if (effect != 1) {
		log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "%s: execute query for %u %s failed\n",
			__FUNCTION__, req->id, req->name);
		resp->result = -1;
		goto done;
	}
	resp->result = 0;
	memcpy(resp->info.name, req->name, sizeof(resp->info.name));	
	resp->info.profession = req->profession;
	resp->info.level = 1;
	resp->info.scene_id = 1;
	resp->info.pos.pos_x = 1000;
	resp->info.pos.pos_y = 500;
done:
	head->len = htons(sizeof(sendbuf));
	head->msg_id = htons(SERVER_PROTO_CREATE_ROLE_RESPONSE);
	resp->id = req->id;
	send_one_msg(fd, head);
	return (0);
}
Exemplo n.º 26
0
static int send_all_cached_gamesrv_buf(int fd)
{
	PROTO_HEAD *head;
	int i;
	int len;

	if (cached_len <= 0)
		return (0);
	
	for (i = 0; i < cached_len; i += len) {
		head = (PROTO_HEAD *)&cached_buf[i];
		len = htons(head->len);
		if (send_one_msg(fd, head, 0) != len) {
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
				"%s: send to gameserver failed err[%d]", __FUNCTION__, errno);
			cached_len = 0;
			return (-1);
		}
	}
	cached_len = 0;
	return (0);
}
Exemplo n.º 27
0
/*
 * return current index;
 *  -1 incomplete command;
 */
int parseArray(struct WData *wdata, int start){
	int index = start;
	index++;
	if(index >= wdata->len){
		saveToBuf(wdata, start, &buf_slave_answer);
		return -1;
	}
	int array_len = 0;
	int enter = 0;
	while(wdata->data[index] != '\r' && index < wdata->len){
		array_len = array_len * 10 +  wdata->data[index] - '0';
		index++;
		enter = 1;
		 if(index >= wdata->len){
			saveToBuf(wdata, start, &buf_slave_answer);
			return -1;
		}
	}
	index += 2;
	if(index > wdata->len || (index == wdata->len && array_len > 0)){
		saveToBuf(wdata, start, &buf_slave_answer);
		return -1;
	}
	char *item_key;
	char *item_value;
	json_t *object = json_object();
	while(array_len > 0){
		if(wdata->data[index] != '$'){
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "unexpected data, index %d, start %d, wdata->len %d, index value %c at line %d in file %s, data %s",index, start, wdata->len, wdata->data[index],  __LINE__, __FILE__, wdata->data);
			printf("2unexpected data, index %d, start %d, wdata->len %d, index %d\n", index, start, wdata->len, wdata->data[index]);
			int i;
			for(i = start; i <= index; i++)
				printf("%c", wdata->data[i]);
			for(i = index + 1; i< wdata->len; i++)
				printf("%c", wdata->data[i]);
				
			exit(1);
		}
		index++;
		if(index >= wdata->len){
			saveToBuf(wdata, start, &buf_slave_answer);
			json_decref(object);
			return -1;
		}
		int item_len = 0;
		int item_start;
		while(wdata->data[index] != '\r' && index < wdata->len){
			item_len = item_len * 10 +  wdata->data[index] - '0';
			index++;
		}
		if(index >= wdata->len){
			saveToBuf(wdata, start, &buf_slave_answer);
			json_decref(object);
			return -1;
		}
		index++;
		item_start = index + 1;
		index += item_len + 2;
		index++;
		if(index > wdata->len || (index == wdata->len && array_len > 1)){
			saveToBuf(wdata, start, &buf_slave_answer);
			json_decref(object);
			return -1;
		}

		if(item_len > 0){
			if(array_len %2 == 0){
				item_key = (char*)calloc(item_len + 1, sizeof(char));
				strncpy(item_key, wdata->data + item_start, item_len);
			}
			else{
				item_value = (char*)calloc(item_len + 1, sizeof(char));
				strncpy(item_value, wdata->data + item_start, item_len);
				if(item_key && item_value){
					json_t *item_json;
					item_json = json_string(item_value);
					free(item_value);
					json_object_set(object, item_key, item_json);
					json_decref(item_json);
					free(item_key);
				}
				else{
					log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "item key is  null or item value is null  at line %d in file %s", __LINE__, __FILE__);
				}
			}
		}

		array_len--;
	}

	popHandle(object);
	return index;
}
Exemplo n.º 28
0
void ParseTsStream::parse_bytestream()
{

	TsPacket *ts_packet;
	int pid = 0;
	while(1){

		ts_pkt_profiler.start();
		level1_timer.start();
		//loop over each TS packet
		log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "---------------------------");
		log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "Parsing Packet #%d", ts_packet_count);

		try
		{
			ts_header_profiler.start();
			//parse the packet TS header and adaptation field
			ts_packet = new TsPacket(ts_packet_count);
			ts_packet->input_bitstream(bitstream);
			ts_header_profiler.stop();

			//pid based switches
			pid = ts_packet->get_pid();
			update_pid_list(pid);

			if(pid == 0)
			{
				pat_parse_timer.start();
				pat_packet->input_bitstream(bitstream, pid);
				pmt_pid = pat_packet->get_pmt_pid();
				log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "pmt_pid %d (hex %x)", pmt_pid, pmt_pid);
				ts_packet->skip_to_end(bitstream);
				pat_parse_timer.stop();

			}
			else if(pmt_pid != 0 && (pid == pmt_pid))
			{
				pmt_parse_timer.start();
				pmt_packet->input_bitstream(bitstream);
				int streamtype;

				video_pid = pmt_packet->get_video_pid(streamtype);
				log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "video_pid %d (hex %x)", video_pid, video_pid);
				video_stream->start_recording(streamtype, video_pid);

				audio_pid = pmt_packet->get_audio_pid(streamtype);
				log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "audio_pid %d (hex %x)", audio_pid, audio_pid);
				audio_stream->start_recording(streamtype, audio_pid);

				ts_packet->skip_to_end(bitstream);
				pmt_parse_timer.stop();

			}
			else
			{

				pes_parse_timer.start();

				bool parse_data = false;
				if(pid == video_pid || pid == audio_pid){
					log4c_category_log(mycat, LOG4C_PRIORITY_TRACE, "parsing data bytes in this packet");
					parse_data = true;
				}

				if(ts_packet->get_PUSI_flag())
					pes_pkt_copy_timer.start();

				ts_packet->parse_data_byte(bitstream, parse_data);

				if(ts_packet->get_PUSI_flag())
					pes_pkt_copy_timer.stop();
				pes_parse_timer.stop();
			}
			level1_timer.stop();
		}
		catch(std::exception & e)
		{
			if(ts_packet->get_PUSI_flag())
				pes_pkt_copy_timer.stop();
			pes_parse_timer.stop();
			level1_timer.stop();
//			skip_timer.start();
			log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "%s", e.what());
			if(e.what() != nullptr)
				std::cout << e.what() << std::endl;
			ts_packet->skip_to_end(bitstream);
//			skip_timer.stop();
		}
		level2_timer.start();
		//save the packets data in program stream, video or audio stream
		if(pid == 0 || pid == pmt_pid)
		{
			ts_pkt_list.push_back(ts_packet);
		}
		else
		{
			if(pid == video_pid && ts_packet->get_PUSI_flag())
			{
				video_pkt_profiler.start();
				video_stream->insert_pkt(ts_packet);
				video_pkt_profiler.stop();
			}
			else if(pid == audio_pid)
			{
				audio_pkt_profiler.start();
				audio_stream->insert_pkt(ts_packet);
				audio_pkt_profiler.stop();
			}
		}

		ts_pkt_profiler.stop();
		delete ts_packet;
		ts_packet_count++;

		level2_timer.stop();
		if(bitstream.is_eof() || (bitstream.get_in_byte_offset() < 0))
			break;

	}
}
Exemplo n.º 29
0
int main(int argc, char **argv)
{
	int i;
	int ret = 0;
	FILE *file;
	char *line;
	int16_t port;
	
	ret = log4c_init();
	if (ret != 0) {
		printf("log4c_init failed[%d]\n", ret);
		return (ret);
	}
	mycat = log4c_category_get("six13log.log.app.application1");
	if (!mycat) {
		printf("log4c_category_get(\"six13log.log.app.application1\"); failed\n");
		return (0);
	}
	for (i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "-d") == 0) {
			change_to_deamon();
			break;
		}
	}

    log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "conn_srv run");

	if (init_conn_client_map() != 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "init client map failed");
		goto done;
	}
	
	ret = game_event_init();
	if (ret != 0)
		goto done;

	callback_server.cb_get_client_map = get_server_client_map;
	callback_server.cb_listen_pre = conn_cb_server_listen_pre;
	callback_server.cb_listen_after = conn_cb_server_listen_after;

	callback_client.cb_get_client_map = get_client_client_map;
	callback_client.cb_listen_pre = conn_cb_client_listen_pre;
	callback_client.cb_listen_after = conn_cb_client_listen_after;	

	file = fopen("../server_info.ini", "r");
	if (!file) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "open server_info.ini failed[%d]", errno);				
		ret = -1;
		goto done;
	}
	line = get_first_key(file, "conn_srv_listen_port");
	port = atoi(get_value(line));
	if (port <= 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "config file wrong, no conn_srv_listen_port");
		ret = -1;
		goto done;
	}
	
	ret = game_add_listen_event(port, &callback_client);
	if (ret != 0)
		goto done;

	line = get_first_key(file, "conn_srv_port");
	port = atoi(get_value(line));
	if (port <= 0) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "config file wrong, no conn_srv_port");
		ret = -1;
		goto done;
	}
	ret = game_add_listen_event(port, &callback_server);
	if (ret != 0)
		goto done;

	fclose(file);

	struct event event_timer;
	struct timeval t1 = {5, 0};	
	add_timer(t1, &event_timer);
//	event_timer.ev_callback = cb_testtool_timer;
	
	struct timeval t2 = {2, 0};	
	add_timer(t2, NULL);

	struct timeval t3 = {4, 0};	
	add_timer(t3, NULL);	

	if (SIG_ERR == signal(SIGPIPE,SIG_IGN)) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "set sigpipe ign failed");		
		return (0);
	}
	
	ret = event_base_loop(base, 0);
	log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "event_base_loop stoped[%d]", ret);	

	struct timeval tv;
	event_base_gettimeofday_cached(base, &tv);
done:
	log4c_category_log(mycat, LOG4C_PRIORITY_INFO, "conn_srv stoped[%d]", ret);
	return (ret);
}
Exemplo n.º 30
0
Arquivo: quest.c Projeto: a1406/txgg
int handle_quest_operate_request(uint32_t session, const uint8_t *data, uint16_t len)
{
	int ret = 0;
	ROLE *role;
	DB_QUEST_INFO quest;
	DB_QUEST_INFO *role_quest;	
	QuestOperateRequest *req = NULL;
	QuestOperateResponse resp;
	size_t size;
	PROTO_HEAD_CONN *head = (PROTO_HEAD_CONN *)toclient_send_buf;	
    CFGMAINQUEST *quest_config;

	role = get_role_by_session(session);
	if (!role) {
		log4c_category_log(mycat, LOG4C_PRIORITY_INFO,
			"%s: get role obj failed[%u]", __FUNCTION__, session);				
		return (-10);
	}

	quest_operate_response__init(&resp);	
	
	req = quest_operate_request__unpack(NULL, len, data);
	if (!req) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: unpack failed by role[%u][%lu] failed", __FUNCTION__, session, role->role_id);
		ret = -15;
		goto done;
	}

	quest.id = req->id;
	quest_config = get_quest_main_config_byid(quest.id);
	if (!quest_config) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: role[%u][%lu] get quest %d config failed", __FUNCTION__, session, role->role_id, quest.id);
		ret = -20;
		goto done;		
	}
	
	switch (req->operator_)
	{
		case QUEST__OPERATOR__ACCEPT:
            ret = quest_status_check (role, &quest , quest_config);
            if( ret < 0 ) {
				log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
					"%s: role[%u][%lu] try to accept quest %d failed[%d]", __FUNCTION__, session, role->role_id, quest.id, ret);
                break;
			}

			add_role_quest(role, &quest);
			break;
		case QUEST__OPERATOR__CANCEL:
			remove_role_quest(role, req->id);			
			break;
		case QUEST__OPERATOR__COMPLETE:
				//1:检查是否是完成状态
                //2:更新主线任务id
			role_quest = get_role_quest(role, req->id);
			if (!role_quest || role_quest->state != QUEST__STATE__FINISHED) {
				log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
					"%s: role[%u][%lu] try to complete quest %d failed", __FUNCTION__, session, role->role_id, quest.id);
				ret = -30;
				goto done;						
			}

			if (give_role_quest_reward(role, quest_config) != 0) {
				log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
					"%s: role[%u][%lu] give quest %d reward failed", __FUNCTION__, session, role->role_id, quest.id);
				ret = -40;
				goto done;										
			}

			role->main_quest_index = req->id;
			
			remove_role_quest(role, req->id);

			trigger_script_event(role, TYPE_QUEST, req->id, QUEST_COMPLETE, 0);
			break;
		default:
			break;
	}

done:
	resp.result = ret;
	if (req) {
		resp.id = req->id;
		resp.operator_ = req->operator_;
		quest_operate_request__free_unpacked(req, NULL);
	}
	
	size = quest_operate_response__pack(&resp, &(toclient_send_buf[sizeof(PROTO_HEAD_CONN)]));
	size += sizeof(PROTO_HEAD_CONN);
	head->len = htons(size);
	head->fd = ROLE_FD(role);
	head->msg_id = htons(CS__MESSAGE__ID__QUEST_OPERATE_RESPONSE);

	if (size != send_to_role(role, (PROTO_HEAD *)head)) {
		log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
			"%s: to %lu failed", __FUNCTION__, role->role_id);		
	}
	
	return (ret);
}