diff options
Diffstat (limited to 'samples')
| -rw-r--r-- | samples/forward.lua | 23 |
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 @@ | |||
| 2 | local socket = require"socket" | 2 | local socket = require"socket" |
| 3 | 3 | ||
| 4 | -- creates a new set data structure | 4 | -- creates a new set data structure |
| 5 | function newset() | 5 | function 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 |
| 30 | local TIMEOUT = 10 | 30 | local TIMEOUT = 10 |
| 31 | -- set of connections waiting to receive data | 31 | -- set of connections waiting to receive data |
| 32 | local receiving = newset() | 32 | local receiving = newset(1) |
| 33 | -- set of sockets waiting to send data | 33 | -- set of sockets waiting to send data |
| 34 | local sending = newset() | 34 | local 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 |
| 89 | function kick(who) | 89 | function 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 |
| 96 | end | 96 | end |
| 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 |
| 164 | end | 165 | end |
| @@ -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() |
