1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. 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,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18
19#ifndef BRPC_ADAPTIVE_CONNECTION_TYPE_H
20#define BRPC_ADAPTIVE_CONNECTION_TYPE_H
21
22// To brpc developers: This is a header included by user, don't depend
23// on internal structures, use opaque pointers instead.
24
25#include "butil/strings/string_piece.h"
26#include "brpc/options.pb.h"
27
28namespace brpc {
29
30// Convert a case-insensitive string to corresponding ConnectionType
31// Possible options are: short, pooled, single
32// Returns: CONNECTION_TYPE_UNKNOWN on error.
33ConnectionType StringToConnectionType(const butil::StringPiece& type,
34 bool print_log_on_unknown);
35inline ConnectionType StringToConnectionType(const butil::StringPiece& type)
36{ return StringToConnectionType(type, true); }
37
38// Convert a ConnectionType to a c-style string.
39const char* ConnectionTypeToString(ConnectionType);
40
41// Assignable by both ConnectionType and names.
42class AdaptiveConnectionType {
43public:
44 AdaptiveConnectionType() : _type(CONNECTION_TYPE_UNKNOWN), _error(false) {}
45 AdaptiveConnectionType(ConnectionType type) : _type(type), _error(false) {}
46 ~AdaptiveConnectionType() {}
47
48 void operator=(ConnectionType type) {
49 _type = type;
50 _error = false;
51 }
52 void operator=(const butil::StringPiece& name);
53
54 operator ConnectionType() const { return _type; }
55 const char* name() const { return ConnectionTypeToString(_type); }
56 bool has_error() const { return _error; }
57
58private:
59 ConnectionType _type;
60 // Since this structure occupies 8 bytes in 64-bit machines anyway,
61 // we add a field to mark if last operator=(name) failed so that
62 // channel can print a error log before re-selecting a valid
63 // ConnectionType for user.
64 bool _error;
65};
66
67} // namespace brpc
68
69
70#endif // BRPC_ADAPTIVE_CONNECTION_TYPE_H
71