bugfix : connection

This commit is contained in:
云风
2012-08-30 16:30:52 +08:00
parent 1791becd09
commit 51ef283887
8 changed files with 34 additions and 20 deletions

View File

@@ -65,6 +65,7 @@ _write(lua_State *L) {
case EINTR:
continue;
}
return 0;
}
assert(err == sz);
return 0;
@@ -104,6 +105,7 @@ _writeblock(lua_State *L) {
case EINTR:
continue;
}
return 0;
}
assert(err == sz +2);
return 0;

View File

@@ -95,6 +95,7 @@ _del(struct connection_server * server, int fd) {
static void
_poll(struct connection_server * server) {
int timeout = 100;
void * buffer = NULL;
for (;;) {
struct connection * c = connection_poll(server->pool, timeout);
if (c==NULL) {
@@ -103,7 +104,9 @@ _poll(struct connection_server * server) {
}
timeout = 0;
void * buffer = malloc(DEFAULT_BUFFER_SIZE);
if (buffer == NULL) {
buffer = malloc(DEFAULT_BUFFER_SIZE);
}
int size = recv(c->fd, buffer, DEFAULT_BUFFER_SIZE, MSG_DONTWAIT);
if (size < 0) {
@@ -112,9 +115,11 @@ _poll(struct connection_server * server) {
if (size == 0) {
connection_del(server->pool, c->fd);
free(buffer);
buffer = NULL;
skynet_send(server->ctx, 0, c->address, SESSION_CLIENT, NULL, 0, DONTCOPY);
} else {
skynet_send(server->ctx, 0, c->address, SESSION_CLIENT, buffer, size, DONTCOPY);
buffer = NULL;
}
}
}
@@ -137,8 +142,8 @@ _main(struct skynet_context * ctx, void * ud, int session, uint32_t source, cons
char addr [addr_sz];
memcpy(addr, endptr+1, addr_sz-1);
addr[addr_sz-1] = '\0';
uint32_t address = strtoul(addr, NULL, 16);
if (address != 0) {
uint32_t address = strtoul(addr+1, NULL, 16);
if (address == 0) {
skynet_error(ctx, "[connection] Invalid ADD command from %x (session = %d)", source, session);
return 0;
}

View File

@@ -30,8 +30,8 @@ local meta = {
}
function redis.connect(dbname)
local handle = skynet.call(".redis-manager",dbname)
assert(handle ~= "")
local handle = skynet.call(".redis-manager",skynet.unpack, skynet.pack(dbname))
assert(handle ~= nil)
return setmetatable({ __handle = handle } , meta)
end

View File

@@ -12,7 +12,9 @@ function socket.connect(addr)
if fd == nil then
return true
end
skynet.send(".connection","ADD "..fd.." "..skynet.self())
print("connect to " .. addr)
local command = "ADD "..fd.." ".. skynet.address(skynet.self())
skynet.send(".connection", command )
object = c.new()
end

View File

@@ -56,10 +56,9 @@ _cb(struct skynet_context * context, void * ud, int session, uint32_t source, co
if (b->init < DEFAULT_NUMBER) {
if (source != b->launcher)
return 0;
assert(sz == 9);
char addr[10];
memcpy(addr, msg, 9);
addr[9] = '\0';
char addr[sz+1];
memcpy(addr, msg, sz);
addr[sz] = '\0';
uint32_t address = strtoul(addr+1, NULL, 16);
assert(address != 0);
_init(b, session, address);

View File

@@ -9,7 +9,7 @@ skynet.dispatch(function(msg, sz , session, address)
-- init notice
local reply = instance[address]
if reply then
skynet.send(reply[2] , reply[1], address)
skynet.send(reply[2] , reply[1], skynet.address(address))
instance[address] = nil
end
else

View File

@@ -1,5 +1,6 @@
local skynet = require "skynet"
local socket = require "socket"
local int64 = require "int64"
local string = string
local table = table
local tonumber = tonumber
@@ -10,6 +11,12 @@ local redis_server, redis_db = ...
local function compose_message(msg)
local lines = { "*" .. #msg }
for _,v in ipairs(msg) do
local t = type(v)
if t == "number" then
v = tostring(v)
elseif t == "userdata" then
v = int64.tostring(int64.new(v),10)
end
table.insert(lines,"$"..#v)
table.insert(lines,v)
end
@@ -101,6 +108,7 @@ redcmd[45] = function(data) -- '-'
end
redcmd[58] = function(data) -- ':'
-- todo: return string later
response(true, tonumber(data))
end
@@ -117,7 +125,6 @@ end
local function init()
while socket.connect(redis_server) do
print("Connect failed : "..redis_server)
skynet.sleep(1000)
end
if redis_db then

View File

@@ -1,27 +1,26 @@
local skynet = require "skynet"
local log = require "log"
local config = require "config"
local name = {
main = "127.0.0.1:6379",
}
local redis_conf = skynet.getenv "redis"
local name = config (redis_conf)
local connection = {}
skynet.dispatch(function(msg, sz , session, from)
local dbname = skynet.tostring(msg,sz)
local dbname = skynet.unpack(msg,sz)
if connection[dbname] then
skynet.ret(connection[dbname])
skynet.ret(skynet.pack(connection[dbname]))
return
end
if name[dbname] == nil then
log.Error("Invalid db name : "..dbname)
skynet.ret("")
skynet.ret(skynet.pack(nil))
return
end
local redis_cli = skynet.launch("snlua", "redis-cli", name[dbname])
connection[dbname] = redis_cli
skynet.ret(redis_cli)
skynet.ret(skynet.pack(redis_cli))
end)
skynet.register ".redis-manager"