def _handleResponse(self, token, data):
     try:
         cursor = self._cursor_cache.get(token)
         if cursor is not None:
             cursor._extend(data)
         elif token in self._user_queries:
             query, deferred = self._user_queries[token]
             res = Response(token, data,
                            self._parent._get_json_decoder(query))
             if res.type == pResponse.SUCCESS_ATOM:
                 deferred.callback(maybe_profile(res.data[0], res))
             elif res.type in (pResponse.SUCCESS_SEQUENCE,
                               pResponse.SUCCESS_PARTIAL):
                 cursor = TwistedCursor(self, query, res)
                 deferred.callback(maybe_profile(cursor, res))
             elif res.type == pResponse.WAIT_COMPLETE:
                 deferred.callback(None)
             elif res.type == pResponse.SERVER_INFO:
                 deferred.callback(res.data[0])
             else:
                 deferred.errback(res.make_error(query))
             del self._user_queries[token]
         elif not self._closing:
             raise ReqlDriverError("Unexpected response received.")
     except Exception as e:
         if not self._closing:
             self.close(exception=e)
    def _reader(self):
        try:
            while True:
                buf = self._socket.recvall(12)
                (
                    token,
                    length,
                ) = struct.unpack("<qL", buf)
                buf = self._socket.recvall(length)

                cursor = self._cursor_cache.get(token)
                if cursor is not None:
                    cursor._extend(buf)
                elif token in self._user_queries:
                    # Do not pop the query from the dict until later, so
                    # we don't lose track of it in case of an exception
                    query, async_res = self._user_queries[token]
                    res = net.Response(token, buf,
                                       self._parent._get_json_decoder(query))
                    if res.type == pResponse.SUCCESS_ATOM:
                        async_res.set(net.maybe_profile(res.data[0], res))
                    elif res.type in (pResponse.SUCCESS_SEQUENCE,
                                      pResponse.SUCCESS_PARTIAL):
                        cursor = GeventCursor(self, query, res)
                        async_res.set(net.maybe_profile(cursor, res))
                    elif res.type == pResponse.WAIT_COMPLETE:
                        async_res.set(None)
                    else:
                        async_res.set_exception(res.make_error(query))
                    del self._user_queries[token]
                elif not self._closing:
                    raise RqlDriverError("Unexpected response received.")
        except Exception as ex:
            if not self._closing:
                self.close(exception=ex)
    def _reader(self):
        try:
            while True:
                buf = yield self._stream.read_bytes(12)
                (
                    token,
                    length,
                ) = struct.unpack("<qL", buf)
                buf = yield self._stream.read_bytes(length)

                cursor = self._cursor_cache.get(token)
                if cursor is not None:
                    cursor._extend(buf)
                elif token in self._user_queries:
                    # Do not pop the query from the dict until later, so
                    # we don't lose track of it in case of an exception
                    query, future = self._user_queries[token]
                    res = Response(token, buf,
                                   self._parent._get_json_decoder(query))
                    if res.type == pResponse.SUCCESS_ATOM:
                        future.set_result(maybe_profile(res.data[0], res))
                    elif res.type in (pResponse.SUCCESS_SEQUENCE,
                                      pResponse.SUCCESS_PARTIAL):
                        cursor = TornadoCursor(self, query, res)
                        future.set_result(maybe_profile(cursor, res))
                    elif res.type == pResponse.WAIT_COMPLETE:
                        future.set_result(None)
                    elif res.type == pResponse.SERVER_INFO:
                        future.set_result(res.data[0])
                    else:
                        future.set_exception(res.make_error(query))
                    del self._user_queries[token]
                elif not self._closing:
                    raise ReqlDriverError("Unexpected response received.")
        except Exception as ex:
            if not self._closing:
                yield self.close(exception=ex)