put global variables into a struct
parent
fe2d4ff780
commit
de94afb9db
5
make.sh
5
make.sh
|
|
@ -1,11 +1,12 @@
|
|||
ENABLE_STATIC_LIB=0
|
||||
ENABLE_SHARED_LIB=1
|
||||
TARGET_PREFIX=$DESTDIR/usr
|
||||
|
||||
TARGET_PREFIX=$DESTDIR/usr/local
|
||||
TARGET_CONF_PATH=$DESTDIR/etc/mc
|
||||
|
||||
DEBUG_FLAG=1
|
||||
|
||||
CFLAGS="$CFLAGS -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I/usr/local/include/fastcommon"
|
||||
CFLAGS="$CFLAGS -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE"
|
||||
if [ "$DEBUG_FLAG" = "1" ]; then
|
||||
CFLAGS="$CFLAGS -g -O1 -DDEBUG_FLAG"
|
||||
else
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
.SUFFIXES: .c .lo
|
||||
|
||||
COMPILE = $(CC) $(CFLAGS) -fPIC
|
||||
INC_PATH = -I/usr/include -I/usr/include/fastcommon
|
||||
INC_PATH = -I/usr/include -I/usr/local/include
|
||||
LIB_PATH = $(LIBS) -lfastcommon
|
||||
TARGET_LIB = $(TARGET_PREFIX)/lib64
|
||||
TARGET_PATH = $(TARGET_PREFIX)/bin
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
#ifndef _SF_DEFINE_H_
|
||||
#define _SF_DEFINE_H_
|
||||
|
||||
#include "common_define.h"
|
||||
#include "fastcommon/common_define.h"
|
||||
|
||||
#define SF_DEF_THREAD_STACK_SIZE (64 * 1024)
|
||||
#define SF_DEF_MAX_PACKAGE_SIZE (16 * 1024)
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@
|
|||
#include <errno.h>
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
#include "shared_func.h"
|
||||
#include "logger.h"
|
||||
#include "sockopt.h"
|
||||
#include "http_func.h"
|
||||
#include "fastcommon/shared_func.h"
|
||||
#include "fastcommon/logger.h"
|
||||
#include "fastcommon/sockopt.h"
|
||||
#include "fastcommon/http_func.h"
|
||||
#include "sf_define.h"
|
||||
#include "sf_global.h"
|
||||
#include "sf_func.h"
|
||||
|
|
@ -95,7 +95,7 @@ int sf_connect_to_server(const char *ip_addr, const int port, int *sock)
|
|||
if(*sock < 0) {
|
||||
return errno != 0 ? errno : ENOMEM;
|
||||
}
|
||||
tcpsetserveropt(*sock, g_sf_network_timeout);
|
||||
tcpsetserveropt(*sock, g_sf_global_vars.network_timeout);
|
||||
|
||||
if ((result=tcpsetnonblockopt(*sock)) != 0) {
|
||||
close(*sock);
|
||||
|
|
@ -104,7 +104,7 @@ int sf_connect_to_server(const char *ip_addr, const int port, int *sock)
|
|||
}
|
||||
|
||||
if ((result=connectserverbyip_nb(*sock, ip_addr, port,
|
||||
g_sf_connect_timeout)) != 0)
|
||||
g_sf_global_vars.connect_timeout)) != 0)
|
||||
{
|
||||
close(*sock);
|
||||
*sock = -1;
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
#ifndef _SF_FUNC_H
|
||||
#define _SF_FUNC_H
|
||||
|
||||
#include "common_define.h"
|
||||
#include "fastcommon/common_define.h"
|
||||
#include "sf_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
|
|
|||
164
src/sf_global.c
164
src/sf_global.c
|
|
@ -11,42 +11,22 @@
|
|||
#include <errno.h>
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
#include "shared_func.h"
|
||||
#include "logger.h"
|
||||
#include "common_define.h"
|
||||
#include "fastcommon/common_define.h"
|
||||
#include "fastcommon/shared_func.h"
|
||||
#include "fastcommon/logger.h"
|
||||
#include "sf_define.h"
|
||||
#include "sf_global.h"
|
||||
|
||||
int g_sf_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
|
||||
int g_sf_network_timeout = DEFAULT_NETWORK_TIMEOUT;
|
||||
char g_sf_base_path[MAX_PATH_SIZE] = {'/', 't', 'm', 'p', '\0'};
|
||||
|
||||
struct nio_thread_data *g_thread_data = NULL;
|
||||
volatile bool g_continue_flag = true;
|
||||
int g_outer_port = 0;
|
||||
int g_inner_port = 0;
|
||||
int g_max_connections = DEFAULT_MAX_CONNECTONS;
|
||||
int g_accept_threads = 1;
|
||||
int g_work_threads = DEFAULT_WORK_THREADS;
|
||||
int g_thread_stack_size = SF_DEF_THREAD_STACK_SIZE;
|
||||
int g_max_pkg_size = SF_DEF_MAX_PACKAGE_SIZE;
|
||||
int g_min_buff_size = SF_DEF_MIN_BUFF_SIZE;
|
||||
int g_max_buff_size = SF_DEF_MAX_BUFF_SIZE;
|
||||
int g_sync_log_buff_interval = SYNC_LOG_BUFF_DEF_INTERVAL;
|
||||
|
||||
bool g_rotate_error_log = false;
|
||||
int g_log_file_keep_days = 0;
|
||||
|
||||
gid_t g_run_by_gid;
|
||||
uid_t g_run_by_uid;
|
||||
char g_run_by_group[32] = {0};
|
||||
char g_run_by_user[32] = {0};
|
||||
time_t g_up_time = 0;
|
||||
|
||||
char g_inner_bind_addr[IP_ADDRESS_SIZE] = {0};
|
||||
char g_outer_bind_addr[IP_ADDRESS_SIZE] = {0};
|
||||
|
||||
SFConnectionStat g_connection_stat = {0, 0};
|
||||
SFGlobalVariables g_sf_global_vars = {
|
||||
DEFAULT_CONNECT_TIMEOUT, DEFAULT_NETWORK_TIMEOUT,
|
||||
{'/', 't', 'm', 'p', '\0'}, NULL, true,
|
||||
0, 0, DEFAULT_MAX_CONNECTONS, 1,
|
||||
DEFAULT_WORK_THREADS, SF_DEF_THREAD_STACK_SIZE,
|
||||
SF_DEF_MAX_PACKAGE_SIZE, SF_DEF_MIN_BUFF_SIZE, SF_DEF_MAX_BUFF_SIZE,
|
||||
SYNC_LOG_BUFF_DEF_INTERVAL, 0, 0, 0,
|
||||
{'\0'}, {'\0'}, false, 0,
|
||||
{'\0'}, {'\0'}, {0, 0}
|
||||
};
|
||||
|
||||
int sf_load_config(const char *server_name, const char *filename,
|
||||
IniContext *pIniContext, const int default_inner_port,
|
||||
|
|
@ -74,85 +54,85 @@ int sf_load_config(const char *server_name, const char *filename,
|
|||
return ENOENT;
|
||||
}
|
||||
|
||||
snprintf(g_sf_base_path, sizeof(g_sf_base_path), "%s", pBasePath);
|
||||
chopPath(g_sf_base_path);
|
||||
if (!fileExists(g_sf_base_path)) {
|
||||
snprintf(g_sf_global_vars.base_path, sizeof(g_sf_global_vars.base_path), "%s", pBasePath);
|
||||
chopPath(g_sf_global_vars.base_path);
|
||||
if (!fileExists(g_sf_global_vars.base_path)) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"\"%s\" can't be accessed, error info: %s",
|
||||
__LINE__, g_sf_base_path, strerror(errno));
|
||||
__LINE__, g_sf_global_vars.base_path, strerror(errno));
|
||||
return errno != 0 ? errno : ENOENT;
|
||||
}
|
||||
if (!isDir(g_sf_base_path)) {
|
||||
if (!isDir(g_sf_global_vars.base_path)) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"\"%s\" is not a directory!",
|
||||
__LINE__, g_sf_base_path);
|
||||
__LINE__, g_sf_global_vars.base_path);
|
||||
return ENOTDIR;
|
||||
}
|
||||
|
||||
g_sf_connect_timeout = iniGetIntValue(NULL, "connect_timeout",
|
||||
g_sf_global_vars.connect_timeout = iniGetIntValue(NULL, "connect_timeout",
|
||||
pIniContext, DEFAULT_CONNECT_TIMEOUT);
|
||||
if (g_sf_connect_timeout <= 0) {
|
||||
g_sf_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
|
||||
if (g_sf_global_vars.connect_timeout <= 0) {
|
||||
g_sf_global_vars.connect_timeout = DEFAULT_CONNECT_TIMEOUT;
|
||||
}
|
||||
|
||||
g_sf_network_timeout = iniGetIntValue(NULL, "network_timeout",
|
||||
g_sf_global_vars.network_timeout = iniGetIntValue(NULL, "network_timeout",
|
||||
pIniContext, DEFAULT_NETWORK_TIMEOUT);
|
||||
if (g_sf_network_timeout <= 0) {
|
||||
g_sf_network_timeout = DEFAULT_NETWORK_TIMEOUT;
|
||||
if (g_sf_global_vars.network_timeout <= 0) {
|
||||
g_sf_global_vars.network_timeout = DEFAULT_NETWORK_TIMEOUT;
|
||||
}
|
||||
|
||||
g_inner_port = iniGetIntValue(NULL, "inner_port", pIniContext,
|
||||
g_sf_global_vars.inner_port = iniGetIntValue(NULL, "inner_port", pIniContext,
|
||||
default_inner_port);
|
||||
if (g_inner_port <= 0) {
|
||||
g_inner_port = default_inner_port;
|
||||
if (g_sf_global_vars.inner_port <= 0) {
|
||||
g_sf_global_vars.inner_port = default_inner_port;
|
||||
}
|
||||
g_outer_port = iniGetIntValue(NULL, "outer_port", pIniContext,
|
||||
g_sf_global_vars.outer_port = iniGetIntValue(NULL, "outer_port", pIniContext,
|
||||
default_outer_port);
|
||||
if (g_outer_port <= 0) {
|
||||
g_outer_port = default_outer_port;
|
||||
if (g_sf_global_vars.outer_port <= 0) {
|
||||
g_sf_global_vars.outer_port = default_outer_port;
|
||||
}
|
||||
|
||||
pBindAddr = iniGetStrValue(NULL, "inner_bind_addr", pIniContext);
|
||||
if (pBindAddr == NULL) {
|
||||
*g_inner_bind_addr = '\0';
|
||||
*g_sf_global_vars.inner_bind_addr = '\0';
|
||||
}
|
||||
else {
|
||||
snprintf(g_inner_bind_addr, sizeof(g_inner_bind_addr), "%s", pBindAddr);
|
||||
snprintf(g_sf_global_vars.inner_bind_addr, sizeof(g_sf_global_vars.inner_bind_addr), "%s", pBindAddr);
|
||||
}
|
||||
|
||||
pBindAddr = iniGetStrValue(NULL, "outer_bind_addr", pIniContext);
|
||||
if (pBindAddr == NULL) {
|
||||
*g_outer_bind_addr = '\0';
|
||||
*g_sf_global_vars.outer_bind_addr = '\0';
|
||||
}
|
||||
else {
|
||||
snprintf(g_outer_bind_addr, sizeof(g_outer_bind_addr), "%s", pBindAddr);
|
||||
snprintf(g_sf_global_vars.outer_bind_addr, sizeof(g_sf_global_vars.outer_bind_addr), "%s", pBindAddr);
|
||||
}
|
||||
|
||||
g_max_connections = iniGetIntValue(NULL, "max_connections",
|
||||
g_sf_global_vars.max_connections = iniGetIntValue(NULL, "max_connections",
|
||||
pIniContext, DEFAULT_MAX_CONNECTONS);
|
||||
if (g_max_connections <= 0) {
|
||||
g_max_connections = DEFAULT_MAX_CONNECTONS;
|
||||
if (g_sf_global_vars.max_connections <= 0) {
|
||||
g_sf_global_vars.max_connections = DEFAULT_MAX_CONNECTONS;
|
||||
}
|
||||
|
||||
g_accept_threads = iniGetIntValue(NULL, "accept_threads",
|
||||
g_sf_global_vars.accept_threads = iniGetIntValue(NULL, "accept_threads",
|
||||
pIniContext, 1);
|
||||
if (g_accept_threads <= 0) {
|
||||
if (g_sf_global_vars.accept_threads <= 0) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"item \"accept_threads\" is invalid, "
|
||||
"value: %d <= 0!", __LINE__, g_accept_threads);
|
||||
"value: %d <= 0!", __LINE__, g_sf_global_vars.accept_threads);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
g_work_threads = iniGetIntValue(NULL, "work_threads",
|
||||
g_sf_global_vars.work_threads = iniGetIntValue(NULL, "work_threads",
|
||||
pIniContext, DEFAULT_WORK_THREADS);
|
||||
if (g_work_threads <= 0) {
|
||||
if (g_sf_global_vars.work_threads <= 0) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"item \"work_threads\" is invalid, "
|
||||
"value: %d <= 0!", __LINE__, g_work_threads);
|
||||
"value: %d <= 0!", __LINE__, g_sf_global_vars.work_threads);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if ((result=set_rlimit(RLIMIT_NOFILE, g_max_connections)) != 0) {
|
||||
if ((result=set_rlimit(RLIMIT_NOFILE, g_sf_global_vars.max_connections)) != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -166,7 +146,7 @@ int sf_load_config(const char *server_name, const char *filename,
|
|||
{
|
||||
return result;
|
||||
}
|
||||
g_max_pkg_size = (int)max_pkg_size;
|
||||
g_sf_global_vars.max_pkg_size = (int)max_pkg_size;
|
||||
|
||||
pMinBuffSize = iniGetStrValue(NULL,
|
||||
"min_buff_size", pIniContext);
|
||||
|
|
@ -178,7 +158,7 @@ int sf_load_config(const char *server_name, const char *filename,
|
|||
{
|
||||
return result;
|
||||
}
|
||||
g_min_buff_size = (int)min_buff_size;
|
||||
g_sf_global_vars.min_buff_size = (int)min_buff_size;
|
||||
|
||||
pMaxBuffSize = iniGetStrValue(NULL,
|
||||
"max_buff_size", pIniContext);
|
||||
|
|
@ -190,32 +170,32 @@ int sf_load_config(const char *server_name, const char *filename,
|
|||
{
|
||||
return result;
|
||||
}
|
||||
g_max_buff_size = (int)max_buff_size;
|
||||
g_sf_global_vars.max_buff_size = (int)max_buff_size;
|
||||
|
||||
if (pMinBuffSize == NULL || pMaxBuffSize == NULL) {
|
||||
g_min_buff_size = g_max_pkg_size;
|
||||
g_max_buff_size = g_max_pkg_size;
|
||||
g_sf_global_vars.min_buff_size = g_sf_global_vars.max_pkg_size;
|
||||
g_sf_global_vars.max_buff_size = g_sf_global_vars.max_pkg_size;
|
||||
}
|
||||
else if (g_max_buff_size < g_max_pkg_size) {
|
||||
g_max_buff_size = g_max_pkg_size;
|
||||
else if (g_sf_global_vars.max_buff_size < g_sf_global_vars.max_pkg_size) {
|
||||
g_sf_global_vars.max_buff_size = g_sf_global_vars.max_pkg_size;
|
||||
}
|
||||
|
||||
pRunByGroup = iniGetStrValue(NULL, "run_by_group", pIniContext);
|
||||
pRunByUser = iniGetStrValue(NULL, "run_by_user", pIniContext);
|
||||
if (pRunByGroup == NULL) {
|
||||
*g_run_by_group = '\0';
|
||||
*g_sf_global_vars.run_by_group = '\0';
|
||||
}
|
||||
else {
|
||||
snprintf(g_run_by_group, sizeof(g_run_by_group),
|
||||
snprintf(g_sf_global_vars.run_by_group, sizeof(g_sf_global_vars.run_by_group),
|
||||
"%s", pRunByGroup);
|
||||
}
|
||||
if (*g_run_by_group == '\0') {
|
||||
g_run_by_gid = getegid();
|
||||
if (*g_sf_global_vars.run_by_group == '\0') {
|
||||
g_sf_global_vars.run_by_gid = getegid();
|
||||
}
|
||||
else {
|
||||
struct group *pGroup;
|
||||
|
||||
pGroup = getgrnam(g_run_by_group);
|
||||
pGroup = getgrnam(g_sf_global_vars.run_by_group);
|
||||
if (pGroup == NULL) {
|
||||
result = errno != 0 ? errno : ENOENT;
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
|
|
@ -225,23 +205,23 @@ int sf_load_config(const char *server_name, const char *filename,
|
|||
return result;
|
||||
}
|
||||
|
||||
g_run_by_gid = pGroup->gr_gid;
|
||||
g_sf_global_vars.run_by_gid = pGroup->gr_gid;
|
||||
}
|
||||
|
||||
if (pRunByUser == NULL) {
|
||||
*g_run_by_user = '\0';
|
||||
*g_sf_global_vars.run_by_user = '\0';
|
||||
}
|
||||
else {
|
||||
snprintf(g_run_by_user, sizeof(g_run_by_user),
|
||||
snprintf(g_sf_global_vars.run_by_user, sizeof(g_sf_global_vars.run_by_user),
|
||||
"%s", pRunByUser);
|
||||
}
|
||||
if (*g_run_by_user == '\0') {
|
||||
g_run_by_uid = geteuid();
|
||||
if (*g_sf_global_vars.run_by_user == '\0') {
|
||||
g_sf_global_vars.run_by_uid = geteuid();
|
||||
}
|
||||
else {
|
||||
struct passwd *pUser;
|
||||
|
||||
pUser = getpwnam(g_run_by_user);
|
||||
pUser = getpwnam(g_sf_global_vars.run_by_user);
|
||||
if (pUser == NULL) {
|
||||
result = errno != 0 ? errno : ENOENT;
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
|
|
@ -251,18 +231,18 @@ int sf_load_config(const char *server_name, const char *filename,
|
|||
return result;
|
||||
}
|
||||
|
||||
g_run_by_uid = pUser->pw_uid;
|
||||
g_sf_global_vars.run_by_uid = pUser->pw_uid;
|
||||
}
|
||||
|
||||
if ((result=set_run_by(g_run_by_group, g_run_by_user)) != 0) {
|
||||
if ((result=set_run_by(g_sf_global_vars.run_by_group, g_sf_global_vars.run_by_user)) != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
g_sync_log_buff_interval = iniGetIntValue(NULL,
|
||||
g_sf_global_vars.sync_log_buff_interval = iniGetIntValue(NULL,
|
||||
"sync_log_buff_interval", pIniContext,
|
||||
SYNC_LOG_BUFF_DEF_INTERVAL);
|
||||
if (g_sync_log_buff_interval <= 0) {
|
||||
g_sync_log_buff_interval = SYNC_LOG_BUFF_DEF_INTERVAL;
|
||||
if (g_sf_global_vars.sync_log_buff_interval <= 0) {
|
||||
g_sf_global_vars.sync_log_buff_interval = SYNC_LOG_BUFF_DEF_INTERVAL;
|
||||
}
|
||||
|
||||
pThreadStackSize = iniGetStrValue(NULL,
|
||||
|
|
@ -275,15 +255,15 @@ int sf_load_config(const char *server_name, const char *filename,
|
|||
{
|
||||
return result;
|
||||
}
|
||||
g_thread_stack_size = (int)thread_stack_size;
|
||||
g_sf_global_vars.thread_stack_size = (int)thread_stack_size;
|
||||
|
||||
g_rotate_error_log = iniGetBoolValue(NULL, "rotate_error_log",
|
||||
g_sf_global_vars.rotate_error_log = iniGetBoolValue(NULL, "rotate_error_log",
|
||||
pIniContext, false);
|
||||
g_log_file_keep_days = iniGetIntValue(NULL, "log_file_keep_days",
|
||||
g_sf_global_vars.log_file_keep_days = iniGetIntValue(NULL, "log_file_keep_days",
|
||||
pIniContext, 0);
|
||||
|
||||
load_log_level(pIniContext);
|
||||
if ((result=log_set_prefix(g_sf_base_path, server_name)) != 0) {
|
||||
if ((result=log_set_prefix(g_sf_global_vars.base_path, server_name)) != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,51 +3,55 @@
|
|||
#ifndef _SF_GLOBAL_H
|
||||
#define _SF_GLOBAL_H
|
||||
|
||||
#include "common_define.h"
|
||||
#include "ini_file_reader.h"
|
||||
#include "ioevent.h"
|
||||
#include "fastcommon/common_define.h"
|
||||
#include "fastcommon/ini_file_reader.h"
|
||||
#include "fastcommon/ioevent.h"
|
||||
|
||||
typedef struct sf_connection_stat {
|
||||
volatile int current_count;
|
||||
volatile int max_count;
|
||||
} SFConnectionStat;
|
||||
|
||||
typedef struct sf_global_variables {
|
||||
int connect_timeout;
|
||||
int network_timeout;
|
||||
char base_path[MAX_PATH_SIZE];
|
||||
|
||||
struct nio_thread_data *thread_data;
|
||||
|
||||
volatile bool continue_flag;
|
||||
int outer_port;
|
||||
int inner_port;
|
||||
int max_connections;
|
||||
int accept_threads;
|
||||
int work_threads;
|
||||
int thread_stack_size;
|
||||
int max_pkg_size;
|
||||
int min_buff_size;
|
||||
int max_buff_size;
|
||||
int sync_log_buff_interval; //sync log buff to disk every interval seconds
|
||||
|
||||
time_t up_time;
|
||||
|
||||
gid_t run_by_gid;
|
||||
uid_t run_by_uid;
|
||||
char run_by_group[32];
|
||||
char run_by_user[32];
|
||||
|
||||
bool rotate_error_log;
|
||||
int log_file_keep_days;
|
||||
|
||||
char inner_bind_addr[IP_ADDRESS_SIZE];
|
||||
char outer_bind_addr[IP_ADDRESS_SIZE];
|
||||
|
||||
SFConnectionStat connection_stat;
|
||||
} SFGlobalVariables;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int g_sf_connect_timeout;
|
||||
extern int g_sf_network_timeout;
|
||||
extern char g_sf_base_path[MAX_PATH_SIZE];
|
||||
|
||||
extern struct nio_thread_data *g_thread_data;
|
||||
|
||||
extern volatile bool g_continue_flag;
|
||||
extern int g_outer_port;
|
||||
extern int g_inner_port;
|
||||
extern int g_max_connections;
|
||||
extern int g_accept_threads;
|
||||
extern int g_work_threads;
|
||||
extern int g_thread_stack_size;
|
||||
extern int g_max_pkg_size;
|
||||
extern int g_min_buff_size;
|
||||
extern int g_max_buff_size;
|
||||
extern int g_sync_log_buff_interval; //sync log buff to disk every interval seconds
|
||||
|
||||
extern time_t g_up_time;
|
||||
|
||||
extern gid_t g_run_by_gid;
|
||||
extern uid_t g_run_by_uid;
|
||||
extern char g_run_by_group[32];
|
||||
extern char g_run_by_user[32];
|
||||
|
||||
extern bool g_rotate_error_log;
|
||||
extern int g_log_file_keep_days;
|
||||
|
||||
extern char g_inner_bind_addr[IP_ADDRESS_SIZE];
|
||||
extern char g_outer_bind_addr[IP_ADDRESS_SIZE];
|
||||
|
||||
extern SFConnectionStat g_connection_stat;
|
||||
extern SFGlobalVariables g_sf_global_vars;
|
||||
|
||||
int sf_load_config(const char *server_name, const char *filename,
|
||||
IniContext *pIniContext, const int default_inner_port,
|
||||
|
|
|
|||
34
src/sf_nio.c
34
src/sf_nio.c
|
|
@ -14,13 +14,13 @@
|
|||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
#include "shared_func.h"
|
||||
#include "sched_thread.h"
|
||||
#include "pthread_func.h"
|
||||
#include "logger.h"
|
||||
#include "sockopt.h"
|
||||
#include "fast_task_queue.h"
|
||||
#include "ioevent_loop.h"
|
||||
#include "fastcommon/shared_func.h"
|
||||
#include "fastcommon/sched_thread.h"
|
||||
#include "fastcommon/pthread_func.h"
|
||||
#include "fastcommon/logger.h"
|
||||
#include "fastcommon/sockopt.h"
|
||||
#include "fastcommon/fast_task_queue.h"
|
||||
#include "fastcommon/ioevent_loop.h"
|
||||
#include "sf_global.h"
|
||||
#include "sf_types.h"
|
||||
#include "sf_nio.h"
|
||||
|
|
@ -87,7 +87,7 @@ void sf_task_finish_clean_up(struct fast_task_info *pTask)
|
|||
ioevent_remove(&pTask->thread_data->ev_puller, pTask);
|
||||
}
|
||||
|
||||
__sync_fetch_and_sub(&g_connection_stat.current_count, 1);
|
||||
__sync_fetch_and_sub(&g_sf_global_vars.connection_stat.current_count, 1);
|
||||
free_queue_push(pTask);
|
||||
}
|
||||
|
||||
|
|
@ -113,15 +113,15 @@ void sf_recv_notify_read(int sock, short event, void *arg)
|
|||
break;
|
||||
}
|
||||
|
||||
current_connections = __sync_add_and_fetch(&g_connection_stat.
|
||||
current_connections = __sync_add_and_fetch(&g_sf_global_vars.connection_stat.
|
||||
current_count, 1);
|
||||
if (current_connections > g_connection_stat.max_count) {
|
||||
g_connection_stat.max_count = current_connections;
|
||||
if (current_connections > g_sf_global_vars.connection_stat.max_count) {
|
||||
g_sf_global_vars.connection_stat.max_count = current_connections;
|
||||
}
|
||||
|
||||
pTask = (struct fast_task_info *)task_ptr;
|
||||
if (ioevent_set(pTask, pTask->thread_data, pTask->event.fd, IOEVENT_READ,
|
||||
(IOEventCallback)client_sock_read, g_sf_network_timeout) != 0)
|
||||
(IOEventCallback)client_sock_read, g_sf_global_vars.network_timeout) != 0)
|
||||
{
|
||||
sf_task_cleanup_func(pTask);
|
||||
continue;
|
||||
|
|
@ -210,7 +210,7 @@ static int client_sock_read(int sock, short event, void *arg)
|
|||
}
|
||||
|
||||
pTask->event.timer.expires = g_current_time +
|
||||
g_sf_network_timeout;
|
||||
g_sf_global_vars.network_timeout;
|
||||
fast_timer_add(&pTask->thread_data->timer,
|
||||
&pTask->event.timer);
|
||||
}
|
||||
|
|
@ -248,7 +248,7 @@ static int client_sock_read(int sock, short event, void *arg)
|
|||
while (1) {
|
||||
fast_timer_modify(&pTask->thread_data->timer,
|
||||
&pTask->event.timer, g_current_time +
|
||||
g_sf_network_timeout);
|
||||
g_sf_global_vars.network_timeout);
|
||||
if (pTask->length == 0) { //recv header
|
||||
recv_bytes = sf_header_size - pTask->offset;
|
||||
}
|
||||
|
|
@ -310,12 +310,12 @@ static int client_sock_read(int sock, short event, void *arg)
|
|||
}
|
||||
|
||||
pTask->length += sf_header_size;
|
||||
if (pTask->length > g_max_pkg_size) {
|
||||
if (pTask->length > g_sf_global_vars.max_pkg_size) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"client ip: %s, pkg length: %d > "
|
||||
"max pkg size: %d", __LINE__,
|
||||
pTask->client_ip, pTask->length,
|
||||
g_max_pkg_size);
|
||||
g_sf_global_vars.max_pkg_size);
|
||||
|
||||
sf_task_cleanup_func(pTask);
|
||||
return -1;
|
||||
|
|
@ -385,7 +385,7 @@ int sf_client_sock_write(int sock, short event, void *arg)
|
|||
while (1) {
|
||||
fast_timer_modify(&pTask->thread_data->timer,
|
||||
&pTask->event.timer, g_current_time +
|
||||
g_sf_network_timeout);
|
||||
g_sf_global_vars.network_timeout);
|
||||
|
||||
bytes = write(sock, pTask->data + pTask->offset,
|
||||
pTask->length - pTask->offset);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "fast_task_queue.h"
|
||||
#include "fastcommon/fast_task_queue.h"
|
||||
#include "sf_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@
|
|||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include "logger.h"
|
||||
#include "sockopt.h"
|
||||
#include "shared_func.h"
|
||||
#include "pthread_func.h"
|
||||
#include "sched_thread.h"
|
||||
#include "ioevent_loop.h"
|
||||
#include "fastcommon/logger.h"
|
||||
#include "fastcommon/sockopt.h"
|
||||
#include "fastcommon/shared_func.h"
|
||||
#include "fastcommon/pthread_func.h"
|
||||
#include "fastcommon/sched_thread.h"
|
||||
#include "fastcommon/ioevent_loop.h"
|
||||
#include "sf_global.h"
|
||||
#include "sf_nio.h"
|
||||
#include "sf_service.h"
|
||||
|
|
@ -68,52 +68,52 @@ int sf_service_init(sf_alloc_thread_extra_data_callback
|
|||
return result;
|
||||
}
|
||||
|
||||
if ((result=init_pthread_attr(&thread_attr, g_thread_stack_size)) != 0) {
|
||||
if ((result=init_pthread_attr(&thread_attr, g_sf_global_vars.thread_stack_size)) != 0) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"init_pthread_attr fail, program exit!", __LINE__);
|
||||
return result;
|
||||
}
|
||||
|
||||
m = g_min_buff_size / (16 * 1024);
|
||||
m = g_sf_global_vars.min_buff_size / (16 * 1024);
|
||||
if (m == 0) {
|
||||
m = 1;
|
||||
} else if (m > 16) {
|
||||
m = 16;
|
||||
}
|
||||
alloc_conn_once = ALLOC_CONNECTIONS_ONCE / m;
|
||||
init_connections = g_max_connections < alloc_conn_once ?
|
||||
g_max_connections : alloc_conn_once;
|
||||
if ((result=free_queue_init_ex(g_max_connections, init_connections,
|
||||
alloc_conn_once, g_min_buff_size, g_max_buff_size,
|
||||
init_connections = g_sf_global_vars.max_connections < alloc_conn_once ?
|
||||
g_sf_global_vars.max_connections : alloc_conn_once;
|
||||
if ((result=free_queue_init_ex(g_sf_global_vars.max_connections, init_connections,
|
||||
alloc_conn_once, g_sf_global_vars.min_buff_size, g_sf_global_vars.max_buff_size,
|
||||
task_arg_size)) != 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
bytes = sizeof(struct nio_thread_data) * g_work_threads;
|
||||
g_thread_data = (struct nio_thread_data *)malloc(bytes);
|
||||
if (g_thread_data == NULL) {
|
||||
bytes = sizeof(struct nio_thread_data) * g_sf_global_vars.work_threads;
|
||||
g_sf_global_vars.thread_data = (struct nio_thread_data *)malloc(bytes);
|
||||
if (g_sf_global_vars.thread_data == NULL) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"malloc %d bytes fail, errno: %d, error info: %s",
|
||||
__LINE__, bytes, errno, strerror(errno));
|
||||
return errno != 0 ? errno : ENOMEM;
|
||||
}
|
||||
memset(g_thread_data, 0, bytes);
|
||||
memset(g_sf_global_vars.thread_data, 0, bytes);
|
||||
|
||||
g_worker_thread_count = 0;
|
||||
pDataEnd = g_thread_data + g_work_threads;
|
||||
for (pThreadData=g_thread_data; pThreadData<pDataEnd; pThreadData++) {
|
||||
pDataEnd = g_sf_global_vars.thread_data + g_sf_global_vars.work_threads;
|
||||
for (pThreadData=g_sf_global_vars.thread_data; pThreadData<pDataEnd; pThreadData++) {
|
||||
pThreadData->thread_loop_callback = thread_loop_callback;
|
||||
if (alloc_thread_extra_data_callback != NULL) {
|
||||
pThreadData->arg = alloc_thread_extra_data_callback(
|
||||
(int)(pThreadData - g_thread_data));
|
||||
(int)(pThreadData - g_sf_global_vars.thread_data));
|
||||
}
|
||||
else {
|
||||
pThreadData->arg = NULL;
|
||||
}
|
||||
|
||||
if (ioevent_init(&pThreadData->ev_puller,
|
||||
g_max_connections + 2, net_timeout_ms, 0) != 0)
|
||||
g_sf_global_vars.max_connections + 2, net_timeout_ms, 0) != 0)
|
||||
{
|
||||
result = errno != 0 ? errno : ENOMEM;
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
|
|
@ -124,7 +124,7 @@ int sf_service_init(sf_alloc_thread_extra_data_callback
|
|||
}
|
||||
|
||||
result = fast_timer_init(&pThreadData->timer,
|
||||
2 * g_sf_network_timeout, g_current_time);
|
||||
2 * g_sf_global_vars.network_timeout, g_current_time);
|
||||
if (result != 0) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"fast_timer_init fail, "
|
||||
|
|
@ -181,12 +181,12 @@ int sf_service_destroy()
|
|||
struct nio_thread_data *pDataEnd, *pThreadData;
|
||||
|
||||
free_queue_destroy();
|
||||
pDataEnd = g_thread_data + g_work_threads;
|
||||
for (pThreadData=g_thread_data; pThreadData<pDataEnd; pThreadData++) {
|
||||
pDataEnd = g_sf_global_vars.thread_data + g_sf_global_vars.work_threads;
|
||||
for (pThreadData=g_sf_global_vars.thread_data; pThreadData<pDataEnd; pThreadData++) {
|
||||
fast_timer_destroy(&pThreadData->timer);
|
||||
}
|
||||
free(g_thread_data);
|
||||
g_thread_data = NULL;
|
||||
free(g_sf_global_vars.thread_data);
|
||||
g_sf_global_vars.thread_data = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -196,7 +196,7 @@ static void *worker_thread_entrance(void* arg)
|
|||
|
||||
pThreadData = (struct nio_thread_data *)arg;
|
||||
ioevent_loop(pThreadData, sf_recv_notify_read, sf_get_task_cleanup_func(),
|
||||
&g_continue_flag);
|
||||
&g_sf_global_vars.continue_flag);
|
||||
ioevent_destroy(&pThreadData->ev_puller);
|
||||
|
||||
__sync_fetch_and_sub(&g_worker_thread_count, 1);
|
||||
|
|
@ -211,7 +211,7 @@ static int _socket_server(const char *bind_addr, int port, int *sock)
|
|||
return result;
|
||||
}
|
||||
|
||||
if ((result=tcpsetserveropt(*sock, g_sf_network_timeout)) != 0) {
|
||||
if ((result=tcpsetserveropt(*sock, g_sf_global_vars.network_timeout)) != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -221,31 +221,31 @@ static int _socket_server(const char *bind_addr, int port, int *sock)
|
|||
int sf_socket_server()
|
||||
{
|
||||
int result;
|
||||
if (g_outer_port != g_inner_port) {
|
||||
if ((result=_socket_server(g_outer_bind_addr, g_outer_port,
|
||||
if (g_sf_global_vars.outer_port != g_sf_global_vars.inner_port) {
|
||||
if ((result=_socket_server(g_sf_global_vars.outer_bind_addr, g_sf_global_vars.outer_port,
|
||||
&g_server_outer_sock)) != 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if ((result=_socket_server(g_inner_bind_addr, g_inner_port,
|
||||
if ((result=_socket_server(g_sf_global_vars.inner_bind_addr, g_sf_global_vars.inner_port,
|
||||
&g_server_inner_sock)) != 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
const char *bind_addr;
|
||||
if (*g_outer_bind_addr != '\0') {
|
||||
if (*g_inner_bind_addr != '\0') {
|
||||
if (*g_sf_global_vars.outer_bind_addr != '\0') {
|
||||
if (*g_sf_global_vars.inner_bind_addr != '\0') {
|
||||
bind_addr = "";
|
||||
} else {
|
||||
bind_addr = g_outer_bind_addr;
|
||||
bind_addr = g_sf_global_vars.outer_bind_addr;
|
||||
}
|
||||
} else {
|
||||
bind_addr = g_inner_bind_addr;
|
||||
bind_addr = g_sf_global_vars.inner_bind_addr;
|
||||
}
|
||||
|
||||
if ((result=_socket_server(bind_addr, g_outer_port,
|
||||
if ((result=_socket_server(bind_addr, g_sf_global_vars.outer_port,
|
||||
&g_server_outer_sock)) != 0)
|
||||
{
|
||||
return result;
|
||||
|
|
@ -266,7 +266,7 @@ static void *accept_thread_entrance(void* arg)
|
|||
char szClientIp[IP_ADDRESS_SIZE];
|
||||
|
||||
server_sock = (long)arg;
|
||||
while (g_continue_flag) {
|
||||
while (g_sf_global_vars.continue_flag) {
|
||||
sockaddr_len = sizeof(inaddr);
|
||||
incomesock = accept(server_sock, (struct sockaddr*)&inaddr, &sockaddr_len);
|
||||
if (incomesock < 0) { //error
|
||||
|
|
@ -298,7 +298,7 @@ static void *accept_thread_entrance(void* arg)
|
|||
strcpy(pTask->client_ip, szClientIp);
|
||||
|
||||
pTask->event.fd = incomesock;
|
||||
pTask->thread_data = g_thread_data + incomesock % g_work_threads;
|
||||
pTask->thread_data = g_sf_global_vars.thread_data + incomesock % g_sf_global_vars.work_threads;
|
||||
if (sf_accept_done_func != NULL) {
|
||||
sf_accept_done_func(pTask, server_sock == g_server_inner_sock);
|
||||
}
|
||||
|
|
@ -327,7 +327,7 @@ void _accept_loop(int server_sock, const int accept_threads)
|
|||
int result;
|
||||
int i;
|
||||
|
||||
if ((result=init_pthread_attr(&thread_attr, g_thread_stack_size)) != 0) {
|
||||
if ((result=init_pthread_attr(&thread_attr, g_sf_global_vars.thread_stack_size)) != 0) {
|
||||
logWarning("file: "__FILE__", line: %d, "
|
||||
"init_pthread_attr fail!", __LINE__);
|
||||
}
|
||||
|
|
@ -351,11 +351,11 @@ void _accept_loop(int server_sock, const int accept_threads)
|
|||
|
||||
void sf_accept_loop()
|
||||
{
|
||||
if (g_outer_port != g_inner_port) {
|
||||
_accept_loop(g_server_inner_sock, g_accept_threads);
|
||||
if (g_sf_global_vars.outer_port != g_sf_global_vars.inner_port) {
|
||||
_accept_loop(g_server_inner_sock, g_sf_global_vars.accept_threads);
|
||||
}
|
||||
|
||||
_accept_loop(g_server_outer_sock, g_accept_threads - 1);
|
||||
_accept_loop(g_server_outer_sock, g_sf_global_vars.accept_threads - 1);
|
||||
accept_thread_entrance((void *)(long)g_server_outer_sock);
|
||||
}
|
||||
|
||||
|
|
@ -372,7 +372,7 @@ static void sigDumpHandler(int sig)
|
|||
bDumpFlag = true;
|
||||
|
||||
snprintf(filename, sizeof(filename),
|
||||
"%s/logs/sf_dump.log", g_sf_base_path);
|
||||
"%s/logs/sf_dump.log", g_sf_global_vars.base_path);
|
||||
//manager_dump_global_vars_to_file(filename);
|
||||
|
||||
bDumpFlag = false;
|
||||
|
|
@ -383,7 +383,7 @@ static void sigQuitHandler(int sig)
|
|||
{
|
||||
if (!bTerminateFlag) {
|
||||
bTerminateFlag = true;
|
||||
g_continue_flag = false;
|
||||
g_sf_global_vars.continue_flag = false;
|
||||
logCrit("file: "__FILE__", line: %d, " \
|
||||
"catch signal %d, program exiting...", \
|
||||
__LINE__, sig);
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "ioevent.h"
|
||||
#include "fast_task_queue.h"
|
||||
#include "fastcommon/ioevent.h"
|
||||
#include "fastcommon/fast_task_queue.h"
|
||||
#include "sf_types.h"
|
||||
|
||||
typedef void* (*sf_alloc_thread_extra_data_callback)(const int thread_index);
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <time.h>
|
||||
#include "connection_pool.h"
|
||||
#include "fastcommon/connection_pool.h"
|
||||
|
||||
struct fast_task_info;
|
||||
typedef void (*sf_accept_done_callback)(struct fast_task_info *pTask,
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@
|
|||
#ifndef _SF_UTIL_H_
|
||||
#define _SF_UTIL_H_
|
||||
|
||||
#include "logger.h"
|
||||
#include <assert.h>
|
||||
#include "fastcommon/logger.h"
|
||||
#ifdef DEBUG_FLAG /*only for format check*/
|
||||
|
||||
#define lemerg(...) snprintf(0,0,__VA_ARGS__), log_plus(LOG_EMERG, __FILE__, __LINE__, __VA_ARGS__)
|
||||
|
|
|
|||
Loading…
Reference in New Issue