@@ -17,21 +17,18 @@ ProtoConnection::ProtoConnection(const std::string & a) :
17
17
{
18
18
throw std::runtime_error (QString (" Wrong address: unable to parse address (%1)" ).arg (address).toStdString ());
19
19
}
20
+ _host = parts[0 ];
20
21
21
22
bool ok;
22
- uint16_t port = parts[1 ].toUShort (&ok);
23
+ _port = parts[1 ].toUShort (&ok);
23
24
if (!ok)
24
25
{
25
26
throw std::runtime_error (QString (" Wrong address: Unable to parse the port number (%1)" ).arg (parts[1 ]).toStdString ());
26
27
}
27
28
28
- _socket.connectToHost (parts[0 ], port);
29
- if (!_socket.waitForConnected ())
30
- {
31
- throw std::runtime_error (" Unable to connect to host" );
32
- }
33
-
34
- std::cout << " Connected to " << a << std::endl;
29
+ // try to connect to host
30
+ std::cout << " Connecting to Hyperion: " << _host.toStdString () << " :" << _port << std::endl;
31
+ connectToHost ();
35
32
}
36
33
37
34
ProtoConnection::~ProtoConnection ()
@@ -54,10 +51,7 @@ void ProtoConnection::setColor(const ColorRgb & color, int priority, int duratio
54
51
colorRequest->set_duration (duration);
55
52
56
53
// send command message
57
- proto::HyperionReply reply = sendMessage (request);
58
-
59
- // parse reply message
60
- parseReply (reply);
54
+ sendMessage (request);
61
55
}
62
56
63
57
void ProtoConnection::setImage (const Image<ColorRgb> &image, int priority, int duration)
@@ -72,10 +66,7 @@ void ProtoConnection::setImage(const Image<ColorRgb> &image, int priority, int d
72
66
imageRequest->set_duration (duration);
73
67
74
68
// send command message
75
- proto::HyperionReply reply = sendMessage (request);
76
-
77
- // parse reply message
78
- // parseReply(reply);
69
+ sendMessage (request);
79
70
}
80
71
81
72
void ProtoConnection::clear (int priority)
@@ -86,10 +77,7 @@ void ProtoConnection::clear(int priority)
86
77
clearRequest->set_priority (priority);
87
78
88
79
// send command message
89
- proto::HyperionReply reply = sendMessage (request);
90
-
91
- // parse reply message
92
- parseReply (reply);
80
+ sendMessage (request);
93
81
}
94
82
95
83
void ProtoConnection::clearAll ()
@@ -98,14 +86,32 @@ void ProtoConnection::clearAll()
98
86
request.set_command (proto::HyperionRequest::CLEARALL);
99
87
100
88
// send command message
101
- proto::HyperionReply reply = sendMessage (request);
89
+ sendMessage (request);
90
+ }
102
91
103
- // parse reply message
104
- parseReply (reply);
92
+ void ProtoConnection::connectToHost ()
93
+ {
94
+ _socket.connectToHost (_host, _port);
95
+ if (_socket.waitForConnected ()) {
96
+ std::cout << " Connected to Hyperion host" << std::endl;
97
+ }
105
98
}
106
99
107
- proto::HyperionReply ProtoConnection::sendMessage (const proto::HyperionRequest &message)
100
+ void ProtoConnection::sendMessage (const proto::HyperionRequest &message)
108
101
{
102
+ if (_socket.state () == QAbstractSocket::UnconnectedState)
103
+ {
104
+ std::cout << " Currently disconnected: trying to connect to host" << std::endl;
105
+ connectToHost ();
106
+ }
107
+
108
+ if (_socket.state () != QAbstractSocket::ConnectedState)
109
+ {
110
+ return ;
111
+ }
112
+
113
+ // We only get here if we are connected
114
+
109
115
// serialize message (FastWriter already appends a newline)
110
116
std::string serializedMessage = message.SerializeAsString ();
111
117
@@ -122,41 +128,44 @@ proto::HyperionReply ProtoConnection::sendMessage(const proto::HyperionRequest &
122
128
count += _socket.write (reinterpret_cast <const char *>(serializedMessage.data ()), length);
123
129
if (!_socket.waitForBytesWritten ())
124
130
{
125
- throw std::runtime_error (" Error while writing data to host" );
131
+ std::cerr << " Error while writing data to host" << std::endl;
132
+ return ;
126
133
}
127
134
128
- // read reply data
129
- QByteArray serializedReply;
130
- length = -1 ;
131
- while (length < 0 && serializedReply.size () < length+4 )
135
+ if (!_skipReply)
132
136
{
133
- // receive reply
134
- if (!_socket.waitForReadyRead ())
137
+ // read reply data
138
+ QByteArray serializedReply;
139
+ length = -1 ;
140
+ while (length < 0 && serializedReply.size () < length+4 )
135
141
{
136
- throw std::runtime_error (" Error while reading data from host" );
142
+ // receive reply
143
+ if (!_socket.waitForReadyRead ())
144
+ {
145
+ std::cerr << " Error while reading data from host" << std::endl;
146
+ return ;
147
+ }
148
+
149
+ serializedReply += _socket.readAll ();
150
+
151
+ if (length < 0 && serializedReply.size () >= 4 )
152
+ {
153
+ // read the message size
154
+ length =
155
+ ((serializedReply[0 ]<<24 ) & 0xFF000000 ) |
156
+ ((serializedReply[1 ]<<16 ) & 0x00FF0000 ) |
157
+ ((serializedReply[2 ]<< 8 ) & 0x0000FF00 ) |
158
+ ((serializedReply[3 ] ) & 0x000000FF );
159
+ }
137
160
}
138
161
139
- serializedReply += _socket.readAll ();
162
+ // parse reply data
163
+ proto::HyperionReply reply;
164
+ reply.ParseFromArray (serializedReply.constData ()+4 , length);
140
165
141
- if (length < 0 && serializedReply.size () >= 4 )
142
- {
143
- // read the message size
144
- length =
145
- ((serializedReply[0 ]<<24 ) & 0xFF000000 ) |
146
- ((serializedReply[1 ]<<16 ) & 0x00FF0000 ) |
147
- ((serializedReply[2 ]<< 8 ) & 0x0000FF00 ) |
148
- ((serializedReply[3 ] ) & 0x000000FF );
149
- }
166
+ // parse reply message
167
+ parseReply (reply);
150
168
}
151
-
152
- // parse reply data
153
- proto::HyperionReply reply;
154
- reply.ParseFromArray (serializedReply.constData ()+4 , length);
155
-
156
- // remove data from receive buffer
157
- serializedReply = serializedReply.mid (length+4 );
158
-
159
- return reply;
160
169
}
161
170
162
171
bool ProtoConnection::parseReply (const proto::HyperionReply &reply)
0 commit comments