Add and use the sendData method which will automatically break long data (longer than 0x1fff bytes) into different stream requests.

This commit is contained in:
NI
2019-09-19 15:04:46 +08:00
parent 2d47cd004c
commit 5131cb8122
3 changed files with 40 additions and 2 deletions

View File

@@ -200,7 +200,7 @@ class SSH {
* *
*/ */
async sendData(data) { async sendData(data) {
return this.sender.send(CLIENT_DATA_STDIN, data); return this.sender.sendData(CLIENT_DATA_STDIN, data);
} }
/** /**

View File

@@ -153,7 +153,7 @@ class Telnet {
* *
*/ */
sendData(data) { sendData(data) {
return this.sender.send(0x00, data); return this.sender.sendData(0x00, data);
} }
/** /**

View File

@@ -19,6 +19,7 @@ import Exception from "./exception.js";
import * as header from "./header.js"; import * as header from "./header.js";
import * as reader from "./reader.js"; import * as reader from "./reader.js";
import * as sender from "./sender.js"; import * as sender from "./sender.js";
import * as common from "./common.js";
export class Sender { export class Sender {
/** /**
@@ -65,6 +66,43 @@ export class Sender {
return this.sender.send(d); return this.sender.send(d);
} }
/**
* Sends data to remote, if the data is too long, it will be separated into
* different stream requests
*
* @param {number} marker binary marker
* @param {Uint8Array} data data to be sent
*
* @throws {Exception} When the sender already been closed
*
*/
async sendData(marker, data) {
if (this.closed) {
throw new Exception(
"Sender already been closed. No data can be send",
false
);
}
let dataSeg = common.separateBuffer(data, header.STREAM_MAX_LENGTH),
reqHeader = new header.Header(header.STREAM);
reqHeader.set(this.id);
for (let i in dataSeg) {
let stHeader = new header.Stream(0, 0),
d = new Uint8Array(dataSeg[i].length + 3);
stHeader.set(marker, dataSeg[i].length);
d[0] = reqHeader.value();
d.set(stHeader.buffer(), 1);
d.set(dataSeg[i], 3);
await this.sender.send(d);
}
}
/** /**
* Send stream signals * Send stream signals
* *