Go to the documentation of this file.00001
00027 #ifndef SOCKETREADER_H
00028 #define SOCKETREADER_H
00029
00030 #include <QObject>
00031 #include <QLocalSocket>
00032 #include <QVector>
00033
00041 class SocketReader : public QObject
00042 {
00043 Q_OBJECT
00044 Q_DISABLE_COPY(SocketReader)
00045
00046 public:
00047
00053 SocketReader(QObject* parent = 0);
00054
00058 ~SocketReader();
00059
00066 bool initiateConnection(int sessionId);
00067
00072 bool dropConnection();
00073
00080 QLocalSocket* socket();
00081
00092 bool read(void* buffer, int size);
00093
00102 template<typename T>
00103 bool read(QVector<T>& values);
00104
00110 bool isConnected();
00111
00112 private:
00117 static const char* channelIDString;
00118
00122 bool readSocketTag();
00123
00124 QLocalSocket* socket_;
00125 bool tagRead_;
00126 };
00127
00128 template<typename T>
00129 bool SocketReader::read(QVector<T>& values)
00130 {
00131 if (!socket_) {
00132 return false;
00133 }
00134
00135 unsigned int count;
00136 if(!read((void*)&count, sizeof(unsigned int)))
00137 {
00138 socket_->readAll();
00139 return false;
00140 }
00141 if(count > 1000)
00142 {
00143 qWarning() << "Too many samples waiting in socket. Flushing it to empty";
00144 socket_->readAll();
00145 return false;
00146 }
00147 values.resize(values.size() + count);
00148 if(!read((void*)values.data(), sizeof(T) * count))
00149 {
00150 qWarning() << "Error occured while reading data from socket: " << socket_->errorString();
00151 socket_->readAll();
00152 return false;
00153 }
00154 return true;
00155 }
00156
00157 #endif // SOCKETREADER_H