aboutsummaryrefslogtreecommitdiff
path: root/src/lua/net/luaopen_Socket.cpp
blob: abcd8f293441684e58577f5e44c9599ec97fe28c (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
127
128
129
130
#include "lua/luax.h"
#include "../luaopen_types.h"
#include "libjin/jin.h"
#include "Buffer.h"
#include "Socket.h"

namespace jin
{
namespace lua
{

    using namespace jin::lua::net;

    const int BUFFER_SIZE = 1024;

    static inline Socket* checkSocket(lua_State* L, int pos = 1)
    {
        Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_SOCKET);
        if (proxy != 0 && proxy != nullptr)
            return (Socket*)proxy->object;
        return nullptr;
    }

    static inline net::Buffer* checkNetBuffer(lua_State* L, int pos = 1)
    {
        Proxy* proxy = (Proxy*)luax_checktype(L, pos, JIN_NETWORK_BUFFER);
        if (proxy != 0 && proxy != nullptr)
            return (net::Buffer*)proxy->object;
        return nullptr;
    }

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

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

    static int l_configBlocking(lua_State* L) 
    {
        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