keep_alive_delegate.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/cast_channel/keep_alive_delegate.h"
  5. #include <string>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "components/cast_channel/cast_channel_enum.h"
  9. #include "components/cast_channel/cast_socket.h"
  10. #include "components/cast_channel/logger.h"
  11. #include "net/base/net_errors.h"
  12. #include "net/traffic_annotation/network_traffic_annotation.h"
  13. #include "third_party/openscreen/src/cast/common/channel/proto/cast_channel.pb.h"
  14. namespace cast_channel {
  15. KeepAliveDelegate::KeepAliveDelegate(
  16. CastSocket* socket,
  17. scoped_refptr<Logger> logger,
  18. std::unique_ptr<CastTransport::Delegate> inner_delegate,
  19. base::TimeDelta ping_interval,
  20. base::TimeDelta liveness_timeout)
  21. : inner_delegate_(std::move(inner_delegate)),
  22. handler_(socket,
  23. std::move(logger),
  24. ping_interval,
  25. liveness_timeout,
  26. base::BindRepeating(&KeepAliveDelegate::OnError,
  27. base::Unretained(this))) {
  28. DCHECK(inner_delegate_);
  29. }
  30. KeepAliveDelegate::~KeepAliveDelegate() = default;
  31. void KeepAliveDelegate::SetTimersForTest(
  32. std::unique_ptr<base::RetainingOneShotTimer> injected_ping_timer,
  33. std::unique_ptr<base::RetainingOneShotTimer> injected_liveness_timer) {
  34. handler_.SetTimersForTest(std::move(injected_ping_timer),
  35. std::move(injected_liveness_timer));
  36. }
  37. // CastTransport::Delegate interface.
  38. void KeepAliveDelegate::Start() {
  39. handler_.Start();
  40. inner_delegate_->Start();
  41. }
  42. void KeepAliveDelegate::OnError(ChannelError error_state) {
  43. DVLOG(1) << "KeepAlive::OnError: "
  44. << ::cast_channel::ChannelErrorToString(error_state);
  45. inner_delegate_->OnError(error_state);
  46. handler_.Stop();
  47. }
  48. void KeepAliveDelegate::OnMessage(const CastMessage& message) {
  49. DVLOG(2) << "KeepAlive::OnMessage : " << message.payload_utf8();
  50. if (!handler_.HandleMessage(message)) {
  51. inner_delegate_->OnMessage(message);
  52. }
  53. }
  54. } // namespace cast_channel