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