Skip to content

Commit

Permalink
增加启动程序 信号处理
Browse files Browse the repository at this point in the history
  • Loading branch information
autohyl committed Apr 22, 2018
1 parent ef6406e commit 8969560
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 42 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ CFLAGS = -g -std=c++0x
TARGET = skynet

SKYNET_OBJ = $(SKYNET_SRC)/skynet_main.o \
$(SKYNET_SRC)/core/skynet_loadconf.o
$(SKYNET_SRC)/core/skynet_loadconf.o \
$(SKYNET_SRC)/core/skynet_start.o

$(TARGET):$(SKYNET_OBJ) $(LUA_LIB)
$(CXX) $(CFLAGS) $(INC) -o $@ $^ $(LIB)
Expand Down
8 changes: 8 additions & 0 deletions skynet-src/core/skynet_daemon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __SKYNET_DAEMON_H__
#define __SKYNET_DAEMON_H__

class Skynet_Daemon {

};

#endif
8 changes: 4 additions & 4 deletions skynet-src/core/skynet_loadconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <iostream>
#include <sstream>

const char *LoadConf::load_config = "\
const char *Skynet_LoadConfig::load_config = "\
local result = {}\n\
local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\
local sep = package.config:sub(1,1)\n\
Expand Down Expand Up @@ -35,7 +35,7 @@ const char *LoadConf::load_config = "\
return result\n\
";

void LoadConf::init() {
void Skynet_LoadConfig::init() {
config.thread = 8;
config.module_path = "./cservice/?.so";
config.harbor = 1;
Expand All @@ -46,7 +46,7 @@ void LoadConf::init() {
config.profile = 1;
}

bool LoadConf::load_config_file(const char* config_file) {
bool Skynet_LoadConfig::load_config_file(const char* config_file) {
int err = luaL_loadbufferx(L, load_config, strlen(load_config), "=[skynet config]", "t");
assert(err == LUA_OK);
lua_pushstring(L, config_file);
Expand Down Expand Up @@ -86,7 +86,7 @@ bool LoadConf::load_config_file(const char* config_file) {
}

template <class T>
void LoadConf::set_opt(const char* key, T value) {
void Skynet_LoadConfig::set_opt(const char* key, T value) {
std::stringstream os;
os << value;
std::string key_str = key;
Expand Down
20 changes: 15 additions & 5 deletions skynet-src/core/skynet_loadconf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#ifndef __SKYNET_LOADCONF_H__
#define __SKYNET_LOADCONF_H__

#include <lua.hpp>
#include <string>
#include "singletion.h"

struct skynet_config {
int thread;
Expand All @@ -12,23 +16,27 @@ struct skynet_config {
std::string logservice;
};

class LoadConf {
class Skynet_LoadConfig {
public:
LoadConf(struct lua_State *l) : L(l) {
Skynet_LoadConfig(struct lua_State *l) : L(l) {
luaL_openlibs(L); // link lua libs
init();
}
LoadConf() {
Skynet_LoadConfig() {
L = luaL_newstate();
luaL_openlibs(L); // link lua libs
init();
}
~LoadConf() {
~Skynet_LoadConfig() {
lua_close(L);
}

bool load_config_file(const char* config_file);

const skynet_config* getConfig() const {
return &config;
}

template <class T>
void set_opt(const char* key, T value);

Expand All @@ -39,4 +47,6 @@ class LoadConf {
struct lua_State *L; //operator lua config
static const char *load_config;
struct skynet_config config;
};
};

#endif
18 changes: 18 additions & 0 deletions skynet-src/core/skynet_start.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "skynet_start.h"

int Skynet_Start::SIG = 0;

void Skynet_Start::start() {
// register SIGHUP for log file reopen
struct sigaction sa;
sa.sa_handler = &handle_hup;
sa.sa_flags = SA_RESTART;
sigfillset(&sa.sa_mask);
sigaction(SIGHUP, &sa, NULL);

if (skynet_config->daemon.size()) {
//if (daemon_init(config->daemon)) {
//exit(1);
//}
}
}
25 changes: 25 additions & 0 deletions skynet-src/core/skynet_start.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef __SKYNET_START_H__
#define __SKYNET_START_H__

#include <signal.h>
#include "skynet_loadconf.h"

#define skynet_config Singletion<Skynet_LoadConfig>::install().getConfig()

class Skynet_Start {
public:
Skynet_Start() {}
~Skynet_Start() {}

void start();

private:
static void handle_hup(int signal) {
if (signal == SIGHUP) {
SIG = 1;
}
}
static int SIG;
};

#endif
38 changes: 6 additions & 32 deletions skynet-src/skynet_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,10 @@
#include <string.h>
#include "core/skynet_loadconf.h"
#include "core/singletion.h"
#include "core/skynet_start.h"

using namespace std;

static const char * load_config = "\
local result = {}\n\
local function getenv(name) return assert(os.getenv(name), [[os.getenv() failed: ]] .. name) end\n\
local sep = package.config:sub(1,1)\n\
local current_path = [[.]]..sep\n\
local function include(filename)\n\
local last_path = current_path\n\
local path, name = filename:match([[(.*]]..sep..[[)(.*)$]])\n\
if path then\n\
if path:sub(1,1) == sep then -- root\n\
current_path = path\n\
else\n\
current_path = current_path .. path\n\
end\n\
else\n\
name = filename\n\
end\n\
local f = assert(io.open(current_path .. name))\n\
local code = assert(f:read [[*a]])\n\
code = string.gsub(code, [[%$([%w_%d]+)]], getenv)\n\
f:close()\n\
assert(load(code,[[@]]..filename,[[t]],result))()\n\
current_path = last_path\n\
end\n\
setmetatable(result, { __index = { include = include } })\n\
local config_name = ...\n\
include(config_name)\n\
setmetatable(result, nil)\n\
return result\n\
";

int main(int argc, char ** argv) {
const char * config_file = NULL;
if (argc > 1) {
Expand All @@ -46,7 +16,11 @@ int main(int argc, char ** argv) {
return 1;
}

Singletion<LoadConf>::install().load_config_file(config_file);
//¼ÓÔØÅäÖÃÎļþ
Singletion<Skynet_LoadConfig>::install().load_config_file(config_file);

//Æô¶¯·þÎñ
Singletion<Skynet_Start>::install().start();

return 0;
}

0 comments on commit 8969560

Please sign in to comment.