Skip to content

Commit 892701d

Browse files
committed
Throw on failed connection to server in stream ctor; refactor test deflect servers
1 parent f54270f commit 892701d

17 files changed

+540
-451
lines changed

deflect/Observer.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
/*********************************************************************/
4141

4242
#include "Observer.h"
43+
#include "NetworkProtocol.h"
4344
#include "StreamPrivate.h"
4445

4546
#include <QDataStream>
@@ -49,8 +50,10 @@
4950

5051
namespace deflect
5152
{
52-
Observer::Observer()
53-
: _impl(new StreamPrivate("", "", Socket::defaultPortNumber, true))
53+
const unsigned short Observer::defaultPortNumber = DEFAULT_PORT_NUMBER;
54+
55+
Observer::Observer(const unsigned short port)
56+
: _impl(new StreamPrivate("", "", port, true))
5457
{
5558
}
5659

deflect/Observer.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,21 @@ class StreamPrivate;
6969
class Observer
7070
{
7171
public:
72+
/** The default communication port */
73+
static const unsigned short defaultPortNumber;
74+
7275
/**
7376
* Open a new connection to the Server using environment variables.
7477
*
7578
* DEFLECT_HOST The address of the target Server instance (required).
7679
* DEFLECT_ID The identifier for the stream. If not provided, a random
7780
* unique identifier will be used.
78-
* @throw std::runtime_error if DEFLECT_HOST was not provided.
81+
* @param port Port of the Server instance, default 1701.
82+
* @throw std::runtime_error if DEFLECT_HOST was not provided or no
83+
* connection to server could be established
7984
* @version 1.3
8085
*/
81-
DEFLECT_API Observer();
86+
DEFLECT_API explicit Observer(unsigned short port = defaultPortNumber);
8287

8388
/**
8489
* Open a new connection to the Server.
@@ -97,7 +102,8 @@ class Observer
97102
* "192.168.1.83". If left empty, the environment variable
98103
* DEFLECT_HOST will be used instead.
99104
* @param port Port of the Server instance, default 1701.
100-
* @throw std::runtime_error if no host was provided.
105+
* @throw std::runtime_error if no host was provided or no
106+
* connection to server could be established
101107
* @version 1.0
102108
*/
103109
DEFLECT_API Observer(const std::string& id, const std::string& host,

deflect/Socket.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ const int RECEIVE_TIMEOUT_MS = 1000;
5757

5858
namespace deflect
5959
{
60-
const unsigned short Socket::defaultPortNumber = DEFAULT_PORT_NUMBER;
61-
6260
Socket::Socket(const std::string& host, const unsigned short port)
6361
: _host(host)
6462
, _socket(new QTcpSocket(this)) // Ensure that _socket parent is

deflect/Socket.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,12 @@ class Socket : public QObject
6666
Q_OBJECT
6767

6868
public:
69-
/** The default communication port */
70-
static const unsigned short defaultPortNumber;
71-
7269
/**
7370
* Construct a Socket and connect to host.
7471
* @param host The target host (IP address or hostname)
7572
* @param port The target port
7673
*/
77-
DEFLECT_API Socket(const std::string& host,
78-
unsigned short port = defaultPortNumber);
74+
DEFLECT_API Socket(const std::string& host, unsigned short port);
7975

8076
/** Destruct a Socket, disconnecting from host. */
8177
DEFLECT_API ~Socket() = default;

deflect/Stream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444

4545
namespace deflect
4646
{
47-
Stream::Stream()
48-
: Observer(new StreamPrivate("", "", Socket::defaultPortNumber, false))
47+
Stream::Stream(const unsigned short port)
48+
: Observer(new StreamPrivate("", "", port, false))
4949
{
5050
}
5151

deflect/Stream.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ class Stream : public Observer
6868
* DEFLECT_HOST The address of the target Server instance (required).
6969
* DEFLECT_ID The identifier for the stream. If not provided, a random
7070
* unique identifier will be used.
71-
* @throw std::runtime_error if DEFLECT_HOST was not provided.
71+
* @param port Port of the Server instance, default 1701.
72+
* @throw std::runtime_error if DEFLECT_HOST was not provided or no
73+
* connection to server could be established
7274
* @version 1.3
7375
*/
74-
DEFLECT_API Stream();
76+
DEFLECT_API explicit Stream(unsigned short port = defaultPortNumber);
7577

7678
/**
7779
* Open a new connection to the Server.
@@ -91,7 +93,8 @@ class Stream : public Observer
9193
* "192.168.1.83". If left empty, the environment variable
9294
* DEFLECT_HOST will be used instead.
9395
* @param port Port of the Server instance, default 1701.
94-
* @throw std::runtime_error if no host was provided.
96+
* @throw std::runtime_error if no host was provided or no
97+
* connection to server could be established
9598
* @version 1.0
9699
*/
97100
DEFLECT_API Stream(const std::string& id, const std::string& host,

deflect/StreamPrivate.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ StreamPrivate::StreamPrivate(const std::string& id_, const std::string& host,
8686
, sendWorker{socket, id}
8787
{
8888
if (!socket.isConnected())
89-
return;
89+
throw std::runtime_error(
90+
"Connection to deflect server could not be established");
9091

9192
socket.connect(&socket, &Socket::disconnected, [this]() {
9293
if (disconnectedCallback)

deflect/StreamSendWorker.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,15 @@ Stream::Future StreamSendWorker::enqueueImage(const ImageWrapper& image,
167167
{
168168
if (_pendingFinish)
169169
{
170-
return make_exception_future<bool>(std::make_exception_ptr(
171-
std::invalid_argument("Pending finish, no send allowed")));
170+
return make_exception_future<bool>(
171+
std::invalid_argument("Pending finish, no send allowed"));
172172
}
173173

174174
if (image.compressionPolicy != COMPRESSION_ON && image.pixelFormat != RGBA)
175175
{
176-
return make_exception_future<bool>(
177-
std::make_exception_ptr(std::invalid_argument(
178-
"Currently, RAW images can only be sent in RGBA format. Other "
179-
"formats support remain to be implemented.")));
176+
return make_exception_future<bool>(std::invalid_argument(
177+
"Currently, RAW images can only be sent in RGBA format. Other "
178+
"formats support remain to be implemented."));
180179
}
181180

182181
if (image.compressionPolicy == COMPRESSION_ON)
@@ -187,7 +186,7 @@ Stream::Future StreamSendWorker::enqueueImage(const ImageWrapper& image,
187186
msg << "JPEG compression quality must be between 1 and 100, got "
188187
<< image.compressionQuality << std::endl;
189188
return make_exception_future<bool>(
190-
std::make_exception_ptr(std::invalid_argument(msg.str())));
189+
std::invalid_argument(msg.str()));
191190
}
192191
}
193192

deflect/types.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,17 @@ std::future<T> make_ready_future(T&& value)
8282
}
8383

8484
template <typename T>
85-
std::future<T> make_exception_future(std::exception_ptr exc)
85+
std::future<T> make_exception_future(std::exception_ptr&& e)
8686
{
8787
std::promise<T> promise;
88-
promise.set_exception(exc);
88+
promise.set_exception(std::move(e));
8989
return promise.get_future();
9090
}
91+
template <typename T, typename Exception>
92+
std::future<T> make_exception_future(Exception&& e)
93+
{
94+
return make_exception_future<T>(std::make_exception_ptr(std::move(e)));
95+
}
9196

9297
class EventReceiver;
9398
class Frame;

0 commit comments

Comments
 (0)