Пример #1
0
 def send(self, data):
     data = str(data)  # FIXME temporary fix if data is of type buffer
     print "Sending data <<<{}>>>".format(data)
     if not self.can_write:
         raise Exception("Cannot write to closed socket")  # FIXME use actual exception
     future = self.channel.writeAndFlush(Unpooled.wrappedBuffer(data))
     self._handle_channel_future(future, "send")
     # FIXME are we sure we are going to be able to send this much data, especially async?
     return len(data)
Пример #2
0
    def sendto(self, string, arg1, arg2=None):
        # Unfortunate overloading
        if arg2 is not None:
            flags = arg1
            address = arg2
        else:
            flags = None
            address = arg1

        print "Sending data", string
        self._datagram_connect()
        # need a helper function to select proper address;
        # this should take in account if AF_INET, AF_INET6
        packet = DatagramPacket(Unpooled.wrappedBuffer(string),
                                _get_inet_addr(address))
        future = self.channel.writeAndFlush(packet)
        self._handle_channel_future(future, "sendto")
        return len(string)
Пример #3
0
def make_request(group, host, port, req):
    bootstrap = Bootstrap().\
        group(group).\
        channel(NioSocketChannel).\
        handler(SSLInitializer()).\
        option(ChannelOption.TCP_NODELAY, True).\
        option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
    channel = bootstrap.connect(host, port).sync().channel()
    data = [None]
    cv = Condition()

    class ReadAdapter(ChannelInboundHandlerAdapter):
        def channelRead(self, ctx, msg):
            try:
                length = msg.writerIndex()
                print "length=", length
                data[0] = buf = jarray.zeros(length, "b")
                msg.getBytes(0, buf)
                cv.acquire()
                cv.notify()
                cv.release()
            finally:
                msg.release()

        def channelReadComplete(self, ctx):
            print "Partial read"
            # partial reads; this seems to be seen in SSL handshaking/wrap/unwrap
            pass

    channel.pipeline().addLast(ReadAdapter())
    channel.writeAndFlush(Unpooled.wrappedBuffer(req)).sync()
    channel.read()

    # block until we get one full read; note that any real usage would
    # require parsing the HTTP header (Content-Length) for the number
    # of bytes to be read
    cv.acquire()
    while not data[0]:
        cv.wait()
    cv.release()
    channel.close().sync()
    return data[0].tostring()