Compare commits

..

No commits in common. "36a2a1d2c60f3088ff5b36c3b32f80b648fe9f3b" and "cde0cf76a7df36501d5cb50e3861c12bfa40f3f2" have entirely different histories.

14 changed files with 202 additions and 181 deletions

1
.gitignore vendored
View File

@ -1,7 +1,6 @@
CMakeLists.txt.user*
_build
_output
.cmake/
*.autosave
files/lib/*
files/log/*

View File

@ -89,13 +89,10 @@ 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

View File

@ -30,17 +30,13 @@ 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

View File

@ -7,54 +7,72 @@
#include <myx/redis/client.hpp>
#endif
#include <myx/redis/client_p.hpp>
namespace myx {
namespace redis {
MYXLIB_INLINE Client::Client( QObject* parent ) :
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 ) :
QObject( parent ),
d ( new ClientPrivate( this ) )
{
}
MYXLIB_INLINE void Client::connectToHost( const QString& hostName, quint16 port )
void Client::connectToHost( const QString& hostName, quint16 port )
{
d->m_socket.connectToHost( hostName, port );
d->socket.connectToHost( hostName, port );
}
MYXLIB_INLINE void Client::disconnectFromHost()
void Client::disconnectFromHost()
{
d->m_socket.disconnectFromHost();
d->socket.disconnectFromHost();
}
MYXLIB_INLINE bool Client::isConnected() const
bool Client::isConnected() const
{
return( d->m_socket.state() == QAbstractSocket::ConnectedState );
return( d->socket.state() == QAbstractSocket::ConnectedState );
}
MYXLIB_INLINE Request* Client::sendCommand( const QByteArray& command )
Request* Client::sendCommand( const QByteArray& command )
{
d->m_socket.write( command + "\r\n" );
d->socket.write( command + "\r\n" );
auto* request = new Request( this );
d->m_queue.enqueue( request );
d->queue.enqueue( request );
return( request );
}
MYXLIB_INLINE bool Client::waitForConnected( int msecs )
bool Client::waitForConnected( int msecs )
{
return( d->m_socket.waitForConnected( msecs ) );
return( d->socket.waitForConnected( msecs ) );
}
MYXLIB_INLINE bool Client::waitForDisconnected( int msecs )
bool Client::waitForDisconnected( int msecs )
{
return( d->m_socket.waitForDisconnected( msecs ) );
return( d->socket.waitForDisconnected( msecs ) );
}
} // namespace redis

View File

@ -4,18 +4,17 @@
#pragma once
#include <myx/base/config.hpp>
#include <myx/redis/lexer.hpp>
#include <myx/redis/parser.hpp>
#include <myx/redis/request.hpp>
#include <QQueue>
#include <QObject>
#include <QScopedPointer>
#include <QTcpSocket>
namespace myx {
namespace redis {
QT_FORWARD_DECLARE_CLASS( ClientPrivate )
/**
* @brief Provides access to a Redis server
*/
@ -31,15 +30,10 @@ 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
*/
~Client() override = default;
virtual ~Client() = default;
/*
* Note: we specifically avoid an overload of connectToHost that
@ -111,33 +105,6 @@ 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
@ -150,4 +117,4 @@ private:
#endif
#endif // ifndef MYX_REDIS_CLIENT_HPP_
#endif // MYX_REDIS_CLIENT_HPP_

View File

@ -0,0 +1,38 @@
#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_

View File

@ -11,10 +11,10 @@ namespace myx {
namespace redis {
MYXLIB_INLINE Lexer::Lexer( QIODevice* device, QObject* parent ) :
Lexer::Lexer( QIODevice* device, QObject* parent ) :
QObject ( parent ),
m_device( device ),
m_state ( kDoingNothing ),
m_state ( DoingNothing ),
m_crlf ( 0 ),
m_length( 0 )
{
@ -22,39 +22,39 @@ MYXLIB_INLINE Lexer::Lexer( QIODevice* device, QObject* parent ) :
}
MYXLIB_INLINE void Lexer::readData()
void Lexer::readData()
{
m_buffer.append( m_device->readAll() );
while ( true )
{
if ( ( m_state == kDoingNothing ) && !readCharacter() )
if ( ( m_state == DoingNothing ) && !readCharacter() )
{
break;
}
switch ( m_state )
{
case kReadingLength:
case kReadingUnsafeString:
case ReadingLength:
case ReadingUnsafeString:
if ( !readUnsafeString() ) { return; }
break;
case kReadingSafeString:
case ReadingSafeString:
if ( !readSafeString() ) { return; }
break;
case kDoingNothing:
case DoingNothing:
break;
}
if ( m_state != kReadingSafeString )
if ( m_state != ReadingSafeString )
{
m_state = kDoingNothing;
m_state = DoingNothing;
}
}
} // Lexer::readData
MYXLIB_INLINE bool Lexer::readCharacter()
bool Lexer::readCharacter()
{
if ( m_buffer.isEmpty() )
{
@ -70,9 +70,9 @@ MYXLIB_INLINE bool Lexer::readCharacter()
case '-':
case ':':
case '*':
m_state = kReadingUnsafeString; break;
m_state = ReadingUnsafeString; break;
case '$':
m_state = kReadingLength; break;
m_state = ReadingLength; break;
}
Q_EMIT character( c );
@ -80,7 +80,7 @@ MYXLIB_INLINE bool Lexer::readCharacter()
} // Lexer::readCharacter
MYXLIB_INLINE bool Lexer::readUnsafeString()
bool Lexer::readUnsafeString()
{
m_crlf = m_buffer.indexOf( "\r\n", m_crlf );
if ( m_crlf == -1 )
@ -92,10 +92,10 @@ MYXLIB_INLINE bool Lexer::readUnsafeString()
QString s = m_buffer.mid( 0, m_crlf );
m_buffer.remove( 0, m_crlf + 2 );
if ( m_state == kReadingLength )
if ( m_state == ReadingLength )
{
m_length = s.toInt();
m_state = kReadingSafeString;
m_state = ReadingSafeString;
}
else
{
@ -107,7 +107,7 @@ MYXLIB_INLINE bool Lexer::readUnsafeString()
} // Lexer::readUnsafeString
MYXLIB_INLINE bool Lexer::readSafeString()
bool Lexer::readSafeString()
{
if ( m_buffer.size() - m_length < 2 )
{
@ -119,7 +119,7 @@ MYXLIB_INLINE bool Lexer::readSafeString()
Q_EMIT safeString( d );
m_state = kDoingNothing;
m_state = DoingNothing;
return( true );
}

View File

@ -17,15 +17,8 @@ class Lexer : public QObject
public:
explicit Lexer( QIODevice*, QObject* = nullptr );
Lexer( const Lexer& ) = delete;
Lexer& operator=( const Lexer& ) = delete;
Lexer( Lexer&& ) = delete;
Lexer& operator=( Lexer&& ) = delete;
~Lexer() override = default;
Lexer( QIODevice*, QObject* = nullptr );
virtual ~Lexer() = default;
Q_SIGNAL void character( char );
Q_SIGNAL void unsafeString( const QString& );
@ -35,6 +28,8 @@ private:
Q_SLOT void readData();
private:
bool readCharacter();
bool readLength();
bool readUnsafeString();
@ -45,10 +40,10 @@ private:
enum
{
kDoingNothing,
kReadingLength,
kReadingUnsafeString,
kReadingSafeString
DoingNothing,
ReadingLength,
ReadingUnsafeString,
ReadingSafeString
} m_state;
int m_crlf;
@ -64,4 +59,4 @@ private:
#endif
#endif // ifndef MYX_REDIS_LEXER_HPP_
#endif // MYX_REDIS_LEXER_HPP_

View File

@ -11,7 +11,7 @@ namespace myx {
namespace redis {
MYXLIB_INLINE Parser::Parser( Lexer* lexer, QObject* parent ) :
Parser::Parser( Lexer* lexer, QObject* parent ) :
QObject( parent )
{
connect( lexer, &Lexer::character, this, &Parser::readCharacter );
@ -20,67 +20,67 @@ MYXLIB_INLINE Parser::Parser( Lexer* lexer, QObject* parent ) :
}
MYXLIB_INLINE void Parser::readCharacter( const char c )
void Parser::readCharacter( const char c )
{
switch ( c )
{
case '+':
m_stack.append( Task( Reply::kStatus ) ); break;
stack.append( Task( Reply::Status ) ); break;
case '-':
m_stack.append( Task( Reply::kError ) ); break;
stack.append( Task( Reply::Error ) ); break;
case ':':
m_stack.append( Task( Reply::kInteger ) ); break;
stack.append( Task( Reply::Integer ) ); break;
case '$':
m_stack.append( Task( Reply::kBulk ) ); break;
stack.append( Task( Reply::Bulk ) ); break;
case '*':
m_stack.append( Task( Reply::kMultiBulk ) ); break;
stack.append( Task( Reply::MultiBulk ) ); break;
default:
break;
}
}
MYXLIB_INLINE void Parser::readUnsafeString( const QString& value )
void Parser::readUnsafeString( const QString& value )
{
if ( tos().m_reply.type() == Reply::kMultiBulk )
if ( tos().reply.type() == Reply::MultiBulk )
{
tos().m_count = value.toInt();
tos().count = value.toInt();
}
else
{
tos().m_reply.value() = value;
tos().reply.value() = value;
}
descend();
}
MYXLIB_INLINE void Parser::readSafeString( const QByteArray& value )
void Parser::readSafeString( const QByteArray& value )
{
tos().m_reply.value() = value;
tos().reply.value() = value;
descend();
}
MYXLIB_INLINE void Parser::descend()
void Parser::descend()
{
while ( true )
{
if ( ( tos().m_reply.type() == Reply::kMultiBulk ) &&
( tos().m_reply.value().toList().count() < tos().m_count ) )
if ( ( tos().reply.type() == Reply::MultiBulk ) &&
( tos().reply.value().toList().count() < tos().count ) )
{
return;
}
if ( m_stack.count() == 1 )
if ( stack.count() == 1 )
{
auto r = m_stack.takeLast().m_reply;
auto r = stack.takeLast().reply;
Q_EMIT reply( r );
return;
}
auto r = m_stack.takeLast().m_reply;
tos().m_reply.value().toList().append( QVariant::fromValue( r ) );
auto r = stack.takeLast().reply;
tos().reply.value().toList().append( QVariant::fromValue( r ) );
}
}

View File

@ -4,9 +4,8 @@
#pragma once
#include <myx/base/config.hpp>
#include <myx/redis/lexer.hpp>
// #include <myx/redis/parser_p.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/lexer.hpp>
#include <QList>
#include <QPair>
@ -21,41 +20,36 @@ class Parser : public QObject
Q_OBJECT
public:
explicit Parser( Lexer*, QObject* = nullptr );
Parser( const Parser& ) = delete;
Parser& operator=( const Parser& ) = delete;
Parser( Parser&& ) = delete;
Parser& operator=( Parser&& ) = delete;
~Parser() override = default;
Parser( Lexer*, QObject* = nullptr );
virtual ~Parser() = default;
Q_SIGNAL void reply( const myx::redis::Reply& );
private:
Q_SLOT void readCharacter( char );
Q_SLOT void readCharacter( const char );
Q_SLOT void readUnsafeString( const QString& );
Q_SLOT void readSafeString( const QByteArray& );
private:
void descend();
class Task
{
friend class Parser;
public:
enum { kUnknown = -2 };
enum { Unknown = -2 };
explicit Task( Reply::Type type ) :
m_reply( type ),
m_count( kUnknown ) {}
Task( Reply::Type type ) :
reply( type ),
count( Unknown ) {}
Reply m_reply;
int m_count;
Reply reply;
int count;
};
QList< Task > m_stack;
QList< Task > stack;
Task& tos() { return( m_stack.last() ); }
Task& tos() { return( stack.last() ); }
}; // class Parser
} // namespace redis
@ -66,4 +60,4 @@ private:
#include "parser-inl.hpp"
#endif
#endif // ifndef MYX_REDIS_PARSER_HPP_
#endif // MYX_REDIS_PARSER_HPP_

View File

@ -28,7 +28,7 @@ public:
*
* This value is only set when the default constructor is used.
*/
kInvalid,
Invalid,
/**
* @brief A status reply
@ -36,7 +36,7 @@ public:
* The value property will contain the status message returned
* by the server as a QString.
*/
kStatus,
Status,
/**
* @brief An error reply
@ -44,7 +44,7 @@ public:
* The value property will contain the error message returned by
* the server as a QString.
*/
kError,
Error,
/**
* @brief An integer reply
@ -52,7 +52,7 @@ public:
* The value property will contain the integer value returned by
* the server as a qlonglong.
*/
kInteger,
Integer,
/**
* @brief A bulk reply
@ -60,7 +60,7 @@ public:
* The value property will contain the bulk reply returned by
* the server as a QByteArray.
*/
kBulk,
Bulk,
/**
* @brief A multi-bulk reply
@ -69,43 +69,44 @@ public:
* by the server as a QVariantList. Each entry in the list is of
* type Reply.
*/
kMultiBulk
MultiBulk
};
/**
* @brief Creates an empty reply
*/
Reply() = default;
Reply() :
_type( Invalid ) {}
/**
* @brief Initializes the reply
* @param type the type of the reply
*/
explicit Reply( Type type ) :
m_type( type ) {}
Reply( Type type ) :
_type( type ) {}
/**
* @brief Returns the type of the reply
* @return the reply type
*/
Type type() const { return( m_type ); }
Type type() const { return( _type ); }
/**
* @brief Returns a reference to the value of the reply
* @return the reply value
*/
QVariant& value() { return( m_value ); }
QVariant& value() { return( _value ); }
private:
Type m_type { kInvalid };
QVariant m_value;
}; // class Reply
Type _type;
QVariant _value;
}; // class MYX_REDIS_EXPORT Reply
} // namespace redis
} // namespace myx
Q_DECLARE_METATYPE( myx::redis::Reply ) // NOLINT
Q_DECLARE_METATYPE( myx::redis::Reply )
#endif // ifndef MYX_REDIS_REPLY_HPP_
#endif // MYX_REDIS_REPLY_HPP_

View File

@ -8,6 +8,7 @@
#ifndef MYXLIB_HEADER_ONLY
#include <myx/redis/request.hpp>
#endif
#include <myx/redis/request_p.hpp>
#include <QTimer>
@ -15,28 +16,34 @@ namespace myx {
namespace redis {
MYXLIB_INLINE Request::Request( QObject* parent ) :
void RequestPrivate::quitEventLoop()
{
loop.exit( 1 );
}
Request::Request( QObject* parent ) :
QObject( parent ),
d ( new Loop )
d ( new RequestPrivate )
{
connect( this, &Request::reply, this, &Request::deleteLater );
}
MYXLIB_INLINE bool Request::waitForReply( int msecs )
bool Request::waitForReply( int msecs )
{
QTimer timer;
timer.setInterval( msecs );
timer.setSingleShot( true );
connect( &timer, &QTimer::timeout, &d->m_loop, &QEventLoop::quit );
connect( this, &Request::reply, d.data(), &Loop::quitEventLoop );
connect( &timer, &QTimer::timeout, &d->loop, &QEventLoop::quit );
connect( this, &Request::reply, d.data(), &RequestPrivate::quitEventLoop );
/*
* If the timer fires, the return value will be 0.
* Otherwise, quitEventLoop() will terminate the loop with 1.
*/
return( ( d->m_loop.exec( QEventLoop::ExcludeUserInputEvents ) != 0 ) );
return( ( d->loop.exec( QEventLoop::ExcludeUserInputEvents ) != 0 ) );
}
} // namespace redis

View File

@ -6,14 +6,15 @@
#include <myx/base/config.hpp>
#include <myx/redis/reply.hpp>
#include <QEventLoop>
#include <QObject>
#include <QScopedPointer>
namespace myx {
namespace redis {
QT_FORWARD_DECLARE_CLASS( RequestPrivate );
/**
* @brief Represents a Redis command and its response
*/
@ -29,15 +30,10 @@ 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
*/
~Request() override = default;
virtual ~Request() = default;
/**
* @brief Waits for the reply to be received
@ -54,19 +50,7 @@ public:
private:
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;
const QScopedPointer< RequestPrivate > d;
}; // class Request
} // namespace redis
@ -77,4 +61,4 @@ private:
#include "request-inl.hpp"
#endif
#endif // ifndef MYX_REDIS_REQUEST_HPP_
#endif // MYX_REDIS_REQUEST_HPP_

View File

@ -0,0 +1,25 @@
#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_