Files
nearby/cpp/core/internal/base_endpoint_channel.h
T
Alexey Polyudov 204f76077d nearby: snapshot as of cl/296436629
Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I2cf5bf225b76f4c1541954651f3a7544a14e0cec
2020-04-04 12:52:31 -07:00

113 lines
3.6 KiB
C++

#ifndef CORE_INTERNAL_BASE_ENDPOINT_CHANNEL_H_
#define CORE_INTERNAL_BASE_ENDPOINT_CHANNEL_H_
#include <cstdint>
#include "core/internal/endpoint_channel.h"
#include "platform/api/atomic_boolean.h"
#include "platform/api/atomic_reference.h"
#include "platform/api/condition_variable.h"
#include "platform/api/input_stream.h"
#include "platform/api/lock.h"
#include "platform/api/output_stream.h"
#include "platform/api/system_clock.h"
#include "platform/byte_array.h"
#include "platform/port/string.h"
#include "platform/ptr.h"
#include "proto/connections_enums.pb.h"
#include "securegcm/d2d_connection_context_v1.h"
namespace location {
namespace nearby {
namespace connections {
template <typename Platform>
class BaseEndpointChannel : public EndpointChannel {
public:
BaseEndpointChannel(const string& channel_name, Ptr<InputStream> reader,
Ptr<OutputStream> writer);
~BaseEndpointChannel() override;
ExceptionOr<ConstPtr<ByteArray> > read() override;
Exception::Value write(ConstPtr<ByteArray> data) override;
// Closes this EndpointChannel, without tracking the closure in analytics.
void close() override;
// Closes this EndpointChannel and records the closure with the given reason.
void close(proto::connections::DisconnectionReason reason) override;
// Returns a one-word type descriptor for the concrete EndpointChannel
// implementation that can be used in log messages; eg: BLUETOOTH, BLE,
// WIFI.
string getType() override;
// Returns the name of the EndpointChannel.
string getName() override;
// Enables encryption on the EndpointChannel.
void enableEncryption(
Ptr<securegcm::D2DConnectionContextV1> encryption_context) override;
// True if the EndpointChannel is currently pausing all writes.
bool isPaused() override;
// Pauses all writes on this EndpointChannel until resume() is called.
void pause() override;
// Resumes any writes on this EndpointChannel that were suspended when pause()
// was called.
void resume() override;
// Returns the timestamp (in elapsedRealtime) of the last read from this
// endpoint, or -1 if no reads have occurred.
std::int64_t getLastReadTimestamp() override;
protected:
virtual void closeImpl() = 0;
private:
// Used to sanity check that our frame sizes are reasonable.
static const std::int32_t kMaxAllowedReadBytes = 1048576; // 1MB
bool isEncryptionEnabled();
void unblockPausedWriter();
void blockUntilUnpaused();
volatile std::int64_t last_read_timestamp_;
const string channel_name_;
ScopedPtr<Ptr<SystemClock> > system_clock_;
// The reader and writer are synchronized independently since we can't have
// writes waiting on reads that might potentially block forever.
ScopedPtr<Ptr<Lock> > reader_lock_;
// Not owned by this class, see the note in the destructor for a special
// restriction on usage.
Ptr<InputStream> reader_;
ScopedPtr<Ptr<Lock> > writer_lock_;
// Not owned by this class, see the note in the destructor for a special
// restriction on usage.
Ptr<OutputStream> writer_;
// An encryptor/decryptor. May be null.
ScopedPtr<Ptr<AtomicReference<Ptr<securegcm::D2DConnectionContextV1> > > >
encryption_context_;
ScopedPtr<Ptr<Lock> > is_paused_lock_;
ScopedPtr<Ptr<ConditionVariable> > is_paused_condition_variable_;
// If true, writes should block until this has been set to false.
ScopedPtr<Ptr<AtomicBoolean> > is_paused_;
};
} // namespace connections
} // namespace nearby
} // namespace location
#include "core/internal/base_endpoint_channel.cc"
#endif // CORE_INTERNAL_BASE_ENDPOINT_CHANNEL_H_