aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/net/je_lua_socket.cpp
blob: 3af3319215bd0a24c17ecfb62034cb0702979fdd (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
#include "common/je_lua_proxy.h"

#include "common/je_lua_common.h"
#include "libjin/jin.h"
#include "je_lua_buffer.h"

using namespace JinEngine::Net;
using namespace JinEngine::Lua::Net;

namespace JinEngine
{
    namespace Lua
    {

        const char* Jin_Lua_Socket = "Socket";

	    typedef Shared<Socket>& SharedSocket;

        const int BUFFER_SIZE = 1024;

        LUA_IMPLEMENT inline SharedSocket checkSocket(lua_State* L, int pos = 1)
        {
            Proxy* proxy = (Proxy*)luax_checktype(L, pos, Jin_Lua_Socket);
		    return proxy->getShared<Socket>();
        }

        LUA_IMPLEMENT inline Shared<Buffer>& checkNetBuffer(lua_State* L, int pos = 1)
        {
            Proxy* proxy = (Proxy*)luax_checktype(L, pos, Jin_Lua_Buffer);
		    return proxy->getShared<Buffer>();
        }

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

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

        LUA_IMPLEMENT int l_configBlocking(lua_State* L) 
        {
		    SharedSocket socket = checkSocket(L);
            bool blocking = luax_checkbool(L, 2);
            socket->configureBlocking(blocking);
            return 0;
        }

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

        LUA_EXPORT void luaopen_Socket(lua_State* L)
        {
            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                }
            };
            luax_newtype(L, Jin_Lua_Socket, socket_function);
        }

    } // namespace Lua
} // namespace JinEngine