Mercurial > code
changeset 183:73146c7c763f
Add implementation of sockets to Lua
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 29 Oct 2013 21:33:00 +0100 |
parents | 15b264d9e833 |
children | 4c746050969a |
files | C++/Lua/LuaSocket.cpp C++/Lua/LuaSocket.h |
diffstat | 2 files changed, 213 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Lua/LuaSocket.cpp Tue Oct 29 21:33:00 2013 +0100 @@ -0,0 +1,192 @@ +/* + * LuaSocket.cpp -- portable Lua socket wrappers + * + * Copyright (c) 2013, David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#define SC_TYPE "Socket" +#define ADDR_TYPE "SocketAddress" + +#define GET_SC(L, idx) *static_cast<Socket **>(luaL_checkudata(L, idx, SC_TYPE)) +#define GET_ADDR(L, idx) *static_cast<SocketAddress **>(luaL_checkudata(L, idx, ADDR_TYPE)) + +#include <cstring> +#include <cerrno> + +#include "LuaSocket.h" + +#include "Socket.h" + +static int l_new(lua_State *L) +{ + int domain = luaL_checkinteger(L, 1); + int type = luaL_checkinteger(L, 2); + int protocol = 0; + Socket *s, **ptr; + + if (lua_gettop(L) >= 3) + protocol = luaL_checkinteger(L, 3); + + try + { + s = new Socket(domain, type, protocol); + } + catch (SocketError error) + { + lua_pushnil(L); + lua_pushstring(L, error.what()); + + return 2; + } + + ptr = static_cast<Socket **>(lua_newuserdata(L, sizeof (void *))); + *ptr = s; + luaL_setmetatable(L, SC_TYPE); + + return 1; +} + +static int l_blockMode(lua_State *L) +{ + Socket *s = GET_SC(L, 1); + bool mode = lua_toboolean(L, 2); + + s->blockMode(mode); + + return 0; +} + +static int l_bind(lua_State *L) +{ + Socket *s = GET_SC(L, 1); + SocketAddress *a = GET_ADDR(L, 2); + + try + { + s->bind(*a); + } + catch (SocketError error) + { + lua_pushboolean(L, false); + lua_pushstring(L, error.what()); + + return 2; + } + + lua_pushboolean(L, true); + + return 1; +} + +static int l_connect(lua_State *L) +{ + Socket *s = GET_SC(L, 1); + SocketAddress *a = GET_ADDR(L, 2); + + try + { + s->connect(*a); + } + catch (SocketError error) + { + lua_pushboolean(L, false); + lua_pushstring(L, error.what()); + + return 2; + } + + lua_pushboolean(L, true); + + return 1; +} + +static int l_accept(lua_State *L) +{ + Socket *s = GET_SC(L, 1); + Socket *c, **ptr; + + try + { + c = new Socket(s->accept()); + } + catch (SocketError error) + { + lua_pushboolean(L, false); + lua_pushstring(L, error.what()); + + return 2; + } + + ptr = static_cast<Socket **>(lua_newuserdata(L, sizeof (void *))); + *ptr = c; + luaL_setmetatable(L, ADDR_TYPE); + + return 1; +} + +static int l_listen(lua_State *L) +{ + Socket *s = GET_SC(L, 1); + int max = luaL_checkinteger(L, 2); + + try + { + s->listen(max); + } + catch (SocketError error) + { + lua_pushboolean(L, false); + lua_pushstring(L, error.what()); + + return 2; + } + + lua_pushboolean(L, true); + + return 1; +} + +static int l_recv(lua_State *L) +{ + Socket *s = GET_SC(L, 1); + unsigned len = luaL_checkinteger(L, 2); + char *data; + + if ((data = (char *)malloc(len)) == NULL) + { + lua_pushnil(L); + lua_pushstring(L, ) + } + + +} + +static const luaL_Reg functions[] = { + { "new", l_new }, + { nullptr, nullptr } +}; + +static const luaL_Reg methods[] = { + { "blockMode", l_blockMode }, + { "bind", l_bind }, + { "connect", l_connect }, + { nullptr, nullptr } +}; + +extern "C" int luaopen_socket(lua_State *L) +{ + + return 1; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C++/Lua/LuaSocket.h Tue Oct 29 21:33:00 2013 +0100 @@ -0,0 +1,21 @@ +/* + * LuaSocket.h -- portable Lua socket wrappers + * + * Copyright (c) 2013, David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <lua.hpp> + +extern "C" int luaopen_socket(lua_State *L);