Home | History | Annotate | Download | only in c_ares
      1 /*
      2  *
      3  * Copyright 2016 gRPC authors.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_DNS_C_ARES_GRPC_ARES_EV_DRIVER_H
     20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_DNS_C_ARES_GRPC_ARES_EV_DRIVER_H
     21 
     22 #include <grpc/support/port_platform.h>
     23 
     24 #include <ares.h>
     25 #include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h"
     26 #include "src/core/lib/gprpp/abstract.h"
     27 #include "src/core/lib/iomgr/pollset_set.h"
     28 
     29 typedef struct grpc_ares_ev_driver grpc_ares_ev_driver;
     30 
     31 /* Start \a ev_driver. It will keep working until all IO on its ares_channel is
     32    done, or grpc_ares_ev_driver_destroy() is called. It may notify the callbacks
     33    bound to its ares_channel when necessary. */
     34 void grpc_ares_ev_driver_start_locked(grpc_ares_ev_driver* ev_driver);
     35 
     36 /* Returns the ares_channel owned by \a ev_driver. To bind a c-ares query to
     37    \a ev_driver, use the ares_channel owned by \a ev_driver as the arg of the
     38    query. */
     39 ares_channel* grpc_ares_ev_driver_get_channel_locked(
     40     grpc_ares_ev_driver* ev_driver);
     41 
     42 /* Creates a new grpc_ares_ev_driver. Returns GRPC_ERROR_NONE if \a ev_driver is
     43    created successfully. */
     44 grpc_error* grpc_ares_ev_driver_create_locked(grpc_ares_ev_driver** ev_driver,
     45                                               grpc_pollset_set* pollset_set,
     46                                               grpc_combiner* combiner,
     47                                               grpc_ares_request* request);
     48 
     49 /* Called back when all DNS lookups have completed. */
     50 void grpc_ares_ev_driver_on_queries_complete_locked(
     51     grpc_ares_ev_driver* ev_driver);
     52 
     53 /* Shutdown all the grpc_fds used by \a ev_driver */
     54 void grpc_ares_ev_driver_shutdown_locked(grpc_ares_ev_driver* ev_driver);
     55 
     56 namespace grpc_core {
     57 
     58 /* A wrapped fd that integrates with the grpc iomgr of the current platform.
     59  * A GrpcPolledFd knows how to create grpc platform-specific iomgr endpoints
     60  * from "ares_socket_t" sockets, and then sign up for readability/writeability
     61  * with that poller, and do shutdown and destruction. */
     62 class GrpcPolledFd {
     63  public:
     64   virtual ~GrpcPolledFd() {}
     65   /* Called when c-ares library is interested and there's no pending callback */
     66   virtual void RegisterForOnReadableLocked(grpc_closure* read_closure)
     67       GRPC_ABSTRACT;
     68   /* Called when c-ares library is interested and there's no pending callback */
     69   virtual void RegisterForOnWriteableLocked(grpc_closure* write_closure)
     70       GRPC_ABSTRACT;
     71   /* Indicates if there is data left even after just being read from */
     72   virtual bool IsFdStillReadableLocked() GRPC_ABSTRACT;
     73   /* Called once and only once. Must cause cancellation of any pending
     74    * read/write callbacks. */
     75   virtual void ShutdownLocked(grpc_error* error) GRPC_ABSTRACT;
     76   /* Get the underlying ares_socket_t that this was created from */
     77   virtual ares_socket_t GetWrappedAresSocketLocked() GRPC_ABSTRACT;
     78   /* A unique name, for logging */
     79   virtual const char* GetName() GRPC_ABSTRACT;
     80 
     81   GRPC_ABSTRACT_BASE_CLASS
     82 };
     83 
     84 /* A GrpcPolledFdFactory is 1-to-1 with and owned by the
     85  * ares event driver. It knows how to create GrpcPolledFd's
     86  * for the current platform, and the ares driver uses it for all of
     87  * its fd's. */
     88 class GrpcPolledFdFactory {
     89  public:
     90   virtual ~GrpcPolledFdFactory() {}
     91   /* Creates a new wrapped fd for the current platform */
     92   virtual GrpcPolledFd* NewGrpcPolledFdLocked(
     93       ares_socket_t as, grpc_pollset_set* driver_pollset_set,
     94       grpc_combiner* combiner) GRPC_ABSTRACT;
     95   /* Optionally configures the ares channel after creation */
     96   virtual void ConfigureAresChannelLocked(ares_channel channel) GRPC_ABSTRACT;
     97 
     98   GRPC_ABSTRACT_BASE_CLASS
     99 };
    100 
    101 UniquePtr<GrpcPolledFdFactory> NewGrpcPolledFdFactory(grpc_combiner* combiner);
    102 
    103 }  // namespace grpc_core
    104 
    105 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_DNS_C_ARES_GRPC_ARES_EV_DRIVER_H \
    106         */
    107