node.js檔案傳輸框架
client.js:
var net = require('net');
var fs = require('fs');
var strm = require('stream');
var listenPort = 9999;
var serverIP = "127.0.0.1";
var clientPort = 8888;
var handler = [];
var args = process.argv.splice(2);
var arg_mode = args[0] ? args[0] : "put_bin_file";
var arg_filename = args[1] ? args[1] : "test.txt";
console.log('transfer file:' + args[0] + ' ' + args[1]);
var client = net.createConnection(listenPort, serverIP, handleClientConnection);
var handleClientConnection = function(){
console.log("connected to server");
};
var handleClientError = function(error){
console.log("server errors:: " + error);
};
var handleClientData = function(data){
console.log("data received : " + data.toString());
};
putFile = function(toPutFileName, writeAbleStream){
var rs = fs.createReadStream(toPutFileName);
console.log("put file name is " + toPutFileName + '\n');
rs.on('data',function(chunk) {
console.log(chunk.length);
writeAbleStream.write(chunk);
rs.pause();
});
rs.on('end', () => {
console.log("GoodBye\n");
rs.close();
client.destroy();
});
writeAbleStream.on('data', function(chunk) {
if(chunk == 'go') {
rs.resume();
}
});
};
var handleGO = function(data){
console.log("tmp received");
str = data.toString().split(' ');
if(str[0] == 'go')
putFile(str[1], this);
this.removeListener('data', handleGO);
};
handler["PUTFILE"] = function(fileName, socket){
console.log("client put file begin:" + fileName);
process.stdin.pause();
socket.write('putfile ' + fileName);
socket.on('data', handleGO);
};
var handleProcessData = function(data){
var str = data.toString().split(' ');
if(handler[str[0].toUpperCase()]){
handler[str[0].toUpperCase()](str[1], client);
}
};
client.on('error', handleClientError);
client.on('data', handleGO);
client.write("putfile " + arg_filename);
server.js
var net = require('net');
var fs = require('fs');
var strm = require('stream');
//var path = './test';
var handler = [];
getFile = function(toGetFileName, readAbleStream){
var ws = fs.createWriteStream(toGetFileName);
console.log("get file name is " + toGetFileName + '\n');
readAbleStream.on('data', function(chunk) {
console.log(chunk.length);
ws.write(chunk);
});
ws.on('drain', () => {
readAbleStream.write('go');
});
readAbleStream.on('end', () => {
console.log("GoodBye\n");
ws.close();
});
};
handler["PUTFILE"] = function(fileName, socket){
socket.write("go " + fileName);
getFile(fileName + 'tmp', socket);
};
var handleConnection = function(client){
process.stdout.write("received a new connecton\n");
client.on('data', handleData);
client.on('error', handleError);
client.on('close', handleClose);
};
var handleData = function(data){
var str = data.toString().split(' ');
// process.stdout.write(data);
if(handler[str[0].toUpperCase()]){
handler[str[0].toUpperCase()](str[1], this);
}
else{
}
};
var handleError = function(err){
process.stdout.write(err);
};
var handleClose = function(){
process.stdout.write("client close\n");
};
var server = net.createServer(handleConnection);
server.listen(9999);
框架2:
client.js
var net = require('net');
var fs = require('fs');
var strm = require('stream');
var listenPort = 9999;
var serverIP = "127.0.0.1";
var clientPort = 8888;
var handler = [];
var args = process.argv.splice(2);
console.log("arguments: %s", args[0]);
var arg_mode = args[0] ? args[0] : "put_txt_file";
var arg_filename = args[1] ? args[1] : "test.txt";
console.log('transfer file:' + args[0] + ' ' + args[1]);
var client = net.createConnection(listenPort, serverIP, handleClientConnection);
var handleClientConnection = function(){
console.log("connected to server");
};
var handleClientError = function(error){
console.log("server errors:: " + error);
};
var handleClientData = function(data){
console.log("data received : " + data.toString());
};
putFile = function(toPutFileName, writeAbleStream){
var statsSave;
var rs = fs.createReadStream(toPutFileName);
statsSave = fs.statSync(toPutFileName);
console.log("put file name is " + toPutFileName + '\n');
rs.on('readable', rsOnReadable);
function rsOnReadable() {
var len;
len = statsSave.size;
console.log("len: " + len);
writeAbleStream.write('' + len);
rs.removeListener("readable", rsOnReadable);
rs.pause();
}
rs.on('data',function(chunk) {
console.log(chunk.length);
writeAbleStream.write(chunk);
rs.pause();
});
rs.on('end', () => {
console.log("GoodBye\n");
rs.close();
client.destroy();
});
writeAbleStream.on('data', function(chunk) {
if(chunk == 'go') {
rs.resume();
}
});
};
getFile = function(readAbleStream){
var toGetFileName = arg_filename.split(':')[2];
var ws = fs.createWriteStream(toGetFileName);
var xferedLen = 0;
var bStart = 1;
var totalLen = 0;
var block_16384 = 0;
console.log('getFile: ' + toGetFileName);
readAbleStream.write('go');
readAbleStream.on('data', function (chunk) {
console.log('read: ' + xferedLen + ' ' + chunk.length);
if(bStart == 1) {
totalLen = + chunk.toString();
readAbleStream.write('go');
bStart = 0;
console.log('totalLen: ' + totalLen);
} else {
block_16384 += chunk.length;
if(block_16384 >= 16384) {
console.log('send go');
readAbleStream.write('go');
block_16384 -= 16384;
}
ws.write(chunk);
xferedLen += chunk.length;
if(xferedLen >= totalLen) {
this.destroy();
}
}
});
}
const op_put_bin_file = 0;
const op_put_txt_file = 1;
const op_write_reg = 2;
const op_read_reg = 3;
const op_get_data = 4;
var oprate = op_read_reg;
switch(arg_mode.toUpperCase()){
case "PUT_BIN_FILE":
oprate = op_put_bin_file;
break;
case "PUT_TXT_FILE":
oprate = op_put_txt_file;
break;
case "WRITE_REG":
oprate = op_write_reg;
break;
case "READ_REG":
opreate = op_read_reg;
break;
case "GET_DATA":
oprate = op_get_data;
break;
}
var handleGO = function(data){
console.log("tmp received, op:%d", oprate);
switch(oprate) {
case op_put_bin_file:
case op_put_txt_file:
str = data.toString().split(' ');
if(str[0] == 'go')
putFile(str[1], this);
break;
case op_write_reg:
console.log("op_write_reg: " + data);
client.destroy();
break;
case op_read_reg:
console.log("op_read_reg: " + data);
client.destroy();
break;
case op_get_data:
str = data.toString().split(' ');
if(str[0] == 'go') {
getFile(this);
}
break;
}
this.removeListener('data', handleGO);
};
client.on('error', handleClientError);
client.on('data', handleGO);
console.log("arg_mode: %d", oprate);
client.write(arg_mode + " " + arg_filename);
server.js
var net = require('net');
var fs = require('fs');
var strm = require('stream');
var readline = require('readline');
//0: write register, 1: only test socket xfer
var test_txt_mode = 1;
if(test_txt_mode == 0) {
addon = require('./build/Release/addon');
console.log('This should be eight:', addon.add(3, 5));
}
if(test_txt_mode == 0) {
var usbStat = addon.open().split();
if(usbStat[0].match('Success')) {
console.log('open CyUSB:', usbStat);
console.log('init CyUSB:', addon.initxfer());
} else {
console.log('open CyUSB:', usbStat);
process.exit();
}
}
var handler = [];
var one_file_start = 0;
getBinFile = function(toGetFileName, base, readAbleStream){
var ws = fs.createWriteStream(toGetFileName);
var AXImode = base >= 0x05d00000 || base < 0x0a800000 ? 1 : 0;
var fileLen = 0;
var fileBuffer = Buffer.from("");
var xferedLen = 0;
console.log("get file name is " + toGetFileName + '\n');
readAbleStream.on('data', function(chunk) {
if(one_file_start == 0) {
xferedLen = 0;
console.log(chunk.length);
one_file_start = 1;
fileLen = + chunk.toString();
console.log(fileLen);
fileBuffer = Buffer.alloc(fileLen);
readAbleStream.write('go');
} else {
// TODO: save data in buffer here:
fileBuffer.fill(chunk, xferedLen);
xferedLen += chunk.length;
console.log("len" + xferedLen);
//console.log(chunk.length);
ws.write(chunk);
}
});
ws.on('drain', () => {
readAbleStream.write('go');
});
readAbleStream.on('end', () => {
console.log("GoodBye\n");
ws.close();
if(AXImode == 0) {
var addr = base;
for(var i=0; i<xferedLen / 4; i++) {
var val = fileBuffer.readInt32BE();
if(test_txt_mode == 0) {
addon.writereg(addr, val);
} else {
console.log('write bin addr: ' + addr + ',' + val);
addr += 4;
}
}
} else {
console.log("donnot support AXI write currently.\n");
}
});
};
getTxtFile = function(toGetFileName, readAbleStream){
var fileLen = 0;
var fileBuffer = Buffer.from("");
var xferedLen = 0;
const rl = readline.createInterface({
input: readAbleStream,
});
var line_cnt = 0;
var ws = fs.createWriteStream(toGetFileName);
console.log("get file name is " + toGetFileName + '\n');
readAbleStream.on('data', function(chunk) {
if(one_file_start == 0) {
xferedLen = 0;
console.log(chunk.length);
console.log(chunk);
one_file_start = 1;
fileLen = + chunk.toString();
console.log(fileLen);
fileBuffer = Buffer.alloc(fileLen);
readAbleStream.write('go');
} else {
// TODO: save data in buffer here:
fileBuffer.fill(chunk, xferedLen);
xferedLen += chunk.length;
console.log("len" + xferedLen);
//console.log(chunk.length);
ws.write(chunk);
}
});
rl.on("line", function(line){
//console.log('line:' + line);
line_cnt ++;
//ws.write(line);
// TODO: when file xfer over, do registers write here
var hexStr = line.split(' ');
//console.log(hexStr.length);
if(hexStr.length > 1) {
if(test_txt_mode == 0) {
var addr = addon.hexstr2Hex(hexStr[1]);
var val = addon.hexstr2Hex(hexStr[2]);
addon.writereg(addr, val);
} else {
var addr = + strArg[1];
var val = + strArg[2];
console.log('write txt addr: ' + addr + ',' + val);
}
}
});
ws.on('drain', () => {
readAbleStream.write('go');
});
readAbleStream.on('end', () => {
console.log("line count: " + line_cnt);
ws.close();
});
};
putBinFile = function(strAddrLen, writeAbleStream){
var strArg = strAddrLen.split(':');
var base;
var len;
if(test_txt_mode == 0) {
base = addon.hexstr2Hex(strArg[0]);
len = addon.hexstr2Hex(strArg[1]);
} else {
base = + strArg[0];
len = + strArg[1];
}
var StreamBuf = Buffer.alloc(16384);
var lenBuf = Buffer.alloc(4);
var startFlag = 1;
var left = len;
var idx = 0;
console.log('xfer_data: ' + base + ' ' + len);
writeAbleStream.on('data', function(chunk){
console.log("idx: " + idx);
if(startFlag == 1) {
if(chunk == 'go') {
lenBuf.fill(len);
writeAbleStream.write('' + len);
startFlag = 0;
}
} else {
if(chunk == 'go') {
if(strArg.length > 1) {
if(left >= 16384) {
var addr = base + idx * 16384;
if(test_txt_mode == 0) {
addon.xferdata_o(addr, 16384, StreamBuf);
} else {
StreamBuf.fill(0);
console.log('get_data: ' + addr + ',' + left);
}
writeAbleStream.write(StreamBuf);
left -= 16384;
} else {
//the last one
var addr = base + (idx - 1) * 16384;
if(test_txt_mode == 0) {
addon.xferdata_o(addr, 16384, StreamBuf);
} else {
console.log('get_data: ' + addr + ',' + left);
}
writeAbleStream.write(StreamBuf, left);
}
idx ++;
}
}
}
});
writeAbleStream.on('end', ()=>{
console.log("xfer end: " + len);
});
}
handler["PUT_BIN_FILE"] = function(strBaseFileName, socket){
var hexStr = strAddrVal.split(':');
if(hexStr.length > 1) {
var base;
if(test_txt_mode == 0) {
base = addon.hexstr2Hex(hexStr[0]);
} else {
base = + hexStr[0];
}
var fileName = hexStr[1];
socket.write("go " + fileName);
getBinFile('test.bin', base, socket);
}
};
handler["PUT_TXT_FILE"] = function(fileName, socket){
socket.write("go " + fileName);
getTxtFile('test.txt', socket);
};
handler["WRITE_REG"] = function(strAddrVal, socket){
var hexStr = strAddrVal.split(':');
console.log(hexStr);
if(hexStr.length > 1) {
if(test_txt_mode == 0) {
var addr = addon.hexstr2Hex(hexStr[0]);
var val = addon.hexstr2Hex(hexStr[1]);
ret = addon.writereg(addr, val);
} else {
var addr = + hexStr[0];
var val = + hexStr[1];
ret = 'OK sim.';
console.log('write_reg: ' + addr + ',' + val);
}
console.log("write_reg: " + ret);
socket.write("write_reg: " + ret);
}
};
handler["READ_REG"] = function(strAddr, socket){
var addr;
var val;
if(test_txt_mode == 0) {
addr = addon.hexstr2Hex(strAddr);
val = addon.readreg(addr);
} else {
addr = + strAddr;
val = 12345678;
}
console.log("read_reg: " + val);
socket.write("read_reg: " + val);
}
handler["GET_DATA"] = function(strAddrLen, socket){
console.log("get_data: " + strAddrLen);
socket.write("go");
putBinFile(strAddrLen, socket);
}
var handleConnection = function(client){
process.stdout.write("received a new connecton\n");
client.on('data', handleData);
client.on('error', handleError);
client.on('close', handleClose);
};
var handleData = function(data){
var str = data.toString().split(' ');
if(handler[str[0].toUpperCase()]){
handler[str[0].toUpperCase()](str[1], this);
}
else{
}
};
var handleError = function(err){
process.stdout.write(err);
};
var handleClose = function(){
one_file_start = 0;
process.stdout.write("client close\n");
};
var server = net.createServer(handleConnection);
server.listen(9999);
Reference:
https://download.csdn.net/download/xljiulong/4509819
相關文章
- 什麼是極速檔案傳輸,極速檔案傳輸如何進行大檔案傳輸
- webservice傳輸檔案Web
- scp 傳輸檔案
- sftp 傳輸檔案FTP
- 大檔案如何傳輸,大檔案的傳輸方式有哪些?
- 【多檔案自平衡雲傳輸】使用展示 —— 檔案傳輸系統
- 檔案傳輸協議的五種安全檔案傳輸替代方案協議
- 鐳速傳輸:如何快速傳輸大檔案?
- Linux 檔案傳輸Linux
- wtfpd檔案傳輸使用
- 利用ssh傳輸檔案
- 有了大檔案傳輸加速功能,不再擔憂傳輸大檔案
- 如何將超大檔案傳輸給別人,超大檔案如何傳輸呢?
- 如何快速傳輸大檔案:4 種大檔案傳輸有效的方法
- 什麼是檔案傳輸,介紹檔案傳輸的發展程式
- 鐳速傳輸淺談TLS 和檔案傳輸TLS
- 鐳速傳輸:安全檔案傳輸的意義
- 揭秘|國內影視檔案傳輸的真相,跨境檔案傳輸更不簡單
- C# Socket 檔案傳送傳輸C#
- rz檔案傳輸失敗
- SyncBird Pro檔案傳輸工具
- ios檔案同步傳輸工具iOS
- Windows -> Linux 檔案傳輸WindowsLinux
- scp傳輸檔案的命令
- 求助!!!Java傳輸XML檔案JavaXML
- 企業經常需要進行傳輸檔案,大檔案傳輸有哪些方法?
- 常見的ftp檔案傳輸工具有哪些?推薦ftp檔案傳輸工具FTP
- 還在使用檔案傳輸協議傳輸機密檔案?你可能是對檔案傳輸協議有什麼誤會協議
- 跨國檔案傳輸軟體有哪些?跨國檔案傳輸遇到的問題
- 如何實現檔案高速傳輸,推薦鐳速高速檔案傳輸解決方案
- Java傳輸檔案使用Base64優化傳輸速率。Java優化
- Macftp檔案傳輸工具——SecureFX for MacMacFTP
- Raysync檔案傳輸協議(FTP)協議FTP
- 檔案傳輸協議介紹協議
- 將本地檔案傳輸到GitHubGithub
- Mac Linux檔案互相傳輸MacLinux
- 什麼是安全檔案傳輸?
- Java Socket圖片檔案傳輸Java