-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from project-slippi/feat/relay
feat: add back console relay
- Loading branch information
Showing
10 changed files
with
270 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Ports } from "@slippi/slippi-js"; | ||
import log from "electron-log"; | ||
import net from "net"; | ||
|
||
interface ConsoleDataBuffer { | ||
buffersToConcat: Buffer[]; | ||
fullBuffer: Buffer; | ||
} | ||
|
||
export class ConsoleRelay { | ||
private server: net.Server | null; | ||
private dataBuffer: ConsoleDataBuffer; | ||
private clients: net.Socket[]; | ||
|
||
public constructor(id: number) { | ||
this.clients = []; | ||
this.dataBuffer = { | ||
buffersToConcat: [], | ||
fullBuffer: Buffer.from([]), | ||
}; | ||
this.server = net.createServer((socket) => { | ||
socket.setNoDelay().setTimeout(20000); | ||
|
||
// Only get the full buffer when the client connects for performance | ||
const buf = this.getFullBuffer(); | ||
socket.write(buf); | ||
|
||
this.clients.push(socket); | ||
socket.on("close", (err) => { | ||
if (err) { | ||
console.warn(err); | ||
} | ||
this.clients = this.clients.filter((client) => socket !== client); | ||
}); | ||
}); | ||
this.server.listen(Ports.RELAY_START + id, "0.0.0.0"); | ||
} | ||
|
||
public stopRelay() { | ||
this.clients.forEach((client) => client.destroy()); | ||
this.clients = []; | ||
if (this.server !== null) { | ||
this.server.close(); | ||
} | ||
this.server = null; | ||
} | ||
|
||
public write(newData: Buffer) { | ||
this.dataBuffer.buffersToConcat.push(newData); | ||
if (this.clients) { | ||
this.clients.forEach((client) => { | ||
try { | ||
client.write(newData); | ||
} catch (err) { | ||
log.warn(`Could not write to a client...${err}`); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public async clearBuffer() { | ||
this.dataBuffer.buffersToConcat = []; | ||
this.dataBuffer.fullBuffer = Buffer.from([]); | ||
} | ||
|
||
private getFullBuffer() { | ||
if (this.dataBuffer.buffersToConcat.length > 0) { | ||
this.dataBuffer.fullBuffer = Buffer.concat([this.dataBuffer.fullBuffer, ...this.dataBuffer.buffersToConcat]); | ||
} | ||
this.dataBuffer.buffersToConcat = []; | ||
return this.dataBuffer.fullBuffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.