Home | History | Annotate | Download | only in netmgr
      1 /*
      2  * Copyright 2018, 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 
     17 #pragma once
     18 
     19 #include <chrono>
     20 #include <vector>
     21 
     22 #include <poll.h>
     23 
     24 /* An interface for pollable classes.
     25  */
     26 class Pollable {
     27 public:
     28     using Clock = std::chrono::steady_clock;
     29     using Timestamp = Clock::time_point;
     30     virtual ~Pollable() = default;
     31 
     32     /* Get the poll data for the next poll loop. The implementation can place
     33      * as many fds as needed in |fds|.
     34      */
     35     virtual void getPollData(std::vector<pollfd>* fds) const = 0;
     36     /* Get the timeout for the next poll loop. This should be a timestamp
     37      * indicating when the timeout should be triggered. Note that this may
     38      * be called at any time and any number of times for a poll loop so the
     39      * deadline should not be adjusted in this call, a set deadline should
     40      * just be returned. Note specifically that if a call to onReadAvailable
     41      * modifies the deadline the timeout for the previous timestamp might not
     42      * fire as the poller will check the timestamp AFTER onReadAvailable is
     43      * called.
     44      */
     45     virtual Timestamp getTimeout() const = 0;
     46     /* Called when there is data available to read on an fd associated with
     47      * the pollable. |fd| indicates which fd to read from. If the call returns
     48      * false the poller will exit its poll loop with a return code of |status|.
     49      */
     50     virtual bool onReadAvailable(int fd, int* status) = 0;
     51     /* Called when an fd associated with the pollable is closed. |fd| indicates
     52      * which fd was closed. If the call returns false the poller will exit its
     53      * poll loop with a return code of |status|.
     54      */
     55     virtual bool onClose(int fd, int* status) = 0;
     56     /* Called when the timeout returned by getPollData has been reached. If
     57      * the call returns false the poller will exit its poll loop with a return
     58      * code of |status|.
     59      */
     60     virtual bool onTimeout(int* status) = 0;
     61 };
     62 
     63