Example #1
0
        class protocol:
          datagramReceived = promise.sequence()

          # Avoid AttributeError: protocol instance has no attribute 'doStop'
          def doStop(ctx):
            pass

          @transport.then
          @promise.resume
          def makeConnection(transport):
            nstHost = host

            try:
              try:
                transport.connect(host, port)

              except ValueError:

                # Avoid ImportError: cannot import name dns
                from untwisted import dns

                nstHost = (yield dns.lookup(host)).answer[0].address

                transport.connect(nstHost, port)

            # tcp.Connector calls socket.getservbyname() but .connect() doesn't : (
            except TypeError:
              nstPort = socket.getservbyname(port, 'udp')

              # Avoid RuntimeError: already connected
              transport._connectedAddr = None

              transport.connect(nstHost, nstPort)

            raise StopIteration(transport)
Example #2
0
        class protocol:
          datagramReceived = promise.sequence()

          # Avoid AttributeError: protocol instance has no attribute 'doStop'
          def doStop(ctx):
            pass

          makeConnection = transport
Example #3
0
      def __init__(ctx):
        ctx.connectionLost = promise.promise()

        ctx.dataReceived = promise.sequence()

        # .dataReceived() must return falsy: SelectReactor._doReadOrWrite()

        __call__ = ctx.dataReceived.__call__

        @untwisted.partial(setattr, ctx.dataReceived, '__call__')
        def _(*args, **kwds):
          __call__(*args, **kwds)
Example #4
0
            def __init__(ctx):
                ctx.connectionLost = promise.promise()

                ctx.dataReceived = promise.sequence()

                # .dataReceived() must return falsy: SelectReactor._doReadOrWrite()

                __call__ = ctx.dataReceived.__call__

                @untwisted.partial(setattr, ctx.dataReceived, '__call__')
                def _(*args, **kwds):
                    __call__(*args, **kwds)
Example #5
0
def listen(port, interface=''):
    transport = promise.sequence()

    # Extend protocol.Factory for .doStart()

    @untwisted.call
    class factory(protocol.Factory):
        class protocol:
            def __init__(ctx):
                ctx.connectionLost = promise.promise()

                ctx.dataReceived = promise.sequence()

                # .dataReceived() must return falsy: SelectReactor._doReadOrWrite()

                __call__ = ctx.dataReceived.__call__

                @untwisted.partial(setattr, ctx.dataReceived, '__call__')
                def _(*args, **kwds):
                    __call__(*args, **kwds)

            makeConnection = transport

    @untwisted.call
    class _(tcp.Port):
        class __metaclass__(type):
            def __call__(ctx):
                try:
                    type.__call__(ctx, port, factory,
                                  interface=interface).startListening()

                # tcp.Connector calls socket.getservbyname() but tcp.Port doesn't : (
                except TypeError:
                    nstPort = socket.getservbyname(port, 'tcp')

                    type.__call__(ctx, nstPort, factory,
                                  interface=interface).startListening()

        class transport(tcp.Server):
            def close(ctx):
                ctx.loseConnection()

                return ctx.protocol.connectionLost

    return transport.shift
Example #6
0
def listen(port, interface=''):
  transport = promise.sequence()

  # Extend protocol.Factory for .doStart()

  @untwisted.call
  class factory(protocol.Factory):
    class protocol:
      def __init__(ctx):
        ctx.connectionLost = promise.promise()

        ctx.dataReceived = promise.sequence()

        # .dataReceived() must return falsy: SelectReactor._doReadOrWrite()

        __call__ = ctx.dataReceived.__call__

        @untwisted.partial(setattr, ctx.dataReceived, '__call__')
        def _(*args, **kwds):
          __call__(*args, **kwds)

      makeConnection = transport

  @untwisted.call
  class _(tcp.Port):
    class __metaclass__(type):
      def __call__(ctx):
        try:
          type.__call__(ctx, port, factory, interface=interface).startListening()

        # tcp.Connector calls socket.getservbyname() but tcp.Port doesn't : (
        except TypeError:
          nstPort = socket.getservbyname(port, 'tcp')

          type.__call__(ctx, nstPort, factory, interface=interface).startListening()

    class transport(tcp.Server):
      def close(ctx):
        ctx.loseConnection()

        return ctx.protocol.connectionLost

  return transport.shift