Beispiel #1
0
static eng_ctx_t* eng_context_init(void* p) {
    eng_ctx_t* eng_ctx = my_malloc(sizeof(eng_ctx_t));
    memset(eng_ctx, 0, sizeof(eng_ctx_t));

    eng_ctx->sql = malloc(SQL_BUFFER_LEN);

    engine_para_t* para = (engine_para_t *)(p);
    eng_ctx->tid = para->tid;

    // dispatch cluster to all thread
    size_t nserver;
    cq_server_t *server;
    char *ip;
    int port;

    cq_cluster_init(&(eng_ctx->thread_cluster));

    cq_server_t** write_servers 
        = cq_cluster_get_all_write_server(cluster_base, &nserver);

    for (int i = 0; i < nserver; i++) {
        ip = get_server_ip(write_servers[i]);
        port = get_server_port(write_servers[i]);

        server = server_init(ip, port, WRITE_SERVER);
        if (RET_FAILED == cq_cluster_add(eng_ctx->thread_cluster, server)) {
            log_error("Failed to add server into thread cluster");
            abort();
        }
    }

    cq_server_t** read_servers 
        = cq_cluster_get_all_read_server(cluster_base, &nserver);

    for (int i = 0; i < nserver; i++) {
        ip = get_server_ip(read_servers[i]);
        port = get_server_port(read_servers[i]);

        server = server_init(ip, port, READ_SERVER);
        if (RET_FAILED == cq_cluster_add(eng_ctx->thread_cluster, server)) {
            log_error("Failed to add server into thread cluster");
            abort();
        }
    }

    memset(&(eng_ctx->stats), 0, sizeof(struct eng_stats));

    my_free(write_servers);
    my_free(read_servers);

    return eng_ctx;
}
Beispiel #2
0
/**
  * @brief connect to switch and send an init package of online information when online is launched
  */
void connect_to_switch()
{
	DEBUG_LOG("do  connect_to_switch");
	switch_fd = connect_to_service(config_get_strval("service_switch"), 0, 65535, 1);
	if (switch_fd != -1) {
		//连上时
 		is_add_timer_to_connect_switch=false;
	   	static uint32_t seqno = 0;
		sw_report_online_info_in sw_in;
	 	sw_in.domain_id= config_get_intval("domain", 0);
		sw_in.online_id= get_server_id();
		memcpy(sw_in.online_name,get_server_name(),sizeof(sw_in.online_name)); 
		memcpy(sw_in.online_ip,get_server_ip(),sizeof(sw_in.online_ip)); 
		sw_in.online_port=get_server_port();
		sw_in.seqno=seqno;
		g_sprite_map->get_userid_list(sw_in.userid_list);

	 	sw_in.user_num=sw_in.userid_list.size() ;
	
	    ++seqno;
	    send_msg_to_switch(NULL,sw_report_online_info_cmd,0,&sw_in );

		KDEBUG_LOG(0, "BEGIN CONNECTING TO CENTRAL ONLINE AND SEND INIT PKG");
	}else{//
		if(! is_add_timer_to_connect_switch){//还没有加入定时器
			DEBUG_LOG("ADD TIMER connect_switch ");
			g_timer_map->add_timer( TimeVal(3),n_sw_report_online_info,0 );
			is_add_timer_to_connect_switch=true;
		}
	}
}
Beispiel #3
0
int connect_server ()
{
	struct sockaddr_in server_addr;

	if ((sock_fd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror ("socket");
		return -1;
	}

	bzero (&server_addr, sizeof (server_addr));
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons (PORT);
	if (inet_pton (AF_INET, get_server_ip (), &server_addr.sin_addr) <= 0)
	{
		perror ("inet_pton");
		return -1;
	}
	
	if (connect (sock_fd, (struct sockaddr *)&server_addr, sizeof (server_addr)) < 0 )
	{
		perror ("connect");
		return -1;
	}
	return 0;
}
Beispiel #4
0
/**
  * @brief AsyncServer框架要求实现的接口之一。 
  *
  */
extern "C" int  init_service(int isparent)
{
	if (!isparent) {
		setup_timer();
		init_all_timer_type();
		init_funcs();
        const char *udp_ip = get_server_ip();
        uint16_t udp_port = get_server_port();
        bind_udp_socket(udp_ip, udp_port);
		g_dirty_word = new Cdirty_word();
		if (!g_dirty_word->is_inited)
			return -1;
		regist_timers();
		if (load_xmlconf("../conf/GameSvr.xml", load_game_svr_config) == -1)
			return -1;

		max_msg_count = config_get_intval("max_msg_count", 1024);
		/*
		if(g_seg_mgr.init("../data/")) {
        	BOOT_LOG(-1,"online udp listen:%s:%d %u",udp_ip, udp_port, max_msg_count);
		}
		*/

        BOOT_LOG(0,"online udp listen:%s:%d %u",udp_ip, udp_port, max_msg_count);
	}

	return 0;
}
Beispiel #5
0
void handle_raw_packet(const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
{
	struct ip_header *ip;
	struct tcp_header *tcp;
	ushort isize;
	ushort tsize;
	uchar *payload;
	ushort psize;
	ulong svip = get_server_ip(config.server);

	ip = (struct ip_header *)(pkt_data+14); // assume ethernet header is 14 bytes TODO: fix
	isize = ip->ihl*4;

	if (isize < 20) {
		log_msg("dbg", "error: incorrect IP header size.\r\n");
		return;
	}

	tcp = (struct tcp_header *)((uchar *)ip+isize);
	tsize = tcp->doff*4;

	if (tsize < 20) {
		log_msg("dbg", "error: incorrect TCP header size.\r\n");
		return;
	}

	_fromsv = ip->src == svip;

	psize = ntohs(ip->len) - isize - tsize;
	payload = (uchar *) ((uchar *)tcp+tsize);

	if (psize > 0) {
		rebuild_stream(payload, psize, ntohl(tcp->seq), tcp->flags & TH_SYN);
	}
}
Beispiel #6
0
int configure_output(char *arg) {
	static int prefix_flag = 1;

	logpool_init(LOGPOOL_DEFAULT);

	if(arg == NULL) {
		if(ltrace == NULL) ltrace = ltrace_open_string_data32(NULL);
		if(prefix_flag) make_prefix();
		prefix_flag = 0;
	}
	else {
		char opt_log_stderr[] = "-log=stderr";
		char opt_log_syslog[] = "-log=syslog";
		char opt_log_file[] = "-log=file";
		char opt_log_memcached[] = "-log=memcached";
		char opt_prefix[] = "-prefix=";

		if(strncmp(arg, opt_log_stderr, OPT_LOG_STDERR_LEN) == 0) {
			if(ltrace == NULL) ltrace = ltrace_open_string_data32(NULL);
		}
		else if(strncmp(arg, opt_log_syslog, OPT_LOG_SYSLOG_LEN) == 0) {
			if(ltrace == NULL) ltrace = ltrace_open_syslog_data32(NULL);
		}
		else if(strncmp(arg, opt_log_file, OPT_LOG_FILE_LEN) == 0) {
			if(ltrace == NULL) ltrace = ltrace_open_file_data32(NULL, "LOG");
		}
		else if(strncmp(arg, opt_log_memcached, OPT_LOG_MEMCACHED_LEN) == 0) {
			if(ltrace == NULL) {
				char *ip = get_server_ip();
				char *host;
				int port;
				if(ip != NULL) {
					host = strtok(ip, ":");
					port = atoi(strtok(NULL, ":"));
				}
				else {
					host = "localhost";
					port = 11211;
				}
				ltrace = ltrace_open_memcache_data32(NULL, host, port);
			}
		}
		else if(strncmp(arg, opt_prefix, OPT_PREFIX_LEN) == 0) {
			arg += 8;
			if(prefix_flag) memcpy(prefix, arg, strlen(arg));
			prefix_flag = 0;
		}
		else {
			return 1;
		}
	}
	return 0;
}
Beispiel #7
0
void flvFileSinker::notifyStopSave()
{
	if (m_inited)
	{
		taskJsonStruct taskSave;
		taskSave.targetProtocol = taskJsonProtocol_RTSP;
		timeval timeNow;
		gettimeofday(&timeNow, 0);

		taskSave.taskId = m_taskJson.taskId;
		taskSave.relativelyDir = m_taskJson.relativelyDir;
		char addr[1024];
		sprintf(addr, "rtsp://%s:%d/%s/%s", get_server_ip(), configData::getInstance().RTSP_server_port(),
			m_taskJson.relativelyDir.c_str(),
			m_taskJson.fileNameFlv.c_str());
		taskSave.fileUrl = addr;
		taskSave.timeBegin = m_taskJson.timeBegin;
		taskSave.timeEnd = timeNow.tv_sec;
		taskSave.userdefString = m_taskJson.userdefString;
		taskSave.taskType = taskJson::getInstance().getOperatorType(em_operationType::opt_type_saveFile);
		taskSave.status = "sucess";
		taskSave.saveFlv = true;
		taskSave.saveMP4 = false;

		std::string taskString;
		if (taskJson::getInstance().json2String(taskSave, taskString))
		{
			unsigned int length = htonl(taskString.size());

			char *buffSend = new char[4 + taskString.size()];
			memcpy(buffSend, (const void*)&length, 4);
			memcpy(buffSend + 4, (const void*)taskString.c_str(), taskString.size());
			taskJsonSendNotify *taskNotify = new taskJsonSendNotify(buffSend, 4 + taskString.size(), m_jsonSocket);
			taskSchedule::getInstance().addTaskObj(taskNotify);
		}
		else
		{
			LOG_WARN(configData::getInstance().get_loggerId(), "generate live start json failed");
		}
	}
}
Beispiel #8
0
/**
  * @brief AsyncServer框架要求实现的接口之一。 
  *
  */
extern "C" int  init_service(int isparent)
{
	if (!isparent) {
		setup_timer();
		init_all_timer_type();
		init_funcs();
        const char *udp_ip = get_server_ip();
        uint16_t udp_port = get_server_port();
        bind_udp_socket(udp_ip, udp_port);
		regist_timers();
		if (load_xmlconf("../conf/GameSvr.xml", load_game_svr_config) == -1)
			return -1;


		max_msg_count = config_get_intval("max_msg_count", 1024);

        BOOT_LOG(0,"online udp listen:%s:%d %u",udp_ip, udp_port, max_msg_count);
	}

	return 0;
}
Beispiel #9
0
void create_arp_req(frame *fr)
{
	ethernet_header * eth = fr->f_data ;
	arp_header * req = 	fr->f_data + ETHER_HDR_LEN;

	memset(&eth->eth_dest_addr,0xff,sizeof(ether_addr));
	memcpy(&eth->eth_src_addr,get_host_mac(),sizeof(ether_addr));
	eth->ether_type = 0x0608;
	req->arp_hard_type 	= 0x0100 ;
	req->arp_proto_type = 0x0008 ;
	req->arp_hard_size 	= 0x06;
	req->arp_proto_size = 0x04;
	req->arp_opcode =  0x0100;
	memcpy(&req->arp_sender_eth,get_host_mac(),sizeof(ether_addr));
	req->arp_sender_ip = *(get_host_ip());
	memset(&req->arp_target_addr,0,sizeof(ether_addr));
	memcpy(&req->arp_target_ip,get_server_ip(),sizeof(ip_address));

	fr->f_len = sizeof(ethernet_header) + sizeof(arp_header) + ETHER_CRC_LEN;

}
Beispiel #10
0
		virtual const char * get_server_address (void)
		{
			return get_server_ip();
		};
int generate_SDP_description(char *url,char *descr)
{
	int iRet=0;
	do 
	{
		unsigned int sdp_length=0;
		char sdp_lines[RTSP_DESC_SIZE];
		iRet=generate_SDP_info(url,sdp_lines,RTSP_DESC_SIZE);
		if (iRet!=0)
		{
			break;
		}
		sdp_length+=strlen(sdp_lines);
		//just set duration 0 now;
		//const char *range_line="a=range:npt=0-\r\n";
		const char *range_line = "a=range:npt=0-\r\n";
		
		char *pIp=const_cast<char*>(get_server_ip());
		const char *description_sdp_string="H.264 Video,streamed by RTSP server";
		const char *server_name=configData::getInstance().get_server_name();
		const char *server_version=configData::getInstance().get_server_version();
		const char *source_filter_line="";
		const char *misc_sdp_lines="";
		timeval time_now;
		gettimeofday(&time_now,0);
		const char *urlBad = "can you guess";
		char const* const sdp_prefix_fmt=
			"v=0\r\n"
			"o=- %ld%06ld %d IN IP4 %s\r\n"
			"s=%s\r\n"
			"i=%s\r\n"
			"t=0 0\r\n"
			"a=tool:%s%s\r\n"
			"a=type:broadcast\r\n"
			"a=control:*\r\n"
			"%s"
			"%s"
			"a=x-qt-text-nam:%s\r\n"
			"a=x-qt-text-inf:%s\r\n"
			"%s";
		sdp_length+=strlen(sdp_prefix_fmt)+
			46+strlen(pIp)+
			strlen(range_line)+
			strlen(description_sdp_string)*2+
			strlen(server_name)*2+
			strlen(server_version)+
			strlen(source_filter_line)+
			strlen(misc_sdp_lines);
		sprintf(descr,sdp_prefix_fmt,
			time_now.tv_sec,time_now.tv_usec,1,pIp,
			description_sdp_string,
			urlBad,
			server_name,
			server_version,
			source_filter_line,
			range_line,
			description_sdp_string,
			urlBad,
			//misc_sdp_lines,
			sdp_lines);
	} while (0);
	return	iRet;
}