scalaz/scalaz-stream — pull request #240 by stucchio (2014-09-16 02:12:02+00:00)
Title: Added listenUDP to io

Added a method to `io` which listens on a UDP socket. Is it desirable to have methods like this in scalaz-stream, or should they be left to some external library?

To test:

scala> io.listenUDP(2525).take(3).runLog.run
res2: IndexedSeq[scalaz.stream.io.UDPMessage] = Vector(UDPPacket([B@7f1deda0,/127.0.0.1,54962), UDPPacket([B@5dbe493,/127.0.0.1,39186), UDPPacket([B@277b71e6,/127.0.0.1,47546))

Then elsewhere:

echo -n `date` | nc -4u -w1 localhost 2525
echo -n `date` | nc -4u -w1 localhost 2525
echo -n `date` | nc -4u -w1 localhost 2525

It's also possible to return a stream of `java.net.DatagramPacket` itself, but that will likely result in too much memory usage. The problem is that if you create a `DatagramPacket` with a 4096 byte buffer, and then the actual message is only 128 bytes, you are passing around 4096-128 bytes unnecessarily. Hence the odd duplication of `java.net.DatagramPacket` to `io.UDPMessage`.