首页 > 代码库 > 第9月第12天 lua_push lua_to luaL_check

第9月第12天 lua_push lua_to luaL_check

1.

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

 

static intlread(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 intlstart(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)endfunction 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 dataend

 

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