Loading...
Searching...
No Matches
net_channel.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21#include <memory>
22
25#include "platform/proto/resdb.pb.h"
26
27namespace resdb {
28
29// NetChannel is used to send data to the server identified by ip:port or
30// via the provided socket. If SignatureVerifier is provided, data will be
31// signed. The client will retry max_retry_time_ if the connection or sned
32// process fail. For a message with a command, it will be set inside a Request
33// message. Each Request will be put into the network message ResDBMessage and
34// signed.
35//
36// Message structre:
37// ResDBMessage:
38// Request:
39// cmd
40// user_message
41// signature
42//
43// For a raw message, it will be set inside a Network Message directly.
44// Message structre:
45// ResDBMessage:
46// user_message
47// signature
48//
50 public:
51 NetChannel(const std::string& ip, int port);
52 // Use the provided socket to send data. If the socket has been connect to
53 // the server, it won't connect again.
54 NetChannel(std::unique_ptr<Socket> socket, bool connected = false);
55
56 virtual ~NetChannel() = default;
57
58 void Close();
59
60 void SetSocket(std::unique_ptr<Socket> socket);
62 void SetDestReplicaInfo(const ReplicaInfo& replica);
63
64 // Send a message request to the server with a commend.
65 // A new request will be generated with command cmd and contain the message.
66 virtual int SendRequest(const google::protobuf::Message& message,
67 Request::Type cmd, bool need_response = false);
68
69 // Send the message to the server directly.
70 virtual int SendRawMessage(const google::protobuf::Message& message);
71 virtual int SendRawMessageData(const std::string& message_str);
72
73 // Recv the data inside ResDBMessage.
74 virtual int RecvRawMessageStr(std::string* message);
75 virtual int RecvRawMessage(google::protobuf::Message* message);
76
77 // Recv the raw string from the server.
78 virtual int RecvRawMessageData(std::string* message_str);
79
80 static std::string GetRawMessageString(
81 const google::protobuf::Message& message,
82 SignatureVerifier* verifier = nullptr);
83
84 void SetRecvTimeout(int microseconds);
85 void IsLongConnection(bool long_connect_tion);
86 void SetAsyncSend(bool is_async_send);
87
88 protected:
89 int SendDataInternal(const std::string& data);
90 int SendFromKeepAlive(const std::string& data);
91 int Send(const std::string& data);
92 int Recv(std::string* data);
93 int Connect();
94
95 protected:
97
98 std::unique_ptr<Socket> socket_;
100 std::string ip_;
101 int port_;
102 bool connected_ = false;
103 int read_timeouts_ = 1000000; // timeout for 1s.
104 bool long_connect_tion_ = false;
105 bool long_connecting_ = false;
106 bool is_async_send_ = false;
107};
108
109} // namespace resdb
std::string data
virtual int SendRawMessage(const google::protobuf::Message &message)
virtual int RecvRawMessageData(std::string *message_str)
std::string ip_
void SetRecvTimeout(int microseconds)
virtual int RecvRawMessageStr(std::string *message)
SignatureVerifier * verifier_
Definition net_channel.h:96
virtual int SendRawMessageData(const std::string &message_str)
std::unique_ptr< Socket > socket_
Definition net_channel.h:98
virtual ~NetChannel()=default
virtual int RecvRawMessage(google::protobuf::Message *message)
virtual int SendRequest(const google::protobuf::Message &message, Request::Type cmd, bool need_response=false)
void SetSocket(std::unique_ptr< Socket > socket)
int SendFromKeepAlive(const std::string &data)
void SetDestReplicaInfo(const ReplicaInfo &replica)
void SetAsyncSend(bool is_async_send)
int Recv(std::string *data)
void SetSignatureVerifier(SignatureVerifier *verifier)
static std::string GetRawMessageString(const google::protobuf::Message &message, SignatureVerifier *verifier=nullptr)
int SendDataInternal(const std::string &data)
int Send(const std::string &data)
void IsLongConnection(bool long_connect_tion)