From 58bdb658aaa1c30a8f3bed46eef880d308fae582 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Mon, 9 Jun 2003 18:23:40 +0000 Subject: Select re-implemented in a nicer way. Few changes in internal class and group registration. Lua modules are compiled and built into library. Dynamic library tested in Linux and Mac OS X. --- samples/daytimeclnt.lua | 6 ++++ samples/echoclnt.lua | 6 ++++ samples/echosrvr.lua | 6 ++++ samples/listener.lua | 4 ++- samples/talker.lua | 6 ++++ samples/tinyirc.lua | 90 ++++++++++++++++++++++++++----------------------- 6 files changed, 75 insertions(+), 43 deletions(-) (limited to 'samples') diff --git a/samples/daytimeclnt.lua b/samples/daytimeclnt.lua index 4debc81..85ddca1 100644 --- a/samples/daytimeclnt.lua +++ b/samples/daytimeclnt.lua @@ -1,3 +1,9 @@ +----------------------------------------------------------------------------- +-- UDP sample: daytime protocol client +-- LuaSocket 1.5 sample files. +-- Author: Diego Nehab +-- RCS ID: $Id$ +----------------------------------------------------------------------------- host = host or "127.0.0.1" port = port or 13 if arg then diff --git a/samples/echoclnt.lua b/samples/echoclnt.lua index cd8b450..bca0b4d 100644 --- a/samples/echoclnt.lua +++ b/samples/echoclnt.lua @@ -1,3 +1,9 @@ +----------------------------------------------------------------------------- +-- UDP sample: echo protocol client +-- LuaSocket 1.5 sample files +-- Author: Diego Nehab +-- RCS ID: $Id$ +----------------------------------------------------------------------------- host = host or "localhost" port = port or 7 if arg then diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua index 6117557..18bd84e 100644 --- a/samples/echosrvr.lua +++ b/samples/echosrvr.lua @@ -1,3 +1,9 @@ +----------------------------------------------------------------------------- +-- UDP sample: echo protocol server +-- LuaSocket 1.5 sample files +-- Author: Diego Nehab +-- RCS ID: $Id$ +----------------------------------------------------------------------------- host = host or "127.0.0.1" port = port or 7 if arg then diff --git a/samples/listener.lua b/samples/listener.lua index c035ab2..4846419 100644 --- a/samples/listener.lua +++ b/samples/listener.lua @@ -1,6 +1,8 @@ ----------------------------------------------------------------------------- --- Little program to dump lines received at a given port +-- TCP sample: Little program to dump lines received at a given port -- LuaSocket 1.5 sample files +-- Author: Diego Nehab +-- RCS ID: $Id$ ----------------------------------------------------------------------------- host = host or "*" port = port or 8080 diff --git a/samples/talker.lua b/samples/talker.lua index 688824f..c7a239a 100644 --- a/samples/talker.lua +++ b/samples/talker.lua @@ -1,3 +1,9 @@ +----------------------------------------------------------------------------- +-- TCP sample: Little program to send text lines to a given host/port +-- LuaSocket 1.5 sample files +-- Author: Diego Nehab +-- RCS ID: $Id$ +----------------------------------------------------------------------------- host = host or "localhost" port = port or 8080 if arg then 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 @@ -function set_add(set, sock) - table.insert(set, sock) -end - -function set_remove(set, sock) - for i = 1, table.getn(set) do - if set[i] == sock then - table.remove(set, i) - break - end - end -end - +----------------------------------------------------------------------------- +-- Select sample: simple text line server +-- LuaSocket 1.5 sample files. +-- Author: Diego Nehab +-- RCS ID: $Id$ +----------------------------------------------------------------------------- host = host or "*" port1 = port1 or 8080 port2 = port2 or 8081 @@ -21,49 +14,62 @@ if arg then end server1, error = socket.bind(host, port1) -if not server1 then print(error) exit() end +assert(server1, error) server1:timeout(1) server2, error = socket.bind(host, port2) -if not server2 then print(error) exit() end +assert(server2, error) server2:timeout(1) -sock_set = {server1, server2} +function newset() + local reverse = {} + local set = {} + setmetatable(set, { __index = { + insert = function(set, value) + table.insert(set, value) + reverse[value] = table.getn(set) + end, + remove = function(set, value) + table.remove(set, reverse[value]) + reverse[value] = nil + end, + id = function(set, value) + return reverse[value] + end + }}) + return set +end + +sockets = newset() -sock_id = {} -sock_id[server1] = 1 -sock_id[server2] = 2 -next_id = 3 +sockets:insert(server1) +sockets:insert(server2) while 1 do - local readable, _, error = socket.select(sock_set, nil) - for _, sock in readable do - -- is it a server socket - if sock_id[sock] < 3 then - local incomming = sock:accept() - if incomming then - incomming:timeout(1) - sock_id[incomming] = next_id - set_add(sock_set, incomming) - io.write("Added client id ", next_id, ". ", - table.getn(sock_set)-2, " total.\n") - next_id = next_id + 1 + local readable, _, error = socket.select(sockets, nil) + for _, input in readable do + -- is it a server socket? + local id = sockets:id(input) + if input == server1 or input == server2 then + local new = input:accept() + if new then + new:timeout(1) + sockets:insert(new) + io.write("Server ", id, " got client ", sockets:id(new), "\n") end -- it is a client socket else - local line, error = sock:receive() - local id = sock_id[sock] + local line, error = input:receive() if error then - sock:close() - set_remove(sock_set, sock) - io.write("Removed client number ", id, ". ", - getn(sock_set)-2, " total.\n") + input:close() + io.write("Removing client ", id, "\n") + sockets:remove(input) else io.write("Broadcasting line '", id, "> ", line, "'.\n") - __, writable, error = socket.select(nil, sock_set, 1) + __, writable, error = socket.select(nil, sockets, 1) if not error then - for ___, outgoing in writable do - io.write("Sending to client ", sock_id[outgoing], "\n") - outgoing:send(id, "> ", line, "\r\n") + for ___, output in writable do + io.write("Sending to client ", sockets:id(output), "\n") + output:send(id, "> ", line, "\r\n") end else io.write("No one ready to listen!!!\n") end end -- cgit v1.2.3-55-g6feb