diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-06-09 18:23:40 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-06-09 18:23:40 +0000 |
commit | 58bdb658aaa1c30a8f3bed46eef880d308fae582 (patch) | |
tree | 5bf880c715daff79c1a2062f2f3ae8336858c83f /src/tcp.c | |
parent | b2724ad2d1cc3768a04270ed3f8014ec65ad133b (diff) | |
download | luasocket-58bdb658aaa1c30a8f3bed46eef880d308fae582.tar.gz luasocket-58bdb658aaa1c30a8f3bed46eef880d308fae582.tar.bz2 luasocket-58bdb658aaa1c30a8f3bed46eef880d308fae582.zip |
Select re-implemented in a nicer way.
Few changes in internal class and group registration.
Lua modules are compiled and built into library.
Dynamic library tested in Linux and Mac OS X.
Diffstat (limited to 'src/tcp.c')
-rw-r--r-- | src/tcp.c | 97 |
1 files changed, 61 insertions, 36 deletions
@@ -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 | \*=========================================================================*/ |
20 | static int tcp_global_create(lua_State *L); | 22 | static int global_create(lua_State *L); |
21 | static int tcp_meth_connect(lua_State *L); | 23 | static int meth_connect(lua_State *L); |
22 | static int tcp_meth_bind(lua_State *L); | 24 | static int meth_bind(lua_State *L); |
23 | static int tcp_meth_send(lua_State *L); | 25 | static int meth_send(lua_State *L); |
24 | static int tcp_meth_getsockname(lua_State *L); | 26 | static int meth_getsockname(lua_State *L); |
25 | static int tcp_meth_getpeername(lua_State *L); | 27 | static int meth_getpeername(lua_State *L); |
26 | static int tcp_meth_receive(lua_State *L); | 28 | static int meth_receive(lua_State *L); |
27 | static int tcp_meth_accept(lua_State *L); | 29 | static int meth_accept(lua_State *L); |
28 | static int tcp_meth_close(lua_State *L); | 30 | static int meth_close(lua_State *L); |
29 | static int tcp_meth_timeout(lua_State *L); | 31 | static int meth_timeout(lua_State *L); |
32 | static int meth_fd(lua_State *L); | ||
33 | static int meth_dirty(lua_State *L); | ||
30 | 34 | ||
31 | /* tcp object methods */ | 35 | /* tcp object methods */ |
32 | static luaL_reg tcp[] = { | 36 | static 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 */ |
48 | static luaL_reg func[] = { | 54 | static 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 | \*-------------------------------------------------------------------------*/ |
79 | static int tcp_meth_send(lua_State *L) | 87 | static 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 | ||
85 | static int tcp_meth_receive(lua_State *L) | 93 | static 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 | \*-------------------------------------------------------------------------*/ | ||
102 | static 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 | |||
109 | static 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 | \*-------------------------------------------------------------------------*/ |
94 | static int tcp_meth_getpeername(lua_State *L) | 119 | static 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 | ||
100 | static int tcp_meth_getsockname(lua_State *L) | 125 | static 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 | \*-------------------------------------------------------------------------*/ |
109 | static int tcp_meth_timeout(lua_State *L) | 134 | static 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 | \*-------------------------------------------------------------------------*/ |
118 | static int tcp_meth_close(lua_State *L) | 143 | static 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 | \*-------------------------------------------------------------------------*/ |
128 | static int tcp_meth_connect(lua_State *L) | 153 | static 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 | \*-------------------------------------------------------------------------*/ |
148 | static int tcp_meth_bind(lua_State *L) | 173 | static 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 | \*-------------------------------------------------------------------------*/ |
170 | static int tcp_meth_accept(lua_State *L) | 195 | static 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 | \*-------------------------------------------------------------------------*/ |
203 | int tcp_global_create(lua_State *L) | 228 | int 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)); |