Mercurial > code
view C++/Lua/LuaSocket.cpp @ 191:5f75779cc7eb
Deprecate C
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 27 Nov 2013 10:53:20 +0100 |
parents | 73146c7c763f |
children | 1c2788f9f55f |
line wrap: on
line source
/* * 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; }