handle partial socket data

This commit is contained in:
Benjamin Wilson
2024-07-04 20:21:41 -04:00
parent fc96b9ea45
commit e0239d2805
+17 -13
View File
@@ -52,7 +52,8 @@ export class StratumV1Client {
public noFee: boolean; public noFee: boolean;
public hashRate: number; public hashRate: number;
private buffer: string = '';
constructor( constructor(
public readonly socket: Socket, public readonly socket: Socket,
private readonly stratumV1JobsService: StratumV1JobsService, private readonly stratumV1JobsService: StratumV1JobsService,
@@ -65,19 +66,22 @@ export class StratumV1Client {
private readonly addressSettingsService: AddressSettingsService private readonly addressSettingsService: AddressSettingsService
) { ) {
const rl = createInterface({ this.socket.on('data', (data: Buffer) => {
input: this.socket this.buffer += data.toString();
}) let lines = this.buffer.split('\n');
this.buffer = lines.pop() || ''; // Save the last part of the data (incomplete line) to the buffer
rl.on("line", async (line) => { lines
try { .filter(m => m.length > 0)
await this.handleMessage(line); .forEach(async (m) => {
} catch (e) { try {
await this.socket.end(); await this.handleMessage(m);
console.error(e); } catch (e) {
} await this.socket.end();
}) console.error(e);
rl.on('error', async (error: Error) => { }); }
});
});
} }