aboutsummaryrefslogtreecommitdiff
path: root/src/lua/net/luaopen_Socket.cpp
blob: 6a6f1e19327f7adfab93770fbf40609a852d9c20 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "lua/luax.h"
#include "../common/common.h"
#include "libjin/jin.h"
#include "Buffer.h"

namespace jin
{
namespace lua
{

	using namespace jin::net;
	using namespace lua::net;

    const int BUFFER_SIZE = 1024;

    static inline Ref<Socket>& checkSocket(lua_State* L, int pos = 1)
    {
        Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET);
		return proxy->getRef<Socket>();
    }

    static inline Ref<Buffer>& checkNetBuffer(lua_State* L, int pos = 1)
    {
        Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER);
		return proxy->getRef<Buffer>();
    }

    // return net.Socket
    static int l_accept(lua_State* L) 
    {
		Ref<Socket>& socket = checkSocket(L);
        Socket* client = socket->accept();
        Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy));
        proxy->bind(new Ref<Socket>(client), JIN_NETWORK_SOCKET);
        return 1;
    }
    
    // return net.Buffer
    static int l_receive(lua_State* L) 
    {
		Ref<Socket>& socket = checkSocket(L);
        char buffer[BUFFER_SIZE] = {0};
        int size = socket->receive(buffer, BUFFER_SIZE);
        Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy));
        net::Buffer* netBuffer = new net::Buffer(buffer, size);
        proxy->bind(new Ref<Buffer>(netBuffer), JIN_NETWORK_BUFFER);
        return 1;
    }
    
    // Socket:receiveFrom(address, port)
    static int l_receiveFrom(lua_State* L) 
    {
		Ref<Socket>& socket = checkSocket(L);
        int address = luax_checkinteger(L, 2);
        int port = luax_checkinteger(L, 3);
        char buffer[BUFFER_SIZE];
        int size = socket->receiveFrom(buffer, BUFFER_SIZE, address, port);
        net::Buffer* netBuffer = new net::Buffer(buffer, size);
        Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy));
        proxy->bind(new Ref<Buffer>(netBuffer), JIN_NETWORK_BUFFER);
        return 1;
    }

    // Socket:send(net.Buffer) -> data_length
    static int l_send(lua_State* L) 
    {
		Ref<Socket>& socket = checkSocket(L);
		Ref<Buffer>& ref = checkNetBuffer(L, 2);
        int len = socket->send(ref->buffer, ref->size);
        luax_pushinteger(L, len);
        return 1;
    }
    
    // Socket:sendTo(address, port, net.Buffer)
    static int l_sendTo(lua_State* L) 
    {
		Ref<Socket>& socket = checkSocket(L);
        int address = luax_checkinteger(L, 2);
        int port = luax_checkinteger(L, 3);
		Ref<Buffer>& buffer = checkNetBuffer(L, 4);
        socket->sendTo(buffer->buffer, buffer->size, address, port);
        return 0;
    }
    
    static int l_close(lua_State* L) 
    {
		Ref<Socket>& socket = checkSocket(L);
        socket->close();
        return 0;
    }

    static int l_configBlocking(lua_State* L) 
    {
		Ref<Socket>& socket = checkSocket(L);
        bool blocking = luax_checkbool(L, 2);
        socket->configureBlocking(blocking);
        return 0;
    }

    static int l_gc(lua_State* L)
    {
        Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_SOCKET);
        proxy->release();
        return 0;
    }

    static const luaL_Reg socket_function[] = {
        { "__gc",           l_gc             },
        { "accept",         l_accept         },
        { "receive",        l_receive        },
        { "receiveFrom",    l_receiveFrom    },
        { "send",           l_send           },
        { "sendTo",         l_sendTo         },
        { "close",          l_close          },
        { "configBlocking", l_configBlocking },
        { 0,                0                }
    };

    int luaopen_Socket(lua_State* L)
    {
        luax_newtype(L, JIN_NETWORK_SOCKET, socket_function);
        return 0;
    }

} // lua
} // jin