rename unix.tcp to unix.stream, unix.udp to unix.dgram

This commit is contained in:
enginix 2016-12-25 23:15:12 +08:00
parent ac3201d620
commit 3a33c37b9c
11 changed files with 129 additions and 129 deletions

View File

@ -307,8 +307,8 @@ UNIX_OBJS=\
timeout.$(O) \ timeout.$(O) \
io.$(O) \ io.$(O) \
usocket.$(O) \ usocket.$(O) \
unixtcp.$(O) \ unixstream.$(O) \
unixudp.$(O) \ unixdgram.$(O) \
compat.$(O) \ compat.$(O) \
unix.$(O) unix.$(O)

View File

@ -5,15 +5,15 @@
#include "lua.h" #include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "unixtcp.h" #include "unixstream.h"
#include "unixudp.h" #include "unixdgram.h"
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Modules and functions * Modules and functions
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static const luaL_Reg mod[] = { static const luaL_Reg mod[] = {
{"tcp", unixtcp_open}, {"stream", unixstream_open},
{"udp", unixudp_open}, {"dgram", unixdgram_open},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -1,5 +1,5 @@
/*=========================================================================*\ /*=========================================================================*\
* Unix domain socket udp submodule * Unix domain socket dgram submodule
* LuaSocket toolkit * LuaSocket toolkit
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
@ -15,7 +15,7 @@
#include "unix.h" #include "unix.h"
#include <sys/un.h> #include <sys/un.h>
#define UNIXUDP_DATAGRAMSIZE 8192 #define UNIXDGRAM_DATAGRAMSIZE 8192
/*=========================================================================*\ /*=========================================================================*\
* Internal function prototypes * Internal function prototypes
@ -36,11 +36,11 @@ static int meth_receivefrom(lua_State *L);
static int meth_sendto(lua_State *L); static int meth_sendto(lua_State *L);
static int meth_getsockname(lua_State *L); static int meth_getsockname(lua_State *L);
static const char *unixudp_tryconnect(p_unix un, const char *path); static const char *unixdgram_tryconnect(p_unix un, const char *path);
static const char *unixudp_trybind(p_unix un, const char *path); static const char *unixdgram_trybind(p_unix un, const char *path);
/* unixudp object methods */ /* unixdgram object methods */
static luaL_Reg unixudp_methods[] = { static luaL_Reg unixdgram_methods[] = {
{"__gc", meth_close}, {"__gc", meth_close},
{"__tostring", auxiliar_tostring}, {"__tostring", auxiliar_tostring},
{"bind", meth_bind}, {"bind", meth_bind},
@ -70,23 +70,23 @@ static t_opt optset[] = {
/* functions in library namespace */ /* functions in library namespace */
static luaL_Reg func[] = { static luaL_Reg func[] = {
{"udp", global_create}, {"dgram", global_create},
{NULL, NULL} {NULL, NULL}
}; };
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Initializes module * Initializes module
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int unixudp_open(lua_State *L) int unixdgram_open(lua_State *L)
{ {
/* create classes */ /* create classes */
auxiliar_newclass(L, "unixudp{connected}", unixudp_methods); auxiliar_newclass(L, "unixdgram{connected}", unixdgram_methods);
auxiliar_newclass(L, "unixudp{unconnected}", unixudp_methods); auxiliar_newclass(L, "unixdgram{unconnected}", unixdgram_methods);
/* create class groups */ /* create class groups */
auxiliar_add2group(L, "unixudp{connected}", "unixudp{any}"); auxiliar_add2group(L, "unixdgram{connected}", "unixdgram{any}");
auxiliar_add2group(L, "unixudp{unconnected}", "unixudp{any}"); auxiliar_add2group(L, "unixdgram{unconnected}", "unixdgram{any}");
auxiliar_add2group(L, "unixudp{connected}", "select{able}"); auxiliar_add2group(L, "unixdgram{connected}", "select{able}");
auxiliar_add2group(L, "unixudp{unconnected}", "select{able}"); auxiliar_add2group(L, "unixdgram{unconnected}", "select{able}");
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
return 0; return 0;
@ -95,7 +95,7 @@ int unixudp_open(lua_State *L)
/*=========================================================================*\ /*=========================================================================*\
* Lua methods * Lua methods
\*=========================================================================*/ \*=========================================================================*/
static const char *unixudp_strerror(int err) static const char *unixdgram_strerror(int err)
{ {
/* a 'closed' error on an unconnected means the target address was not /* a 'closed' error on an unconnected means the target address was not
* accepted by the transport layer */ * accepted by the transport layer */
@ -105,7 +105,7 @@ static const char *unixudp_strerror(int err)
static int meth_send(lua_State *L) static int meth_send(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixudp{connected}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixdgram{connected}", 1);
p_timeout tm = &un->tm; p_timeout tm = &un->tm;
size_t count, sent = 0; size_t count, sent = 0;
int err; int err;
@ -114,7 +114,7 @@ static int meth_send(lua_State *L)
err = socket_send(&un->sock, data, count, &sent, tm); err = socket_send(&un->sock, data, count, &sent, tm);
if (err != IO_DONE) { if (err != IO_DONE) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, unixudp_strerror(err)); lua_pushstring(L, unixdgram_strerror(err));
return 2; return 2;
} }
lua_pushnumber(L, (lua_Number) sent); lua_pushnumber(L, (lua_Number) sent);
@ -122,11 +122,11 @@ static int meth_send(lua_State *L)
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Send data through unconnected unixudp socket * Send data through unconnected unixdgram socket
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_sendto(lua_State *L) static int meth_sendto(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixudp{unconnected}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixdgram{unconnected}", 1);
size_t count, sent = 0; size_t count, sent = 0;
const char *data = luaL_checklstring(L, 2, &count); const char *data = luaL_checklstring(L, 2, &count);
const char *path = luaL_checkstring(L, 3); const char *path = luaL_checkstring(L, 3);
@ -155,7 +155,7 @@ static int meth_sendto(lua_State *L)
#endif #endif
if (err != IO_DONE) { if (err != IO_DONE) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, unixudp_strerror(err)); lua_pushstring(L, unixdgram_strerror(err));
return 2; return 2;
} }
lua_pushnumber(L, (lua_Number) sent); lua_pushnumber(L, (lua_Number) sent);
@ -163,8 +163,8 @@ static int meth_sendto(lua_State *L)
} }
static int meth_receive(lua_State *L) { static int meth_receive(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
char buf[UNIXUDP_DATAGRAMSIZE]; char buf[UNIXDGRAM_DATAGRAMSIZE];
size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf)); size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf; char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
int err; int err;
@ -176,10 +176,10 @@ static int meth_receive(lua_State *L) {
return 2; return 2;
} }
err = socket_recv(&un->sock, dgram, wanted, &got, tm); err = socket_recv(&un->sock, dgram, wanted, &got, tm);
/* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ /* Unlike STREAM, recv() of zero is not closed, but a zero-length packet. */
if (err != IO_DONE && err != IO_CLOSED) { if (err != IO_DONE && err != IO_CLOSED) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, unixudp_strerror(err)); lua_pushstring(L, unixdgram_strerror(err));
if (wanted > sizeof(buf)) free(dgram); if (wanted > sizeof(buf)) free(dgram);
return 2; return 2;
} }
@ -189,11 +189,11 @@ static int meth_receive(lua_State *L) {
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Receives data and sender from a UDP socket * Receives data and sender from a DGRAM socket
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_receivefrom(lua_State *L) { static int meth_receivefrom(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixudp{unconnected}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixdgram{unconnected}", 1);
char buf[UNIXUDP_DATAGRAMSIZE]; char buf[UNIXDGRAM_DATAGRAMSIZE];
size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf)); size_t got, wanted = (size_t) luaL_optnumber(L, 2, sizeof(buf));
char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf; char *dgram = wanted > sizeof(buf)? (char *) malloc(wanted): buf;
struct sockaddr_un addr; struct sockaddr_un addr;
@ -208,10 +208,10 @@ static int meth_receivefrom(lua_State *L) {
} }
err = socket_recvfrom(&un->sock, dgram, wanted, &got, (SA *) &addr, err = socket_recvfrom(&un->sock, dgram, wanted, &got, (SA *) &addr,
&addr_len, tm); &addr_len, tm);
/* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ /* Unlike STREAM, recv() of zero is not closed, but a zero-length packet. */
if (err != IO_DONE && err != IO_CLOSED) { if (err != IO_DONE && err != IO_CLOSED) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, unixudp_strerror(err)); lua_pushstring(L, unixdgram_strerror(err));
if (wanted > sizeof(buf)) free(dgram); if (wanted > sizeof(buf)) free(dgram);
return 2; return 2;
} }
@ -227,7 +227,7 @@ static int meth_receivefrom(lua_State *L) {
* Just call option handler * Just call option handler
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_setoption(lua_State *L) { static int meth_setoption(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
return opt_meth_setoption(L, optset, &un->sock); return opt_meth_setoption(L, optset, &un->sock);
} }
@ -235,20 +235,20 @@ static int meth_setoption(lua_State *L) {
* Select support methods * Select support methods
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_getfd(lua_State *L) { static int meth_getfd(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
lua_pushnumber(L, (int) un->sock); lua_pushnumber(L, (int) un->sock);
return 1; return 1;
} }
/* this is very dangerous, but can be handy for those that are brave enough */ /* this is very dangerous, but can be handy for those that are brave enough */
static int meth_setfd(lua_State *L) { static int meth_setfd(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
un->sock = (t_socket) luaL_checknumber(L, 2); un->sock = (t_socket) luaL_checknumber(L, 2);
return 0; return 0;
} }
static int meth_dirty(lua_State *L) { static int meth_dirty(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
(void) un; (void) un;
lua_pushboolean(L, 0); lua_pushboolean(L, 0);
return 1; return 1;
@ -257,7 +257,7 @@ static int meth_dirty(lua_State *L) {
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Binds an object to an address * Binds an object to an address
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static const char *unixudp_trybind(p_unix un, const char *path) { static const char *unixdgram_trybind(p_unix un, const char *path) {
struct sockaddr_un local; struct sockaddr_un local;
size_t len = strlen(path); size_t len = strlen(path);
int err; int err;
@ -280,9 +280,9 @@ static const char *unixudp_trybind(p_unix un, const char *path) {
static int meth_bind(lua_State *L) static int meth_bind(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixudp{unconnected}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixdgram{unconnected}", 1);
const char *path = luaL_checkstring(L, 2); const char *path = luaL_checkstring(L, 2);
const char *err = unixudp_trybind(un, path); const char *err = unixdgram_trybind(un, path);
if (err) { if (err) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, err);
@ -294,7 +294,7 @@ static int meth_bind(lua_State *L)
static int meth_getsockname(lua_State *L) static int meth_getsockname(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
struct sockaddr_un peer = {0}; struct sockaddr_un peer = {0};
socklen_t peer_len = sizeof(peer); socklen_t peer_len = sizeof(peer);
@ -309,9 +309,9 @@ static int meth_getsockname(lua_State *L)
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Turns a master unixudp object into a client object. * Turns a master unixdgram object into a client object.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static const char *unixudp_tryconnect(p_unix un, const char *path) static const char *unixdgram_tryconnect(p_unix un, const char *path)
{ {
struct sockaddr_un remote; struct sockaddr_un remote;
int err; int err;
@ -335,16 +335,16 @@ static const char *unixudp_tryconnect(p_unix un, const char *path)
static int meth_connect(lua_State *L) static int meth_connect(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
const char *path = luaL_checkstring(L, 2); const char *path = luaL_checkstring(L, 2);
const char *err = unixudp_tryconnect(un, path); const char *err = unixdgram_tryconnect(un, path);
if (err) { if (err) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, err);
return 2; return 2;
} }
/* turn unconnected object into a connected object */ /* turn unconnected object into a connected object */
auxiliar_setclass(L, "unixudp{connected}", 1); auxiliar_setclass(L, "unixdgram{connected}", 1);
lua_pushnumber(L, 1); lua_pushnumber(L, 1);
return 1; return 1;
} }
@ -354,7 +354,7 @@ static int meth_connect(lua_State *L)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_close(lua_State *L) static int meth_close(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
socket_destroy(&un->sock); socket_destroy(&un->sock);
lua_pushnumber(L, 1); lua_pushnumber(L, 1);
return 1; return 1;
@ -365,13 +365,13 @@ static int meth_close(lua_State *L)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_settimeout(lua_State *L) static int meth_settimeout(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
return timeout_meth_settimeout(L, &un->tm); return timeout_meth_settimeout(L, &un->tm);
} }
static int meth_gettimeout(lua_State *L) static int meth_gettimeout(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixdgram{any}", 1);
return timeout_meth_gettimeout(L, &un->tm); return timeout_meth_gettimeout(L, &un->tm);
} }
@ -379,7 +379,7 @@ static int meth_gettimeout(lua_State *L)
* Library functions * Library functions
\*=========================================================================*/ \*=========================================================================*/
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Creates a master unixudp object * Creates a master unixdgram object
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int global_create(lua_State *L) static int global_create(lua_State *L)
{ {
@ -387,10 +387,10 @@ static int global_create(lua_State *L)
int err = socket_create(&sock, AF_UNIX, SOCK_DGRAM, 0); int err = socket_create(&sock, AF_UNIX, SOCK_DGRAM, 0);
/* try to allocate a system socket */ /* try to allocate a system socket */
if (err == IO_DONE) { if (err == IO_DONE) {
/* allocate unixudp object */ /* allocate unixdgram object */
p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix));
/* set its type as master object */ /* set its type as master object */
auxiliar_setclass(L, "unixudp{unconnected}", -1); auxiliar_setclass(L, "unixdgram{unconnected}", -1);
/* initialize remaining structure fields */ /* initialize remaining structure fields */
socket_setnonblocking(&sock); socket_setnonblocking(&sock);
un->sock = sock; un->sock = sock;

View File

@ -1,13 +1,13 @@
#ifndef UNIXUDP_H #ifndef UNIXDGRAM_H
#define UNIXUDP_H #define UNIXDGRAM_H
/*=========================================================================*\ /*=========================================================================*\
* UDP object * DGRAM object
* LuaSocket toolkit * LuaSocket toolkit
* *
* The udp.h module provides LuaSocket with support for UDP protocol * The dgram.h module provides LuaSocket with support for DGRAM protocol
* (AF_INET, SOCK_DGRAM). * (AF_INET, SOCK_DGRAM).
* *
* Two classes are defined: connected and unconnected. UDP objects are * Two classes are defined: connected and unconnected. DGRAM objects are
* originally unconnected. They can be "connected" to a given address * originally unconnected. They can be "connected" to a given address
* with a call to the setpeername function. The same function can be used to * with a call to the setpeername function. The same function can be used to
* break the connection. * break the connection.
@ -15,6 +15,6 @@
#include "unix.h" #include "unix.h"
int unixudp_open(lua_State *L); int unixdgram_open(lua_State *L);
#endif /* UNIXUDP_H */ #endif /* UNIXDGRAM_H */

View File

@ -1,5 +1,5 @@
/*=========================================================================*\ /*=========================================================================*\
* Unix domain socket tcp sub module * Unix domain socket stream sub module
* LuaSocket toolkit * LuaSocket toolkit
\*=========================================================================*/ \*=========================================================================*/
#include <string.h> #include <string.h>
@ -11,7 +11,7 @@
#include "auxiliar.h" #include "auxiliar.h"
#include "socket.h" #include "socket.h"
#include "options.h" #include "options.h"
#include "unixtcp.h" #include "unixstream.h"
#include <sys/un.h> #include <sys/un.h>
/*=========================================================================*\ /*=========================================================================*\
@ -35,11 +35,11 @@ static int meth_getstats(lua_State *L);
static int meth_setstats(lua_State *L); static int meth_setstats(lua_State *L);
static int meth_getsockname(lua_State *L); static int meth_getsockname(lua_State *L);
static const char *unixtcp_tryconnect(p_unix un, const char *path); static const char *unixstream_tryconnect(p_unix un, const char *path);
static const char *unixtcp_trybind(p_unix un, const char *path); static const char *unixstream_trybind(p_unix un, const char *path);
/* unixtcp object methods */ /* unixstream object methods */
static luaL_Reg unixtcp_methods[] = { static luaL_Reg unixstream_methods[] = {
{"__gc", meth_close}, {"__gc", meth_close},
{"__tostring", auxiliar_tostring}, {"__tostring", auxiliar_tostring},
{"accept", meth_accept}, {"accept", meth_accept},
@ -73,24 +73,24 @@ static t_opt optset[] = {
/* functions in library namespace */ /* functions in library namespace */
static luaL_Reg func[] = { static luaL_Reg func[] = {
{"tcp", global_create}, {"stream", global_create},
{NULL, NULL} {NULL, NULL}
}; };
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Initializes module * Initializes module
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int unixtcp_open(lua_State *L) int unixstream_open(lua_State *L)
{ {
/* create classes */ /* create classes */
auxiliar_newclass(L, "unixtcp{master}", unixtcp_methods); auxiliar_newclass(L, "unixstream{master}", unixstream_methods);
auxiliar_newclass(L, "unixtcp{client}", unixtcp_methods); auxiliar_newclass(L, "unixstream{client}", unixstream_methods);
auxiliar_newclass(L, "unixtcp{server}", unixtcp_methods); auxiliar_newclass(L, "unixstream{server}", unixstream_methods);
/* create class groups */ /* create class groups */
auxiliar_add2group(L, "unixtcp{master}", "unixtcp{any}"); auxiliar_add2group(L, "unixstream{master}", "unixstream{any}");
auxiliar_add2group(L, "unixtcp{client}", "unixtcp{any}"); auxiliar_add2group(L, "unixstream{client}", "unixstream{any}");
auxiliar_add2group(L, "unixtcp{server}", "unixtcp{any}"); auxiliar_add2group(L, "unixstream{server}", "unixstream{any}");
luaL_setfuncs(L, func, 0); luaL_setfuncs(L, func, 0);
return 0; return 0;
@ -103,22 +103,22 @@ int unixtcp_open(lua_State *L)
* Just call buffered IO methods * Just call buffered IO methods
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_send(lua_State *L) { static int meth_send(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{client}", 1);
return buffer_meth_send(L, &un->buf); return buffer_meth_send(L, &un->buf);
} }
static int meth_receive(lua_State *L) { static int meth_receive(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{client}", 1);
return buffer_meth_receive(L, &un->buf); return buffer_meth_receive(L, &un->buf);
} }
static int meth_getstats(lua_State *L) { static int meth_getstats(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{client}", 1);
return buffer_meth_getstats(L, &un->buf); return buffer_meth_getstats(L, &un->buf);
} }
static int meth_setstats(lua_State *L) { static int meth_setstats(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{client}", 1);
return buffer_meth_setstats(L, &un->buf); return buffer_meth_setstats(L, &un->buf);
} }
@ -126,7 +126,7 @@ static int meth_setstats(lua_State *L) {
* Just call option handler * Just call option handler
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_setoption(lua_State *L) { static int meth_setoption(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixstream{any}", 1);
return opt_meth_setoption(L, optset, &un->sock); return opt_meth_setoption(L, optset, &un->sock);
} }
@ -134,20 +134,20 @@ static int meth_setoption(lua_State *L) {
* Select support methods * Select support methods
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_getfd(lua_State *L) { static int meth_getfd(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixstream{any}", 1);
lua_pushnumber(L, (int) un->sock); lua_pushnumber(L, (int) un->sock);
return 1; return 1;
} }
/* this is very dangerous, but can be handy for those that are brave enough */ /* this is very dangerous, but can be handy for those that are brave enough */
static int meth_setfd(lua_State *L) { static int meth_setfd(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixstream{any}", 1);
un->sock = (t_socket) luaL_checknumber(L, 2); un->sock = (t_socket) luaL_checknumber(L, 2);
return 0; return 0;
} }
static int meth_dirty(lua_State *L) { static int meth_dirty(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixstream{any}", 1);
lua_pushboolean(L, !buffer_isempty(&un->buf)); lua_pushboolean(L, !buffer_isempty(&un->buf));
return 1; return 1;
} }
@ -157,14 +157,14 @@ static int meth_dirty(lua_State *L) {
* server object * server object
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_accept(lua_State *L) { static int meth_accept(lua_State *L) {
p_unix server = (p_unix) auxiliar_checkclass(L, "unixtcp{server}", 1); p_unix server = (p_unix) auxiliar_checkclass(L, "unixstream{server}", 1);
p_timeout tm = timeout_markstart(&server->tm); p_timeout tm = timeout_markstart(&server->tm);
t_socket sock; t_socket sock;
int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); int err = socket_accept(&server->sock, &sock, NULL, NULL, tm);
/* if successful, push client socket */ /* if successful, push client socket */
if (err == IO_DONE) { if (err == IO_DONE) {
p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix)); p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix));
auxiliar_setclass(L, "unixtcp{client}", -1); auxiliar_setclass(L, "unixstream{client}", -1);
/* initialize structure fields */ /* initialize structure fields */
socket_setnonblocking(&sock); socket_setnonblocking(&sock);
clnt->sock = sock; clnt->sock = sock;
@ -183,7 +183,7 @@ static int meth_accept(lua_State *L) {
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Binds an object to an address * Binds an object to an address
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static const char *unixtcp_trybind(p_unix un, const char *path) { static const char *unixstream_trybind(p_unix un, const char *path) {
struct sockaddr_un local; struct sockaddr_un local;
size_t len = strlen(path); size_t len = strlen(path);
int err; int err;
@ -205,9 +205,9 @@ static const char *unixtcp_trybind(p_unix un, const char *path) {
} }
static int meth_bind(lua_State *L) { static int meth_bind(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{master}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{master}", 1);
const char *path = luaL_checkstring(L, 2); const char *path = luaL_checkstring(L, 2);
const char *err = unixtcp_trybind(un, path); const char *err = unixstream_trybind(un, path);
if (err) { if (err) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, err);
@ -219,7 +219,7 @@ static int meth_bind(lua_State *L) {
static int meth_getsockname(lua_State *L) static int meth_getsockname(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixstream{any}", 1);
struct sockaddr_un peer = {0}; struct sockaddr_un peer = {0};
socklen_t peer_len = sizeof(peer); socklen_t peer_len = sizeof(peer);
@ -234,9 +234,9 @@ static int meth_getsockname(lua_State *L)
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Turns a master unixtcp object into a client object. * Turns a master unixstream object into a client object.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static const char *unixtcp_tryconnect(p_unix un, const char *path) static const char *unixstream_tryconnect(p_unix un, const char *path)
{ {
struct sockaddr_un remote; struct sockaddr_un remote;
int err; int err;
@ -260,16 +260,16 @@ static const char *unixtcp_tryconnect(p_unix un, const char *path)
static int meth_connect(lua_State *L) static int meth_connect(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{master}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{master}", 1);
const char *path = luaL_checkstring(L, 2); const char *path = luaL_checkstring(L, 2);
const char *err = unixtcp_tryconnect(un, path); const char *err = unixstream_tryconnect(un, path);
if (err) { if (err) {
lua_pushnil(L); lua_pushnil(L);
lua_pushstring(L, err); lua_pushstring(L, err);
return 2; return 2;
} }
/* turn master object into a client object */ /* turn master object into a client object */
auxiliar_setclass(L, "unixtcp{client}", 1); auxiliar_setclass(L, "unixstream{client}", 1);
lua_pushnumber(L, 1); lua_pushnumber(L, 1);
return 1; return 1;
} }
@ -279,7 +279,7 @@ static int meth_connect(lua_State *L)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_close(lua_State *L) static int meth_close(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixstream{any}", 1);
socket_destroy(&un->sock); socket_destroy(&un->sock);
lua_pushnumber(L, 1); lua_pushnumber(L, 1);
return 1; return 1;
@ -290,7 +290,7 @@ static int meth_close(lua_State *L)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_listen(lua_State *L) static int meth_listen(lua_State *L)
{ {
p_unix un = (p_unix) auxiliar_checkclass(L, "unixtcp{master}", 1); p_unix un = (p_unix) auxiliar_checkclass(L, "unixstream{master}", 1);
int backlog = (int) luaL_optnumber(L, 2, 32); int backlog = (int) luaL_optnumber(L, 2, 32);
int err = socket_listen(&un->sock, backlog); int err = socket_listen(&un->sock, backlog);
if (err != IO_DONE) { if (err != IO_DONE) {
@ -299,7 +299,7 @@ static int meth_listen(lua_State *L)
return 2; return 2;
} }
/* turn master object into a server object */ /* turn master object into a server object */
auxiliar_setclass(L, "unixtcp{server}", 1); auxiliar_setclass(L, "unixstream{server}", 1);
lua_pushnumber(L, 1); lua_pushnumber(L, 1);
return 1; return 1;
} }
@ -311,9 +311,9 @@ static int meth_shutdown(lua_State *L)
{ {
/* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */ /* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */
static const char* methods[] = { "receive", "send", "both", NULL }; static const char* methods[] = { "receive", "send", "both", NULL };
p_unix tcp = (p_unix) auxiliar_checkclass(L, "unixtcp{client}", 1); p_unix stream = (p_unix) auxiliar_checkclass(L, "unixstream{client}", 1);
int how = luaL_checkoption(L, 2, "both", methods); int how = luaL_checkoption(L, 2, "both", methods);
socket_shutdown(&tcp->sock, how); socket_shutdown(&stream->sock, how);
lua_pushnumber(L, 1); lua_pushnumber(L, 1);
return 1; return 1;
} }
@ -322,7 +322,7 @@ static int meth_shutdown(lua_State *L)
* Just call tm methods * Just call tm methods
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int meth_settimeout(lua_State *L) { static int meth_settimeout(lua_State *L) {
p_unix un = (p_unix) auxiliar_checkgroup(L, "unixtcp{any}", 1); p_unix un = (p_unix) auxiliar_checkgroup(L, "unixstream{any}", 1);
return timeout_meth_settimeout(L, &un->tm); return timeout_meth_settimeout(L, &un->tm);
} }
@ -330,17 +330,17 @@ static int meth_settimeout(lua_State *L) {
* Library functions * Library functions
\*=========================================================================*/ \*=========================================================================*/
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Creates a master unixtcp object * Creates a master unixstream object
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
static int global_create(lua_State *L) { static int global_create(lua_State *L) {
t_socket sock; t_socket sock;
int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0); int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0);
/* try to allocate a system socket */ /* try to allocate a system socket */
if (err == IO_DONE) { if (err == IO_DONE) {
/* allocate unixtcp object */ /* allocate unixstream object */
p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix));
/* set its type as master object */ /* set its type as master object */
auxiliar_setclass(L, "unixtcp{master}", -1); auxiliar_setclass(L, "unixstream{master}", -1);
/* initialize remaining structure fields */ /* initialize remaining structure fields */
socket_setnonblocking(&sock); socket_setnonblocking(&sock);
un->sock = sock; un->sock = sock;

21
src/unixstream.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef UNIXSTREAM_H
#define UNIXSTREAM_H
/*=========================================================================*\
* UNIX STREAM object
* LuaSocket toolkit
*
* The unixstream.h module is basicly a glue that puts together modules buffer.h,
* timeout.h socket.h and inet.h to provide the LuaSocket UNIX STREAM (AF_UNIX,
* SOCK_STREAM) support.
*
* Three classes are defined: master, client and server. The master class is
* a newly created unixstream object, that has not been bound or connected. Server
* objects are unixstream objects bound to some local address. Client objects are
* unixstream objects either connected to some address or returned by the accept
* method of a server object.
\*=========================================================================*/
#include "unix.h"
int unixstream_open(lua_State *L);
#endif /* UNIXSTREAM_H */

View File

@ -1,21 +0,0 @@
#ifndef UNIXTCP_H
#define UNIXTCP_H
/*=========================================================================*\
* UNIX TCP object
* LuaSocket toolkit
*
* The unixtcp.h module is basicly a glue that puts together modules buffer.h,
* timeout.h socket.h and inet.h to provide the LuaSocket UNIX TCP (AF_UNIX,
* SOCK_STREAM) support.
*
* Three classes are defined: master, client and server. The master class is
* a newly created unixtcp object, that has not been bound or connected. Server
* objects are unixtcp objects bound to some local address. Client objects are
* unixtcp objects either connected to some address or returned by the accept
* method of a server object.
\*=========================================================================*/
#include "unix.h"
int unixtcp_open(lua_State *L);
#endif /* UNIXTCP_H */

View File

@ -1,7 +1,7 @@
socket = require"socket" socket = require"socket"
socket.unix = require"socket.unix" socket.unix = require"socket.unix"
c = assert(socket.unix.udp()) c = assert(socket.unix.dgram())
c:bind("/tmp/bar") print(c:bind("/tmp/bar"))
while 1 do while 1 do
local l = io.read("*l") local l = io.read("*l")
assert(c:sendto(l, "/tmp/foo")) assert(c:sendto(l, "/tmp/foo"))

View File

@ -1,6 +1,6 @@
socket = require"socket" socket = require"socket"
socket.unix = require"socket.unix" socket.unix = require"socket.unix"
u = assert(socket.unix.udp()) u = assert(socket.unix.dgram())
assert(u:bind("/tmp/foo")) assert(u:bind("/tmp/foo"))
while 1 do while 1 do
x, r = assert(u:receivefrom()) x, r = assert(u:receivefrom())

View File

@ -1,6 +1,6 @@
socket = require"socket" socket = require"socket"
socket.unix = require"socket.unix" socket.unix = require"socket.unix"
c = assert(socket.unix.tcp()) c = assert(socket.unix.stream())
assert(c:connect("/tmp/foo")) assert(c:connect("/tmp/foo"))
while 1 do while 1 do
local l = io.read() local l = io.read()

View File

@ -1,6 +1,6 @@
socket = require"socket" socket = require"socket"
socket.unix = require"socket.unix" socket.unix = require"socket.unix"
u = assert(socket.unix.tcp()) u = assert(socket.unix.stream())
assert(u:bind("/tmp/foo")) assert(u:bind("/tmp/foo"))
assert(u:listen()) assert(u:listen())
c = assert(u:accept()) c = assert(u:accept())