|
@@ -1,5 +1,5 @@
|
|
/**
|
|
/**
|
|
- * Copyright (c) 2019 Paul-Louis Ageneau
|
|
|
|
|
|
+ * Copyright (c) 2019-2022 Paul-Louis Ageneau
|
|
*
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
@@ -34,62 +34,25 @@ public:
|
|
enum class State { Disconnected, Connecting, Connected, Completed, Failed };
|
|
enum class State { Disconnected, Connecting, Connected, Completed, Failed };
|
|
using state_callback = std::function<void(State state)>;
|
|
using state_callback = std::function<void(State state)>;
|
|
|
|
|
|
- Transport(shared_ptr<Transport> lower = nullptr, state_callback callback = nullptr)
|
|
|
|
- : mLower(std::move(lower)), mStateChangeCallback(std::move(callback)) {}
|
|
|
|
|
|
+ Transport(shared_ptr<Transport> lower = nullptr, state_callback callback = nullptr);
|
|
|
|
+ virtual ~Transport();
|
|
|
|
|
|
- virtual ~Transport() { stop(); }
|
|
|
|
|
|
+ virtual void start();
|
|
|
|
+ virtual bool stop();
|
|
|
|
|
|
- virtual void start() { mStopped = false; }
|
|
|
|
|
|
+ void registerIncoming();
|
|
|
|
+ State state() const;
|
|
|
|
|
|
- virtual bool stop() {
|
|
|
|
- if (mStopped.exchange(true))
|
|
|
|
- return false;
|
|
|
|
|
|
+ void onRecv(message_callback callback);
|
|
|
|
+ void onStateChange(state_callback callback);
|
|
|
|
|
|
- // We don't want incoming() to be called by the lower layer anymore
|
|
|
|
- if (mLower) {
|
|
|
|
- PLOG_VERBOSE << "Unregistering incoming callback";
|
|
|
|
- mLower->onRecv(nullptr);
|
|
|
|
- }
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- void registerIncoming() {
|
|
|
|
- if (mLower) {
|
|
|
|
- PLOG_VERBOSE << "Registering incoming callback";
|
|
|
|
- mLower->onRecv(std::bind(&Transport::incoming, this, std::placeholders::_1));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- void onRecv(message_callback callback) { mRecvCallback = std::move(callback); }
|
|
|
|
- void onStateChange(state_callback callback) { mStateChangeCallback = std::move(callback); }
|
|
|
|
- State state() const { return mState; }
|
|
|
|
-
|
|
|
|
- virtual bool send(message_ptr message) { return outgoing(message); }
|
|
|
|
|
|
+ virtual bool send(message_ptr message);
|
|
|
|
|
|
protected:
|
|
protected:
|
|
- void recv(message_ptr message) {
|
|
|
|
- try {
|
|
|
|
- mRecvCallback(message);
|
|
|
|
- } catch (const std::exception &e) {
|
|
|
|
- PLOG_WARNING << e.what();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- void changeState(State state) {
|
|
|
|
- try {
|
|
|
|
- if (mState.exchange(state) != state)
|
|
|
|
- mStateChangeCallback(state);
|
|
|
|
- } catch (const std::exception &e) {
|
|
|
|
- PLOG_WARNING << e.what();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- virtual void incoming(message_ptr message) { recv(message); }
|
|
|
|
- virtual bool outgoing(message_ptr message) {
|
|
|
|
- if (mLower)
|
|
|
|
- return mLower->send(message);
|
|
|
|
- else
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ void recv(message_ptr message);
|
|
|
|
+ void changeState(State state);
|
|
|
|
+ virtual void incoming(message_ptr message);
|
|
|
|
+ virtual bool outgoing(message_ptr message);
|
|
|
|
|
|
private:
|
|
private:
|
|
const shared_ptr<Transport> mLower;
|
|
const shared_ptr<Transport> mLower;
|