当前位置: 首页 > article >正文

动态库实现lua网络请求GET, POST, 下载文件

DLL需要使用的网络封装

WinHttp异步实现GET, POST, 多线程下载文件_webclient post下载文件-CSDN博客文章浏览阅读726次。基于WinHttp封装, 实现异步多线程文件下载, GET请求, POST请求_webclient post下载文件https://blog.csdn.net/Flame_Cyclone/article/details/142644088

lua_network.h

#pragma once

#include "../common/include/lua.hpp"
#include "lua_utils.h"
#include "Win32Utils/fcjson.h"

class lua_network
{
public:
    static void register_functions(lua_State* L, const char* lib_name);

    static int get(lua_State* L);
    static int post(lua_State* L);
    static int download(lua_State* L);
};

lua_network.cpp

#include "lua_network.h"
#include "lua_utils.h"
#include "Win32Utils/CStrUtils.h"
#include "Win32Utils/CWinHttpClient.h"

static luaL_Reg lib_functions[] =
{
    {"get", lua_network::get},
    {"post", lua_network::post},
    {"download", lua_network::download},
    { NULL, NULL}
};

void lua_network::register_functions(lua_State* L, const char* lib_name)
{
    lua_newtable(L);

    luaL_Reg* fun_info = lib_functions;
    while (fun_info->func)
    {
        lua_pushcfunction(L, fun_info->func);
        lua_setfield(L, -2, fun_info->name);
        fun_info++;
    }
    lua_setfield(L, -2, lib_name);
}

int lua_network::get(lua_State* L)
{
    int nTop = lua_gettop(L);
    _tstring strUrl;
    std::string strParam;
    _tstring strHeader;
    bool fCallback = false;
    int ref = 0;

    if (lua_isfunction(L, -1))
    {
        fCallback = true;
        ref = luaL_ref(L, LUA_REGISTRYINDEX);
    }

    if (nTop >= 1 && lua_isstring(L, 1))
    {
        strUrl = CStrUtils::AStrToTStr(lua_tostring(L, 1));
    }

    if (nTop >= 2)
    {
        if (lua_isstring(L, 2))
        {
            strParam = lua_tostring(L, 2);
        }
        else if (lua_istable(L, 2))
        {
            lua_utils::dump_value(L, strParam, 2, dump_type::dump_get);
        }
    }

    if (nTop >= 3)
    {
        if (lua_isstring(L, 3))
        {
            strHeader = lua_tostring(L, 3);
        }
        else if (lua_istable(L, 3))
        {
            lua_utils::dump_value(L, strHeader, 3, dump_type::dump_header);
        }
    }

    CWinHttpClient obj;

    CWinHttpResult res = obj.Get(strUrl, strParam, strHeader, [&L, ref, fCallback](const WINHTTP_PROGRESS_INFO& progress) -> bool {

        if (fCallback)
        {
            lua_rawgeti(L, LUA_REGISTRYINDEX, ref);

            lua_newtable(L);
            lua_utils::set_key_value(L, "Progress", progress.lfProgress);
            lua_utils::set_key_value(L, "RemainTime", progress.lfRemainTime);
            lua_utils::set_key_value(L, "Speed", progress.lfSpeed);
            lua_utils::set_key_value(L, "Cur", (double)progress.ullCur);
            lua_utils::set_key_value(L, "Total", (double)progress.ullTotal);

            lua_pcall(L, 1, 0, 0);
        }

        return true;
        }
    );

    if (fCallback)
    {
        luaL_unref(L, LUA_REGISTRYINDEX, ref);
    }

    lua_newtable(L);

    lua_utils::set_key_value(L, "code", (lua_Integer)res.code);
    lua_utils::set_key_value(L, "result", CStrUtils::U8StrToAStr(res.result).c_str());

    return 1;
}

int lua_network::post(lua_State* L)
{
    int nTop = lua_gettop(L);
    _tstring strUrl;
    std::string strParam;
    _tstring strHeader;
    bool fCallback = false;
    int ref = 0;

    if (lua_isfunction(L, -1))
    {
        fCallback = true;
        ref = luaL_ref(L, LUA_REGISTRYINDEX);
    }

    if (nTop >= 1 && lua_isstring(L, 1))
    {
        strUrl = CStrUtils::AStrToTStr(lua_tostring(L, 1));
    }

    if (nTop >= 2)
    {
        if (lua_isstring(L, 2))
        {
            strParam = lua_tostring(L, 2);
        }
        else if (lua_istable(L, 2))
        {
            lua_utils::dump_value(L, strParam, 2, dump_type::dump_json);
        }
    }

    if (nTop >= 3)
    {
        if (lua_isstring(L, 3))
        {
            strHeader = lua_tostring(L, 3);
        }
        else if (lua_istable(L, 3))
        {
            lua_utils::dump_value(L, strHeader, 3, dump_type::dump_header);
        }
    }

    CWinHttpClient obj;

    CWinHttpResult res = obj.Post(strUrl, strParam, strHeader, [&L, ref, fCallback](const WINHTTP_PROGRESS_INFO& progress) -> bool {

        if (fCallback)
        {
            lua_rawgeti(L, LUA_REGISTRYINDEX, ref);

            lua_newtable(L);
            lua_utils::set_key_value(L, "Progress", progress.lfProgress);
            lua_utils::set_key_value(L, "RemainTime", progress.lfRemainTime);
            lua_utils::set_key_value(L, "Speed", progress.lfSpeed);
            lua_utils::set_key_value(L, "Cur", (double)progress.ullCur);
            lua_utils::set_key_value(L, "Total", (double)progress.ullTotal);

            lua_pcall(L, 1, 0, 0);
        }

        return true;
        }
    );

    if (fCallback)
    {
        luaL_unref(L, LUA_REGISTRYINDEX, ref);
    }

    lua_newtable(L);

    lua_utils::set_key_value(L, "code", (lua_Integer)res.code);
    lua_utils::set_key_value(L, "result", CStrUtils::U8StrToAStr(res.result).c_str());

    return 1;
}

int lua_network::download(lua_State* L)
{
    int nTop = lua_gettop(L);
    _tstring strUrl;
    _tstring strHeader;
    _tstring strPath;
    bool fCallback = false;
    int ref = 0;

    if (lua_isfunction(L, -1))
    {
        fCallback = true;
        ref = luaL_ref(L, LUA_REGISTRYINDEX);
    }

    if (nTop >= 1 && lua_isstring(L, 1))
    {
        strUrl = CStrUtils::AStrToTStr(lua_tostring(L, 1));
    }

    if (nTop >= 2 && lua_isstring(L, 2))
    {
        strPath = CStrUtils::AStrToTStr(lua_tostring(L, 2));
    }

    if (nTop >= 3)
    {
        if (lua_isstring(L, 3))
        {
            strHeader = lua_tostring(L, 3);
        }
        else if (lua_istable(L, 3))
        {
            lua_utils::dump_value(L, strHeader, 3, dump_type::dump_header);
        }
    }

    CWinHttpClient obj;

    CWinHttpResult res = obj.DownloadFile(strUrl, strPath, strHeader, [&L, ref, fCallback](const WINHTTP_PROGRESS_INFO& progress) -> bool {

        if (fCallback)
        {
            lua_rawgeti(L, LUA_REGISTRYINDEX, ref);

            lua_newtable(L);
            lua_utils::set_key_value(L, "Progress", progress.lfProgress);
            lua_utils::set_key_value(L, "RemainTime", progress.lfRemainTime);
            lua_utils::set_key_value(L, "Speed", progress.lfSpeed);
            lua_utils::set_key_value(L, "Cur", (double)progress.ullCur);
            lua_utils::set_key_value(L, "Total", (double)progress.ullTotal);

            lua_pcall(L, 1, 0, 0);
        }

        return true;
    }
    , std::thread::hardware_concurrency());

    if (fCallback)
    {
        luaL_unref(L, LUA_REGISTRYINDEX, ref);
    }

    lua_newtable(L);
    lua_utils::set_key_value(L, "code", (lua_Integer)res.code);
    lua_utils::set_key_value(L, "result", CStrUtils::U8StrToAStr(res.result).c_str());

    return 1;
}

lua_demo.lua

json = require("JSON")
fclib = require("FCLIB")
lib = nil
--判断当前运行位数
function is64bit()
    arc = os.getenv("PROCESSOR_ARCHITECTURE")
    if arc == "x86" then
        return false
    end
    
    return true
end

--加载动态库
if is64bit() then
    lib = require("lua_library_x64")
else
    lib = require("lua_library_x86")
end

lib.debug.print(lib)

-- 获取弹幕信息
function get_last_msg()

    --请求链接
    url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=4412054'
    
    -- 参数
    local param = {}

    --请求头
    local header = {
        ['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0'
    }

    res = lib.network.get(url, param, header)
    lib.debug.print(res)
    
    json_val = lib.json.decode(res.result)
    
    cur_msg = nil
    if json_val then
        for k, v in pairs(json_val['data']['room']) do
            if 0 ~= k then
                cur_msg = v
            end
        end
    end
    
    return cur_msg
end

lib.debug.print(get_last_msg())

function download(info)
    unit = 1024 * 1024
    fmt = '%.2f%% %.3gMB/%.3gMB %.3gMB/s'
    info = string.format(fmt, info['Progress'] * 100, info['Cur'] / unit, info['Total'] / unit, info['Speed'] / unit)
    print(info)
end

--下载链接
download_url = "https://drivers.amd.com/drivers/whql-amd-software-adrenalin-edition-24.8.1-win10-win11-aug-rdna.exe"

file_path = 'whql-amd-software-adrenalin-edition-24.8.1-win10-win11-aug-rdna.exe'

--请求头
header = {
    ['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0',
    ['Referer']= 'https://www.amd.com/'
}

--下载文件
fResult = lib.network.download(download_url, file_path, header, download)

效果

lua_demo_x64.exe lua_demo.lua

 仓库

https://gitee.com/flame_cyclone/lua_example


http://www.kler.cn/a/382983.html

相关文章:

  • 五十六:Stream的状态变迁
  • 19_HTML5 Web Workers --[HTML5 API 学习之旅]
  • 【C语言】库函数常见的陷阱与缺陷(二):字符串转化函数[3]--strtol
  • Node.js 工具:在 Windows 11 中配置 Node.js 的详细步骤
  • Unity3D Huatuo技术原理剖析详解
  • 事件驱动编程与异步编程:原理、对比及实践案例
  • 龙蜥副理事长张东:加速推进 AI+OS 深度融合,打造最 AI 的服务器操作系统
  • Java 中的 `wait()` 与 `sleep()`:深入解析两者的不同
  • SQLite数据库是什么?DB Browser for SQLite是什么?
  • Python 在PDF中绘制形状(线条、矩形、椭圆形等)
  • keep-alive - 2024最新版前端秋招面试短期突击面试题【100道】
  • A02、JVM性能监测调优
  • Docker学习—Docker核心概念总结
  • 机器学习—矩阵乘法
  • NeurIPS24 | 多无人机协作精确预测车辆等目标移动轨迹, Drones Help Drones
  • 【comfyui教程】ComfyUI即将迎来全新界面:升级体验就在11月15日
  • 如何利用低代码开源框架实现高效开发?
  • react18中redux-promise搭配redux-thunk完美简化异步数据操作
  • Nginx转发MySQL端口及添加stream模块
  • group by 聚合操作出错解决办法
  • 华为机试HJ25 数据分类处理
  • WPF MVVM入门系列教程(二、依赖属性)
  • 自注意力(Self-attention)与卷积神经网络(CNN)的相似性和区别
  • 如何在算家云搭建Aatrox-Bert-VITS2(音频生成)
  • 【python】OpenCV—findContours(4.6)
  • vue cli源码学习之cli-service