local function _pack(len)
local tmp = len
local bitt = {}
for i=1,4 do
local quotient = math.floor(tmp/256)
local remainder = tmp - quotient*256
tmp = quotient
bitt[i] = remainder
end
return string.char(bitt[1],bitt[2],bitt[3],bitt[4],1,0,0,0,0,0,0,0,0,0,0,0)
end
local function _unpack(line)
local r1=string.byte(line,1)
local r2=string.byte(line,2)
local r3=string.byte(line,3)
local r4=string.byte(line,4)
local rev_len=r4*256*256*256 + r3*256*256 + r2*256 + r1
return rev_len
end
local function log_debug(begin_time_d, output_str_list_d, title, err_d, args_d)
-- debug log time
local cost_time_1 = math.floor((NewTimeKey() - begin_time_d) * 1000)
table.insert(output_str_list_d, tostring(cost_time_1))
table.insert(output_str_list_d, tostring(args_d.ip))
table.insert(output_str_list_d, tostring(args_d.port))
table.insert(output_str_list_d, "title_" .. tostring(title))
table.insert(output_str_list_d, tostring(err_d))
local output_str = table.concat(output_str_list_d, "\t")
write("vertical_video_group", output_str)
end
ngx.header.content_type = "text/plain";
local args = getargs()
local exptime = args.exptime or 100
-- debug log time
local output_str_list = {}
local begin_time = NewTimeKey()
local current_time = os.date("%Y-%m-%d %H:%M:%S", os.time())
table.insert(output_str_list, current_time)
table.insert(output_str_list, "woo_compress")
-- connect
local sock = ngx.socket.tcp()
sock:settimeout(exptime) -- conn timeout 100ms
local ok,err = sock:connect(args.ip, args.port)
if checkerr(ok, "failed to connect"..args.ip, 401, err)== -1 then
log_debug(begin_time,output_str_list, "connect",err, args)
return
end
local req_str = args.req
req_str = compress(req_str)
local l = string.len(req_str)
local s = _pack(l)
sock:settimeout(exptime) -- send timeout 100ms
local bytes, err = sock:send(s .. req_str)
if checkerr(bytes, "failed to send", 401, err)== -1 then
log_debug(begin_time,output_str_list, "send",err, args)
return
end
-- debug log time
local cost_time_rec = math.floor((NewTimeKey() - begin_time) * 1000)
table.insert(output_str_list, tostring(cost_time_rec))
sock:settimeout(exptime) -- recv timeout 100ms
local line, err = sock:receive(4)
if checkerr(line, "failed to receive1"..args.ip, 401, err)== -1 then
log_debug(begin_time, output_str_list, 1, err, args)
return
end
local rev_len = _unpack(line)
local line, err = sock:receive(12)
if checkerr(line, "failed to receive2"..args.ip, 401, err)== -1 then
log_debug(begin_time,output_str_list, 2,err, args)
return
end
local line, err = sock:receive(rev_len)
if checkerr(line, "failed to receive3"..args.ip, 401, err)== -1 then
log_debug(begin_time,output_str_list, 3, err, args)
return
end
ngx.say(line)
-- debug log time
local cost_time = math.floor((NewTimeKey() - begin_time) * 1000)
table.insert(output_str_list, tostring(cost_time))
local ok, err = sock:close()
if not ok then
log_debug(begin_time,output_str_list, 4, err, args)
return
end
--local ok, err = sock:setkeepalive(10000, 10) --(max_idle_time-1s,max_connection_num)
--if checkerr(ok, "failed to put the connection into pool"..args.ip, 200, err)== -1 then return end
-- debug log time
log_debug(begin_time,output_str_list, "_succ", "ok", args)