码迷,mamicode.com
首页 > 其他好文 > 详细

第9月第12天 lua_push lua_to luaL_check

时间:2017-06-12 10:40:33      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:recv   prompt   read   cocos2dx   printf   family   soc   cti   har   

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

标签:recv   prompt   read   cocos2dx   printf   family   soc   cti   har   

原文地址:http://www.cnblogs.com/javastart/p/6991220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!