以協程作為連線處理器

lua-users home
wiki

此範例顯示用於處理多個 socket (例如 TCP 等等) 的基本架構,協程作為中介處理器。(為了篇幅精簡,真正的 socket 程式碼已被略過。)不用寫這個架構,你可以交替使用 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 :)")

另請參閱


RecentChanges · 偏好設定
編輯 · 歷程
最後編輯於 2007 年 1 月 10 日上午 5:08 GMT (diff)