1. 程式人生 > >第9月第12天 lua_push lua_to luaL_check

第9月第12天 lua_push lua_to luaL_check

sets lua lis cnblogs welcom down list split() ret

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