aboutsummaryrefslogtreecommitdiff
path: root/src/tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tcp.c')
-rw-r--r--src/tcp.c97
1 files changed, 61 insertions, 36 deletions
diff --git a/src/tcp.c b/src/tcp.c
index db6a38e..74857c9 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -10,43 +10,49 @@
10 10
11#include "luasocket.h" 11#include "luasocket.h"
12 12
13#include "aux.h" 13#include "auxiliar.h"
14#include "socket.h"
14#include "inet.h" 15#include "inet.h"
16#include "error.h"
15#include "tcp.h" 17#include "tcp.h"
16 18
17/*=========================================================================*\ 19/*=========================================================================*\
18* Internal function prototypes 20* Internal function prototypes
19\*=========================================================================*/ 21\*=========================================================================*/
20static int tcp_global_create(lua_State *L); 22static int global_create(lua_State *L);
21static int tcp_meth_connect(lua_State *L); 23static int meth_connect(lua_State *L);
22static int tcp_meth_bind(lua_State *L); 24static int meth_bind(lua_State *L);
23static int tcp_meth_send(lua_State *L); 25static int meth_send(lua_State *L);
24static int tcp_meth_getsockname(lua_State *L); 26static int meth_getsockname(lua_State *L);
25static int tcp_meth_getpeername(lua_State *L); 27static int meth_getpeername(lua_State *L);
26static int tcp_meth_receive(lua_State *L); 28static int meth_receive(lua_State *L);
27static int tcp_meth_accept(lua_State *L); 29static int meth_accept(lua_State *L);
28static int tcp_meth_close(lua_State *L); 30static int meth_close(lua_State *L);
29static int tcp_meth_timeout(lua_State *L); 31static int meth_timeout(lua_State *L);
32static int meth_fd(lua_State *L);
33static int meth_dirty(lua_State *L);
30 34
31/* tcp object methods */ 35/* tcp object methods */
32static luaL_reg tcp[] = { 36static luaL_reg tcp[] = {
33 {"connect", tcp_meth_connect}, 37 {"connect", meth_connect},
34 {"send", tcp_meth_send}, 38 {"send", meth_send},
35 {"receive", tcp_meth_receive}, 39 {"receive", meth_receive},
36 {"bind", tcp_meth_bind}, 40 {"bind", meth_bind},
37 {"accept", tcp_meth_accept}, 41 {"accept", meth_accept},
38 {"setpeername", tcp_meth_connect}, 42 {"setpeername", meth_connect},
39 {"setsockname", tcp_meth_bind}, 43 {"setsockname", meth_bind},
40 {"getpeername", tcp_meth_getpeername}, 44 {"getpeername", meth_getpeername},
41 {"getsockname", tcp_meth_getsockname}, 45 {"getsockname", meth_getsockname},
42 {"timeout", tcp_meth_timeout}, 46 {"timeout", meth_timeout},
43 {"close", tcp_meth_close}, 47 {"close", meth_close},
48 {"fd", meth_fd},
49 {"dirty", meth_dirty},
44 {NULL, NULL} 50 {NULL, NULL}
45}; 51};
46 52
47/* functions in library namespace */ 53/* functions in library namespace */
48static luaL_reg func[] = { 54static luaL_reg func[] = {
49 {"tcp", tcp_global_create}, 55 {"tcp", global_create},
50 {NULL, NULL} 56 {NULL, NULL}
51}; 57};
52 58
@@ -60,11 +66,13 @@ void tcp_open(lua_State *L)
60 aux_newclass(L, "tcp{client}", tcp); 66 aux_newclass(L, "tcp{client}", tcp);
61 aux_newclass(L, "tcp{server}", tcp); 67 aux_newclass(L, "tcp{server}", tcp);
62 /* create class groups */ 68 /* create class groups */
63 aux_add2group(L, "tcp{client}", "tcp{client, server}");
64 aux_add2group(L, "tcp{server}", "tcp{client, server}");
65 aux_add2group(L, "tcp{master}", "tcp{any}"); 69 aux_add2group(L, "tcp{master}", "tcp{any}");
66 aux_add2group(L, "tcp{client}", "tcp{any}"); 70 aux_add2group(L, "tcp{client}", "tcp{any}");
67 aux_add2group(L, "tcp{server}", "tcp{any}"); 71 aux_add2group(L, "tcp{server}", "tcp{any}");
72 aux_add2group(L, "tcp{client}", "tcp{client, server}");
73 aux_add2group(L, "tcp{server}", "tcp{client, server}");
74 aux_add2group(L, "tcp{client}", "select{able}");
75 aux_add2group(L, "tcp{server}", "select{able}");
68 /* define library functions */ 76 /* define library functions */
69 luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); 77 luaL_openlib(L, LUASOCKET_LIBNAME, func, 0);
70 lua_pop(L, 1); 78 lua_pop(L, 1);
@@ -76,28 +84,45 @@ void tcp_open(lua_State *L)
76/*-------------------------------------------------------------------------*\ 84/*-------------------------------------------------------------------------*\
77* Just call buffered IO methods 85* Just call buffered IO methods
78\*-------------------------------------------------------------------------*/ 86\*-------------------------------------------------------------------------*/
79static int tcp_meth_send(lua_State *L) 87static int meth_send(lua_State *L)
80{ 88{
81 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); 89 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1);
82 return buf_meth_send(L, &tcp->buf); 90 return buf_meth_send(L, &tcp->buf);
83} 91}
84 92
85static int tcp_meth_receive(lua_State *L) 93static int meth_receive(lua_State *L)
86{ 94{
87 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); 95 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1);
88 return buf_meth_receive(L, &tcp->buf); 96 return buf_meth_receive(L, &tcp->buf);
89} 97}
90 98
91/*-------------------------------------------------------------------------*\ 99/*-------------------------------------------------------------------------*\
100* Select support methods
101\*-------------------------------------------------------------------------*/
102static int meth_fd(lua_State *L)
103{
104 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client, server}", 1);
105 lua_pushnumber(L, tcp->sock);
106 return 1;
107}
108
109static int meth_dirty(lua_State *L)
110{
111 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client, server}", 1);
112 lua_pushboolean(L, !buf_isempty(&tcp->buf));
113 return 1;
114}
115
116/*-------------------------------------------------------------------------*\
92* Just call inet methods 117* Just call inet methods
93\*-------------------------------------------------------------------------*/ 118\*-------------------------------------------------------------------------*/
94static int tcp_meth_getpeername(lua_State *L) 119static int meth_getpeername(lua_State *L)
95{ 120{
96 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); 121 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1);
97 return inet_meth_getpeername(L, &tcp->sock); 122 return inet_meth_getpeername(L, &tcp->sock);
98} 123}
99 124
100static int tcp_meth_getsockname(lua_State *L) 125static int meth_getsockname(lua_State *L)
101{ 126{
102 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1); 127 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{client, server}", 1);
103 return inet_meth_getsockname(L, &tcp->sock); 128 return inet_meth_getsockname(L, &tcp->sock);
@@ -106,7 +131,7 @@ static int tcp_meth_getsockname(lua_State *L)
106/*-------------------------------------------------------------------------*\ 131/*-------------------------------------------------------------------------*\
107* Just call tm methods 132* Just call tm methods
108\*-------------------------------------------------------------------------*/ 133\*-------------------------------------------------------------------------*/
109static int tcp_meth_timeout(lua_State *L) 134static int meth_timeout(lua_State *L)
110{ 135{
111 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 136 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
112 return tm_meth_timeout(L, &tcp->tm); 137 return tm_meth_timeout(L, &tcp->tm);
@@ -115,7 +140,7 @@ static int tcp_meth_timeout(lua_State *L)
115/*-------------------------------------------------------------------------*\ 140/*-------------------------------------------------------------------------*\
116* Closes socket used by object 141* Closes socket used by object
117\*-------------------------------------------------------------------------*/ 142\*-------------------------------------------------------------------------*/
118static int tcp_meth_close(lua_State *L) 143static int meth_close(lua_State *L)
119{ 144{
120 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); 145 p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1);
121 sock_destroy(&tcp->sock); 146 sock_destroy(&tcp->sock);
@@ -125,7 +150,7 @@ static int tcp_meth_close(lua_State *L)
125/*-------------------------------------------------------------------------*\ 150/*-------------------------------------------------------------------------*\
126* Turns a master tcp object into a client object. 151* Turns a master tcp object into a client object.
127\*-------------------------------------------------------------------------*/ 152\*-------------------------------------------------------------------------*/
128static int tcp_meth_connect(lua_State *L) 153static int meth_connect(lua_State *L)
129{ 154{
130 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); 155 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1);
131 const char *address = luaL_checkstring(L, 2); 156 const char *address = luaL_checkstring(L, 2);
@@ -145,7 +170,7 @@ static int tcp_meth_connect(lua_State *L)
145/*-------------------------------------------------------------------------*\ 170/*-------------------------------------------------------------------------*\
146* Turns a master object into a server object 171* Turns a master object into a server object
147\*-------------------------------------------------------------------------*/ 172\*-------------------------------------------------------------------------*/
148static int tcp_meth_bind(lua_State *L) 173static int meth_bind(lua_State *L)
149{ 174{
150 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); 175 p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1);
151 const char *address = luaL_checkstring(L, 2); 176 const char *address = luaL_checkstring(L, 2);
@@ -167,10 +192,10 @@ static int tcp_meth_bind(lua_State *L)
167* Waits for and returns a client object attempting connection to the 192* Waits for and returns a client object attempting connection to the
168* server object 193* server object
169\*-------------------------------------------------------------------------*/ 194\*-------------------------------------------------------------------------*/
170static int tcp_meth_accept(lua_State *L) 195static int meth_accept(lua_State *L)
171{ 196{
172 struct sockaddr_in addr; 197 struct sockaddr_in addr;
173 size_t addr_len = sizeof(addr); 198 socklen_t addr_len = sizeof(addr);
174 p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1); 199 p_tcp server = (p_tcp) aux_checkclass(L, "tcp{server}", 1);
175 p_tm tm = &server->tm; 200 p_tm tm = &server->tm;
176 p_tcp client = lua_newuserdata(L, sizeof(t_tcp)); 201 p_tcp client = lua_newuserdata(L, sizeof(t_tcp));
@@ -200,7 +225,7 @@ static int tcp_meth_accept(lua_State *L)
200/*-------------------------------------------------------------------------*\ 225/*-------------------------------------------------------------------------*\
201* Creates a master tcp object 226* Creates a master tcp object
202\*-------------------------------------------------------------------------*/ 227\*-------------------------------------------------------------------------*/
203int tcp_global_create(lua_State *L) 228int global_create(lua_State *L)
204{ 229{
205 /* allocate tcp object */ 230 /* allocate tcp object */
206 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); 231 p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));