aboutsummaryrefslogtreecommitdiff
path: root/src/lua/net
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/net')
-rw-r--r--src/lua/net/Buffer.h96
-rw-r--r--src/lua/net/luaopen_Buffer.cpp135
-rw-r--r--src/lua/net/luaopen_Socket.cpp126
-rw-r--r--src/lua/net/luaopen_net.cpp86
4 files changed, 0 insertions, 443 deletions
diff --git a/src/lua/net/Buffer.h b/src/lua/net/Buffer.h
deleted file mode 100644
index 89a4fc0..0000000
--- a/src/lua/net/Buffer.h
+++ /dev/null
@@ -1,96 +0,0 @@
-#ifndef __JIN_LUA_NET_NETBUFFER_H
-#define __JIN_LUA_NET_NETBUFFER_H
-
-#include <cstring>
-#include <cstdlib>
-#include "../common/common.h"
-
-namespace jin
-{
-namespace lua
-{
-namespace net
-{
-
- class Buffer
- {
- public:
- Buffer(size_t s = 0)
- : size(s)
- {
- buffer = new char[size];
- memset(buffer, 0, size);
- }
-
- Buffer(const char* data, size_t s)
- : size(s)
- {
- buffer = new char[size];
- memcpy(buffer, data, size);
- }
-
- ~Buffer()
- {
- if (buffer != nullptr)
- {
- delete[] buffer;
- buffer = nullptr;
- size = 0;
- }
- }
-
- void append(const void* data, size_t s)
- {
- if (data == nullptr)
- return;
- char* buf = buffer;
- buffer = new char[size + s];
- memcpy(buffer, buf, size);
- memcpy(buffer + size, data, s);
- delete[] buf;
- size += s;
- return;
- }
-
- const char* grabString(int* length, int offset = 0)
- {
- int l = offset;
- for (; l < size; ++l)
- {
- if (buffer[l] == 0)
- break;
- }
- *length = l - offset + 1;
- char* str = (char*)malloc(*length);
- memcpy(str, buffer + offset, *length);
- return str;
- }
-
- int grabInteger(int* length, int offset = 0)
- {
- *length = sizeof(int);
- return *((int*)(buffer + offset));
- }
-
- float grabFloat(int* length, int offset = 0)
- {
- *length = sizeof(float);
- return *((float*)(buffer + offset));
- }
-
- bool grabBoolean(int* length, int offset = 0)
- {
- *length = sizeof(bool);
- return *((bool*)(buffer + offset));
- }
-
- char* buffer;
- size_t size;
-
- };
-
-} // net
-} // lua
-} // jin
-
-#endif \ No newline at end of file
diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp
deleted file mode 100644
index 1018dcc..0000000
--- a/src/lua/net/luaopen_Buffer.cpp
+++ /dev/null
@@ -1,135 +0,0 @@
-#include "lua/luax.h"
-#include "../common/common.h"
-#include "libjin/jin.h"
-#include "Buffer.h"
-
-namespace jin
-{
-namespace lua
-{
-namespace net
-{
-
- static inline Ref<Buffer>& checkNetBuffer(lua_State* L)
- {
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER);
- return proxy->getRef<Buffer>();
- }
-
- // net.Buffer:append(value) -> value_length
- static int l_append(lua_State* L)
- {
- Ref<Buffer>& ref = checkNetBuffer(L);
- const int vp = 2;
- if (luax_isintegerstrict(L, vp))
- {
- int n = luax_checkinteger(L, vp);
- int size = sizeof(n);
- ref->append(&n, size);
- luax_pushinteger(L, size);
- return 1;
- }
- else if (luax_isfloatstrict(L, vp))
- {
- float n = luax_checknumber(L, vp);
- int size = sizeof(n);
- ref->append(&n, size);
- luax_pushinteger(L, size);
- return 1;
- }
- else if (luax_isbooleanstrict(L, vp))
- {
- bool n = luax_checkbool(L, vp);
- int size = sizeof(n);
- ref->append(&n, size);
- luax_pushinteger(L, size);
- return 1;
- }
- else if (luax_isstringstrict(L, vp))
- {
- const char* str = luax_checkstring(L, vp);
- int size = strlen(str) + 1;
- ref->append(str, size);
- luax_pushinteger(L, size);
- return 1;
- }
- else
- {
- luax_typerror(L, vp, "number, bool or string");
- return 0;
- }
- }
-
- // net.Buffer:grabString(offset) -> string, length
- static int l_grabString(lua_State* L)
- {
- Ref<Buffer>& ref = checkNetBuffer(L);
- int offset = luax_checkinteger(L, 2);
- int len;
- const char* str = ref->grabString(&len, offset);
- luax_pushstring(L, str);
- luax_pushinteger(L, len);
- return 2;
- }
-
- // net.Buffer:grabInteger(offset) -> integer, length
- static int l_grabInteger(lua_State* L)
- {
- Ref<Buffer>& ref = checkNetBuffer(L);
- int offset = luax_checkinteger(L, 2);
- int len;
- int integer = ref->grabInteger(&len, offset);
- luax_pushinteger(L, integer);
- luax_pushinteger(L, len);
- return 2;
- }
-
- static int l_grabFloat(lua_State* L)
- {
- Ref<Buffer>& ref = checkNetBuffer(L);
- int offset = luax_checkinteger(L, 2);
- int len;
- float floatv = ref->grabFloat(&len, offset);
- luax_pushnumber(L, floatv);
- luax_pushinteger(L, len);
- return 2;
- }
-
- static int l_grabBoolean(lua_State* L)
- {
- Ref<Buffer>& ref = checkNetBuffer(L);
- int offset = luax_checkinteger(L, 2);
- int len;
- bool boolean = ref->grabBoolean(&len, offset);
- luax_pushboolean(L, boolean);
- luax_pushinteger(L, len);
- return 2;
- }
-
- static int l_gc(lua_State* L)
- {
- Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_NETWORK_BUFFER);
- proxy->release();
- return 0;
- }
-
- static const luaL_Reg netbuffer_function[] = {
- { "__gc", l_gc },
- { "append", l_append },
- { "grabString", l_grabString },
- { "grabInteger", l_grabInteger },
- { "grabBoolean", l_grabBoolean },
- { "grabFloat", l_grabFloat },
- { 0, 0 }
- };
-
-} // net
-
- int luaopen_Buffer(lua_State* L)
- {
- luax_newtype(L, JIN_NETWORK_BUFFER, net::netbuffer_function);
- return 0;
- }
-
-} // lua
-} // jin \ No newline at end of file
diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp
deleted file mode 100644
index def3d43..0000000
--- a/src/lua/net/luaopen_Socket.cpp
+++ /dev/null
@@ -1,126 +0,0 @@
-#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 \ No newline at end of file
diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp
deleted file mode 100644
index 12e87eb..0000000
--- a/src/lua/net/luaopen_net.cpp
+++ /dev/null
@@ -1,86 +0,0 @@
-#include "lua/luax.h"
-#include "libjin/jin.h"
-#include "../common/common.h"
-#include "Buffer.h"
-
-namespace jin
-{
-namespace lua
-{
- using namespace jin::lua::net;
- using namespace jin::net;
-
- static int l_initNetwork(lua_State* L)
- {
- jin::net::Net::get()->init();
- return 1;
- }
-
- // jin.net.Socket()
- static int l_Socket(lua_State* L)
- {
- SocketInformation info = { 0 };
- {
- const char* socketType = luax_checkstring(L, 1);
- if (strcmp(socketType, "TCP") == 0)
- info.type = SocketType::TCP;
- else if (strcmp(socketType, "UDP") == 0)
- info.type = SocketType::UDP;
- else
- {
- luax_error(L, "jin.net.Socket() first paramter wrong, must be TCP or UDP");
- return 0;
- }
- // type, port
- if (luax_gettop(L) == 2)
- {
- info.port = luax_checkinteger(L, 2);
- }
- // type, address, port
- else if (luax_gettop(L) == 3)
- {
- if (luax_isstringstrict(L, 2))
- info.address = tk_strtohl(luax_checkstring(L, 2));
- else if (luax_isintegerstrict(L, 2))
- info.address = luax_checkinteger(L, 2);
- info.port = luax_checkinteger(L, 3);
- }
- }
- Socket* socket = new Socket(info);
- Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy));
- proxy->bind(new Ref<Socket>(socket, JIN_NETWORK_SOCKET));
- return 1;
- }
-
- // jin.net.Buffer()
- static int l_Buffer(lua_State* L)
- {
- int size = luax_checkinteger(L, 1);
- Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy));
- net::Buffer* buffer = new net::Buffer(size);
- proxy->bind(new Ref<Buffer>(buffer, JIN_NETWORK_BUFFER));
- return 1;
- }
-
- static const luaL_Reg f[] = {
- { "init", l_initNetwork },
- { "newSocket", l_Socket },
- { "newBuffer", l_Buffer },
- { 0, 0 }
- };
-
- extern int luaopen_Socket(lua_State* L);
- extern int luaopen_Buffer(lua_State* L);
-
- int luaopen_net(lua_State* L)
- {
- luaopen_Socket(L);
- luaopen_Buffer(L);
-
- luax_newlib(L, f);
-
- return 1;
- }
-
-} // lua
-} // jin \ No newline at end of file