Skip to content

Commit

Permalink
Merge pull request #6 from alexkar598/master
Browse files Browse the repository at this point in the history
Adds timeout for long lived sockets(timeout is disabled when the socket is not in use ofc)
  • Loading branch information
tigercat2000 authored Mar 28, 2022
2 parents 06c07c9 + 481c540 commit cb37644
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "http2byond",
"description": "Communication layer between node.js and BYOND game servers.",
"version": "2.1.1",
"version": "2.2.0",
"scripts": {
"prepare": "npx tsc"
},
Expand Down
8 changes: 5 additions & 3 deletions src/http2byond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export function _sendTopic(socket: Socket, _topic: string): Promise<TopicReturnT
_topic

return new Promise((resolve, reject) => {
//Get rid of all listeners to prepare the socket to be reused
socket.removeAllListeners()

function errorHandler(reason: string) {
return function(originalError?: Error) {
originalError ?
Expand All @@ -28,8 +31,6 @@ export function _sendTopic(socket: Socket, _topic: string): Promise<TopicReturnT
let bodyBuffer: Buffer;

function processResponse() {
//Get rid of all listeners to prepare the socket to be reused
socket.removeAllListeners()
const type = bodyBuffer[0];
switch (type) {
case 0x00: {
Expand Down Expand Up @@ -109,7 +110,6 @@ export function _sendTopic(socket: Socket, _topic: string): Promise<TopicReturnT
ptr += topicBuffer.length
dataBuffer[ptr] = 0x00

console.log(dataBuffer)
socket.write(dataBuffer)
}

Expand Down Expand Up @@ -149,6 +149,7 @@ export function createTopicConnection(config: SocketConfig): TopicConnection {
const next = queue.shift()
if(!next) {
busy = false
socket.setTimeout(0)
return value
}
const [topic, resolve, reject] = next;
Expand All @@ -165,6 +166,7 @@ export function createTopicConnection(config: SocketConfig): TopicConnection {
//Immediate
if(!busy) {
busy = true
socket.setTimeout(config.timeout ?? 10000)
return _sendTopic(socket, topic).then(runNextTopic)
}

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ export interface SocketConfig {
host: string;
//Port to connect to
port: number;
//Time to wait before aborting connection
timeout?: number;
}

export interface SingleRunConfiguration extends SocketConfig {
//URL params to send to BYOND
topic: string;
//Time to wait before aborting connection
timeout?: number;
}

export type TopicReturnType = string | number | null
Expand Down
2 changes: 1 addition & 1 deletion typings/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export interface SocketConfig {
host: string;
port: number;
timeout?: number;
}
export interface SingleRunConfiguration extends SocketConfig {
topic: string;
timeout?: number;
}
export declare type TopicReturnType = string | number | null;
export interface TopicConnection {
Expand Down

0 comments on commit cb37644

Please sign in to comment.