use connection service for console

This commit is contained in:
云风
2012-08-31 21:56:10 +08:00
parent 0a886cd57c
commit 243dc58c35
4 changed files with 59 additions and 15 deletions

View File

@@ -51,7 +51,8 @@ _expand(struct connection_server * server) {
int i;
for (i=0;i<server->max_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;

View File

@@ -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)

View File

@@ -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)

View File

@@ -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")