redesign connection module

This commit is contained in:
云风
2012-08-14 22:37:30 +08:00
parent 4b3ce3ba5b
commit 4a0265020f
13 changed files with 466 additions and 705 deletions

View File

@@ -18,10 +18,11 @@ local function suspend(co, result, command, param, size)
local co_address = session_coroutine_address[co]
c.send(co_address, co_session, param, size)
return suspend(co, coroutine.resume(co))
else
assert(command == nil, command)
elseif command == nil then
session_coroutine_id[co] = nil
session_coroutine_address[co] = nil
else
error("Unknown command : " .. command)
end
end
@@ -99,6 +100,7 @@ function skynet.setenv(key, value)
end
skynet.send = assert(c.send)
skynet.genid = assert(c.genid)
skynet.redirect = assert(c.redirect)
skynet.pack = assert(c.pack)
skynet.tostring = assert(c.tostring)
@@ -126,6 +128,9 @@ end
local function default_dispatch(f)
return function(session, address , msg, sz)
if session == nil then
return
end
if session <= 0 then
session = - session
co = coroutine.create(f)

46
lualib/socket.lua Normal file
View File

@@ -0,0 +1,46 @@
local skynet = require "skynet"
local c = require "socket.c"
local socket = {}
local fd
local object
function socket.connect(addr)
local ip, port = string.match(addr,"([^:]+):(.+)")
port = tonumber(port)
fd = c.open(ip,port)
skynet.send(".connection","ADD "..fd.." "..skynet.self())
object = c.new()
end
function socket.push(msg,sz)
if msg == nil then
socket.close()
else
c.push(object, msg, sz)
end
end
function socket.read(bytes)
return c.read(object, bytes)
end
function socket.readline(sep)
return c.readline(object, sep)
end
function socket.write(...)
c.write(fd, ...)
end
function socket.yield()
c.yield(object)
end
function socket.close()
skynet.send(".connection","DEL "..fd)
fd = nil
end
return socket