Home | History | Annotate | Download | only in channel
      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 #include <grpc/support/port_platform.h>
     20 
     21 #include "src/core/lib/channel/handshaker_registry.h"
     22 
     23 #include <string.h>
     24 
     25 #include <grpc/support/alloc.h>
     26 
     27 //
     28 // grpc_handshaker_factory_list
     29 //
     30 
     31 typedef struct {
     32   grpc_handshaker_factory** list;
     33   size_t num_factories;
     34 } grpc_handshaker_factory_list;
     35 
     36 static void grpc_handshaker_factory_list_register(
     37     grpc_handshaker_factory_list* list, bool at_start,
     38     grpc_handshaker_factory* factory) {
     39   list->list = static_cast<grpc_handshaker_factory**>(gpr_realloc(
     40       list->list,
     41       (list->num_factories + 1) * sizeof(grpc_handshaker_factory*)));
     42   if (at_start) {
     43     memmove(list->list + 1, list->list,
     44             sizeof(grpc_handshaker_factory*) * list->num_factories);
     45     list->list[0] = factory;
     46   } else {
     47     list->list[list->num_factories] = factory;
     48   }
     49   ++list->num_factories;
     50 }
     51 
     52 static void grpc_handshaker_factory_list_add_handshakers(
     53     grpc_handshaker_factory_list* list, const grpc_channel_args* args,
     54     grpc_handshake_manager* handshake_mgr) {
     55   for (size_t i = 0; i < list->num_factories; ++i) {
     56     grpc_handshaker_factory_add_handshakers(list->list[i], args, handshake_mgr);
     57   }
     58 }
     59 
     60 static void grpc_handshaker_factory_list_destroy(
     61     grpc_handshaker_factory_list* list) {
     62   for (size_t i = 0; i < list->num_factories; ++i) {
     63     grpc_handshaker_factory_destroy(list->list[i]);
     64   }
     65   gpr_free(list->list);
     66 }
     67 
     68 //
     69 // plugin
     70 //
     71 
     72 static grpc_handshaker_factory_list
     73     g_handshaker_factory_lists[NUM_HANDSHAKER_TYPES];
     74 
     75 void grpc_handshaker_factory_registry_init() {
     76   memset(g_handshaker_factory_lists, 0, sizeof(g_handshaker_factory_lists));
     77 }
     78 
     79 void grpc_handshaker_factory_registry_shutdown() {
     80   for (size_t i = 0; i < NUM_HANDSHAKER_TYPES; ++i) {
     81     grpc_handshaker_factory_list_destroy(&g_handshaker_factory_lists[i]);
     82   }
     83 }
     84 
     85 void grpc_handshaker_factory_register(bool at_start,
     86                                       grpc_handshaker_type handshaker_type,
     87                                       grpc_handshaker_factory* factory) {
     88   grpc_handshaker_factory_list_register(
     89       &g_handshaker_factory_lists[handshaker_type], at_start, factory);
     90 }
     91 
     92 void grpc_handshakers_add(grpc_handshaker_type handshaker_type,
     93                           const grpc_channel_args* args,
     94                           grpc_handshake_manager* handshake_mgr) {
     95   grpc_handshaker_factory_list_add_handshakers(
     96       &g_handshaker_factory_lists[handshaker_type], args, handshake_mgr);
     97 }
     98