示例#1
0
    def _reader(self):
        try:
            while True:
                buf = yield from self._streamreader.readexactly(12)
                (token, length,) = struct.unpack("<qL", buf)
                buf = yield from self._streamreader.readexactly(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 = AsyncioCursor(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 from self.close(exception=ex)
示例#2
0
 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)