mirror of
https://github.com/lunarmodules/luasocket.git
synced 2025-02-12 15:02:53 +01:00
Bug in usocket_recv...
This commit is contained in:
parent
bd54cd42d4
commit
167727be3c
@ -2,45 +2,49 @@
|
|||||||
# Distribution makefile
|
# Distribution makefile
|
||||||
#--------------------------------------------------------------------------
|
#--------------------------------------------------------------------------
|
||||||
|
|
||||||
DIST = luasocket-1.5-alpha
|
DIST = luasocket-2.0-alpha
|
||||||
|
|
||||||
LUA = \
|
LUA = \
|
||||||
concat.lua \
|
auxiliar.lua
|
||||||
code.lua \
|
code.lua
|
||||||
url.lua \
|
concat.lua
|
||||||
http.lua \
|
ftp.lua
|
||||||
smtp.lua \
|
|
||||||
ftp.lua \
|
|
||||||
select.lua \
|
|
||||||
luasocket.lua
|
|
||||||
|
|
||||||
TESTS = \
|
TESTS = \
|
||||||
testclnt.lua \
|
codetest.lua
|
||||||
testsrvr.lua \
|
concattest.lua
|
||||||
testcmd.lua \
|
ftptest.lua
|
||||||
codetest.lua \
|
|
||||||
urltest.lua \
|
|
||||||
concattest.lua \
|
|
||||||
ftptest.lua \
|
|
||||||
httptest.lua \
|
|
||||||
smtptest.lua \
|
|
||||||
mbox.lua \
|
|
||||||
udptest.lua
|
|
||||||
|
|
||||||
EXAMPLES = \
|
EXAMPLES = \
|
||||||
check-links.lua \
|
check-links.lua
|
||||||
daytimeclnt.lua \
|
daytimeclnt.lua
|
||||||
echoclnt.lua \
|
echoclnt.lua
|
||||||
echosrvr.lua \
|
echosrvr.lua
|
||||||
get.lua \
|
dict.lua
|
||||||
listener.lua \
|
|
||||||
talker.lua \
|
|
||||||
tinyirc.lua
|
|
||||||
|
|
||||||
ETC = \
|
ETC = \
|
||||||
cl-compat.lua \
|
cl-compat.lua
|
||||||
tftp.lua \
|
|
||||||
dict.lua
|
get.lua
|
||||||
|
http.lua
|
||||||
|
httptest.lua
|
||||||
|
listener.lua
|
||||||
|
lua.lua
|
||||||
|
luasocket.lua
|
||||||
|
mbox.lua
|
||||||
|
noglobals.lua
|
||||||
|
select.lua
|
||||||
|
smtp.lua
|
||||||
|
smtptest.lua
|
||||||
|
talker.lua
|
||||||
|
testclnt.lua
|
||||||
|
test.lua
|
||||||
|
testsrvr.lua
|
||||||
|
tftp.lua
|
||||||
|
tinyirc.lua
|
||||||
|
udptest.lua
|
||||||
|
url.lua
|
||||||
|
urltest.lua
|
||||||
|
|
||||||
MAIN = \
|
MAIN = \
|
||||||
auxiliar.c \
|
auxiliar.c \
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
host = host or "*"
|
host = host or "*"
|
||||||
port1 = port1 or 8080
|
port1 = port1 or 8080
|
||||||
port2 = port2 or 8081
|
port2 = port2 or 8181
|
||||||
if arg then
|
if arg then
|
||||||
host = arg[1] or host
|
host = arg[1] or host
|
||||||
port1 = arg[2] or port1
|
port1 = arg[2] or port1
|
||||||
@ -15,11 +15,16 @@ end
|
|||||||
|
|
||||||
server1, error = socket.bind(host, port1)
|
server1, error = socket.bind(host, port1)
|
||||||
assert(server1, error)
|
assert(server1, error)
|
||||||
server1:timeout(1)
|
server1:timeout(1) -- make sure we don't block in accept
|
||||||
server2, error = socket.bind(host, port2)
|
server2, error = socket.bind(host, port2)
|
||||||
assert(server2, error)
|
assert(server2, error)
|
||||||
server2:timeout(1)
|
server2:timeout(1) -- make sure we don't block in accept
|
||||||
|
|
||||||
|
io.write("Servers bound\n")
|
||||||
|
|
||||||
|
-- simple set implementation
|
||||||
|
-- the select function doesn't care about what is passed to it as long as
|
||||||
|
-- it behaves like a table
|
||||||
function newset()
|
function newset()
|
||||||
local reverse = {}
|
local reverse = {}
|
||||||
local set = {}
|
local set = {}
|
||||||
@ -32,46 +37,43 @@ function newset()
|
|||||||
table.remove(set, reverse[value])
|
table.remove(set, reverse[value])
|
||||||
reverse[value] = nil
|
reverse[value] = nil
|
||||||
end,
|
end,
|
||||||
id = function(set, value)
|
|
||||||
return reverse[value]
|
|
||||||
end
|
|
||||||
}})
|
}})
|
||||||
return set
|
return set
|
||||||
end
|
end
|
||||||
|
|
||||||
sockets = newset()
|
set = newset()
|
||||||
|
|
||||||
sockets:insert(server1)
|
io.write("Inserting servers in set\n")
|
||||||
sockets:insert(server2)
|
set:insert(server1)
|
||||||
|
set:insert(server2)
|
||||||
|
|
||||||
while 1 do
|
while 1 do
|
||||||
local readable, _, error = socket.select(sockets, nil)
|
local readable, _, error = socket.select(set, nil)
|
||||||
for _, input in readable do
|
for _, input in readable do
|
||||||
-- is it a server socket?
|
-- is it a server socket?
|
||||||
local id = sockets:id(input)
|
|
||||||
if input == server1 or input == server2 then
|
if input == server1 or input == server2 then
|
||||||
|
io.write("Waiting for clients\n")
|
||||||
local new = input:accept()
|
local new = input:accept()
|
||||||
if new then
|
if new then
|
||||||
new:timeout(1)
|
new:timeout(1)
|
||||||
sockets:insert(new)
|
io.write("Inserting client in set\n")
|
||||||
io.write("Server ", id, " got client ", sockets:id(new), "\n")
|
set:insert(new)
|
||||||
end
|
end
|
||||||
-- it is a client socket
|
-- it is a client socket
|
||||||
else
|
else
|
||||||
local line, error = input:receive()
|
local line, error = input:receive()
|
||||||
if error then
|
if error then
|
||||||
input:close()
|
input:close()
|
||||||
io.write("Removing client ", id, "\n")
|
io.write("Removing client from set\n")
|
||||||
sockets:remove(input)
|
set:remove(input)
|
||||||
else
|
else
|
||||||
io.write("Broadcasting line '", id, "> ", line, "'.\n")
|
io.write("Broadcasting line '", line, "'\n")
|
||||||
__, writable, error = socket.select(nil, sockets, 1)
|
__, writable, error = socket.select(nil, set, 1)
|
||||||
if not error then
|
if not error then
|
||||||
for ___, output in writable do
|
for ___, output in writable do
|
||||||
io.write("Sending to client ", sockets:id(output), "\n")
|
output:send(line .. "\n")
|
||||||
output:send(id, "> ", line, "\r\n")
|
|
||||||
end
|
end
|
||||||
else io.write("No one ready to listen!!!\n") end
|
else io.write("No client ready to receive!!!\n") end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -25,6 +25,11 @@ void aux_open(lua_State *L)
|
|||||||
lua_pushnumber(L, 1);
|
lua_pushnumber(L, 1);
|
||||||
lua_rawset(L, -3);
|
lua_rawset(L, -3);
|
||||||
#endif
|
#endif
|
||||||
|
/* make version string available so scripts */
|
||||||
|
lua_pushstring(L, "version");
|
||||||
|
lua_pushstring(L, LUASOCKET_VERSION);
|
||||||
|
lua_rawset(L, -3);
|
||||||
|
/* store namespace as global */
|
||||||
lua_settable(L, LUA_GLOBALSINDEX);
|
lua_settable(L, LUA_GLOBALSINDEX);
|
||||||
/* make sure modules know what is our namespace */
|
/* make sure modules know what is our namespace */
|
||||||
lua_pushstring(L, "LUASOCKET_LIBNAME");
|
lua_pushstring(L, "LUASOCKET_LIBNAME");
|
||||||
|
@ -177,7 +177,6 @@ const char *inet_tryconnect(p_sock ps, const char *address,
|
|||||||
if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) {
|
if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) {
|
||||||
struct hostent *hp = gethostbyname(address);
|
struct hostent *hp = gethostbyname(address);
|
||||||
struct in_addr **addr;
|
struct in_addr **addr;
|
||||||
remote.sin_family = AF_INET;
|
|
||||||
if (!hp) return sock_hoststrerror();
|
if (!hp) return sock_hoststrerror();
|
||||||
addr = (struct in_addr **) hp->h_addr_list;
|
addr = (struct in_addr **) hp->h_addr_list;
|
||||||
memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
|
memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user