aboutsummaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-04-21 05:38:07 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-04-21 05:38:07 +0000
commit9596c7f95d3ab990bb741107f7cf94ec5dff3e3b (patch)
treea4f014b1a2ab769b7b0d8801e761a6b18a2cd97f /samples
parent434e8e014cb21b8d15d512b966ea4f397fd0d369 (diff)
downloadluasocket-9596c7f95d3ab990bb741107f7cf94ec5dff3e3b.tar.gz
luasocket-9596c7f95d3ab990bb741107f7cf94ec5dff3e3b.tar.bz2
luasocket-9596c7f95d3ab990bb741107f7cf94ec5dff3e3b.zip
Bug in forward.lua. Wasn't breaking from the loop.
Diffstat (limited to 'samples')
-rw-r--r--samples/forward.lua23
1 files changed, 14 insertions, 9 deletions
diff --git a/samples/forward.lua b/samples/forward.lua
index e51c5ce..a53ab5d 100644
--- a/samples/forward.lua
+++ b/samples/forward.lua
@@ -2,7 +2,7 @@
2local socket = require"socket" 2local socket = require"socket"
3 3
4-- creates a new set data structure 4-- creates a new set data structure
5function newset() 5function newset(a)
6 local reverse = {} 6 local reverse = {}
7 local set = {} 7 local set = {}
8 return setmetatable(set, {__index = { 8 return setmetatable(set, {__index = {
@@ -29,7 +29,7 @@ end
29-- timeout before an inactive thread is kicked 29-- timeout before an inactive thread is kicked
30local TIMEOUT = 10 30local TIMEOUT = 10
31-- set of connections waiting to receive data 31-- set of connections waiting to receive data
32local receiving = newset() 32local receiving = newset(1)
33-- set of sockets waiting to send data 33-- set of sockets waiting to send data
34local sending = newset() 34local sending = newset()
35-- context for connections and servers 35-- context for connections and servers
@@ -77,8 +77,8 @@ function connect(who, host, port)
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 79 if not ret and err ~= "already connected" then
80 kick(who)
81 kick(context[who].peer) 80 kick(context[who].peer)
81 kick(who)
82 return 82 return
83 end 83 end
84 end 84 end
@@ -87,11 +87,11 @@ end
87 87
88-- gets rid of a client 88-- gets rid of a client
89function kick(who) 89function kick(who)
90 if who and context[who] then 90 if who then
91 sending:remove(who) 91 sending:remove(who)
92 receiving:remove(who) 92 receiving:remove(who)
93 context[who] = nil
94 who:close() 93 who:close()
94 context[who] = nil
95 end 95 end
96end 96end
97 97
@@ -159,6 +159,7 @@ function forward(who)
159 if not rec_err then 159 if not rec_err then
160 kick(who) 160 kick(who)
161 kick(peer) 161 kick(peer)
162 break
162 end 163 end
163 end 164 end
164end 165end
@@ -171,13 +172,17 @@ function go()
171 readable, writable = socket.select(receiving, sending) 172 readable, writable = socket.select(receiving, sending)
172 -- for all readable connections, resume its thread 173 -- for all readable connections, resume its thread
173 for _, who in ipairs(readable) do 174 for _, who in ipairs(readable) do
174 receiving:remove(who) 175 if context[who] then
175 coroutine.resume(context[who].thread, who) 176 receiving:remove(who)
177 coroutine.resume(context[who].thread, who)
178 end
176 end 179 end
177 -- for all writable connections, do the same 180 -- for all writable connections, do the same
178 for _, who in ipairs(writable) do 181 for _, who in ipairs(writable) do
179 sending:remove(who) 182 if context[who] then
180 coroutine.resume(context[who].thread, who) 183 sending:remove(who)
184 coroutine.resume(context[who].thread, who)
185 end
181 end 186 end
182 -- put all inactive threads in death row 187 -- put all inactive threads in death row
183 local now = socket.gettime() 188 local now = socket.gettime()