Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit d8dfc3f

Browse files
committed
Added logic to reconnect after connection failure;
Implemented option to stop receiving the proto replies;
1 parent b15c31b commit d8dfc3f

File tree

2 files changed

+70
-54
lines changed

2 files changed

+70
-54
lines changed

src/hyperion-v4l2/ProtoConnection.cpp

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,18 @@ ProtoConnection::ProtoConnection(const std::string & a) :
1717
{
1818
throw std::runtime_error(QString("Wrong address: unable to parse address (%1)").arg(address).toStdString());
1919
}
20+
_host = parts[0];
2021

2122
bool ok;
22-
uint16_t port = parts[1].toUShort(&ok);
23+
_port = parts[1].toUShort(&ok);
2324
if (!ok)
2425
{
2526
throw std::runtime_error(QString("Wrong address: Unable to parse the port number (%1)").arg(parts[1]).toStdString());
2627
}
2728

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();
3532
}
3633

3734
ProtoConnection::~ProtoConnection()
@@ -54,10 +51,7 @@ void ProtoConnection::setColor(const ColorRgb & color, int priority, int duratio
5451
colorRequest->set_duration(duration);
5552

5653
// send command message
57-
proto::HyperionReply reply = sendMessage(request);
58-
59-
// parse reply message
60-
parseReply(reply);
54+
sendMessage(request);
6155
}
6256

6357
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
7266
imageRequest->set_duration(duration);
7367

7468
// send command message
75-
proto::HyperionReply reply = sendMessage(request);
76-
77-
// parse reply message
78-
// parseReply(reply);
69+
sendMessage(request);
7970
}
8071

8172
void ProtoConnection::clear(int priority)
@@ -86,10 +77,7 @@ void ProtoConnection::clear(int priority)
8677
clearRequest->set_priority(priority);
8778

8879
// send command message
89-
proto::HyperionReply reply = sendMessage(request);
90-
91-
// parse reply message
92-
parseReply(reply);
80+
sendMessage(request);
9381
}
9482

9583
void ProtoConnection::clearAll()
@@ -98,14 +86,32 @@ void ProtoConnection::clearAll()
9886
request.set_command(proto::HyperionRequest::CLEARALL);
9987

10088
// send command message
101-
proto::HyperionReply reply = sendMessage(request);
89+
sendMessage(request);
90+
}
10291

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+
}
10598
}
10699

107-
proto::HyperionReply ProtoConnection::sendMessage(const proto::HyperionRequest &message)
100+
void ProtoConnection::sendMessage(const proto::HyperionRequest &message)
108101
{
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+
109115
// serialize message (FastWriter already appends a newline)
110116
std::string serializedMessage = message.SerializeAsString();
111117

@@ -122,41 +128,44 @@ proto::HyperionReply ProtoConnection::sendMessage(const proto::HyperionRequest &
122128
count += _socket.write(reinterpret_cast<const char *>(serializedMessage.data()), length);
123129
if (!_socket.waitForBytesWritten())
124130
{
125-
throw std::runtime_error("Error while writing data to host");
131+
std::cerr << "Error while writing data to host" << std::endl;
132+
return;
126133
}
127134

128-
// read reply data
129-
QByteArray serializedReply;
130-
length = -1;
131-
while (length < 0 && serializedReply.size() < length+4)
135+
if (!_skipReply)
132136
{
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)
135141
{
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+
}
137160
}
138161

139-
serializedReply += _socket.readAll();
162+
// parse reply data
163+
proto::HyperionReply reply;
164+
reply.ParseFromArray(serializedReply.constData()+4, length);
140165

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);
150168
}
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;
160169
}
161170

162171
bool ProtoConnection::parseReply(const proto::HyperionReply &reply)

src/hyperion-v4l2/ProtoConnection.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ class ProtoConnection
6868
void clearAll();
6969

7070
private:
71+
/// Try to connect to the Hyperion host
72+
void connectToHost();
73+
7174
///
7275
/// Send a command message and receive its reply
7376
///
7477
/// @param message The message to send
7578
///
76-
/// @return The returned reply
77-
///
78-
proto::HyperionReply sendMessage(const proto::HyperionRequest & message);
79+
void sendMessage(const proto::HyperionRequest & message);
7980

8081
///
8182
/// Parse a reply message
@@ -90,6 +91,12 @@ class ProtoConnection
9091
/// The TCP-Socket with the connection to the server
9192
QTcpSocket _socket;
9293

94+
/// Host address
95+
QString _host;
96+
97+
/// Host port
98+
uint16_t _port;
99+
93100
/// Skip receiving reply messages from Hyperion if set
94101
bool _skipReply;
95102
};

0 commit comments

Comments
 (0)