#ifndef CORE_INTERNAL_BASE_ENDPOINT_CHANNEL_H_ #define CORE_INTERNAL_BASE_ENDPOINT_CHANNEL_H_ #include #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" #include "absl/strings/string_view.h" namespace location { namespace nearby { namespace connections { class BaseEndpointChannel : public EndpointChannel { public: BaseEndpointChannel(absl::string_view channel_name, Ptr reader, Ptr writer); ~BaseEndpointChannel() override; ExceptionOr > read() override; Exception::Value write(ConstPtr 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 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 constexpr std::int32_t kMaxAllowedReadBytes = 1048576; // 1MB bool isEncryptionEnabled(); void unblockPausedWriter(); void blockUntilUnpaused(); volatile std::int64_t last_read_timestamp_; const string channel_name_; ScopedPtr > system_clock_; // The reader and writer are synchronized independently since we can't have // writes waiting on reads that might potentially block forever. ScopedPtr > reader_lock_; // Not owned by this class, see the note in the destructor for a special // restriction on usage. Ptr reader_; ScopedPtr > writer_lock_; // Not owned by this class, see the note in the destructor for a special // restriction on usage. Ptr writer_; // An encryptor/decryptor. May be null. ScopedPtr > > > encryption_context_; ScopedPtr > is_paused_lock_; ScopedPtr > is_paused_condition_variable_; // If true, writes should block until this has been set to false. ScopedPtr > is_paused_; }; } // namespace connections } // namespace nearby } // namespace location #endif // CORE_INTERNAL_BASE_ENDPOINT_CHANNEL_H_