#include "watch.hpp" #include namespace watch { Port::Port() : opened_(0), closed_(0), delivered_(0) { } bool Port::valid_key(int key) { return key >= 1 && key <= 1000000; } bool Port::valid_mask(unsigned mask) { return mask >= 1 && mask <= 7; } std::size_t Port::watch_count() const { std::size_t total = 0; for (const auto& item : instances_) { total += item.second.watches.size(); } return total; } std::size_t Port::queued_count() const { std::size_t total = 0; for (const auto& item : instances_) { total += item.second.queue.size(); } return total; } int Port::next_fd() const { for (int fd = 3; fd < 11; ++fd) { if (instances_.find(fd) == instances_.end()) { return fd; } } return exhausted; } int Port::open() { const int fd = next_fd(); if (fd < 0) { return fd; } instances_.emplace(fd, Instance{}); ++opened_; return fd; } int Port::close(int fd) { const auto found = instances_.find(fd); if (found == instances_.end()) { return badfd; } instances_.erase(found); ++closed_; return 0; } int Port::add(int fd, int key, unsigned mask) { const auto found = instances_.find(fd); if (found == instances_.end()) { return badfd; } if (!valid_key(key) || !valid_mask(mask)) { return invalid; } auto& instance = found->second; for (auto& item : instance.watches) { if (item.second.key == key) { item.second.mask = mask; return item.first; } } if (watch_count() >= 32) { return no_space; } const int wd = instance.next_wd++; instance.watches.emplace(wd, Watch{wd, key, mask}); return wd; } int Port::remove(int fd, int wd) { const auto found = instances_.find(fd); if (found == instances_.end()) { return badfd; } const auto item = found->second.watches.find(wd); if (item == found->second.watches.end()) { return missing; } found->second.watches.erase(item); return 0; } int Port::emit(int key, unsigned mask, std::uint64_t cookie) { if (!valid_key(key) || !valid_mask(mask)) { return invalid; } struct Pending { int fd; Event event; }; std::vector pending; for (const auto& object : instances_) { for (const auto& item : object.second.watches) { const Watch& watched = item.second; const unsigned selected = watched.mask & mask; if (watched.key == key && selected != 0) { if (object.second.queue.size() >= 64) { return no_space; } pending.push_back(Pending{object.first, Event{watched.wd, key, selected, cookie}}); } } } for (const auto& item : pending) { instances_.at(item.fd).queue.push_back(item.event); } return 0; } int Port::read(int fd, std::size_t capacity, std::vector& output) { const auto found = instances_.find(fd); if (found == instances_.end()) { return badfd; } if (capacity > 64) { return invalid; } auto& queue = found->second.queue; const std::size_t count = std::min(capacity, queue.size()); std::vector result; result.reserve(count); for (std::size_t index = 0; index < count; ++index) { result.push_back(queue.front()); queue.pop_front(); } delivered_ += count; output.swap(result); return static_cast(count); } int Port::inspect(int fd, std::vector& output) const { const auto found = instances_.find(fd); if (found == instances_.end()) { return badfd; } std::vector result; result.reserve(found->second.watches.size()); for (const auto& item : found->second.watches) { result.push_back(item.second); } output.swap(result); return static_cast(output.size()); } Counts Port::counts() const { Counts result; result.instances = instances_.size(); result.watches = watch_count(); result.queued = queued_count(); result.opened = opened_; result.closed = closed_; result.delivered = delivered_; return result; } void Port::reset() { instances_.clear(); opened_ = 0; closed_ = 0; delivered_ = 0; } }