⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 px_ss_module_work.c

📁 MMORPG游戏服务器端架构及函数
💻 C
📖 第 1 页 / 共 2 页
字号:
/* must been first include begin */
#include "..\ProjectX_Common\ProjectX_Copyright.h"
#include "..\ProjectX_Common\ProjectX_Common.h"
/* must been first include end */


/* std and common include */
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>

/* program specify include */
#include "..\ProjectX_Common\ProjectX_MessageCode.h"
#include "..\ProjectX_Common\ProjectX_Shared.h"
#include "..\ProjectX_Common\ProjectX_Utils.h"
#include "px_ss_config.h"
#include "px_ss_state.h"
#include "px_ss_ui.h"
#include "px_ss_time_service.h"
#include "px_ss_statistic.h"
#include "px_ss_module_cl.h"
#include "px_ss_module_rs.h"
#include "px_ss_module_work.h"

BOOL							bln_run_work = TRUE;
HANDLE							h_winsock2 = NULL;
PX_SS_CL_VERSION			*	cl_version_head = NULL;
PX_SS_RS_VERSION			*	rs_version_head = NULL;
PX_SS_CONFIG				*	g_config;
PX_SS_STAT					*	g_stat;

extern HANDLE					h_event_module_rs;
extern HANDLE					h_event_module_cl;
extern PX_SS_AREA			*	module_rs_area_head;
extern PX_SS_RS_KEY			*	module_rs_key_head;
extern PX_SS_CL_KEY			*	module_cl_key_head;
extern char					*	area_send_buf;
extern int						area_send_buf_len;
extern char						base_path[FILENAME_MAX];
extern char					**	pcfg_buf;
extern PX_SS_STATE			*	g_state;
extern PX_SS_TIME_SERVICE	*	g_time_service;


/************************************************************************
sub proc
************************************************************************/
void init_winsock2(){
	WSADATA wsaData;
	if (h_winsock2 == NULL) {
		h_winsock2 = LoadLibrary("ws2_32.dll");
		if (h_winsock2 == NULL) {
			x_debug_error("LoadLibrary of winsock2 fail");
			return;
		}else{
			if(WSAStartup(0x0202,&wsaData) != 0){
				x_debug_error("WSAStartup fail");
				return;
			}
		}
	}
	return;
}
void init_read_config(){
	int		nlines;
	int		i;
	char	cache[FILENAME_MAX];
	char *	pstr;
	if (g_config == NULL) {
		g_config = (LPPX_SS_CONFIG)ms_malloc(sizeof(PX_SS_CONFIG));
		memset(g_config,0,sizeof(PX_SS_CONFIG));
	}
	cache[0] = '\0';
	strcpy(cache,base_path);
	strcat(cache,"\\configuration\\ss_config.txt");
	nlines = GetnLinesOfFile(cache);
	init_read_specify_file(cache,nlines);
	for(i = 0;i < nlines;i++){
		pstr = pcfg_buf[i];
		DeleteCommentAndSpace(pstr);
		if (pstr[0] == '\0') {
			continue;
		}
		PopLeftString(pstr,cache);
		//version
		if ((strncmp(cache,"version",strlen("version")) == 0)&&(strlen("version") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			strcpy(g_config->version,cache);
			continue;
		}
		//ip_to_client
		if ((strncmp(cache,"ip_to_client",strlen("ip_to_client")) == 0)&&(strlen("ip_to_client") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			strcpy(g_config->ip_to_client,cache);
			continue;
		}
		//port_to_client
		if ((strncmp(cache,"port_to_client",strlen("port_to_client")) == 0)&&(strlen("port_to_client") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->port_to_client = atoi(cache);
			continue;
		}
		//ip_to_routingserver
		if ((strncmp(cache,"ip_to_routingserver",strlen("ip_to_routingserver")) == 0)&&(strlen("ip_to_routingserver") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			strcpy(g_config->ip_to_rs,cache);
			continue;
		}
		//port_to_rountingserver
		if ((strncmp(cache,"port_to_rountingserver",strlen("port_to_rountingserver")) == 0)&&(strlen("port_to_rountingserver") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->port_to_rs = atoi(cache);
			continue;
		}
		//max_client_online
		if ((strncmp(cache,"max_client_online",strlen("max_client_online")) == 0)&&(strlen("max_client_online") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->max_client_online = atoi(cache);
			continue;
		}
		//max_routingserver_online
		if ((strncmp(cache,"max_routingserver_online",strlen("max_routingserver_online")) == 0)&&(strlen("max_routingserver_online") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->max_rs_online = atoi(cache);
			continue;
		}
		//auto_cls
		if ((strncmp(cache,"auto_cls",strlen("auto_cls")) == 0)&&(strlen("auto_cls") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			if (strcmp(cache,"TRUE") == 0) {
				g_config->auto_cls = TRUE;
			}else{
				g_config->auto_cls = FALSE;
			}
			continue;
		}
		//display_internal_version
		if ((strncmp(cache,"display_internal_version",strlen("display_internal_version")) == 0)&&(strlen("display_internal_version") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			if (strcmp(cache,"TRUE") == 0) {
				g_config->display_internal_version = TRUE;
			}else{
				g_config->display_internal_version = FALSE;
			}
			continue;
		}
		//accept_internal_ip
		if ((strncmp(cache,"accept_internal_ip",strlen("accept_internal_ip")) == 0)&&(strlen("accept_internal_ip") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			if (strcmp(cache,"TRUE") == 0) {
				g_config->accept_internal_ip = TRUE;
			}else{
				g_config->accept_internal_ip = FALSE;
			}
			continue;
		}
		//client_recv_buf_len
		if ((strncmp(cache,"client_recv_buf_len",strlen("client_recv_buf_len")) == 0)&&(strlen("client_recv_buf_len") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->cl_recv_buf_len = atoi(cache);
			continue;
		}
		//client_send_buf_len
		if ((strncmp(cache,"client_send_buf_len",strlen("client_send_buf_len")) == 0)&&(strlen("client_send_buf_len") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->cl_send_buf_len = atoi(cache);
			continue;
		}
		//client_iocp_buf_len
		if ((strncmp(cache,"client_iocp_buf_len",strlen("client_iocp_buf_len")) == 0)&&(strlen("client_iocp_buf_len") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->cl_iocp_buf_len = atoi(cache);
			continue;
		}
		//routingserver_recv_buf_len
		if ((strncmp(cache,"routingserver_recv_buf_len",strlen("routingserver_recv_buf_len")) == 0)&&(strlen("routingserver_recv_buf_len") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->rs_recv_buf_len = atoi(cache);
			continue;
		}
		//routingserver_send_buf_len
		if ((strncmp(cache,"routingserver_send_buf_len",strlen("routingserver_send_buf_len")) == 0)&&(strlen("routingserver_send_buf_len") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->rs_send_buf_len = atoi(cache);
			continue;
		}
		//routingserver_iocp_buf_len
		if ((strncmp(cache,"routingserver_iocp_buf_len",strlen("routingserver_iocp_buf_len")) == 0)&&(strlen("routingserver_iocp_buf_len") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->rs_iocp_buf_len = atoi(cache);
			continue;
		}
		//num_client_iocp_thread
		if ((strncmp(cache,"num_client_iocp_thread",strlen("num_client_iocp_thread")) == 0)&&(strlen("num_client_iocp_thread") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->num_cl_iocp_thread = atoi(cache);
			continue;
		}
		//num_routingserver_iocp_thread
		if ((strncmp(cache,"num_routingserver_iocp_thread",strlen("num_routingserver_iocp_thread")) == 0)&&(strlen("num_routingserver_iocp_thread") == strlen(cache))) {
			PopLeftString(pstr,cache);		
			g_config->num_rs_iocp_thread = atoi(cache);
			continue;
		}
	}
	init_free_cache(nlines);
	/*
	 *	with no time for config check ,or may need make the default value if got incrrect config
	 */
	g_config->ip_local = get_ip_by_string(g_config->ip_to_client);
	return;
}
void init_common_var(){
	int i;
	if (g_stat == NULL) {
		g_stat = (LPPX_SS_STAT)ms_malloc(sizeof(PX_SS_STAT));
		memset(g_stat,0,sizeof(PX_SS_STAT));
	}
	if (module_cl_key_head == NULL) {
		g_stat->num_cl_key = g_config->max_client_online*2.5;
		module_cl_key_head = (LPPX_SS_CL_KEY)ms_malloc(g_stat->num_cl_key*sizeof(PX_SS_CL_KEY));
		//no need do memset for set 0
		for(i = 0;i < g_stat->num_cl_key;i++){
			(module_cl_key_head + i)->state = STATE_KEY_NO_INIT;
			(module_cl_key_head + i)->prdi = NULL;
		}
	}
	if (module_rs_key_head == NULL) {
		g_stat->num_rs_key = g_config->max_rs_online*2.5;
		module_rs_key_head = (LPPX_SS_RS_KEY)ms_malloc(g_stat->num_rs_key*sizeof(PX_SS_RS_KEY));
		for(i = 0;i < g_stat->num_rs_key;i++){
			(module_rs_key_head + i)->state = STATE_KEY_NO_INIT;
			(module_rs_key_head + i)->prdi = NULL;
		}
	}
	return;
}
void init_read_cl_version(){
	int		nlines;
	int		i;
	int		nconfig;
	char	cache[FILENAME_MAX];
	char *	pstr;
	PX_SS_CL_VERSION * pcv;
	cache[0] = '\0';
	strcpy(cache,base_path);
	strcat(cache,"\\configuration\\ss_cl_version.txt");
	nlines = GetnLinesOfFile(cache);
	init_read_specify_file(cache,nlines);
	nconfig = init_get_valid_num_config(nlines);
	cl_version_head = (LPPX_SS_CL_VERSION)ms_malloc(nconfig*sizeof(PX_SS_CL_VERSION));
	memset(cl_version_head,0,nconfig*sizeof(PX_SS_CL_VERSION));
	g_stat->num_cl_version = nconfig;
	pcv = cl_version_head;
	for(i = 0;i < nlines;i++){
		pstr = pcfg_buf[i];
		if (pstr[0] == '\0') {
			continue;
		}
		PopLeftString(pstr,cache);
		strcpy(pcv->version,cache);
		PopLeftString(pstr,cache);
		strcpy(pcv->md5code,cache);
		PopLeftString(pstr,cache);
		if (strcmp(cache,"TRUE") == 0) {
			pcv->accept = TRUE;
		}else{
			pcv->accept = FALSE;
		}
		PopLeftString(pstr,cache);
		strcpy(pcv->antihackcode,cache);
		pcv++;
	}
	init_free_cache(nlines);
	return;
}
void init_read_rs_version(){
	int		nlines;
	int		i;
	int		nconfig;
	char	cache[FILENAME_MAX];
	char *	pstr;
	PX_SS_RS_VERSION * prv;
	cache[0] = '\0';
	strcpy(cache,base_path);
	strcat(cache,"\\configuration\\ss_rs_version.txt");
	nlines = GetnLinesOfFile(cache);
	init_read_specify_file(cache,nlines);
	nconfig = init_get_valid_num_config(nlines);
	rs_version_head = (LPPX_SS_RS_VERSION)ms_malloc(nconfig*sizeof(PX_SS_RS_VERSION));
	memset(rs_version_head,0,nconfig*sizeof(PX_SS_RS_VERSION));
	g_stat->num_rs_version = nconfig;
	prv = rs_version_head;
	for(i = 0;i < nlines;i++){
		pstr = pcfg_buf[i];
		if (pstr[0] == '\0') {
			continue;
		}
		PopLeftString(pstr,cache);
		strcpy(prv->version,cache);
		PopLeftString(pstr,cache);
		strcpy(prv->md5code,cache);
		PopLeftString(pstr,cache);
		if (strcmp(cache,"TRUE") == 0) {
			prv->accept = TRUE;
		}else{
			prv->accept = FALSE;
		}
		PopLeftString(pstr,cache);
		strcpy(prv->antihackcode,cache);
		prv++;
	}
	init_free_cache(nlines);
	return;
}



void free_common_var(){

	return;
}
void free_cl_version(){
	if (cl_version_head != NULL) {
		ms_free(cl_version_head);
		cl_version_head = NULL;
	}
	return;
}
void free_rs_version(){
	if (rs_version_head != NULL) {
		ms_free(rs_version_head);
		rs_version_head = NULL;
	}
	return;
}
void free_winsock2(){
	if (h_winsock2 != NULL) {
		if (WSACleanup() != 0) {
			x_debug_error("WSACleanup fail");
			return;
		}
		FreeLibrary(h_winsock2);
		h_winsock2 = NULL;
	}
	return;
}

void set_ss_state(byte state){
	EnterCriticalSection(g_state->cts_state);
	g_state->prev_state = g_state->curr_state;
	g_state->prev_time = g_state->curr_time;
	g_state->curr_state = state;
	g_state->curr_time = GetTime64();
	LeaveCriticalSection(g_state->cts_state);
	return;
}


BOOL valid_ss_state(byte state){
	EnterCriticalSection(g_state->cts_state);
	if (g_state->curr_state == state) {
		LeaveCriticalSection(g_state->cts_state);
		return TRUE;
	}else{
		LeaveCriticalSection(g_state->cts_state);
		return FALSE;
	}
}


/************************************************************************
main state handle entry
************************************************************************/
void px_ss_do_state_no_init(){
	Sleep(500);	//for saving cpu time

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -