mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2025-03-17 13:21:43 +01:00
handle partial socket data
This commit is contained in:
parent
fc96b9ea45
commit
e0239d2805
@ -52,7 +52,8 @@ export class StratumV1Client {
|
||||
public noFee: boolean;
|
||||
public hashRate: number;
|
||||
|
||||
|
||||
private buffer: string = '';
|
||||
|
||||
constructor(
|
||||
public readonly socket: Socket,
|
||||
private readonly stratumV1JobsService: StratumV1JobsService,
|
||||
@ -65,19 +66,22 @@ export class StratumV1Client {
|
||||
private readonly addressSettingsService: AddressSettingsService
|
||||
) {
|
||||
|
||||
const rl = createInterface({
|
||||
input: this.socket
|
||||
})
|
||||
this.socket.on('data', (data: Buffer) => {
|
||||
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) => {
|
||||
try {
|
||||
await this.handleMessage(line);
|
||||
} catch (e) {
|
||||
await this.socket.end();
|
||||
console.error(e);
|
||||
}
|
||||
})
|
||||
rl.on('error', async (error: Error) => { });
|
||||
lines
|
||||
.filter(m => m.length > 0)
|
||||
.forEach(async (m) => {
|
||||
try {
|
||||
await this.handleMessage(m);
|
||||
} catch (e) {
|
||||
await this.socket.end();
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user