File: channel_manager.cpp

package info (click to toggle)
android-platform-frameworks-native 1%3A8.1.0%2Br23-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 20,612 kB
  • sloc: cpp: 199,842; xml: 48,803; ansic: 23,250; java: 5,012; python: 1,624; sh: 225; asm: 105; perl: 74; makefile: 22
file content (52 lines) | stat: -rw-r--r-- 1,631 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <uds/channel_manager.h>

#include <log/log.h>

namespace android {
namespace pdx {
namespace uds {

ChannelManager& ChannelManager::Get() {
  static ChannelManager instance;
  return instance;
}

void ChannelManager::CloseHandle(int32_t handle) {
  std::lock_guard<std::mutex> autolock(mutex_);
  auto channel = channels_.find(handle);
  if (channel == channels_.end()) {
    ALOGE("Invalid channel handle: %d", handle);
  } else {
    channels_.erase(channel);
  }
}

LocalChannelHandle ChannelManager::CreateHandle(LocalHandle data_fd,
                                                LocalHandle pollin_event_fd,
                                                LocalHandle pollhup_event_fd) {
  if (data_fd && pollin_event_fd && pollhup_event_fd) {
    std::lock_guard<std::mutex> autolock(mutex_);
    const int32_t handle = data_fd.Get();
    channels_.emplace(
        handle,
        ChannelEventReceiver{std::move(data_fd), std::move(pollin_event_fd),
                             std::move(pollhup_event_fd)});
    return LocalChannelHandle(this, handle);
  } else {
    ALOGE(
        "ChannelManager::CreateHandle: Invalid arguments: data_fd=%d "
        "pollin_event_fd=%d pollhup_event_fd=%d",
        data_fd.Get(), pollin_event_fd.Get(), pollhup_event_fd.Get());
    return LocalChannelHandle(nullptr, -1);
  }
}

ChannelEventReceiver* ChannelManager::GetChannelData(int32_t handle) {
  std::lock_guard<std::mutex> autolock(mutex_);
  auto channel = channels_.find(handle);
  return channel != channels_.end() ? &channel->second : nullptr;
}

}  // namespace uds
}  // namespace pdx
}  // namespace android