diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-04-21 03:15:34 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-04-21 03:15:34 +0000 |
commit | 434e8e014cb21b8d15d512b966ea4f397fd0d369 (patch) | |
tree | f8384d8064364331bf0549b2f518742d8f65ea9d | |
parent | 4e3cf63c95fc3c01a1c759e237678e9d3b2b8494 (diff) | |
download | luasocket-434e8e014cb21b8d15d512b966ea4f397fd0d369.tar.gz luasocket-434e8e014cb21b8d15d512b966ea4f397fd0d369.tar.bz2 luasocket-434e8e014cb21b8d15d512b966ea4f397fd0d369.zip |
Better connection handling.
-rw-r--r-- | TODO | 3 | ||||
-rw-r--r-- | samples/forward.lua | 12 | ||||
-rw-r--r-- | samples/tinyirc.lua | 25 | ||||
-rw-r--r-- | src/tcp.c | 2 |
4 files changed, 26 insertions, 16 deletions
@@ -1,5 +1,4 @@ | |||
1 | 1 | ||
2 | BUG NO SET DO TINYIRC!!! SINISTRO. | ||
3 | talk about the non-blocking connect in the manual | 2 | talk about the non-blocking connect in the manual |
4 | test it on Windows!!! | 3 | test it on Windows!!! |
5 | 4 | ||
@@ -41,3 +40,5 @@ testar os options! | |||
41 | - inet_ntoa também é uma merda. | 40 | - inet_ntoa também é uma merda. |
42 | 41 | ||
43 | eliminate globals from namespaces created by module(). | 42 | eliminate globals from namespaces created by module(). |
43 | |||
44 | * BUG NO SET DO TINYIRC!!! SINISTRO. | ||
diff --git a/samples/forward.lua b/samples/forward.lua index ff65b84..e51c5ce 100644 --- a/samples/forward.lua +++ b/samples/forward.lua | |||
@@ -76,13 +76,13 @@ function connect(who, host, port) | |||
76 | if not ret and err == "timeout" then | 76 | if not ret and err == "timeout" then |
77 | wait(who, "output") | 77 | wait(who, "output") |
78 | ret, err = who:connect(host, port) | 78 | ret, err = who:connect(host, port) |
79 | if not ret and err ~= "already connected" then | ||
80 | kick(who) | ||
81 | kick(context[who].peer) | ||
82 | return | ||
83 | end | ||
79 | end | 84 | end |
80 | if not ret then | 85 | return forward(who) |
81 | kick(who) | ||
82 | kick(context[who].peer) | ||
83 | else | ||
84 | return forward(who) | ||
85 | end | ||
86 | end | 86 | end |
87 | 87 | ||
88 | -- gets rid of a client | 88 | -- gets rid of a client |
diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua index 684e7c0..e3dd517 100644 --- a/samples/tinyirc.lua +++ b/samples/tinyirc.lua | |||
@@ -24,20 +24,29 @@ io.write("Servers bound\n") | |||
24 | -- simple set implementation | 24 | -- simple set implementation |
25 | -- the select function doesn't care about what is passed to it as long as | 25 | -- the select function doesn't care about what is passed to it as long as |
26 | -- it behaves like a table | 26 | -- it behaves like a table |
27 | -- creates a new set data structure | ||
27 | function newset() | 28 | function newset() |
28 | local reverse = {} | 29 | local reverse = {} |
29 | local set = {} | 30 | local set = {} |
30 | setmetatable(set, { __index = { | 31 | return setmetatable(set, {__index = { |
31 | insert = function(set, value) | 32 | insert = function(set, value) |
32 | table.insert(set, value) | 33 | if not reverse[value] then |
33 | reverse[value] = table.getn(set) | 34 | table.insert(set, value) |
35 | reverse[value] = table.getn(set) | ||
36 | end | ||
34 | end, | 37 | end, |
35 | remove = function(set, value) | 38 | remove = function(set, value) |
36 | table.remove(set, reverse[value]) | 39 | local index = reverse[value] |
37 | reverse[value] = nil | 40 | if index then |
38 | end, | 41 | reverse[value] = nil |
42 | local top = table.remove(set) | ||
43 | if top ~= value then | ||
44 | reverse[top] = index | ||
45 | set[index] = top | ||
46 | end | ||
47 | end | ||
48 | end | ||
39 | }}) | 49 | }}) |
40 | return set | ||
41 | end | 50 | end |
42 | 51 | ||
43 | set = newset() | 52 | set = newset() |
@@ -208,7 +208,7 @@ static int meth_bind(lua_State *L) | |||
208 | \*-------------------------------------------------------------------------*/ | 208 | \*-------------------------------------------------------------------------*/ |
209 | static int meth_connect(lua_State *L) | 209 | static int meth_connect(lua_State *L) |
210 | { | 210 | { |
211 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{master}", 1); | 211 | p_tcp tcp = (p_tcp) aux_checkgroup(L, "tcp{any}", 1); |
212 | const char *address = luaL_checkstring(L, 2); | 212 | const char *address = luaL_checkstring(L, 2); |
213 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); | 213 | unsigned short port = (unsigned short) luaL_checknumber(L, 3); |
214 | p_tm tm = tm_markstart(&tcp->tm); | 214 | p_tm tm = tm_markstart(&tcp->tm); |