From 243dc58c35ed05d366a11617e088e60bf006f9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Fri, 31 Aug 2012 21:56:10 +0800 Subject: [PATCH] use connection service for console --- connection/main.c | 18 ++++++++++++----- lualib/socket.lua | 5 +++++ service/console.lua | 47 +++++++++++++++++++++++++++++++++++++-------- service/main.lua | 4 ++-- 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/connection/main.c b/connection/main.c index 75fd959b..615a0e57 100644 --- a/connection/main.c +++ b/connection/main.c @@ -51,7 +51,8 @@ _expand(struct connection_server * server) { int i; for (i=0;imax_connection;i++) { struct connection * c = &server->conn[i]; - connection_add(server->pool, c->fd , c); + int err = connection_add(server->pool, c->fd , c); + assert(err == 0); } server->max_connection *= 2; } @@ -108,7 +109,7 @@ _poll(struct connection_server * server) { buffer = malloc(DEFAULT_BUFFER_SIZE); } - int size = recv(c->fd, buffer, DEFAULT_BUFFER_SIZE, MSG_DONTWAIT); + int size = read(c->fd, buffer, DEFAULT_BUFFER_SIZE); if (size < 0) { continue; } @@ -126,7 +127,7 @@ _poll(struct connection_server * server) { } static int -_main(struct skynet_context * ctx, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { +_connection_main(struct skynet_context * ctx, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) { if (type == PTYPE_RESPONSE) { _poll(ud); return 0; @@ -141,6 +142,10 @@ _main(struct skynet_context * ctx, void * ud, int type, int session, uint32_t so return 0; } int addr_sz = sz - (endptr - (char *)msg); + if (addr_sz <= 1) { + skynet_error(ctx, "[connection] Invalid ADD command from %x (session = %d)", source, session); + return 0; + } char addr [addr_sz]; memcpy(addr, endptr+1, addr_sz-1); addr[addr_sz-1] = '\0'; @@ -170,13 +175,16 @@ connection_init(struct connection_server * server, struct skynet_context * ctx, server->pool = connection_newpool(DEFAULT_CONNECTION); if (server->pool == NULL) return 1; - server->max_connection = DEFAULT_CONNECTION; + server->max_connection = strtol(param, NULL, 10); + if (server->max_connection == 0) { + server->max_connection = DEFAULT_CONNECTION; + } server->current_connection = 0; server->ctx = ctx; server->conn = malloc(server->max_connection * sizeof(struct connection)); memset(server->conn, 0, server->max_connection * sizeof(struct connection)); - skynet_callback(ctx, server, _main); + skynet_callback(ctx, server, _connection_main); skynet_command(ctx,"REG",".connection"); skynet_command(ctx,"TIMEOUT","0"); return 0; diff --git a/lualib/socket.lua b/lualib/socket.lua index b248f7b8..715e78ce 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -16,6 +16,11 @@ function socket.connect(addr) object = c.new() end +function socket.stdin() + skynet.send(".connection", "text", "ADD", 1 , skynet.address(skynet.self())) + object = c.new() +end + function socket.push(msg,sz) if msg then c.push(object, msg, sz) diff --git a/service/console.lua b/service/console.lua index 95c3d5a2..f7669273 100644 --- a/service/console.lua +++ b/service/console.lua @@ -1,12 +1,43 @@ local skynet = require "skynet" +local socket = require "socket" + +local function readline(sep) + while true do + local line = socket.readline(sep) + if line then + return line + end + coroutine.yield() + end +end + +local function split_package() + while true do + local cmd = readline "\n" + if cmd ~= "" then + local handle = skynet.launch("snlua", cmd) + if handle == nil then + print("Launch error:",cmd) + end + end + end +end + +local split_co = coroutine.create(split_package) + +skynet.register_protocol { + name = "client", + id = 3, + pack = function(...) return ... end, + unpack = function(msg,sz) + assert(msg , "Stdin closed") + socket.push(msg,sz) + assert(coroutine.resume(split_co)) + end, + dispatch = function () end +} + skynet.start(function() - while true do - local cmd = io.read() - local handle = skynet.launch(cmd) - if handle == nil then - print("Launch error:",cmd) - end - skynet.yield() - end + socket.stdin() end) diff --git a/service/main.lua b/service/main.lua index c67b0d4a..b3367e90 100644 --- a/service/main.lua +++ b/service/main.lua @@ -3,14 +3,14 @@ local skynet = require "skynet" skynet.start(function() print("Server start") local launcher = skynet.launch("snlua","launcher") + local connection = skynet.launch("connection","256") local lualog = skynet.launch("snlua","lualog") + local console = skynet.launch("snlua","console") local group_mgr = skynet.launch("snlua", "group_mgr") local group_agent = skynet.launch("snlua", "group_agent") local remoteroot = skynet.launch("snlua","remote_root") - local console = skynet.launch("snlua","console") local watchdog = skynet.launch("snlua","watchdog","8888 4 0") local db = skynet.launch("snlua","simpledb") - local connection = skynet.launch("connection","256") local redis = skynet.launch("snlua","redis-mgr") -- skynet.launch("snlua","testgroup")