| 106 | } |
| 107 | |
| 108 | export class IPCMessageWriter extends AbstractMessageWriter implements MessageWriter { |
| 109 | |
| 110 | private process: NodeJS.Process | ChildProcess; |
| 111 | private queue: Message[]; |
| 112 | private sending: boolean; |
| 113 | private errorCount: number; |
| 114 | |
| 115 | public constructor(process: NodeJS.Process | ChildProcess) { |
| 116 | super(); |
| 117 | this.process = process; |
| 118 | this.errorCount = 0; |
| 119 | this.queue = []; |
| 120 | this.sending = false; |
| 121 | let eventEmitter: NodeJS.EventEmitter = this.process; |
| 122 | eventEmitter.on('error', (error: any) => this.fireError(error)); |
| 123 | eventEmitter.on('close', () => this.fireClose); |
| 124 | } |
| 125 | |
| 126 | public write(msg: Message): void { |
| 127 | if (!this.sending && this.queue.length === 0) { |
| 128 | // See https://github.com/nodejs/node/issues/7657 |
| 129 | this.doWriteMessage(msg); |
| 130 | } else { |
| 131 | this.queue.push(msg); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public doWriteMessage(msg: Message): void { |
| 136 | try { |
| 137 | if (this.process.send) { |
| 138 | this.sending = true; |
| 139 | (this.process.send as Function)(msg, undefined, undefined, (error: any) => { |
| 140 | this.sending = false; |
| 141 | if (error) { |
| 142 | this.errorCount++; |
| 143 | this.fireError(error, msg, this.errorCount); |
| 144 | } else { |
| 145 | this.errorCount = 0; |
| 146 | } |
| 147 | if (this.queue.length > 0) { |
| 148 | this.doWriteMessage(this.queue.shift()!); |
| 149 | } |
| 150 | }); |
| 151 | } |
| 152 | } catch (error) { |
| 153 | this.errorCount++; |
| 154 | this.fireError(error, msg, this.errorCount); |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | export class SocketMessageWriter extends AbstractMessageWriter implements MessageWriter { |
| 161 |
nothing calls this directly
no outgoing calls
no test coverage detected