Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
38
Task/Echo-server/Lua/echo-server-1.lua
Normal file
38
Task/Echo-server/Lua/echo-server-1.lua
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
local socket = require("socket")
|
||||
|
||||
local function has_value(tab, value)
|
||||
for i, v in ipairs(tab) do
|
||||
if v == value then return i end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function checkOn(client)
|
||||
local line, err = client:receive()
|
||||
if line then
|
||||
client:send(line .. "\n")
|
||||
end
|
||||
if err and err ~= "timeout" then
|
||||
print(tostring(client) .. " " .. err)
|
||||
client:close()
|
||||
return true -- end this connection
|
||||
end
|
||||
return false -- do not end this connection
|
||||
end
|
||||
|
||||
local server = assert(socket.bind("*",12321))
|
||||
server:settimeout(0) -- make non-blocking
|
||||
local connections = { } -- a list of the client connections
|
||||
while true do
|
||||
local newClient = server:accept()
|
||||
if newClient then
|
||||
newClient:settimeout(0) -- make non-blocking
|
||||
table.insert(connections, newClient)
|
||||
end
|
||||
local readList = socket.select({server, table.unpack(connections)})
|
||||
for _, conn in ipairs(readList) do
|
||||
if conn ~= server and checkOn(conn) then
|
||||
table.remove(connections, has_value(connections, conn))
|
||||
end
|
||||
end
|
||||
end
|
||||
32
Task/Echo-server/Lua/echo-server-2.lua
Normal file
32
Task/Echo-server/Lua/echo-server-2.lua
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
local socket=require("socket")
|
||||
|
||||
function checkOn (client)
|
||||
local line, err = client:receive()
|
||||
if line then
|
||||
print(tostring(client) .. " said " .. line)
|
||||
client:send(line .. "\n")
|
||||
end
|
||||
if err and err ~= "timeout" then
|
||||
print(tostring(client) .. " " .. err)
|
||||
client:close()
|
||||
return true -- end this connection
|
||||
end
|
||||
return false -- do not end this connection
|
||||
end
|
||||
|
||||
local delay = 0.004 -- anything less than this uses up my CPU
|
||||
local connections = {} -- an array of connections
|
||||
local newClient
|
||||
local server = assert(socket.bind("*", 12321))
|
||||
server:settimeout(delay)
|
||||
while true do
|
||||
repeat
|
||||
newClient = server:accept()
|
||||
for idx, client in ipairs(connections) do
|
||||
if checkOn(client) then table.remove(connections, idx) end
|
||||
end
|
||||
until newClient
|
||||
newClient:settimeout(delay)
|
||||
print(tostring(newClient) .. " connected")
|
||||
table.insert(connections, newClient)
|
||||
end
|
||||
31
Task/Echo-server/Lua/echo-server-3.lua
Normal file
31
Task/Echo-server/Lua/echo-server-3.lua
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
local http = require("http")
|
||||
|
||||
http.createServer(function(req, res)
|
||||
print(("Connection from %s"):format(req.socket:address().ip))
|
||||
|
||||
local chunks = {}
|
||||
local function dumpChunks()
|
||||
for i=1,#chunks do
|
||||
res:write(table.remove(chunks, 1))
|
||||
end
|
||||
end
|
||||
|
||||
req:on("data", function(data)
|
||||
for line, nl in data:gmatch("([^\n]+)(\n?)") do
|
||||
if nl == "\n" then
|
||||
dumpChunks()
|
||||
res:write(line)
|
||||
res:write("\n")
|
||||
else
|
||||
table.insert(chunks, line)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
req:on("end", function()
|
||||
dumpChunks()
|
||||
res:finish()
|
||||
end)
|
||||
end):listen(12321, "127.0.0.1")
|
||||
|
||||
print("Server running at http://127.0.0.1:12321/")
|
||||
Loading…
Add table
Add a link
Reference in a new issue