协程作为连接处理程序

lua-users home
wiki

这展示了使用协程处理多个套接字(例如 TCP 或其他)的基本脚手架。(为了简洁起见,实际的套接字代码被省略。)为了避免编写此脚手架,您可以选择使用 Copas 库,如 CopasExample 中所示。

function read()
    return coroutine.yield()
end

function write_socket(socket, data)
    print("i'm writing to socket " .. socket .. ": " .. data)
end

function connection_handler(socket)
    write_socket(socket, "hi!")
    local input = read()
    write_socket(socket, "the input was: " .. input)
    local input = read()
    write_socket(socket, "even more input was: " .. input)
    if socket == 2 then
       error("example error in socket 2")
    end
end

function create_connection_handler(socket)
    local handler = coroutine.create(connection_handler)
    coroutine.resume(handler, socket)
    return handler
end

connections = {}

function accept_connection(socket)
    print("accepted socket " .. socket)
    connections[socket] = create_connection_handler(socket)
end

function close_connection(socket)
    connections[socket] = nil
    -- close your socket here
    print("socket " .. socket .. " closed")
end

function handle_socket_data(socket, data)
    local ok, msg = coroutine.resume(connections[socket], data)
    if not ok then
        print("the handler for socket " .. socket .. " failed: " .. msg)
        close_connection(socket)
    elseif ok and coroutine.status(connections[socket]) == "dead" then
        print("the handler for socket " .. socket .. " finished")
        close_connection(socket)
    end
end

-- In a real application, these will be created after you 
-- accept()ed a new connection
accept_connection(1)
accept_connection(2)
accept_connection(3)

-- In a real application, you'll use some dispatcher to read 
-- data from sockets and then call handle_socket_data.

-- Data on socket 1
handle_socket_data(1, "here is some data")

-- Data on socket 2
handle_socket_data(2, "here is data for socket 2")

-- More Data on socket 1
handle_socket_data(1, "wow. more data")

-- hm. socket 3 was closed by the user
close_connection(3)

-- Data on socket 2
handle_socket_data(2, "ok. enough :)")

另请参阅


最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2007 年 1 月 10 日凌晨 5:08 GMT (差异)