Giter Club home page Giter Club logo

luabus's Introduction

luabus

luabus是一个为LUA提供序列化消息传输的简易网络库.

编译环境

目前支持Windows, Linux, MacOS三平台,编译器必须支持C++17.

  • Windows: Visual studio 2017以上版本,需要自行编译lua的dll库.
  • MacOS: 需要自行编译安装lua.
  • Linux: 需要自行编译安装lua.

性能指标

目前尚未专门为性能做测试及优化,只是为了简单好用.

网络

首先需要一个socket_mgr对象:

lbus = require("lbus");
socket_mgr = lbus.create_socket_mgr(最大句柄数);
--用户需要在主循环中调用wait函数,比如:
--其参数为最大阻塞时长(实际是传给epoll_wait之类函数),单位ms.
socket_mgr.wait(50);

监听端口:

--注意保持listener的生存期,一旦被gc,则端口就自动关了
--第三个参数为listen的backlog,可以不传,默认16
listener = mgr.listen("127.0.0.1", 8080, 32);

--设置accept回调
listener.on_accept = function(stream)
    stream.on_recv = function(msg, ...)
        -- ...
    end

    stream.on_error = function(err)
        -- ...
    end
end

发起连接:

--注意保持stream对象生存期,被gc的话,连接会自动关闭
--连接都是异步进行的,一直要stream上面触发'on_connect'事件后,连接才可用
--connect(ip, port, timeout)
stream = mgr.connect("127.0.0.1", 8080, 2000);
--设置连接事件回调:
stream.on_connect = function(result)
    --如果连接成功,result为"ok"
    --否则,result表明具体的错误
end

向对端发送消息:

stream.send("on_login", acc, password);

响应消息:

stream.on_error = function (err)
    --发生任何错误时触发
end

stream.on_recv = function (msg, ...)
    --收到对端消息时触发.
    --通常这里是以...为参数调用msg对应的函数过程.
end

其他方法:

--设置nodelay属性,默认设置为true
stream.set_nodelay(true);
--设置收发缓冲区,至少应该可以容纳一条消息,默认64K
stream.set_recv_buffer_size(1024 * 64);
stream.set_send_buffer_size(1024 * 64);
stream.ip: 对端ip
stream.token: 本连接的唯一标识

主动断开:

--调用close即可:
stream.close();

luabus's People

Contributors

trumanzhao 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.