joyent/node — pull request #4940 by wanderview (2013-03-07 03:20:25+00:00)
Title: Streams2-ify `dgram.Socket`.

This is a proof-of-concept implementation of a stream-based `dgram.Socket`.
The class based on `stream.Duplex` with `{objectMode: true}`.

Received UDP packets can be retrieved by calling `read()` and look like
this:

{
data: udpDataPayload,
ip: { src: '1.2.3.4' },
udp { srcPort: 52 }
}

UDP packets can be sent by passing objects that look like the following to
`write()`:

{
data: udpDataPayload,
ip: { dst: '4.3.2.1' },
udp: { dstPort: 123 }
}

Backward compatibility is maintained. The existing `send()` call can be
used freely. If `on('message')` is called, then packets will automatically
be drained from the read queue and emitted using the existing `'message'`
event API.

See the new stream-based ping-pong test in `test-dgram-pingpong-stream.js`.

Again, this is only a proof-of-concept to get feedback at this point. If
this is considered something worth pursuing then more tests and API
documentation would need to be written. I just wanted to get some feedback
before spending more time on it.

The main goal of this change is to be able to take advantage of the `pipe()`
API to easily combine object streams together. Obviously the streams
back-pressure mechanism won't benefit UDP, but it shouldn't harm it either
since packets can already be lost at any layer during transmission.

Clearly this could also be done as a module wrapper.

Thank you.