Ticket #454: udpbroadcast.patch

File udpbroadcast.patch, 1.7 KB (added by hawkowl, 3 years ago)

Implementation of set/getBroadcastAllowed and corresponding test

  • twisted/internet/test/test_udp.py

    diff --git twisted/internet/test/test_udp.py twisted/internet/test/test_udp.py
    index 4452e2c..1392fe4 100644
    class UDPPortTestsMixin(object): 
    192192        self.assertIn(repr(port.getHost().port), str(port))
    193193
    194194
     195    def test_allowBroadcast(self):
     196        """
     197        L{IListeningPort.setBroadcastAllowed} sets broadcast to be allowed
     198        on the socket.
     199        """
     200        reactor = self.buildReactor()
     201        port = self.getListeningPort(reactor, DatagramProtocol())
     202        port.setBroadcastAllowed(True)
     203        self.assertTrue(port.getBroadcastAllowed())
     204
     205
    195206
    196207class UDPServerTestsBuilder(ReactorBuilder,
    197208                            UDPPortTestsMixin, DatagramTransportTestsMixin):
  • twisted/internet/udp.py

    diff --git twisted/internet/udp.py twisted/internet/udp.py
    index 9dc55df..b68f63d 100644
    class Port(base.BasePort): 
    354354        return address.IPv4Address('UDP', *self.socket.getsockname())
    355355
    356356
     357    def setBroadcastAllowed(self, enabled):
     358        """
     359        Set whether this UDP socket may broadcast. This is disabled by default.
     360
     361        @param enabled: Whether the socket may broadcast.
     362        @type enabled: C{bool}
     363        """
     364        if enabled == True: enabled_int = 1
     365        else: enabled_int = 0
     366        self.socket.setsockopt(
     367            socket.SOL_SOCKET, socket.SO_BROADCAST, enabled_int)
     368
     369
     370    def getBroadcastAllowed(self):
     371        """
     372        Return whether this UDP socket may broadcast.
     373        """
     374        return operator.truth(
     375            self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST))
     376
     377
    357378
    358379class MulticastMixin:
    359380    """