Compare commits
7 Commits
cde0cf76a7
...
36a2a1d2c6
Author | SHA1 | Date | |
---|---|---|---|
36a2a1d2c6 | |||
d244c67cba | |||
45fa0f4f73 | |||
d7968819b7 | |||
16b6b5b322 | |||
b242a96259 | |||
f308e935fb |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,6 +1,7 @@
|
||||
CMakeLists.txt.user*
|
||||
_build
|
||||
_output
|
||||
.cmake/
|
||||
*.autosave
|
||||
files/lib/*
|
||||
files/log/*
|
||||
|
@ -89,10 +89,13 @@ class Paths
|
||||
protected:
|
||||
Paths();
|
||||
~Paths() = default;
|
||||
Paths( const Paths& ) = delete; // NOLINT
|
||||
Paths& operator=( const Paths& ) = delete; // NOLINT
|
||||
|
||||
public:
|
||||
Paths( const Paths& ) = delete;
|
||||
Paths& operator=( const Paths& ) = delete;
|
||||
Paths( Paths&& ) = delete;
|
||||
Paths& operator=( Paths&& ) = delete;
|
||||
|
||||
/**
|
||||
* @brief getInstance
|
||||
* @return Уникальный экземпляр класса Paths
|
||||
|
@ -30,13 +30,17 @@ class PathsMT : public Paths
|
||||
{
|
||||
PathsMT();
|
||||
~PathsMT() = default;
|
||||
PathsMT( const PathsMT& ) = delete; // NOLINT
|
||||
PathsMT& operator=( const PathsMT& ) = delete; // NOLINT
|
||||
|
||||
static std::atomic< PathsMT* > mInstance;
|
||||
static std::mutex mMutex;
|
||||
|
||||
|
||||
public:
|
||||
PathsMT( const PathsMT& ) = delete;
|
||||
PathsMT& operator=( const PathsMT& ) = delete;
|
||||
PathsMT( PathsMT&& ) = delete;
|
||||
PathsMT& operator=( PathsMT&& ) = delete;
|
||||
|
||||
/**
|
||||
* @brief getInstance
|
||||
* @return Уникальный экземпляр класса PathsMT
|
||||
|
@ -7,72 +7,54 @@
|
||||
#include <myx/redis/client.hpp>
|
||||
#endif
|
||||
|
||||
#include <myx/redis/client_p.hpp>
|
||||
|
||||
namespace myx {
|
||||
|
||||
namespace redis {
|
||||
|
||||
ClientPrivate::ClientPrivate( Client* client ) :
|
||||
lexer ( &socket ),
|
||||
parser( &lexer )
|
||||
{
|
||||
connect( &socket, &QTcpSocket::connected, client, &Client::connected );
|
||||
connect( &socket, &QTcpSocket::disconnected, client, &Client::disconnected );
|
||||
connect( &parser, &Parser::reply, this, &ClientPrivate::sendReply );
|
||||
}
|
||||
|
||||
|
||||
void ClientPrivate::sendReply( const Reply& reply )
|
||||
{
|
||||
Q_EMIT queue.dequeue()->reply( reply );
|
||||
}
|
||||
|
||||
|
||||
Client::Client( QObject* parent ) :
|
||||
MYXLIB_INLINE Client::Client( QObject* parent ) :
|
||||
QObject( parent ),
|
||||
d ( new ClientPrivate( this ) )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void Client::connectToHost( const QString& hostName, quint16 port )
|
||||
MYXLIB_INLINE void Client::connectToHost( const QString& hostName, quint16 port )
|
||||
{
|
||||
d->socket.connectToHost( hostName, port );
|
||||
d->m_socket.connectToHost( hostName, port );
|
||||
}
|
||||
|
||||
|
||||
void Client::disconnectFromHost()
|
||||
MYXLIB_INLINE void Client::disconnectFromHost()
|
||||
{
|
||||
d->socket.disconnectFromHost();
|
||||
d->m_socket.disconnectFromHost();
|
||||
}
|
||||
|
||||
|
||||
bool Client::isConnected() const
|
||||
MYXLIB_INLINE bool Client::isConnected() const
|
||||
{
|
||||
return( d->socket.state() == QAbstractSocket::ConnectedState );
|
||||
return( d->m_socket.state() == QAbstractSocket::ConnectedState );
|
||||
}
|
||||
|
||||
|
||||
Request* Client::sendCommand( const QByteArray& command )
|
||||
MYXLIB_INLINE Request* Client::sendCommand( const QByteArray& command )
|
||||
{
|
||||
d->socket.write( command + "\r\n" );
|
||||
d->m_socket.write( command + "\r\n" );
|
||||
|
||||
auto* request = new Request( this );
|
||||
d->queue.enqueue( request );
|
||||
d->m_queue.enqueue( request );
|
||||
return( request );
|
||||
}
|
||||
|
||||
|
||||
bool Client::waitForConnected( int msecs )
|
||||
MYXLIB_INLINE bool Client::waitForConnected( int msecs )
|
||||
{
|
||||
return( d->socket.waitForConnected( msecs ) );
|
||||
return( d->m_socket.waitForConnected( msecs ) );
|
||||
}
|
||||
|
||||
|
||||
bool Client::waitForDisconnected( int msecs )
|
||||
MYXLIB_INLINE bool Client::waitForDisconnected( int msecs )
|
||||
{
|
||||
return( d->socket.waitForDisconnected( msecs ) );
|
||||
return( d->m_socket.waitForDisconnected( msecs ) );
|
||||
}
|
||||
|
||||
} // namespace redis
|
||||
|
@ -4,17 +4,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <myx/base/config.hpp>
|
||||
#include <myx/redis/lexer.hpp>
|
||||
#include <myx/redis/parser.hpp>
|
||||
#include <myx/redis/request.hpp>
|
||||
|
||||
#include <QObject>
|
||||
#include <QQueue>
|
||||
#include <QScopedPointer>
|
||||
#include <QTcpSocket>
|
||||
|
||||
namespace myx {
|
||||
|
||||
namespace redis {
|
||||
|
||||
QT_FORWARD_DECLARE_CLASS( ClientPrivate )
|
||||
|
||||
/**
|
||||
* @brief Provides access to a Redis server
|
||||
*/
|
||||
@ -30,10 +31,15 @@ public:
|
||||
*/
|
||||
explicit Client( QObject* parent = nullptr );
|
||||
|
||||
Client( const Client& ) = delete;
|
||||
Client& operator=( const Client& ) = delete;
|
||||
Client( Client&& ) = delete;
|
||||
Client& operator=( Client&& ) = delete;
|
||||
|
||||
/**
|
||||
* @brief Destroys the client
|
||||
*/
|
||||
virtual ~Client() = default;
|
||||
~Client() override = default;
|
||||
|
||||
/*
|
||||
* Note: we specifically avoid an overload of connectToHost that
|
||||
@ -105,6 +111,33 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
class ClientPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
friend class Client;
|
||||
|
||||
explicit ClientPrivate( Client* client = nullptr ) :
|
||||
m_lexer ( &m_socket ),
|
||||
m_parser( &m_lexer )
|
||||
{
|
||||
connect( &m_socket, &QTcpSocket::connected, client, &Client::connected );
|
||||
connect( &m_socket, &QTcpSocket::disconnected, client, &Client::disconnected );
|
||||
connect( &m_parser, &Parser::reply, this, &ClientPrivate::sendReply );
|
||||
}
|
||||
|
||||
|
||||
QTcpSocket m_socket;
|
||||
QQueue< Request* > m_queue;
|
||||
Lexer m_lexer;
|
||||
Parser m_parser;
|
||||
|
||||
Q_SLOT void sendReply( const myx::redis::Reply& reply )
|
||||
{
|
||||
Q_EMIT m_queue.dequeue()->reply( reply );
|
||||
}
|
||||
}; // class ClientPrivate
|
||||
|
||||
const QScopedPointer< ClientPrivate > d;
|
||||
}; // class Client
|
||||
|
||||
@ -117,4 +150,4 @@ private:
|
||||
#endif
|
||||
|
||||
|
||||
#endif // MYX_REDIS_CLIENT_HPP_
|
||||
#endif // ifndef MYX_REDIS_CLIENT_HPP_
|
||||
|
@ -1,38 +0,0 @@
|
||||
#ifndef MYX_REDIS_CLIENT_P_HPP_
|
||||
#define MYX_REDIS_CLIENT_P_HPP_
|
||||
|
||||
#include <QObject>
|
||||
#include <QQueue>
|
||||
#include <QTcpSocket>
|
||||
|
||||
#include <client.hpp>
|
||||
#include <reply.hpp>
|
||||
#include <request.hpp>
|
||||
#include <lexer.hpp>
|
||||
#include <parser.hpp>
|
||||
|
||||
namespace myx {
|
||||
|
||||
namespace redis {
|
||||
|
||||
class ClientPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ClientPrivate( Client* );
|
||||
|
||||
QTcpSocket socket;
|
||||
QQueue< Request* > queue;
|
||||
|
||||
Lexer lexer;
|
||||
Parser parser;
|
||||
|
||||
private:
|
||||
Q_SLOT void sendReply(const myx::redis::Reply & );
|
||||
}; // class ClientPrivate
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif // MYX_REDIS_CLIENT_P_HPP_
|
@ -11,10 +11,10 @@ namespace myx {
|
||||
|
||||
namespace redis {
|
||||
|
||||
Lexer::Lexer( QIODevice* device, QObject* parent ) :
|
||||
MYXLIB_INLINE Lexer::Lexer( QIODevice* device, QObject* parent ) :
|
||||
QObject ( parent ),
|
||||
m_device( device ),
|
||||
m_state ( DoingNothing ),
|
||||
m_state ( kDoingNothing ),
|
||||
m_crlf ( 0 ),
|
||||
m_length( 0 )
|
||||
{
|
||||
@ -22,39 +22,39 @@ Lexer::Lexer( QIODevice* device, QObject* parent ) :
|
||||
}
|
||||
|
||||
|
||||
void Lexer::readData()
|
||||
MYXLIB_INLINE void Lexer::readData()
|
||||
{
|
||||
m_buffer.append( m_device->readAll() );
|
||||
|
||||
while ( true )
|
||||
{
|
||||
if ( ( m_state == DoingNothing ) && !readCharacter() )
|
||||
if ( ( m_state == kDoingNothing ) && !readCharacter() )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
switch ( m_state )
|
||||
{
|
||||
case ReadingLength:
|
||||
case ReadingUnsafeString:
|
||||
case kReadingLength:
|
||||
case kReadingUnsafeString:
|
||||
if ( !readUnsafeString() ) { return; }
|
||||
break;
|
||||
case ReadingSafeString:
|
||||
case kReadingSafeString:
|
||||
if ( !readSafeString() ) { return; }
|
||||
break;
|
||||
case DoingNothing:
|
||||
case kDoingNothing:
|
||||
break;
|
||||
}
|
||||
|
||||
if ( m_state != ReadingSafeString )
|
||||
if ( m_state != kReadingSafeString )
|
||||
{
|
||||
m_state = DoingNothing;
|
||||
m_state = kDoingNothing;
|
||||
}
|
||||
}
|
||||
} // Lexer::readData
|
||||
|
||||
|
||||
bool Lexer::readCharacter()
|
||||
MYXLIB_INLINE bool Lexer::readCharacter()
|
||||
{
|
||||
if ( m_buffer.isEmpty() )
|
||||
{
|
||||
@ -70,9 +70,9 @@ bool Lexer::readCharacter()
|
||||
case '-':
|
||||
case ':':
|
||||
case '*':
|
||||
m_state = ReadingUnsafeString; break;
|
||||
m_state = kReadingUnsafeString; break;
|
||||
case '$':
|
||||
m_state = ReadingLength; break;
|
||||
m_state = kReadingLength; break;
|
||||
}
|
||||
|
||||
Q_EMIT character( c );
|
||||
@ -80,7 +80,7 @@ bool Lexer::readCharacter()
|
||||
} // Lexer::readCharacter
|
||||
|
||||
|
||||
bool Lexer::readUnsafeString()
|
||||
MYXLIB_INLINE bool Lexer::readUnsafeString()
|
||||
{
|
||||
m_crlf = m_buffer.indexOf( "\r\n", m_crlf );
|
||||
if ( m_crlf == -1 )
|
||||
@ -92,10 +92,10 @@ bool Lexer::readUnsafeString()
|
||||
QString s = m_buffer.mid( 0, m_crlf );
|
||||
m_buffer.remove( 0, m_crlf + 2 );
|
||||
|
||||
if ( m_state == ReadingLength )
|
||||
if ( m_state == kReadingLength )
|
||||
{
|
||||
m_length = s.toInt();
|
||||
m_state = ReadingSafeString;
|
||||
m_state = kReadingSafeString;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -107,7 +107,7 @@ bool Lexer::readUnsafeString()
|
||||
} // Lexer::readUnsafeString
|
||||
|
||||
|
||||
bool Lexer::readSafeString()
|
||||
MYXLIB_INLINE bool Lexer::readSafeString()
|
||||
{
|
||||
if ( m_buffer.size() - m_length < 2 )
|
||||
{
|
||||
@ -119,7 +119,7 @@ bool Lexer::readSafeString()
|
||||
|
||||
Q_EMIT safeString( d );
|
||||
|
||||
m_state = DoingNothing;
|
||||
m_state = kDoingNothing;
|
||||
return( true );
|
||||
}
|
||||
|
||||
|
@ -17,8 +17,15 @@ class Lexer : public QObject
|
||||
|
||||
public:
|
||||
|
||||
Lexer( QIODevice*, QObject* = nullptr );
|
||||
virtual ~Lexer() = default;
|
||||
explicit Lexer( QIODevice*, QObject* = nullptr );
|
||||
|
||||
Lexer( const Lexer& ) = delete;
|
||||
Lexer& operator=( const Lexer& ) = delete;
|
||||
Lexer( Lexer&& ) = delete;
|
||||
Lexer& operator=( Lexer&& ) = delete;
|
||||
|
||||
|
||||
~Lexer() override = default;
|
||||
|
||||
Q_SIGNAL void character( char );
|
||||
Q_SIGNAL void unsafeString( const QString& );
|
||||
@ -28,8 +35,6 @@ private:
|
||||
|
||||
Q_SLOT void readData();
|
||||
|
||||
private:
|
||||
|
||||
bool readCharacter();
|
||||
bool readLength();
|
||||
bool readUnsafeString();
|
||||
@ -40,10 +45,10 @@ private:
|
||||
|
||||
enum
|
||||
{
|
||||
DoingNothing,
|
||||
ReadingLength,
|
||||
ReadingUnsafeString,
|
||||
ReadingSafeString
|
||||
kDoingNothing,
|
||||
kReadingLength,
|
||||
kReadingUnsafeString,
|
||||
kReadingSafeString
|
||||
} m_state;
|
||||
|
||||
int m_crlf;
|
||||
@ -59,4 +64,4 @@ private:
|
||||
#endif
|
||||
|
||||
|
||||
#endif // MYX_REDIS_LEXER_HPP_
|
||||
#endif // ifndef MYX_REDIS_LEXER_HPP_
|
||||
|
@ -11,7 +11,7 @@ namespace myx {
|
||||
|
||||
namespace redis {
|
||||
|
||||
Parser::Parser( Lexer* lexer, QObject* parent ) :
|
||||
MYXLIB_INLINE Parser::Parser( Lexer* lexer, QObject* parent ) :
|
||||
QObject( parent )
|
||||
{
|
||||
connect( lexer, &Lexer::character, this, &Parser::readCharacter );
|
||||
@ -20,67 +20,67 @@ Parser::Parser( Lexer* lexer, QObject* parent ) :
|
||||
}
|
||||
|
||||
|
||||
void Parser::readCharacter( const char c )
|
||||
MYXLIB_INLINE void Parser::readCharacter( const char c )
|
||||
{
|
||||
switch ( c )
|
||||
{
|
||||
case '+':
|
||||
stack.append( Task( Reply::Status ) ); break;
|
||||
m_stack.append( Task( Reply::kStatus ) ); break;
|
||||
case '-':
|
||||
stack.append( Task( Reply::Error ) ); break;
|
||||
m_stack.append( Task( Reply::kError ) ); break;
|
||||
case ':':
|
||||
stack.append( Task( Reply::Integer ) ); break;
|
||||
m_stack.append( Task( Reply::kInteger ) ); break;
|
||||
case '$':
|
||||
stack.append( Task( Reply::Bulk ) ); break;
|
||||
m_stack.append( Task( Reply::kBulk ) ); break;
|
||||
case '*':
|
||||
stack.append( Task( Reply::MultiBulk ) ); break;
|
||||
m_stack.append( Task( Reply::kMultiBulk ) ); break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Parser::readUnsafeString( const QString& value )
|
||||
MYXLIB_INLINE void Parser::readUnsafeString( const QString& value )
|
||||
{
|
||||
if ( tos().reply.type() == Reply::MultiBulk )
|
||||
if ( tos().m_reply.type() == Reply::kMultiBulk )
|
||||
{
|
||||
tos().count = value.toInt();
|
||||
tos().m_count = value.toInt();
|
||||
}
|
||||
else
|
||||
{
|
||||
tos().reply.value() = value;
|
||||
tos().m_reply.value() = value;
|
||||
}
|
||||
|
||||
descend();
|
||||
}
|
||||
|
||||
|
||||
void Parser::readSafeString( const QByteArray& value )
|
||||
MYXLIB_INLINE void Parser::readSafeString( const QByteArray& value )
|
||||
{
|
||||
tos().reply.value() = value;
|
||||
tos().m_reply.value() = value;
|
||||
descend();
|
||||
}
|
||||
|
||||
|
||||
void Parser::descend()
|
||||
MYXLIB_INLINE void Parser::descend()
|
||||
{
|
||||
while ( true )
|
||||
{
|
||||
if ( ( tos().reply.type() == Reply::MultiBulk ) &&
|
||||
( tos().reply.value().toList().count() < tos().count ) )
|
||||
if ( ( tos().m_reply.type() == Reply::kMultiBulk ) &&
|
||||
( tos().m_reply.value().toList().count() < tos().m_count ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ( stack.count() == 1 )
|
||||
if ( m_stack.count() == 1 )
|
||||
{
|
||||
auto r = stack.takeLast().reply;
|
||||
auto r = m_stack.takeLast().m_reply;
|
||||
Q_EMIT reply( r );
|
||||
return;
|
||||
}
|
||||
|
||||
auto r = stack.takeLast().reply;
|
||||
tos().reply.value().toList().append( QVariant::fromValue( r ) );
|
||||
auto r = m_stack.takeLast().m_reply;
|
||||
tos().m_reply.value().toList().append( QVariant::fromValue( r ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,8 +4,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <myx/base/config.hpp>
|
||||
#include <myx/redis/reply.hpp>
|
||||
#include <myx/redis/lexer.hpp>
|
||||
// #include <myx/redis/parser_p.hpp>
|
||||
#include <myx/redis/reply.hpp>
|
||||
|
||||
#include <QList>
|
||||
#include <QPair>
|
||||
@ -20,36 +21,41 @@ class Parser : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Parser( Lexer*, QObject* = nullptr );
|
||||
virtual ~Parser() = default;
|
||||
explicit Parser( Lexer*, QObject* = nullptr );
|
||||
|
||||
Parser( const Parser& ) = delete;
|
||||
Parser& operator=( const Parser& ) = delete;
|
||||
Parser( Parser&& ) = delete;
|
||||
Parser& operator=( Parser&& ) = delete;
|
||||
|
||||
~Parser() override = default;
|
||||
|
||||
Q_SIGNAL void reply( const myx::redis::Reply& );
|
||||
|
||||
private:
|
||||
Q_SLOT void readCharacter( const char );
|
||||
Q_SLOT void readCharacter( char );
|
||||
Q_SLOT void readUnsafeString( const QString& );
|
||||
Q_SLOT void readSafeString( const QByteArray& );
|
||||
|
||||
private:
|
||||
void descend();
|
||||
|
||||
class Task
|
||||
{
|
||||
public:
|
||||
friend class Parser;
|
||||
|
||||
enum { Unknown = -2 };
|
||||
enum { kUnknown = -2 };
|
||||
|
||||
Task( Reply::Type type ) :
|
||||
reply( type ),
|
||||
count( Unknown ) {}
|
||||
explicit Task( Reply::Type type ) :
|
||||
m_reply( type ),
|
||||
m_count( kUnknown ) {}
|
||||
|
||||
Reply reply;
|
||||
int count;
|
||||
Reply m_reply;
|
||||
int m_count;
|
||||
};
|
||||
|
||||
QList< Task > stack;
|
||||
QList< Task > m_stack;
|
||||
|
||||
Task& tos() { return( stack.last() ); }
|
||||
Task& tos() { return( m_stack.last() ); }
|
||||
}; // class Parser
|
||||
|
||||
} // namespace redis
|
||||
@ -60,4 +66,4 @@ public:
|
||||
#include "parser-inl.hpp"
|
||||
#endif
|
||||
|
||||
#endif // MYX_REDIS_PARSER_HPP_
|
||||
#endif // ifndef MYX_REDIS_PARSER_HPP_
|
||||
|
@ -28,7 +28,7 @@ public:
|
||||
*
|
||||
* This value is only set when the default constructor is used.
|
||||
*/
|
||||
Invalid,
|
||||
kInvalid,
|
||||
|
||||
/**
|
||||
* @brief A status reply
|
||||
@ -36,7 +36,7 @@ public:
|
||||
* The value property will contain the status message returned
|
||||
* by the server as a QString.
|
||||
*/
|
||||
Status,
|
||||
kStatus,
|
||||
|
||||
/**
|
||||
* @brief An error reply
|
||||
@ -44,7 +44,7 @@ public:
|
||||
* The value property will contain the error message returned by
|
||||
* the server as a QString.
|
||||
*/
|
||||
Error,
|
||||
kError,
|
||||
|
||||
/**
|
||||
* @brief An integer reply
|
||||
@ -52,7 +52,7 @@ public:
|
||||
* The value property will contain the integer value returned by
|
||||
* the server as a qlonglong.
|
||||
*/
|
||||
Integer,
|
||||
kInteger,
|
||||
|
||||
/**
|
||||
* @brief A bulk reply
|
||||
@ -60,7 +60,7 @@ public:
|
||||
* The value property will contain the bulk reply returned by
|
||||
* the server as a QByteArray.
|
||||
*/
|
||||
Bulk,
|
||||
kBulk,
|
||||
|
||||
/**
|
||||
* @brief A multi-bulk reply
|
||||
@ -69,44 +69,43 @@ public:
|
||||
* by the server as a QVariantList. Each entry in the list is of
|
||||
* type Reply.
|
||||
*/
|
||||
MultiBulk
|
||||
kMultiBulk
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Creates an empty reply
|
||||
*/
|
||||
Reply() :
|
||||
_type( Invalid ) {}
|
||||
Reply() = default;
|
||||
|
||||
/**
|
||||
* @brief Initializes the reply
|
||||
* @param type the type of the reply
|
||||
*/
|
||||
Reply( Type type ) :
|
||||
_type( type ) {}
|
||||
explicit Reply( Type type ) :
|
||||
m_type( type ) {}
|
||||
|
||||
/**
|
||||
* @brief Returns the type of the reply
|
||||
* @return the reply type
|
||||
*/
|
||||
Type type() const { return( _type ); }
|
||||
Type type() const { return( m_type ); }
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the value of the reply
|
||||
* @return the reply value
|
||||
*/
|
||||
QVariant& value() { return( _value ); }
|
||||
QVariant& value() { return( m_value ); }
|
||||
|
||||
private:
|
||||
|
||||
Type _type;
|
||||
QVariant _value;
|
||||
}; // class MYX_REDIS_EXPORT Reply
|
||||
Type m_type { kInvalid };
|
||||
QVariant m_value;
|
||||
}; // class Reply
|
||||
|
||||
} // namespace redis
|
||||
|
||||
} // namespace myx
|
||||
|
||||
Q_DECLARE_METATYPE( myx::redis::Reply )
|
||||
Q_DECLARE_METATYPE( myx::redis::Reply ) // NOLINT
|
||||
|
||||
#endif // MYX_REDIS_REPLY_HPP_
|
||||
#endif // ifndef MYX_REDIS_REPLY_HPP_
|
||||
|
@ -8,7 +8,6 @@
|
||||
#ifndef MYXLIB_HEADER_ONLY
|
||||
#include <myx/redis/request.hpp>
|
||||
#endif
|
||||
#include <myx/redis/request_p.hpp>
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
@ -16,34 +15,28 @@ namespace myx {
|
||||
|
||||
namespace redis {
|
||||
|
||||
void RequestPrivate::quitEventLoop()
|
||||
{
|
||||
loop.exit( 1 );
|
||||
}
|
||||
|
||||
|
||||
Request::Request( QObject* parent ) :
|
||||
MYXLIB_INLINE Request::Request( QObject* parent ) :
|
||||
QObject( parent ),
|
||||
d ( new RequestPrivate )
|
||||
d ( new Loop )
|
||||
{
|
||||
connect( this, &Request::reply, this, &Request::deleteLater );
|
||||
}
|
||||
|
||||
|
||||
bool Request::waitForReply( int msecs )
|
||||
MYXLIB_INLINE bool Request::waitForReply( int msecs )
|
||||
{
|
||||
QTimer timer;
|
||||
timer.setInterval( msecs );
|
||||
timer.setSingleShot( true );
|
||||
|
||||
connect( &timer, &QTimer::timeout, &d->loop, &QEventLoop::quit );
|
||||
connect( this, &Request::reply, d.data(), &RequestPrivate::quitEventLoop );
|
||||
connect( &timer, &QTimer::timeout, &d->m_loop, &QEventLoop::quit );
|
||||
connect( this, &Request::reply, d.data(), &Loop::quitEventLoop );
|
||||
|
||||
/*
|
||||
* If the timer fires, the return value will be 0.
|
||||
* Otherwise, quitEventLoop() will terminate the loop with 1.
|
||||
*/
|
||||
return( ( d->loop.exec( QEventLoop::ExcludeUserInputEvents ) != 0 ) );
|
||||
return( ( d->m_loop.exec( QEventLoop::ExcludeUserInputEvents ) != 0 ) );
|
||||
}
|
||||
|
||||
} // namespace redis
|
||||
|
@ -6,15 +6,14 @@
|
||||
#include <myx/base/config.hpp>
|
||||
#include <myx/redis/reply.hpp>
|
||||
|
||||
#include <QObject>
|
||||
#include <QEventLoop>
|
||||
#include <QScopedPointer>
|
||||
|
||||
|
||||
namespace myx {
|
||||
|
||||
namespace redis {
|
||||
|
||||
QT_FORWARD_DECLARE_CLASS( RequestPrivate );
|
||||
|
||||
/**
|
||||
* @brief Represents a Redis command and its response
|
||||
*/
|
||||
@ -30,10 +29,15 @@ public:
|
||||
*/
|
||||
explicit Request( QObject* parent = nullptr );
|
||||
|
||||
Request( const Request& ) = delete;
|
||||
Request& operator=( const Request& ) = delete;
|
||||
Request( Request&& ) = delete;
|
||||
Request& operator=( Request&& ) = delete;
|
||||
|
||||
/**
|
||||
* @brief Destroys the request
|
||||
*/
|
||||
virtual ~Request() = default;
|
||||
~Request() override = default;
|
||||
|
||||
/**
|
||||
* @brief Waits for the reply to be received
|
||||
@ -50,7 +54,19 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
const QScopedPointer< RequestPrivate > d;
|
||||
class Loop : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
friend class Request;
|
||||
explicit Loop( QObject* parent = nullptr );
|
||||
|
||||
QEventLoop m_loop;
|
||||
Q_SLOT void quitEventLoop() { m_loop.exit( 1 ); }
|
||||
};
|
||||
|
||||
|
||||
const QScopedPointer< Loop > d;
|
||||
}; // class Request
|
||||
|
||||
} // namespace redis
|
||||
@ -61,4 +77,4 @@ private:
|
||||
#include "request-inl.hpp"
|
||||
#endif
|
||||
|
||||
#endif // MYX_REDIS_REQUEST_HPP_
|
||||
#endif // ifndef MYX_REDIS_REQUEST_HPP_
|
||||
|
@ -1,25 +0,0 @@
|
||||
#ifndef MYX_REDIS_REQUEST_P_HPP_
|
||||
#define MYX_REDIS_REQUEST_P_HPP_
|
||||
|
||||
#include <QEventLoop>
|
||||
#include <QObject>
|
||||
|
||||
namespace myx {
|
||||
|
||||
namespace redis {
|
||||
|
||||
class RequestPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit RequestPrivate( QObject* parent = nullptr );
|
||||
|
||||
QEventLoop loop;
|
||||
Q_SLOT void quitEventLoop();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
#endif // MYX_REDIS_REQUEST_P_HPP_
|
Loading…
Reference in New Issue
Block a user