Bläddra i källkod

add: httpplus,添加限速功能

Wendal Chen 7 månader sedan
förälder
incheckning
8f60bd435d
1 ändrade filer med 76 tillägg och 12 borttagningar
  1. 76 12
      script/libs/httpplus.lua

+ 76 - 12
script/libs/httpplus.lua

@@ -386,6 +386,31 @@ local function resp_parse(opts)
     -- 完结散花
 end
 
+local function write_and_wait(opts, data, ctx)
+    time_now = mcu.ticks()
+    socket.tx(opts.netc, data)
+    speed_limit = ctx.speed_limit
+    if speed_limit and speed_limit > 0 then
+        -- log.info("httpplus", "限速1", ctx.sent_len, ctx.data_len, mcu.hz(), speed_limit)
+        ctx.sent_len = ctx.sent_len + ctx.data_len
+        -- log.info("httpplus", "限速2", ctx.sent_len, mcu.ticks() - ctx.time_start, mcu.hz())
+        local ticks_used = mcu.ticks() - ctx.time_start
+        -- log.info("httpplus", "限速4", ticks_used, mcu.hz(), type(mcu.hz()))
+        -- log.info("httpplus", "限速5", ticks_used, mcu.hz(), ticks_used // mcu.hz())
+        local time_used = (mcu.ticks() - ctx.time_start + (mcu.hz() - 1)) * ( 1000 // mcu.hz())
+        local time_used_expect = ((ctx.sent_len) // speed_limit * 1000)
+        -- log.info("httpplus", "限速3", ctx.sent_len, time_used, time_used_expect)
+        if time_used < time_used_expect and time_used_expect - time_used > 10 then
+            time_wait = time_used_expect - time_used
+            if time_wait > 1000 then
+                time_wait = 1000
+            end
+            opts.log(TAG, "限速等待", time_wait, "ms")
+            sys.waitUntil(opts.topic, time_wait)
+        end
+    end
+end
+
 -- socket 回调函数
 local function http_socket_cb(opts, event)
     opts.log(TAG, "tcp.event", event)
@@ -400,7 +425,7 @@ local function http_socket_cb(opts, event)
     elseif event == socket.EVENT then
         -- 收到数据或者链接断开了, 这里总需要读取一次才知道
         local succ, data_len = socket.rx(opts.netc, opts.rx_buff)
-        if succ and data_len > 0 then
+        if succ and data_len and data_len > 0 then
             opts.log(TAG, "收到数据", data_len, "总长", #opts.rx_buff)
             -- opts.log(TAG, "数据", opts.rx_buff:query())
         else
@@ -427,6 +452,7 @@ local function http_exec(opts)
         log.error(TAG, "创建socket失败了!!")
         return -102
     end
+    local speed_ctx = {}
     opts.netc = netc
     opts.rx_buff = zbuff.create(1024)
     opts.topic = tostring(netc)
@@ -458,7 +484,32 @@ local function http_exec(opts)
     -- 然后是body
     local rbody = ""
     local write_counter = 0
-    local fbuf = zbuff.create(1024 * 24, 0, zbuff.HEAP_PSRAM) -- 根据psram状态来选
+    local fbuf = nil
+    local psram = rtos.meminfo("psram")
+    local speed_limit = opts.speed_limit
+    if psram and psram > 1024*1024 then
+        fbuf = zbuff.create(1024 * 64, 0, zbuff.HEAP_PSRAM)
+    else
+        fbuf = zbuff.create(1024 * 8)
+    end
+    if fbuf == nil then
+        fbuf = zbuff.create(1024 * 4)
+    end
+    opts.log(TAG, "上传缓冲区大小", fbuf:len())
+    if speed_limit == nil then
+        if hmeta and (hmeta.model() == "Air8101" or hmeta.model() == "Air8101A") then
+            if (opts.adapter or socket.dft() or 0) == socket.LWIP_STA then
+                -- 限制上传速度
+                speed_limit = 48 * 1024
+            end
+        end
+    end
+    if speed_limit and speed_limit > 0 then
+        opts.log(TAG, "启用上传限速", speed_limit)
+        speed_ctx.speed_limit = speed_limit
+        speed_ctx.time_start = mcu.ticks()
+    end
+
     if opts.mp and #opts.mp > 0 then
         opts.log(TAG, "执行mulitpart上传模式")
         for k, v in pairs(opts.mp) do
@@ -469,6 +520,8 @@ local function http_exec(opts)
                 local fd = io.open(v[1], "rb")
                 -- log.info("写入文件数据", v[1])
                 if fd then
+                    speed_ctx.time_start = mcu.ticks()
+                    speed_ctx.sent_len = 0
                     while not opts.is_closed do
                         fbuf:seek(0)
                         local ok, flen = fd:fill(fbuf)
@@ -477,10 +530,12 @@ local function http_exec(opts)
                         end
                         fbuf:seek(flen)
                         -- log.info("写入文件数据", "长度", #fdata)
-                        socket.tx(netc, fbuf)
+                        speed_ctx.data_len = fbuf:len()
+                        write_and_wait(opts, fbuf, speed_ctx)
+                        -- socket.tx(netc, fbuf)
                         write_counter = write_counter + flen
                         -- 注意, 这里要等待TX_OK事件
-                        sys.waitUntil(opts.topic, 300)
+                        -- sys.waitUntil(opts.topic, 300)
                     end
                     fd:close()
                 end
@@ -500,6 +555,8 @@ local function http_exec(opts)
         local fd = io.open(opts.bodyfile, "rb")
         -- log.info("写入文件数据", v[1])
         if fd then
+            speed_ctx.time_start = mcu.ticks()
+            speed_ctx.sent_len = write_counter
             while not opts.is_closed do
                 fbuf:seek(0)
                 local ok, flen = fd:fill(fbuf)
@@ -508,10 +565,12 @@ local function http_exec(opts)
                 end
                 fbuf:seek(flen)
                 -- log.info("写入文件数据", "长度", #fdata)
-                socket.tx(netc, fbuf)
+                speed_ctx.data_len = flen
+                write_and_wait(opts, fbuf, speed_ctx)
+                -- socket.tx(netc, fbuf)
                 write_counter = write_counter + flen
                 -- 注意, 这里要等待TX_OK事件
-                sys.waitUntil(opts.topic, 300)
+                -- sys.waitUntil(opts.topic, 300)
             end
             fd:close()
         end
@@ -527,16 +586,21 @@ local function http_exec(opts)
                 local offset = 0
                 local tmpbuff = opts.body
                 local tsize = tmpbuff:used()
+                speed_ctx.time_start = mcu.ticks()
+                speed_ctx.sent_len = write_counter
                 while offset < tsize do
                     opts.log(TAG, "body(zbuff)分段写入", offset, tsize)
-                    if tsize - offset > 4096 then
-                        socket.tx(netc, tmpbuff:toStr(offset, 4096))
-                        offset = offset + 4096
-                        sys.waitUntil(opts.topic, 300)
+                    fbuf:seek(0)
+                    if tsize - offset > fbuf:len() then
+                        fbuf:copy(0, tmpbuff, offset, fbuf:len())
+                        offset = offset + fbuf:len()
                     else
-                        socket.tx(netc, tmpbuff:toStr(offset, tsize - offset))
-                        break
+                        fbuf:copy(0, tmpbuff, offset, tsize - offset)
+                        offset = tsize
                     end
+                    write_and_wait(opts, fbuf, speed_ctx)
+                    -- socket.tx(netc, fbuf)
+                    -- sys.waitUntil(opts.topic, 300)
                 end
             end
         end