feat: hex or utf8 automatic conversion for server traces

This commit is contained in:
Fabio Di Stasio 2023-04-16 10:47:09 +02:00
parent 45c9537cc9
commit 46e9d157d6
6 changed files with 18 additions and 9 deletions

View File

@ -9,7 +9,7 @@ export interface ClientHost {
export interface ClientMessage {
enabled: boolean;
format: 'hex' | 'ascii' | 'binary';
format: 'hex' | 'utf-8' | 'binary';
message: string;
name: string;
}

View File

@ -142,8 +142,8 @@ class Sender {
let msg;
switch (this.messages[index].format) {
case 'ascii':
msg = Buffer.from(this.messages[index].message, 'ascii');
case 'utf-8':
msg = Buffer.from(this.messages[index].message, 'utf-8');
break;
case 'hex':
msg = Buffer.from(this.messages[index].message.replace(/\s|0x/g, ''), 'hex');

View File

@ -75,7 +75,16 @@ class Server {
this.sendLog(null, '', 'clientConnectedOnPort', { port });
socket.on('data', (msg: Buffer) => {
const msgString = msg.toString();
let msgString: string;
try {
new TextDecoder('utf8', { fatal: true }).decode(msg);
msgString = msg.toString('utf-8');
}
catch (err) {
msgString = msg.toString('hex');
}
if (this.echo) socket.write(msg);
this.nBytes[i] += msg.length;
this.nMsgs[i]++;

View File

@ -27,8 +27,8 @@
<option value="" disabled>
{{ t('word.select') }}
</option>
<option value="ascii">
ASCII
<option value="utf-8">
UTF-8
</option>
<option value="hex">
HEX

View File

@ -27,8 +27,8 @@
<option value="" disabled>
{{ t('word.select') }}
</option>
<option value="ascii">
ASCII
<option value="utf-8">
UTF-8
</option>
<option value="hex">
HEX

View File

@ -13,7 +13,7 @@ export const useClientStore = defineStore('client', {
}]) as ClientHost[],
messages: persistentStore.get('messages', [{
enabled: true,
format: 'ascii',
format: 'utf-8',
message: 'Hello, World!',
name: 'Hello, World!'
}]) as ClientMessage[]