第9月第12天 lua_push lua_to luaL_check

Posted lianhuaren

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第9月第12天 lua_push lua_to luaL_check相关的知识,希望对你有一定的参考价值。

1.

c代码中通过lua_push 把数据压入堆栈。luaL_check是对lua_to的封装,从堆栈中获取lua代码中函数调用的数据。

 

static int
lread(lua_State *L) {
    struct socket * s = lua_touserdata(L,1);
    if (s == NULL || s->listen_fd < 0) {
        return luaL_error(L, "start socket first");
    }
    size_t sz = 0;
    const char * welcome = luaL_checklstring(L,2,&sz);
    int fd = test(s, welcome,sz);
    if (fd >= 0) {
        char buffer[BUFFER_SIZE];
        int rd = recv(fd, buffer, BUFFER_SIZE, 0);
        if (rd <= 0) {
            s->closed = 1;
            lua_pushboolean(L, 0);
            return 1;
        }
        lua_pushlstring(L, buffer, rd);
        return 1;
    }
    return 0;
}

 

static int
lstart(lua_State *L) {
    const char * addr = luaL_checkstring(L,1);
    int port = luaL_checkinteger(L,2);

    struct socket * s = lua_newuserdata(L, sizeof(*s));
    s->listen_fd = -1;
    s->fd = -1;
    s->closed = 0;

    int lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    int reuse = 1;
    setsockopt(s->listen_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int));

    struct sockaddr_in service;
 
    service.sin_family = AF_INET;
    service.sin_addr.s_addr = inet_addr(addr);
    service.sin_port = htons(port);

    if (bind(lfd, (const struct sockaddr *)&service, sizeof(service)) < 0) {
        closesocket(lfd);
        printf("bind() failed");
        exit(1);
    }
    if (listen(lfd, 1) < 0) {
        printf("listen(): Error");
        exit(1);
    }
    s->listen_fd = lfd;

    return 1;
}

 

function ldebug.start(host)
    local ip = (host and host.ip) or "127.0.0.1"
    local port = (host and host.port) or 6789
    socks_fd = csock.start(ip , port)
end

function readline()
    local ret = split()
    if ret then
        return ret
    end
    local data = csock.read(socks_fd, socks_prompt)
    if data then
        socks_buffer = socks_buffer .. data
        return split()
    end

    return data
end

 

2.quick

 

http://cocos2d-lua.org/download/3-6-4.md

https://github.com/u0u0/Quick-Cocos2dx-Community

 

以上是关于第9月第12天 lua_push lua_to luaL_check的主要内容,如果未能解决你的问题,请参考以下文章

第9月第30天 MVP

第9月第3天 uilabel contentscale

第9月第5天 AVVideoAverageBitRateKey

第9月第26天 pairs和ipairs

第37月第9天 微信小程序base64

第12月第25天 ImagePickerSheetController