diff options
Diffstat (limited to 'cast/streaming/environment.h')
-rw-r--r-- | cast/streaming/environment.h | 51 |
1 files changed, 43 insertions, 8 deletions
diff --git a/cast/streaming/environment.h b/cast/streaming/environment.h index 0ab9a399..606f408f 100644 --- a/cast/streaming/environment.h +++ b/cast/streaming/environment.h @@ -33,6 +33,36 @@ class Environment : public UdpSocket::Client { virtual ~PacketConsumer(); }; + // Consumers of the environment's UDP socket should be careful to check the + // socket's state before accessing its methods, especially + // GetBoundLocalEndpoint(). If the environment is |kStarting|, the + // local endpoint may not be set yet and will be zero initialized. + enum class SocketState { + // Socket is still initializing. Usually the UDP socket bind is + // the last piece. + kStarting, + + // The socket is ready for use and has been bound. + kReady, + + // The socket is either closed (normally or due to an error) or in an + // invalid state. Currently the environment does not create a new socket + // in this case, so to be used again the environment itself needs to be + // recreated. + kInvalid + }; + + // Classes concerned with the Environment's UDP socket state may inherit from + // |Subscriber| and then |Subscribe|. + class SocketSubscriber { + public: + // Event that occurs when the environment is ready for use. + virtual void OnSocketReady() = 0; + + // Event that occurs when the environment has experienced a fatal error. + virtual void OnSocketInvalid(Error error) = 0; + }; + // Construct with the given clock source and TaskRunner. Creates and // internally-owns a UdpSocket, and immediately binds it to the given // |local_endpoint|. If embedders do not care what interface/address the UDP @@ -54,12 +84,6 @@ class Environment : public UdpSocket::Client { // is a bound socket. virtual IPEndpoint GetBoundLocalEndpoint() const; - // Set a handler function to run whenever non-recoverable socket errors occur. - // If never set, the default is to emit log messages at error priority. - void set_socket_error_handler(std::function<void(Error)> handler) { - socket_error_handler_ = handler; - } - // Get/Set the remote endpoint. This is separate from the constructor because // the remote endpoint is, in some cases, discovered only after receiving a // packet. @@ -68,6 +92,15 @@ class Environment : public UdpSocket::Client { remote_endpoint_ = endpoint; } + // Returns the current state of the UDP socket. This method is virtual + // to allow tests to simulate socket state. + SocketState socket_state() const { return state_; } + void set_socket_state_for_testing(SocketState state) { state_ = state; } + + // Subscribe to socket changes. Callers can unsubscribe by passing + // nullptr. + void SetSocketSubscriber(SocketSubscriber* subscriber); + // Start/Resume delivery of incoming packets to the given |packet_consumer|. // Delivery will continue until DropIncomingPackets() is called. void ConsumeIncomingPackets(PacketConsumer* packet_consumer); @@ -97,20 +130,22 @@ class Environment : public UdpSocket::Client { private: // UdpSocket::Client implementation. + void OnBound(UdpSocket* socket) final; void OnError(UdpSocket* socket, Error error) final; void OnSendError(UdpSocket* socket, Error error) final; void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet_or_error) final; - // The UDP socket bound to the local endpoint that was passed into the // constructor, or null if socket creation failed. const std::unique_ptr<UdpSocket> socket_; // These are externally set/cleared. Behaviors are described in getter/setter // method comments above. - std::function<void(Error)> socket_error_handler_; + IPEndpoint local_endpoint_{}; IPEndpoint remote_endpoint_{}; PacketConsumer* packet_consumer_ = nullptr; + SocketState state_ = SocketState::kStarting; + SocketSubscriber* socket_subscriber_ = nullptr; }; } // namespace cast |