GithubHelp home page GithubHelp logo

lkcp's Introduction

lkcp

一个封装kcp和udp的lua扩展库!

依赖

  • lua5.3以上
  • 项目路径如下
    |--proj
     |--lua
     |--lkcp

用法

  • UDP Server
local lkcp = require("lkcp")
local log_debug     = logger.debug
local thread_mgr    = quanta.get("thread_mgr")

local udp = lkcp.udp()
local ok, err = udp:listen("127.0.0.1", 8600)
log_debug("udp-svr listen: %s-%s", ok, err)
thread_mgr:fork(function()
    local index = 0
    while true do
        local ok, buf, ip, port = udp:recv()
        if ok then
            index = index + 1
            log_debug("udp-svr recv: %s from %s:%s", buf, ip, port)
            local buff = string.format("server send %s", index)
            udp:send(buff, #buff, ip, port)
        else
            if buf ~= "EWOULDBLOCK" then
                log_debug("udp-svr recv failed: %s", buf)
            end
        end
        thread_mgr:sleep(1000)
    end
end)
  • UDP Client
local lkcp = require("lkcp")
local log_debug     = logger.debug
local thread_mgr    = quanta.get("thread_mgr")

local udp = lkcp.udp()
thread_mgr:fork(function()
    local index = 0
    local cdata = "client send 0!"
    udp:send(cdata, #cdata, "127.0.0.1", 8600)
    while true do
        local ok, buf, ip, port = udp:recv()
        if ok then
            index = index + 1
            log_debug("udp-cli recv: %s from %s:%s", buf, ip, port)
            local buff = string.format("client send %s", index)
            udp:send(buff, #buff, ip, port)
        else
            if buf ~= "EWOULDBLOCK" then
                log_debug("udp-cli recv failed: %s", buf)
            end
        end
        thread_mgr:sleep(1000)
    end
end)
  • KCP Server
local lkcp = require("lkcp")
local log_debug     = logger.debug
local thread_mgr    = quanta.get("thread_mgr")

local udp = lkcp.udp()
local sessions = {}
--kcp output
local kcp_output = function(buff, session_id)
    local session = sessions[session_id]
    if session then
        udp:send(buff, #buff, session.ip, session.port)
    end
end
local find_kcp = function(ip, port)
    for session_id, session in pairs(sessions) do
        if session.ip == ip and session.port == port then
            return session.kcp
        end
    end
    local session_id = 1111
    local kcp = lkcp.kcp(session_id, kcp_output)
    sessions[session_id] = { kcp = kcp, ip = ip, port = port }
    return kcp
end
local ok, err = udp:listen("127.0.0.1", 8600)
log_debug("kcp-svr listen: %s-%s", ok, err)
thread_mgr:fork(function()
    local index = 0
    while true do
        local ok, buf, ip, port = udp:recv()
        if ok then
            --input
            local kcp = find_kcp(ip, port)
            kcp:input(buf)
        else
            if buf ~= "EWOULDBLOCK" then
                og_debug("kcp-svr recv failed: %s", buf)
            end
        end
        for session_id, session in pairs(sessions) do
            local kcp = session.kcp
            --update
            kcp:update(quanta.now_ms)
            --recv
            local len, buf = kcp:recv()
            if len > 0 then
                log_debug("kcp-svr recv: %s(len: %s) from %s", buf, len, session_id)
                --send
                index = index + 1
                local buff = string.format("server send %s", index)
                kcp:send(buff)
            end
        end
        --sleep
        thread_mgr:sleep(1000)
    end
end)
  • KCP Client
local udp = lkcp.udp()
--kcp output
local kcp_output = function(buff)
    udp:send(buff, #buff, "127.0.0.1", 8600)
end
local session_id = 1111
local kcp = lkcp.kcp(session_id, kcp_output)
thread_mgr:fork(function()
    local index = 0
    local cdata = "kcp send 0!"
    kcp:send(cdata)
    while true do
        local ok, buf, ip, port = udp:recv()
        if ok then
            kcp:input(buf)
        else
            if buf ~= "EWOULDBLOCK" then
                log_debug("kcp-cli recv failed: %s", buf)
            end
        end
        --update
        kcp:update(quanta.now_ms)
        --recv
        local len, buf = kcp:recv()
        if len > 0 then
            log_debug("kcp-cli recv: %s(len: %s) from %s", buf, len, session_id)
            --send
            index = index + 1
            local buff = string.format("client send %s", index)
            kcp:send(buff)
        end
        --sleep
        thread_mgr:sleep(1000)
    end
end)

lkcp's People

Contributors

xiyoo0812 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.