diff options
Diffstat (limited to 'src/udp.c')
-rw-r--r-- | src/udp.c | 54 |
1 files changed, 27 insertions, 27 deletions
@@ -41,7 +41,6 @@ static int meth_settimeout(lua_State *L); | |||
41 | static int meth_getfd(lua_State *L); | 41 | static int meth_getfd(lua_State *L); |
42 | static int meth_setfd(lua_State *L); | 42 | static int meth_setfd(lua_State *L); |
43 | static int meth_dirty(lua_State *L); | 43 | static int meth_dirty(lua_State *L); |
44 | static void pusherror(lua_State *L, int code); | ||
45 | 44 | ||
46 | /* udp object methods */ | 45 | /* udp object methods */ |
47 | static luaL_reg udp[] = { | 46 | static luaL_reg udp[] = { |
@@ -100,18 +99,14 @@ int udp_open(lua_State *L) | |||
100 | return 0; | 99 | return 0; |
101 | } | 100 | } |
102 | 101 | ||
103 | |||
104 | /*=========================================================================*\ | 102 | /*=========================================================================*\ |
105 | * Lua methods | 103 | * Lua methods |
106 | \*=========================================================================*/ | 104 | \*=========================================================================*/ |
107 | /*-------------------------------------------------------------------------*\ | 105 | const char *udp_strerror(int err) { |
108 | * Pushes the error message | 106 | /* a 'closed' error on an unconnected means the target address was not |
109 | \*-------------------------------------------------------------------------*/ | 107 | * accepted by the transport layer */ |
110 | void pusherror(lua_State *L, int code) { | 108 | if (err == IO_CLOSED) return "refused"; |
111 | const char *err = code != IO_USER? io_strerror(code): "refused"; | 109 | else return sock_strerror(err); |
112 | err = err? err: sock_strerror(); | ||
113 | if (err) lua_pushstring(L, err); | ||
114 | else lua_pushnil(L); | ||
115 | } | 110 | } |
116 | 111 | ||
117 | /*-------------------------------------------------------------------------*\ | 112 | /*-------------------------------------------------------------------------*\ |
@@ -125,12 +120,13 @@ static int meth_send(lua_State *L) { | |||
125 | const char *data = luaL_checklstring(L, 2, &count); | 120 | const char *data = luaL_checklstring(L, 2, &count); |
126 | tm_markstart(tm); | 121 | tm_markstart(tm); |
127 | err = sock_send(&udp->sock, data, count, &sent, tm); | 122 | err = sock_send(&udp->sock, data, count, &sent, tm); |
128 | if (err == IO_DONE) lua_pushnumber(L, sent); | 123 | if (err != IO_DONE) { |
129 | else lua_pushnil(L); | 124 | lua_pushnil(L); |
130 | /* a 'closed' error on an unconnected means the target address was not | 125 | lua_pushstring(L, udp_strerror(err)); |
131 | * accepted by the transport layer */ | 126 | return 2; |
132 | pusherror(L, err == IO_CLOSED ? IO_USER : err); | 127 | } |
133 | return 2; | 128 | lua_pushnumber(L, sent); |
129 | return 1; | ||
134 | } | 130 | } |
135 | 131 | ||
136 | /*-------------------------------------------------------------------------*\ | 132 | /*-------------------------------------------------------------------------*\ |
@@ -153,12 +149,13 @@ static int meth_sendto(lua_State *L) { | |||
153 | tm_markstart(tm); | 149 | tm_markstart(tm); |
154 | err = sock_sendto(&udp->sock, data, count, &sent, | 150 | err = sock_sendto(&udp->sock, data, count, &sent, |
155 | (SA *) &addr, sizeof(addr), tm); | 151 | (SA *) &addr, sizeof(addr), tm); |
156 | if (err == IO_DONE) lua_pushnumber(L, sent); | 152 | if (err != IO_DONE) { |
157 | else lua_pushnil(L); | 153 | lua_pushnil(L); |
158 | /* a 'closed' error on an unconnected means the target address was not | 154 | lua_pushstring(L, udp_strerror(err)); |
159 | * accepted by the transport layer */ | 155 | return 2; |
160 | pusherror(L, err == IO_CLOSED ? IO_USER : err); | 156 | } |
161 | return 2; | 157 | lua_pushnumber(L, sent); |
158 | return 1; | ||
162 | } | 159 | } |
163 | 160 | ||
164 | /*-------------------------------------------------------------------------*\ | 161 | /*-------------------------------------------------------------------------*\ |
@@ -173,10 +170,13 @@ static int meth_receive(lua_State *L) { | |||
173 | count = MIN(count, sizeof(buffer)); | 170 | count = MIN(count, sizeof(buffer)); |
174 | tm_markstart(tm); | 171 | tm_markstart(tm); |
175 | err = sock_recv(&udp->sock, buffer, count, &got, tm); | 172 | err = sock_recv(&udp->sock, buffer, count, &got, tm); |
176 | if (err == IO_DONE) lua_pushlstring(L, buffer, got); | 173 | if (err != IO_DONE) { |
177 | else lua_pushnil(L); | 174 | lua_pushnil(L); |
178 | pusherror(L, err); | 175 | lua_pushstring(L, udp_strerror(err)); |
179 | return 2; | 176 | return 2; |
177 | } | ||
178 | lua_pushlstring(L, buffer, got); | ||
179 | return 1; | ||
180 | } | 180 | } |
181 | 181 | ||
182 | /*-------------------------------------------------------------------------*\ | 182 | /*-------------------------------------------------------------------------*\ |
@@ -201,7 +201,7 @@ static int meth_receivefrom(lua_State *L) { | |||
201 | return 3; | 201 | return 3; |
202 | } else { | 202 | } else { |
203 | lua_pushnil(L); | 203 | lua_pushnil(L); |
204 | pusherror(L, err); | 204 | lua_pushstring(L, udp_strerror(err)); |
205 | return 2; | 205 | return 2; |
206 | } | 206 | } |
207 | } | 207 | } |