* Write UDP receive event * @param {Object} msg - UDP message with data, srcIP, srcPort, dstIP, dstPort
(msg)
| 231 | * @param {Object} msg - UDP message with data, srcIP, srcPort, dstIP, dstPort |
| 232 | */ |
| 233 | writeUdpRecv(msg) { |
| 234 | // Format: srcIPLen(1) + srcIP + srcPort(2) + dstIPLen(1) + dstIP + dstPort(2) + data |
| 235 | const srcIPBytes = Buffer.from(msg.srcIP, 'utf8'); |
| 236 | const dstIPBytes = Buffer.from(msg.dstIP, 'utf8'); |
| 237 | const dataBytes = Buffer.isBuffer(msg.data) ? msg.data : Buffer.from(msg.data); |
| 238 | |
| 239 | const payload = Buffer.alloc(1 + srcIPBytes.length + 2 + 1 + dstIPBytes.length + 2 + dataBytes.length); |
| 240 | let offset = 0; |
| 241 | |
| 242 | payload[offset++] = srcIPBytes.length; |
| 243 | srcIPBytes.copy(payload, offset); |
| 244 | offset += srcIPBytes.length; |
| 245 | |
| 246 | payload.writeUInt16LE(msg.srcPort, offset); |
| 247 | offset += 2; |
| 248 | |
| 249 | payload[offset++] = dstIPBytes.length; |
| 250 | dstIPBytes.copy(payload, offset); |
| 251 | offset += dstIPBytes.length; |
| 252 | |
| 253 | payload.writeUInt16LE(msg.dstPort, offset); |
| 254 | offset += 2; |
| 255 | |
| 256 | dataBytes.copy(payload, offset); |
| 257 | |
| 258 | return this.writeMessage(NET_MSG_UDP_RECV, payload); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Write stdin data |
no test coverage detected