Skip to content

Commit

Permalink
release 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
hansSchall committed Sep 27, 2024
1 parent 9107923 commit c73dc89
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 51 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# @Deno-PLC / Adapter-TCP
# [Deno-PLC](https://github.com/deno-plc) / [Adapter-TCP](https://jsr.io/@deno-plc/adapter-tcp)

Base adapter for devices that can be controlled via a TCP socket#

## Installation

`$ deno add @deno-plc/adapter-tcp`
[Use JSR: ![JSR](https://jsr.io/badges/@deno-plc/adapter-tcp)](https://jsr.io/@deno-plc/adapter-tcp)

## Usage

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@deno-plc/adapter-tcp",
"version": "1.0.3",
"version": "1.0.4",
"exports": "./mod.ts",
"fmt": {
"indentWidth": 4
Expand Down
92 changes: 44 additions & 48 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,48 @@ export class TCPAdapter {

#current_conn: Deno.TcpConn | null = null;

#handle_err(err: unknown) {
batch(() => {
this.status.value = TCPAdapterConnectionStatus.DISCONNECTED;
if (err instanceof Error) {
if (this.verbose) {
console.error(
`%c[TCPAdapter] [${this.host}:${this.port}] error ${err.name}`,
"color: #f00",
);
}
if (err.name === "ConnectionRefused") {
this.details.value =
TCPAdapterConnectionDetails.ECONNREFUSED;
} else if (err.name === "ConnectionReset") {
this.details.value = TCPAdapterConnectionDetails.ECONNRESET;
} else if (err.name === "Interrupted") {
this.details.value =
TCPAdapterConnectionDetails.INTERRUPTED;
} else if (err.name === "TimedOut") {
this.details.value = TCPAdapterConnectionDetails.ETIMEDOUT;
} else if (err.name === "ConnectionAborted") {
this.details.value = TCPAdapterConnectionDetails.NO_ERROR;
} else {
console.error(
`%c[TCPAdapter] [${this.host}:${this.port}] unknown error ${err.name}`,
"color: red",
err,
);
this.details.value =
TCPAdapterConnectionDetails.UNKNOWN_ERROR;
}
} else {
console.error(
`%c[TCPAdapter] [${this.host}:${this.port}] unknown error`,
"color: red",
err,
);
this.details.value = TCPAdapterConnectionDetails.UNKNOWN_ERROR;
}
});
}

async #loop() {
const connStart = performance.now();
let session: TCPAdapterSession | null = null;
Expand Down Expand Up @@ -181,11 +223,7 @@ export class TCPAdapter {
// socket write callback
if (conn === this.#current_conn) {
conn.write(data).catch((err) => {
console.error(
`%c[TCPAdapter] [${this.host}:${this.port}] write failed`,
"color: #f00",
err,
);
this.#handle_err(err);
});
} else {
console.error(
Expand All @@ -199,49 +237,7 @@ export class TCPAdapter {
session.recv(data);
}
} catch (err) {
batch(() => {
this.status.value = TCPAdapterConnectionStatus.DISCONNECTED;
if (err instanceof Error) {
if (this.verbose) {
console.error(
`%c[TCPAdapter] [${this.host}:${this.port}] error ${err.name}`,
"color: #f00",
);
}
if (err.name === "ConnectionRefused") {
this.details.value =
TCPAdapterConnectionDetails.ECONNREFUSED;
} else if (err.name === "ConnectionReset") {
this.details.value =
TCPAdapterConnectionDetails.ECONNRESET;
} else if (err.name === "Interrupted") {
this.details.value =
TCPAdapterConnectionDetails.INTERRUPTED;
} else if (err.name === "TimedOut") {
this.details.value =
TCPAdapterConnectionDetails.ETIMEDOUT;
} else if (err.name === "ConnectionAborted") {
this.details.value =
TCPAdapterConnectionDetails.NO_ERROR;
} else {
console.error(
`%c[TCPAdapter] [${this.host}:${this.port}] unknown error`,
"color: red",
err,
);
this.details.value =
TCPAdapterConnectionDetails.UNKNOWN_ERROR;
}
} else {
console.error(
`%c[TCPAdapter] [${this.host}:${this.port}] unknown error`,
"color: red",
err,
);
this.details.value =
TCPAdapterConnectionDetails.UNKNOWN_ERROR;
}
});
this.#handle_err(err);
}
this.status.value = TCPAdapterConnectionStatus.DISCONNECTED;
this.#current_conn = null;
Expand Down

0 comments on commit c73dc89

Please sign in to comment.