main_loop.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <array>
  17. #include <cstdint>
  18. #include <cstring>
  19. #include <memory>
  20. #include <utility>
  21. #include "android-base/logging.h"
  22. #include "wifilogd/main_loop.h"
  23. #include "wifilogd/protocol.h"
  24. namespace android {
  25. namespace wifilogd {
  26. namespace {
  27. constexpr auto kMainBufferSizeBytes = 128 * 1024;
  28. // TODO(b/32840641): Tune the sleep time.
  29. constexpr auto kTransientErrorSleepTimeNsec = 100 * 1000; // 100 usec
  30. }
  31. MainLoop::MainLoop(const std::string& socket_name)
  32. : MainLoop(socket_name, std::make_unique<Os>(),
  33. std::make_unique<CommandProcessor>(kMainBufferSizeBytes)) {}
  34. MainLoop::MainLoop(const std::string& socket_name, std::unique_ptr<Os> os,
  35. std::unique_ptr<CommandProcessor> command_processor)
  36. : os_(std::move(os)), command_processor_(std::move(command_processor)) {
  37. Os::Errno err;
  38. std::tie(sock_fd_, err) = os_->GetControlSocket(socket_name);
  39. if (err) {
  40. PLOG(FATAL) << "Failed to get control socket";
  41. }
  42. }
  43. void MainLoop::RunOnce() {
  44. std::array<uint8_t, protocol::kMaxMessageSize> input_buf;
  45. size_t datagram_len;
  46. Os::Errno err;
  47. std::tie(datagram_len, err) =
  48. os_->ReceiveDatagram(sock_fd_, input_buf.data(), input_buf.size());
  49. if (err) {
  50. ProcessError(err);
  51. return;
  52. }
  53. if (datagram_len > protocol::kMaxMessageSize) {
  54. // TODO(b/32098735): Increment stats counter.
  55. datagram_len = protocol::kMaxMessageSize;
  56. }
  57. command_processor_->ProcessCommand(input_buf.data(), datagram_len,
  58. Os::kInvalidFd);
  59. }
  60. // Private methods below.
  61. void MainLoop::ProcessError(Os::Errno err) {
  62. if (err == EINTR || err == ENOMEM) {
  63. // TODO(b/32098735): Increment stats counter.
  64. os_->Nanosleep(kTransientErrorSleepTimeNsec);
  65. return;
  66. }
  67. // Any other error is unexpected, and assumed to be non-recoverable.
  68. // (If, e.g., our socket is in a bad state, then we won't be able to receive
  69. // any new log messages.)
  70. PLOG(FATAL) << "Unexpected error";
  71. }
  72. } // namespace wifilogd
  73. } // namespace android