gate launch by watchdog

This commit is contained in:
云风
2012-08-09 19:58:23 +08:00
parent 8556d5225e
commit d3f92b32d5
6 changed files with 43 additions and 21 deletions

View File

@@ -10,8 +10,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#define WATCHDOG ".watchdog"
struct connection { struct connection {
char * agent; char * agent;
int connection_id; int connection_id;
@@ -20,6 +18,7 @@ struct connection {
struct gate { struct gate {
struct mread_pool * pool; struct mread_pool * pool;
const char * watchdog;
int id_index; int id_index;
int cap; int cap;
int max_connection; int max_connection;
@@ -101,14 +100,14 @@ _ctrl(struct skynet_context * ctx, struct gate * g, const void * msg, int sz) {
} }
static void static void
_report(struct skynet_context * ctx, const char * data, ...) { _report(struct gate *g, struct skynet_context * ctx, const char * data, ...) {
va_list ap; va_list ap;
va_start(ap, data); va_start(ap, data);
char tmp[1024]; char tmp[1024];
int n = vsnprintf(tmp, sizeof(tmp), data, ap); int n = vsnprintf(tmp, sizeof(tmp), data, ap);
va_end(ap); va_end(ap);
skynet_send(ctx, WATCHDOG, 0, tmp, n, 0); skynet_send(ctx, g->watchdog, 0, tmp, n, 0);
} }
static void static void
@@ -120,7 +119,7 @@ _forward(struct skynet_context * ctx,struct gate *g, int uid, void * data, size_
char * tmp = malloc(len + 32); char * tmp = malloc(len + 32);
int n = snprintf(tmp,len+32,"%d data ",uid); int n = snprintf(tmp,len+32,"%d data ",uid);
memcpy(tmp+n,data,len); memcpy(tmp+n,data,len);
skynet_send(ctx, WATCHDOG, 0, tmp, len + n, DONTCOPY); skynet_send(ctx, g->watchdog, 0, tmp, len + n, DONTCOPY);
} }
} }
@@ -181,13 +180,13 @@ _cb(struct skynet_context * ctx, void * ud, int session, const char * uid, const
struct sockaddr_in remote_addr; struct sockaddr_in remote_addr;
socklen_t len = sizeof(struct sockaddr_in); socklen_t len = sizeof(struct sockaddr_in);
getpeername(fd, (struct sockaddr *)&remote_addr, &len); getpeername(fd, (struct sockaddr *)&remote_addr, &len);
_report(ctx, "%d open %d %s:%u",id,fd,inet_ntoa(remote_addr.sin_addr),ntohs(remote_addr.sin_port)); _report(g, ctx, "%d open %d %s:%u",id,fd,inet_ntoa(remote_addr.sin_addr),ntohs(remote_addr.sin_port));
} }
uint8_t * plen = mread_pull(m,2); uint8_t * plen = mread_pull(m,2);
if (plen == NULL) { if (plen == NULL) {
if (mread_closed(m)) { if (mread_closed(m)) {
_remove_id(g,id); _remove_id(g,id);
_report(ctx, "%d close", id); _report(g, ctx, "%d close", id);
} }
goto _break; goto _break;
} }
@@ -197,7 +196,7 @@ _cb(struct skynet_context * ctx, void * ud, int session, const char * uid, const
if (data == NULL) { if (data == NULL) {
if (mread_closed(m)) { if (mread_closed(m)) {
_remove_id(g,id); _remove_id(g,id);
_report(ctx, "%d close", id); _report(g, ctx, "%d close", id);
} }
goto _break; goto _break;
} }
@@ -214,8 +213,9 @@ gate_init(struct gate *g , struct skynet_context * ctx, char * parm) {
int port = 0; int port = 0;
int max = 0; int max = 0;
int buffer = 0; int buffer = 0;
int n = sscanf(parm, "%d %d %d",&port,&max,&buffer); char watchdog[strlen(parm)+1];
if (n!=3) { int n = sscanf(parm, "%s %d %d %d",watchdog, &port,&max,&buffer);
if (n!=4) {
skynet_error(ctx, "Invalid gate parm %s",parm); skynet_error(ctx, "Invalid gate parm %s",parm);
return 1; return 1;
} }
@@ -224,7 +224,7 @@ gate_init(struct gate *g , struct skynet_context * ctx, char * parm) {
skynet_error(ctx, "Create gate %s failed",parm); skynet_error(ctx, "Create gate %s failed",parm);
return 1; return 1;
} }
g->watchdog = strdup(watchdog);
g->pool = pool; g->pool = pool;
int cap = 1; int cap = 1;
while (cap < max) { while (cap < max) {
@@ -245,7 +245,6 @@ gate_init(struct gate *g , struct skynet_context * ctx, char * parm) {
} }
skynet_callback(ctx,g,_cb); skynet_callback(ctx,g,_cb);
skynet_command(ctx,"REG","gate");
skynet_command(ctx,"TIMEOUT","0"); skynet_command(ctx,"TIMEOUT","0");
return 0; return 0;

View File

@@ -49,6 +49,10 @@ function skynet.register(name)
return c.command("REG", name) return c.command("REG", name)
end end
function skynet.name(name, handle)
c.command("NAME", name .. " " .. handle)
end
function skynet.self() function skynet.self()
return c.command("REG") return c.command("REG")
end end

View File

@@ -4,11 +4,11 @@ local client = ...
skynet.dispatch(function(msg, sz , session, address) skynet.dispatch(function(msg, sz , session, address)
local message = skynet.tostring(msg,sz) local message = skynet.tostring(msg,sz)
if session == 0 then if session == 0 then
print("client command",msg) print("client command",message)
local result = skynet.call("SIMPLEDB",msg) local result = skynet.call("SIMPLEDB",message)
skynet.send(client, result) skynet.send(client, result)
else else
print("server command",msg) print("server command",message)
if msg == "CLOSE" then if msg == "CLOSE" then
skynet.kill(client) skynet.kill(client)
skynet.exit() skynet.exit()

View File

@@ -8,10 +8,8 @@ skynet.start(function()
print("launcher", launcher) print("launcher", launcher)
local console = skynet.launch("snlua","console") local console = skynet.launch("snlua","console")
print("console",console) print("console",console)
local watchdog = skynet.launch("snlua","watchdog") local watchdog = skynet.launch("snlua","watchdog","8888 4 0")
print("watchdog",watchdog) print("watchdog",watchdog)
local gate = skynet.launch("gate","8888 4 0")
print("gate",gate)
local db = skynet.launch("snlua","simpledb") local db = skynet.launch("snlua","simpledb")
print("simpledb",db) print("simpledb",db)
local connection = skynet.launch("connection","256") local connection = skynet.launch("connection","256")

View File

@@ -2,6 +2,7 @@ local skynet = require "skynet"
local command = {} local command = {}
local agent_all = {} local agent_all = {}
local gate = skynet.launch("gate" , skynet.self(), ...)
function command:open(parm) function command:open(parm)
local fd,addr = string.match(parm,"(%d+) ([^%s]+)") local fd,addr = string.match(parm,"(%d+) ([^%s]+)")
@@ -9,10 +10,10 @@ function command:open(parm)
skynet.send("LOG", string.format("%d %d %s",self,fd,addr)) skynet.send("LOG", string.format("%d %d %s",self,fd,addr))
local client = skynet.launch("client",fd) local client = skynet.launch("client",fd)
skynet.send("LOG", "client " .. client) skynet.send("LOG", "client " .. client)
local agent = skynet.launch("snlua","agent.lua",client) local agent = skynet.launch("snlua","agent",client)
if agent then if agent then
agent_all[self] = agent agent_all[self] = agent
skynet.send("gate", "forward ".. self .. " " .. agent) skynet.send(gate, "forward ".. self .. " " .. agent)
end end
end end
@@ -43,4 +44,4 @@ skynet.dispatch(function(msg, sz)
end end
end) end)
skynet.register ".watchdog" skynet.register(".watchdog")

View File

@@ -277,6 +277,26 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
} }
} }
if (strcmp(cmd,"NAME") == 0) {
int size = strlen(param);
char name[size+1];
char handle[size+1];
sscanf(param,"%s %s",name,handle);
if (handle[0] != ':') {
return NULL;
}
uint32_t handle_id = strtoul(handle+1, NULL, 16);
if (handle_id == 0) {
return NULL;
}
if (name[0] == '.') {
return skynet_handle_namehandle(handle_id, name + 1);
} else {
skynet_harbor_register(name, handle_id);
}
return NULL;
}
if (strcmp(cmd,"EXIT") == 0) { if (strcmp(cmd,"EXIT") == 0) {
skynet_handle_retire(context->handle); skynet_handle_retire(context->handle);
return NULL; return NULL;