Skip to content

Commit

Permalink
feat: hex or utf8 automatic conversion for server traces
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Apr 16, 2023
1 parent 45c9537 commit 46e9d15
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/common/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/libs/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
11 changes: 10 additions & 1 deletion src/main/libs/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]++;
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/ModalEditMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/ModalNewMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/stores/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down

0 comments on commit 46e9d15

Please sign in to comment.