diff options
Diffstat (limited to 'samples/tinyirc.lua')
| -rw-r--r-- | samples/tinyirc.lua | 90 |
1 files changed, 48 insertions, 42 deletions
diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua index d3e56e7..0b20303 100644 --- a/samples/tinyirc.lua +++ b/samples/tinyirc.lua | |||
| @@ -1,16 +1,9 @@ | |||
| 1 | function set_add(set, sock) | 1 | ----------------------------------------------------------------------------- |
| 2 | table.insert(set, sock) | 2 | -- Select sample: simple text line server |
| 3 | end | 3 | -- LuaSocket 1.5 sample files. |
| 4 | 4 | -- Author: Diego Nehab | |
| 5 | function set_remove(set, sock) | 5 | -- RCS ID: $Id$ |
| 6 | for i = 1, table.getn(set) do | 6 | ----------------------------------------------------------------------------- |
| 7 | if set[i] == sock then | ||
| 8 | table.remove(set, i) | ||
| 9 | break | ||
| 10 | end | ||
| 11 | end | ||
| 12 | end | ||
| 13 | |||
| 14 | host = host or "*" | 7 | host = host or "*" |
| 15 | port1 = port1 or 8080 | 8 | port1 = port1 or 8080 |
| 16 | port2 = port2 or 8081 | 9 | port2 = port2 or 8081 |
| @@ -21,49 +14,62 @@ if arg then | |||
| 21 | end | 14 | end |
| 22 | 15 | ||
| 23 | server1, error = socket.bind(host, port1) | 16 | server1, error = socket.bind(host, port1) |
| 24 | if not server1 then print(error) exit() end | 17 | assert(server1, error) |
| 25 | server1:timeout(1) | 18 | server1:timeout(1) |
| 26 | server2, error = socket.bind(host, port2) | 19 | server2, error = socket.bind(host, port2) |
| 27 | if not server2 then print(error) exit() end | 20 | assert(server2, error) |
| 28 | server2:timeout(1) | 21 | server2:timeout(1) |
| 29 | 22 | ||
| 30 | sock_set = {server1, server2} | 23 | function newset() |
| 24 | local reverse = {} | ||
| 25 | local set = {} | ||
| 26 | setmetatable(set, { __index = { | ||
| 27 | insert = function(set, value) | ||
| 28 | table.insert(set, value) | ||
| 29 | reverse[value] = table.getn(set) | ||
| 30 | end, | ||
| 31 | remove = function(set, value) | ||
| 32 | table.remove(set, reverse[value]) | ||
| 33 | reverse[value] = nil | ||
| 34 | end, | ||
| 35 | id = function(set, value) | ||
| 36 | return reverse[value] | ||
| 37 | end | ||
| 38 | }}) | ||
| 39 | return set | ||
| 40 | end | ||
| 41 | |||
| 42 | sockets = newset() | ||
| 31 | 43 | ||
| 32 | sock_id = {} | 44 | sockets:insert(server1) |
| 33 | sock_id[server1] = 1 | 45 | sockets:insert(server2) |
| 34 | sock_id[server2] = 2 | ||
| 35 | next_id = 3 | ||
| 36 | 46 | ||
| 37 | while 1 do | 47 | while 1 do |
| 38 | local readable, _, error = socket.select(sock_set, nil) | 48 | local readable, _, error = socket.select(sockets, nil) |
| 39 | for _, sock in readable do | 49 | for _, input in readable do |
| 40 | -- is it a server socket | 50 | -- is it a server socket? |
| 41 | if sock_id[sock] < 3 then | 51 | local id = sockets:id(input) |
| 42 | local incomming = sock:accept() | 52 | if input == server1 or input == server2 then |
| 43 | if incomming then | 53 | local new = input:accept() |
| 44 | incomming:timeout(1) | 54 | if new then |
| 45 | sock_id[incomming] = next_id | 55 | new:timeout(1) |
| 46 | set_add(sock_set, incomming) | 56 | sockets:insert(new) |
| 47 | io.write("Added client id ", next_id, ". ", | 57 | io.write("Server ", id, " got client ", sockets:id(new), "\n") |
| 48 | table.getn(sock_set)-2, " total.\n") | ||
| 49 | next_id = next_id + 1 | ||
| 50 | end | 58 | end |
| 51 | -- it is a client socket | 59 | -- it is a client socket |
| 52 | else | 60 | else |
| 53 | local line, error = sock:receive() | 61 | local line, error = input:receive() |
| 54 | local id = sock_id[sock] | ||
| 55 | if error then | 62 | if error then |
| 56 | sock:close() | 63 | input:close() |
| 57 | set_remove(sock_set, sock) | 64 | io.write("Removing client ", id, "\n") |
| 58 | io.write("Removed client number ", id, ". ", | 65 | sockets:remove(input) |
| 59 | getn(sock_set)-2, " total.\n") | ||
| 60 | else | 66 | else |
| 61 | io.write("Broadcasting line '", id, "> ", line, "'.\n") | 67 | io.write("Broadcasting line '", id, "> ", line, "'.\n") |
| 62 | __, writable, error = socket.select(nil, sock_set, 1) | 68 | __, writable, error = socket.select(nil, sockets, 1) |
| 63 | if not error then | 69 | if not error then |
| 64 | for ___, outgoing in writable do | 70 | for ___, output in writable do |
| 65 | io.write("Sending to client ", sock_id[outgoing], "\n") | 71 | io.write("Sending to client ", sockets:id(output), "\n") |
| 66 | outgoing:send(id, "> ", line, "\r\n") | 72 | output:send(id, "> ", line, "\r\n") |
| 67 | end | 73 | end |
| 68 | else io.write("No one ready to listen!!!\n") end | 74 | else io.write("No one ready to listen!!!\n") end |
| 69 | end | 75 | end |
