溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Node.Js怎么實(shí)現(xiàn)sendfile數(shù)據(jù)傳輸

發(fā)布時(shí)間:2022-12-02 09:54:39 來(lái)源:億速云 閱讀:106 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“Node.Js怎么實(shí)現(xiàn)sendfile數(shù)據(jù)傳輸”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Node.Js怎么實(shí)現(xiàn)sendfile數(shù)據(jù)傳輸”吧!

首先我們看一下sendfile的好處是什么。

  • sendfile() copies data between one file descriptor and another. Because this  copying is done within the kernel, sendfile() is more efficient than the  combination of read(2) and write(2), which would require transferring data to  and from user space.

我們看到sendfile通過(guò)把內(nèi)核完成數(shù)據(jù)的傳輸,減少了內(nèi)核和用戶層的數(shù)據(jù)復(fù)制,從而提高了效率。下面我們通過(guò)napi寫(xiě)一個(gè)addon來(lái)實(shí)現(xiàn)這個(gè)功能。

#include <sys/sendfile.h>  #include <stdio.h>  #include <unistd.h> #include <fcntl.h> #include <node_api.h> static napi_value copyFile(napi_env env, napi_callback_info info) {   size_t argc = 3;   napi_value args[3];   // 拿到j(luò)s層的入?yún)?,這里是三個(gè)   napi_get_cb_info(env, info, &argc, args, NULL, NULL);   int fd1;   int fd2;   int len;   // js傳入的是一個(gè)數(shù)字,v8轉(zhuǎn)成了對(duì)象,這里再次把入?yún)⑥D(zhuǎn)成int型   napi_get_value_int32(env, args[0], &fd1);   napi_get_value_int32(env, args[1], &fd2);   napi_get_value_int32(env, args[2], &len);   int writed = sendfile(fd2, fd1, 0,len);   napi_value ret;   napi_create_int32(env, writed, &ret);   return ret; }  napi_value Init(napi_env env, napi_value exports) {   napi_value func;   // 創(chuàng)建一個(gè)函數(shù)并且設(shè)置為exports對(duì)象的getArray屬性的值   napi_create_function(env,                       NULL,                       NAPI_AUTO_LENGTH,                       copyFile,                       NULL,                       &func);   napi_set_named_property(env, exports, "copyFile", func);   return exports; } NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

下面我們看看怎么使用。首先用這個(gè)addon來(lái)復(fù)制文件,類似Node.js的copyyFile

const fs= require('fs'); const { copyFile } = require('./build/Release/sendfile.node'); const {   O_WRONLY,   O_CREAT, } = fs.constants; async function test() {   const [fd1, fd2] = await Promise.all([openFile('1.txt', 'r'), openFile('2.txt', O_WRONLY | O_CREAT)]);   const { size } = await getFileInfo(fd1);   console.log(copyFile(fd1, fd2, size));   fs.close(fd1, () => {});   fs.close(fd2, () => {}); } function openFile(filename, mode) {   return new Promise((resolve, reject) => {     fs.open(filename, mode, (err, fd) => {       if (err) {         reject(err);       } else {         resolve(fd);       }     });   })}  function getFileInfo(fd) {   return new Promise((resolve, reject) => {     fs.fstat(fd, (err, stat) => {       if (err) {         reject(err)       }else {         resolve(stat);       }     });   }) } test();

執(zhí)行上面代碼,我們可以看到文件會(huì)成功復(fù)制2.txt。接著我們?cè)賮?lái)試一下網(wǎng)絡(luò)IO的場(chǎng)景。

const fs= require('fs'); const http = require('http'); const { copyFile } = require('./build/Release/sendfile.node'); const server = http.createServer(async (req, res) => {   const fd = await openFile('1.txt', 'r');   const { size } = await getFileInfo(fd);   const ret = copyFile(fd, res.socket._handle.fd, size);   res.socket.end(); }).listen(8002);  const {   O_WRONLY,   O_CREAT, } = fs.constants;  function openFile(filename, mode) {   return new Promise((resolve, reject) => {     fs.open(filename, mode, (err, fd) => {       if (err) {         reject(err);       } else {         resolve(fd);       }     });   })}  function getFileInfo(fd) {   return new Promise((resolve, reject) => {     fs.fstat(fd, (err, stat) => {       if (err) {         reject(err)       }else {         resolve(stat);       }     });   })}

以上代碼首先啟動(dòng)一個(gè)http服務(wù)器,然后收到請(qǐng)求的時(shí)候,通過(guò)addon調(diào)用sendfile給前端返回對(duì)應(yīng)的內(nèi)容,最后關(guān)閉連接。結(jié)果如下。

Node.Js怎么實(shí)現(xiàn)sendfile數(shù)據(jù)傳輸

到此,相信大家對(duì)“Node.Js怎么實(shí)現(xiàn)sendfile數(shù)據(jù)傳輸”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI