Lua   发布时间:2019-10-08  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了skynet的ssdb驱动大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
local skynet = require "skynet"
local socket = require "socket"
local socketchAnnel = require "socketchAnnel"
local int64 = require "int64"

local ssdb = {}
local command = {}
local meta = {
    __index = command,-- DO NOT close chAnnel in __gc
}

local function read_block( fd )
    local result = fd:readline "\n"
    -- print('read_block result',result,String.len(result))
    local len = tonumber(result)
    -- print('read_block result len',len)
    if not len  then
        return false
    elseif len == 0 then 
        local result = fd:readline "\n\n"
        return false
    end

    local value = fd:read(len+1)
    -- print('read_block value',value)
    local data = String.sub(value,1,-2)
    -- print('read_block data',data)
    -- print('data',data)
    return true,data
end


local function read_response(fd)
    local ok,result = read_block(fd)
    -- print('read_response read_block result',ok,result)
    if ok then
        if result~='ok' then
            read_block(fd)
            
            -- print('read_response')
            return true
        end
    end
    
    local bulk = {}
    local ok,d = read_block(fd)
    while ok do

        table.insert(bulk,d)

        ok,d = read_block(fd)
        if not ok then
            break
        end
    end
    
    if #bulk<=1 then
        -- print('read_response')
        return true,bulk[1]
    end
    -- print('read_response')
    return true,bulk
end

local function pack_value(lines,v)
    if v == nil then
        return
    end

    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

local function compose_message(cmd,msg)
    local t = type(msg)

    local lines = {}
    pack_value(lines,cmd)

    if t == "table" then
        for _,v in ipairs(msg) do
            pack_value(lines,v)
        end
    else
        pack_value(lines,msg)
    end

    local chunk =  table.concat(lines,"\n")..'\n\n'
    -- print("chunk",chunk)
    return chunk
end

setmetatable(command,{ __index = function(t,k)
    local cmd = String.lower(k)
    local f = function (self,v,...)
        if type(v) == "table" then
            return self[1]:request(compose_message(cmd,v),read_responsE)
        else
            local s = compose_message(cmd,{v,...})
            return self[1]:request(s,read_responsE)
        end
    end
    t[k] = f
    return f
enD})


function ssdb.connect(db_conf)
    local chAnnel = socketchAnnel.chAnnel {
        host = db_conf.host,port = db_conf.port or 8888,}
    -- try connect first only once
    chAnnel:connect(true)
    return setmetatable( { chAnnel },meta )
end


return ssdb

大佬总结

以上是大佬教程为你收集整理的skynet的ssdb驱动全部内容,希望文章能够帮你解决skynet的ssdb驱动所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: