sensorfw: qt-api/socketreader.h Source File

sensorfw

socketreader.h
Go to the documentation of this file.
1
27#ifndef SOCKETREADER_H
28#define SOCKETREADER_H
29
30#include <QObject>
31#include <QLocalSocket>
32#include <QVector>
33
41 class SocketReader : public QObject
42{
43 Q_OBJECT
44 Q_DISABLE_COPY(SocketReader)
45
46public:
47
53 SocketReader(QObject* parent = 0);
54
58 ~SocketReader();
59
66 bool initiateConnection(int sessionId);
67
72 bool dropConnection();
73
80 QLocalSocket* socket();
81
92 bool read(void* buffer, int size);
93
102 template<typename T>
103 bool read(QVector<T>& values);
104
110 bool isConnected();
111
112private:
117 static const char* channelIDString;
118
122 bool readSocketTag();
123
124 QLocalSocket* socket_;
125 bool tagRead_;
126};
127
128template<typename T>
129 bool SocketReader::read(QVector<T>& values)
130{
131 if (!socket_) {
132 return false;
133 }
134
135 unsigned int count;
136 if(!read((void*)&count, sizeof(unsigned int)))
137 {
138 socket_->readAll();
139 return false;
140 }
141 if(count > 1000)
142 {
143 qWarning() << "Too many samples waiting in socket. Flushing it to empty";
144 socket_->readAll();
145 return false;
146 }
147 values.resize(values.size() + count);
148 if(!read((void*)values.data(), sizeof(T) * count))
149 {
150 qWarning() << "Error occured while reading data from socket: " << socket_->errorString();
151 socket_->readAll();
152 return false;
153 }
154 return true;
155}
156
157#endif // SOCKETREADER_H
SocketReader
Helper class for reading socket datachannel from sensord.
Definition socketreader.h:42
SocketReader::SocketReader
SocketReader(QObject *parent=0)
Constructor.
SocketReader::~SocketReader
~SocketReader()
Destructor.
SocketReader::dropConnection
bool dropConnection()
Drops socket connection.
SocketReader::isConnected
bool isConnected()
Returns whether the socket is currently connected.
SocketReader::initiateConnection
bool initiateConnection(int sessionId)
Initiates new data socket connection.
SocketReader::socket
QLocalSocket * socket()
Provides access to the internal QLocalSocket for direct reading.
SocketReader::read
bool read(void *buffer, int size)
Attempt to read given number of bytes from the socket.