aboutsummaryrefslogtreecommitdiff
path: root/src/lua/net/luaopen_net.cpp
blob: 821f27398dacfc9365fef5cec5ab37fa5cb3292a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/** 
* Notice: the net module is not finished yet. 
*/
#include "lua/luax.h"
#include "3rdparty/tekcos/tekcos.h"

namespace jin
{
namespace lua
{
    struct
    {
        tk_TCPsocket* sk; 
    }context;

    /**
    * A table is needed. For example: 
    *   local conf = { 
    *       mode = "server", 
    *       ip = "", 
    *       port = 8000
    *   } 
    */
    static int l_open(lua_State* L)
    {
        // init context.sk 
        context.sk = 0; 
        if (!luax_istable(L, 1))
        {
            luax_typerror(L, 1, "table is needed"); 
            return 0;
        }
        luax_getfield(L, 1, "mode");
        if (luax_isnil(L, -1))
        {// no mode field 
            luax_error(L, "mode field is needed, but get nil");
            return 0;
        }
        const char* mode = luax_checkstring(L, -1);
        if (strcmp(mode, "server") == 0 || strcmp(mode, "client") == 0)
        {
            
            if (strcmp(mode, "server") == 0)
            {// a server, ignore ip field
                
            }
            else
            {

            }
        }
        else
        {
            luax_error(L, "\"server\" or \"client\" is needed, but get %s", mode);
            return 0; 
        }
        return 1;
    }

    static int l_accept(lua_State* L)
    {
        return 1; 
    }

    static int l_send(lua_State* L)
    {
        return 1;
    }

    static int l_recv(lua_State* L)
    {
        return 1;
    }

    static int l_close(lua_State* L)
    {
        return 1;
    }

    static int l_nonblocking(lua_State* L)
    {
        return 1;
    }

    // block mode by default 
    static int l_blocking(lua_State* L)
    {
        return 1;
    }

    static const luaL_Reg f[] = {
        {"open",        l_open},
        {"accept",      l_accept}, 
        {"send",        l_send},
        {"recv",        l_recv},
        {"close",       l_close},
        {"blocking",    l_blocking },
        {"nonblocking", l_nonblocking},
        {0, 0}
    };

    // only tcp
    int luaopen_net(lua_State* L)
    {
        luax_newlib(L, f);

        return 1;
    }
}
}