starpc 0.44.0 → 0.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/echo/echo_e2e_test.cpp +794 -35
- package/echo/echo_srpc.pb.cpp +40 -28
- package/echo/echo_srpc.pb.hpp +131 -106
- package/go.mod +2 -2
- package/go.sum +4 -4
- package/mock/mock_srpc.pb.cpp +13 -10
- package/mock/mock_srpc.pb.hpp +34 -28
- package/package.json +5 -4
- package/srpc/client-rpc.cpp +21 -19
- package/srpc/client-rpc.hpp +14 -11
- package/srpc/client.cpp +12 -16
- package/srpc/client.hpp +17 -25
- package/srpc/common-rpc.cpp +13 -14
- package/srpc/common-rpc.hpp +13 -11
- package/srpc/errors.hpp +33 -20
- package/srpc/handler.hpp +3 -3
- package/srpc/invoker.hpp +26 -29
- package/srpc/message.hpp +4 -6
- package/srpc/msg-stream.hpp +13 -11
- package/srpc/mux.cpp +19 -18
- package/srpc/mux.hpp +15 -14
- package/srpc/packet.cpp +25 -27
- package/srpc/packet.hpp +14 -17
- package/srpc/server-rpc.cpp +22 -22
- package/srpc/server-rpc.hpp +15 -13
- package/srpc/starpc.hpp +1 -1
- package/srpc/stream.hpp +18 -16
- package/srpc/writer.hpp +15 -12
package/srpc/errors.hpp
CHANGED
|
@@ -8,8 +8,8 @@ namespace starpc {
|
|
|
8
8
|
// Error codes matching Go starpc errors (see errors.go)
|
|
9
9
|
enum class Error {
|
|
10
10
|
OK = 0,
|
|
11
|
-
Unimplemented,
|
|
12
|
-
Completed,
|
|
11
|
+
Unimplemented, // ErrUnimplemented - RPC method was not implemented
|
|
12
|
+
Completed, // ErrCompleted - unexpected packet after rpc was completed
|
|
13
13
|
UnrecognizedPacket, // ErrUnrecognizedPacket - unrecognized packet type
|
|
14
14
|
EmptyPacket, // ErrEmptyPacket - invalid empty packet
|
|
15
15
|
InvalidMessage, // ErrInvalidMessage - message failed to parse
|
|
@@ -22,37 +22,50 @@ enum class Error {
|
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
// Convert Error to string (matches Go error messages)
|
|
25
|
-
inline const char*
|
|
25
|
+
inline const char *ErrorString(Error err) {
|
|
26
26
|
switch (err) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
case Error::OK:
|
|
28
|
+
return "ok";
|
|
29
|
+
case Error::Unimplemented:
|
|
30
|
+
return "unimplemented";
|
|
31
|
+
case Error::Completed:
|
|
32
|
+
return "unexpected packet after rpc was completed";
|
|
33
|
+
case Error::UnrecognizedPacket:
|
|
34
|
+
return "unrecognized packet type";
|
|
35
|
+
case Error::EmptyPacket:
|
|
36
|
+
return "invalid empty packet";
|
|
37
|
+
case Error::InvalidMessage:
|
|
38
|
+
return "invalid message";
|
|
39
|
+
case Error::EmptyMethodID:
|
|
40
|
+
return "method id empty";
|
|
41
|
+
case Error::EmptyServiceID:
|
|
42
|
+
return "service id empty";
|
|
43
|
+
case Error::NoAvailableClients:
|
|
44
|
+
return "no available rpc clients";
|
|
45
|
+
case Error::NilWriter:
|
|
46
|
+
return "writer cannot be nil";
|
|
47
|
+
case Error::Canceled:
|
|
48
|
+
return "canceled";
|
|
49
|
+
case Error::EOF_:
|
|
50
|
+
return "EOF";
|
|
51
|
+
default:
|
|
52
|
+
return "unknown error";
|
|
40
53
|
}
|
|
41
54
|
}
|
|
42
55
|
|
|
43
56
|
// StarpcError exception for error propagation
|
|
44
57
|
class StarpcError : public std::runtime_error {
|
|
45
|
-
|
|
58
|
+
public:
|
|
46
59
|
explicit StarpcError(Error code)
|
|
47
60
|
: std::runtime_error(ErrorString(code)), code_(code) {}
|
|
48
61
|
|
|
49
|
-
StarpcError(Error code, const std::string&
|
|
62
|
+
StarpcError(Error code, const std::string &message)
|
|
50
63
|
: std::runtime_error(message), code_(code) {}
|
|
51
64
|
|
|
52
65
|
Error code() const noexcept { return code_; }
|
|
53
66
|
|
|
54
|
-
|
|
67
|
+
private:
|
|
55
68
|
Error code_;
|
|
56
69
|
};
|
|
57
70
|
|
|
58
|
-
}
|
|
71
|
+
} // namespace starpc
|
package/srpc/handler.hpp
CHANGED
|
@@ -10,14 +10,14 @@ namespace starpc {
|
|
|
10
10
|
// Handler describes a SRPC call handler implementation.
|
|
11
11
|
// Matches Go Handler interface in handler.go
|
|
12
12
|
class Handler : public Invoker {
|
|
13
|
-
|
|
13
|
+
public:
|
|
14
14
|
~Handler() override = default;
|
|
15
15
|
|
|
16
16
|
// GetServiceID returns the ID of the service.
|
|
17
|
-
virtual const std::string&
|
|
17
|
+
virtual const std::string &GetServiceID() const = 0;
|
|
18
18
|
|
|
19
19
|
// GetMethodIDs returns the list of methods for the service.
|
|
20
20
|
virtual std::vector<std::string> GetMethodIDs() const = 0;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
}
|
|
23
|
+
} // namespace starpc
|
package/srpc/invoker.hpp
CHANGED
|
@@ -12,47 +12,47 @@ namespace starpc {
|
|
|
12
12
|
// Invoker is a function for invoking SRPC service methods.
|
|
13
13
|
// Matches Go Invoker interface in invoker.go
|
|
14
14
|
class Invoker {
|
|
15
|
-
|
|
15
|
+
public:
|
|
16
16
|
virtual ~Invoker() = default;
|
|
17
17
|
|
|
18
18
|
// InvokeMethod invokes the method matching the service & method ID.
|
|
19
19
|
// Returns {found, error} - found is false if method not found.
|
|
20
20
|
// If service string is empty, ignore it.
|
|
21
|
-
virtual std::pair<bool, Error> InvokeMethod(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Stream* strm) = 0;
|
|
21
|
+
virtual std::pair<bool, Error> InvokeMethod(const std::string &service_id,
|
|
22
|
+
const std::string &method_id,
|
|
23
|
+
Stream *strm) = 0;
|
|
25
24
|
};
|
|
26
25
|
|
|
27
26
|
// QueryableInvoker can be used to check if a service and method is implemented.
|
|
28
27
|
// Matches Go QueryableInvoker interface in invoker.go
|
|
29
28
|
class QueryableInvoker {
|
|
30
|
-
|
|
29
|
+
public:
|
|
31
30
|
virtual ~QueryableInvoker() = default;
|
|
32
31
|
|
|
33
32
|
// HasService checks if the service ID exists in the handlers.
|
|
34
|
-
virtual bool HasService(const std::string&
|
|
33
|
+
virtual bool HasService(const std::string &service_id) const = 0;
|
|
35
34
|
|
|
36
35
|
// HasServiceMethod checks if <service-id, method-id> exists in the handlers.
|
|
37
|
-
virtual bool HasServiceMethod(const std::string&
|
|
36
|
+
virtual bool HasServiceMethod(const std::string &service_id,
|
|
37
|
+
const std::string &method_id) const = 0;
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
// InvokerSlice is a list of invokers.
|
|
41
41
|
// Matches Go InvokerSlice in invoker.go
|
|
42
42
|
class InvokerSlice : public Invoker {
|
|
43
|
-
|
|
43
|
+
public:
|
|
44
44
|
InvokerSlice() = default;
|
|
45
|
-
explicit InvokerSlice(std::vector<Invoker*> invokers)
|
|
45
|
+
explicit InvokerSlice(std::vector<Invoker *> invokers)
|
|
46
46
|
: invokers_(std::move(invokers)) {}
|
|
47
47
|
|
|
48
|
-
void Add(Invoker*
|
|
48
|
+
void Add(Invoker *invoker) { invokers_.push_back(invoker); }
|
|
49
49
|
|
|
50
|
-
std::pair<bool, Error> InvokeMethod(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
std::pair<bool, Error> InvokeMethod(const std::string &service_id,
|
|
51
|
+
const std::string &method_id,
|
|
52
|
+
Stream *strm) override {
|
|
53
|
+
for (auto *invoker : invokers_) {
|
|
54
|
+
if (invoker == nullptr)
|
|
55
|
+
continue;
|
|
56
56
|
auto [found, err] = invoker->InvokeMethod(service_id, method_id, strm);
|
|
57
57
|
if (found || err != Error::OK) {
|
|
58
58
|
return {true, err};
|
|
@@ -61,34 +61,31 @@ class InvokerSlice : public Invoker {
|
|
|
61
61
|
return {false, Error::OK};
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
std::vector<Invoker*> invokers_;
|
|
64
|
+
private:
|
|
65
|
+
std::vector<Invoker *> invokers_;
|
|
66
66
|
};
|
|
67
67
|
|
|
68
68
|
// InvokerFunc is a function implementing InvokeMethod.
|
|
69
69
|
// Matches Go InvokerFunc in invoker.go
|
|
70
70
|
using InvokerFunc = std::function<std::pair<bool, Error>(
|
|
71
|
-
const std::string&
|
|
72
|
-
const std::string& method_id,
|
|
73
|
-
Stream* strm)>;
|
|
71
|
+
const std::string &service_id, const std::string &method_id, Stream *strm)>;
|
|
74
72
|
|
|
75
73
|
// InvokerFuncWrapper wraps an InvokerFunc as an Invoker.
|
|
76
74
|
class InvokerFuncWrapper : public Invoker {
|
|
77
|
-
|
|
75
|
+
public:
|
|
78
76
|
explicit InvokerFuncWrapper(InvokerFunc fn) : fn_(std::move(fn)) {}
|
|
79
77
|
|
|
80
|
-
std::pair<bool, Error> InvokeMethod(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
Stream* strm) override {
|
|
78
|
+
std::pair<bool, Error> InvokeMethod(const std::string &service_id,
|
|
79
|
+
const std::string &method_id,
|
|
80
|
+
Stream *strm) override {
|
|
84
81
|
if (!fn_) {
|
|
85
82
|
return {false, Error::OK};
|
|
86
83
|
}
|
|
87
84
|
return fn_(service_id, method_id, strm);
|
|
88
85
|
}
|
|
89
86
|
|
|
90
|
-
|
|
87
|
+
private:
|
|
91
88
|
InvokerFunc fn_;
|
|
92
89
|
};
|
|
93
90
|
|
|
94
|
-
}
|
|
91
|
+
} // namespace starpc
|
package/srpc/message.hpp
CHANGED
|
@@ -13,18 +13,16 @@ using Message = google::protobuf::MessageLite;
|
|
|
13
13
|
|
|
14
14
|
// MarshalVT serializes the message to bytes.
|
|
15
15
|
// (VT suffix matches vtprotobuf convention used in Go)
|
|
16
|
-
inline bool MarshalVT(const Message&
|
|
16
|
+
inline bool MarshalVT(const Message &msg, std::string *out) {
|
|
17
17
|
return msg.SerializeToString(out);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
// UnmarshalVT deserializes the message from bytes.
|
|
21
|
-
inline bool UnmarshalVT(Message*
|
|
21
|
+
inline bool UnmarshalVT(Message *msg, const std::string &data) {
|
|
22
22
|
return msg->ParseFromString(data);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
// SizeVT returns the serialized size of the message.
|
|
26
|
-
inline size_t SizeVT(const Message& msg)
|
|
27
|
-
return msg.ByteSizeLong();
|
|
28
|
-
}
|
|
26
|
+
inline size_t SizeVT(const Message &msg) { return msg.ByteSizeLong(); }
|
|
29
27
|
|
|
30
|
-
}
|
|
28
|
+
} // namespace starpc
|
package/srpc/msg-stream.hpp
CHANGED
|
@@ -12,15 +12,16 @@ namespace starpc {
|
|
|
12
12
|
// MsgStreamRw is the read-write interface for MsgStream.
|
|
13
13
|
// Matches Go MsgStreamRw interface in msg-stream.go
|
|
14
14
|
class MsgStreamRw {
|
|
15
|
-
|
|
15
|
+
public:
|
|
16
16
|
virtual ~MsgStreamRw() = default;
|
|
17
17
|
|
|
18
18
|
// ReadOne reads a single message and returns.
|
|
19
19
|
// Returns EOF_ if the stream ended.
|
|
20
|
-
virtual Error ReadOne(std::string*
|
|
20
|
+
virtual Error ReadOne(std::string *out) = 0;
|
|
21
21
|
|
|
22
22
|
// WriteCallData writes a call data packet.
|
|
23
|
-
virtual Error WriteCallData(const std::string&
|
|
23
|
+
virtual Error WriteCallData(const std::string &data, bool data_is_zero,
|
|
24
|
+
bool complete, Error err) = 0;
|
|
24
25
|
|
|
25
26
|
// WriteCallCancel writes a call cancel (close) packet.
|
|
26
27
|
virtual Error WriteCallCancel() = 0;
|
|
@@ -29,13 +30,13 @@ class MsgStreamRw {
|
|
|
29
30
|
// MsgStream implements the stream interface passed to implementations.
|
|
30
31
|
// Matches Go MsgStream struct in msg-stream.go
|
|
31
32
|
class MsgStream : public Stream {
|
|
32
|
-
|
|
33
|
+
public:
|
|
33
34
|
// Constructor matching NewMsgStream in Go
|
|
34
|
-
MsgStream(MsgStreamRw*
|
|
35
|
+
MsgStream(MsgStreamRw *rw, std::function<void()> close_cb)
|
|
35
36
|
: rw_(rw), close_cb_(std::move(close_cb)) {}
|
|
36
37
|
|
|
37
38
|
// MsgSend sends the message to the remote.
|
|
38
|
-
Error MsgSend(const Message&
|
|
39
|
+
Error MsgSend(const Message &msg) override {
|
|
39
40
|
std::string msg_data;
|
|
40
41
|
if (!msg.SerializeToString(&msg_data)) {
|
|
41
42
|
return Error::InvalidMessage;
|
|
@@ -44,7 +45,7 @@ class MsgStream : public Stream {
|
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
// MsgRecv receives an incoming message from the remote.
|
|
47
|
-
Error MsgRecv(Message*
|
|
48
|
+
Error MsgRecv(Message *msg) override {
|
|
48
49
|
std::string data;
|
|
49
50
|
Error err = rw_->ReadOne(&data);
|
|
50
51
|
if (err != Error::OK) {
|
|
@@ -70,15 +71,16 @@ class MsgStream : public Stream {
|
|
|
70
71
|
return err;
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
|
|
74
|
-
MsgStreamRw*
|
|
74
|
+
private:
|
|
75
|
+
MsgStreamRw *rw_;
|
|
75
76
|
std::function<void()> close_cb_;
|
|
76
77
|
};
|
|
77
78
|
|
|
78
79
|
// NewMsgStream constructs a new Stream with a MsgStreamRw.
|
|
79
80
|
// Matches Go NewMsgStream function in msg-stream.go
|
|
80
|
-
inline std::unique_ptr<MsgStream> NewMsgStream(MsgStreamRw*
|
|
81
|
+
inline std::unique_ptr<MsgStream> NewMsgStream(MsgStreamRw *rw,
|
|
82
|
+
std::function<void()> close_cb) {
|
|
81
83
|
return std::make_unique<MsgStream>(rw, std::move(close_cb));
|
|
82
84
|
}
|
|
83
85
|
|
|
84
|
-
}
|
|
86
|
+
} // namespace starpc
|
package/srpc/mux.cpp
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
//go:build deps_only
|
|
1
|
+
// go:build deps_only
|
|
2
2
|
|
|
3
3
|
#include "mux.hpp"
|
|
4
|
+
#include <mutex>
|
|
4
5
|
|
|
5
6
|
namespace starpc {
|
|
6
7
|
|
|
7
|
-
Mux::Mux(std::vector<Invoker*> fallback_invokers)
|
|
8
|
+
Mux::Mux(std::vector<Invoker *> fallback_invokers)
|
|
8
9
|
: fallback_(std::move(fallback_invokers)) {}
|
|
9
10
|
|
|
10
|
-
Error Mux::Register(Handler*
|
|
11
|
-
const std::string&
|
|
11
|
+
Error Mux::Register(Handler *handler) {
|
|
12
|
+
const std::string &service_id = handler->GetServiceID();
|
|
12
13
|
auto method_ids = handler->GetMethodIDs();
|
|
13
14
|
|
|
14
15
|
if (service_id.empty()) {
|
|
@@ -17,8 +18,8 @@ Error Mux::Register(Handler* handler) {
|
|
|
17
18
|
|
|
18
19
|
std::unique_lock<std::shared_mutex> lock(mtx_);
|
|
19
20
|
|
|
20
|
-
auto&
|
|
21
|
-
for (const auto&
|
|
21
|
+
auto &service_methods = services_[service_id];
|
|
22
|
+
for (const auto &method_id : method_ids) {
|
|
22
23
|
if (!method_id.empty()) {
|
|
23
24
|
service_methods[method_id] = handler;
|
|
24
25
|
}
|
|
@@ -27,7 +28,7 @@ Error Mux::Register(Handler* handler) {
|
|
|
27
28
|
return Error::OK;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
bool Mux::HasService(const std::string&
|
|
31
|
+
bool Mux::HasService(const std::string &service_id) const {
|
|
31
32
|
if (service_id.empty()) {
|
|
32
33
|
return false;
|
|
33
34
|
}
|
|
@@ -37,7 +38,8 @@ bool Mux::HasService(const std::string& service_id) const {
|
|
|
37
38
|
return it != services_.end() && !it->second.empty();
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
bool Mux::HasServiceMethod(const std::string&
|
|
41
|
+
bool Mux::HasServiceMethod(const std::string &service_id,
|
|
42
|
+
const std::string &method_id) const {
|
|
41
43
|
if (service_id.empty() || method_id.empty()) {
|
|
42
44
|
return false;
|
|
43
45
|
}
|
|
@@ -48,8 +50,8 @@ bool Mux::HasServiceMethod(const std::string& service_id, const std::string& met
|
|
|
48
50
|
return false;
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
for (const auto&
|
|
52
|
-
for (const auto&
|
|
53
|
+
for (const auto &[mid, handler] : svc_it->second) {
|
|
54
|
+
for (const auto &handler_method : handler->GetMethodIDs()) {
|
|
53
55
|
if (handler_method == method_id) {
|
|
54
56
|
return true;
|
|
55
57
|
}
|
|
@@ -59,18 +61,17 @@ bool Mux::HasServiceMethod(const std::string& service_id, const std::string& met
|
|
|
59
61
|
return false;
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
std::pair<bool, Error> Mux::InvokeMethod(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
Handler* handler = nullptr;
|
|
64
|
+
std::pair<bool, Error> Mux::InvokeMethod(const std::string &service_id,
|
|
65
|
+
const std::string &method_id,
|
|
66
|
+
Stream *strm) {
|
|
67
|
+
Handler *handler = nullptr;
|
|
67
68
|
|
|
68
69
|
{
|
|
69
70
|
std::shared_lock<std::shared_mutex> lock(mtx_);
|
|
70
71
|
|
|
71
72
|
if (service_id.empty()) {
|
|
72
73
|
// If service string is empty, search all services
|
|
73
|
-
for (const auto&
|
|
74
|
+
for (const auto &[svc_id, svc_methods] : services_) {
|
|
74
75
|
auto it = svc_methods.find(method_id);
|
|
75
76
|
if (it != svc_methods.end()) {
|
|
76
77
|
handler = it->second;
|
|
@@ -93,7 +94,7 @@ std::pair<bool, Error> Mux::InvokeMethod(
|
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
// Try fallback invokers
|
|
96
|
-
for (auto*
|
|
97
|
+
for (auto *invoker : fallback_) {
|
|
97
98
|
if (invoker != nullptr) {
|
|
98
99
|
auto [handled, err] = invoker->InvokeMethod(service_id, method_id, strm);
|
|
99
100
|
if (err != Error::OK || handled) {
|
|
@@ -105,4 +106,4 @@ std::pair<bool, Error> Mux::InvokeMethod(
|
|
|
105
106
|
return {false, Error::OK};
|
|
106
107
|
}
|
|
107
108
|
|
|
108
|
-
}
|
|
109
|
+
} // namespace starpc
|
package/srpc/mux.hpp
CHANGED
|
@@ -14,38 +14,38 @@ namespace starpc {
|
|
|
14
14
|
// Mux contains a set of <service, method> handlers.
|
|
15
15
|
// Matches Go Mux interface in mux.go
|
|
16
16
|
class Mux : public Invoker, public QueryableInvoker {
|
|
17
|
-
|
|
17
|
+
public:
|
|
18
18
|
// Constructor matching NewMux in Go
|
|
19
|
-
explicit Mux(std::vector<Invoker*> fallback_invokers = {});
|
|
19
|
+
explicit Mux(std::vector<Invoker *> fallback_invokers = {});
|
|
20
20
|
~Mux() override = default;
|
|
21
21
|
|
|
22
22
|
// Register registers a new RPC method handler (service).
|
|
23
23
|
// Matches Go Register in mux.go
|
|
24
|
-
Error Register(Handler*
|
|
24
|
+
Error Register(Handler *handler);
|
|
25
25
|
|
|
26
26
|
// InvokeMethod invokes the method matching the service & method ID.
|
|
27
27
|
// Returns {found, error} - found is false if method not found.
|
|
28
28
|
// If service string is empty, ignore it.
|
|
29
29
|
// Matches Go InvokeMethod in mux.go
|
|
30
|
-
std::pair<bool, Error> InvokeMethod(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
Stream* strm) override;
|
|
30
|
+
std::pair<bool, Error> InvokeMethod(const std::string &service_id,
|
|
31
|
+
const std::string &method_id,
|
|
32
|
+
Stream *strm) override;
|
|
34
33
|
|
|
35
34
|
// HasService checks if the service ID exists in the handlers.
|
|
36
35
|
// Matches Go HasService in mux.go
|
|
37
|
-
bool HasService(const std::string&
|
|
36
|
+
bool HasService(const std::string &service_id) const override;
|
|
38
37
|
|
|
39
38
|
// HasServiceMethod checks if <service-id, method-id> exists in the handlers.
|
|
40
39
|
// Matches Go HasServiceMethod in mux.go
|
|
41
|
-
bool HasServiceMethod(const std::string&
|
|
40
|
+
bool HasServiceMethod(const std::string &service_id,
|
|
41
|
+
const std::string &method_id) const override;
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
private:
|
|
44
44
|
// Mapping from method id to handler
|
|
45
|
-
using MuxMethods = std::unordered_map<std::string, Handler*>;
|
|
45
|
+
using MuxMethods = std::unordered_map<std::string, Handler *>;
|
|
46
46
|
|
|
47
47
|
// Fallback invokers
|
|
48
|
-
std::vector<Invoker*> fallback_;
|
|
48
|
+
std::vector<Invoker *> fallback_;
|
|
49
49
|
|
|
50
50
|
// Read-write mutex guards services_
|
|
51
51
|
mutable std::shared_mutex mtx_;
|
|
@@ -56,8 +56,9 @@ class Mux : public Invoker, public QueryableInvoker {
|
|
|
56
56
|
|
|
57
57
|
// NewMux constructs a new Mux.
|
|
58
58
|
// Matches Go NewMux function in mux.go
|
|
59
|
-
inline std::unique_ptr<Mux>
|
|
59
|
+
inline std::unique_ptr<Mux>
|
|
60
|
+
NewMux(std::vector<Invoker *> fallback_invokers = {}) {
|
|
60
61
|
return std::make_unique<Mux>(std::move(fallback_invokers));
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
}
|
|
64
|
+
} // namespace starpc
|
package/srpc/packet.cpp
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//go:build deps_only
|
|
1
|
+
// go:build deps_only
|
|
2
2
|
|
|
3
3
|
#include "packet.hpp"
|
|
4
4
|
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
namespace starpc {
|
|
8
8
|
|
|
9
9
|
PacketDataHandler NewPacketDataHandler(PacketHandler handler) {
|
|
10
|
-
return [handler](const std::string&
|
|
10
|
+
return [handler](const std::string &data) -> Error {
|
|
11
11
|
srpc::Packet pkt;
|
|
12
12
|
if (!pkt.ParseFromString(data)) {
|
|
13
13
|
return Error::InvalidMessage;
|
|
@@ -16,20 +16,20 @@ PacketDataHandler NewPacketDataHandler(PacketHandler handler) {
|
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
Error ValidatePacket(const srpc::Packet&
|
|
19
|
+
Error ValidatePacket(const srpc::Packet &pkt) {
|
|
20
20
|
switch (pkt.body_case()) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
case srpc::Packet::kCallStart:
|
|
22
|
+
return ValidateCallStart(pkt.call_start());
|
|
23
|
+
case srpc::Packet::kCallData:
|
|
24
|
+
return ValidateCallData(pkt.call_data());
|
|
25
|
+
case srpc::Packet::kCallCancel:
|
|
26
|
+
return Error::OK;
|
|
27
|
+
default:
|
|
28
|
+
return Error::UnrecognizedPacket;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
Error ValidateCallStart(const srpc::CallStart&
|
|
32
|
+
Error ValidateCallStart(const srpc::CallStart &pkt) {
|
|
33
33
|
if (pkt.rpc_method().empty()) {
|
|
34
34
|
return Error::EmptyMethodID;
|
|
35
35
|
}
|
|
@@ -39,20 +39,20 @@ Error ValidateCallStart(const srpc::CallStart& pkt) {
|
|
|
39
39
|
return Error::OK;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
Error ValidateCallData(const srpc::CallData&
|
|
43
|
-
if (pkt.data().empty() && !pkt.complete() && pkt.error().empty() &&
|
|
42
|
+
Error ValidateCallData(const srpc::CallData &pkt) {
|
|
43
|
+
if (pkt.data().empty() && !pkt.complete() && pkt.error().empty() &&
|
|
44
|
+
!pkt.data_is_zero()) {
|
|
44
45
|
return Error::EmptyPacket;
|
|
45
46
|
}
|
|
46
47
|
return Error::OK;
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
std::unique_ptr<srpc::Packet> NewCallStartPacket(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
bool data_is_zero) {
|
|
50
|
+
std::unique_ptr<srpc::Packet> NewCallStartPacket(const std::string &service,
|
|
51
|
+
const std::string &method,
|
|
52
|
+
const std::string &data,
|
|
53
|
+
bool data_is_zero) {
|
|
54
54
|
auto pkt = std::make_unique<srpc::Packet>();
|
|
55
|
-
auto*
|
|
55
|
+
auto *call_start = pkt->mutable_call_start();
|
|
56
56
|
call_start->set_rpc_service(service);
|
|
57
57
|
call_start->set_rpc_method(method);
|
|
58
58
|
call_start->set_data(data);
|
|
@@ -60,13 +60,11 @@ std::unique_ptr<srpc::Packet> NewCallStartPacket(
|
|
|
60
60
|
return pkt;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
std::unique_ptr<srpc::Packet> NewCallDataPacket(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
bool complete,
|
|
67
|
-
Error err) {
|
|
63
|
+
std::unique_ptr<srpc::Packet> NewCallDataPacket(const std::string &data,
|
|
64
|
+
bool data_is_zero,
|
|
65
|
+
bool complete, Error err) {
|
|
68
66
|
auto pkt = std::make_unique<srpc::Packet>();
|
|
69
|
-
auto*
|
|
67
|
+
auto *call_data = pkt->mutable_call_data();
|
|
70
68
|
call_data->set_data(data);
|
|
71
69
|
call_data->set_data_is_zero(data_is_zero);
|
|
72
70
|
call_data->set_complete(err != Error::OK || complete);
|
|
@@ -82,4 +80,4 @@ std::unique_ptr<srpc::Packet> NewCallCancelPacket() {
|
|
|
82
80
|
return pkt;
|
|
83
81
|
}
|
|
84
82
|
|
|
85
|
-
}
|
|
83
|
+
} // namespace starpc
|
package/srpc/packet.hpp
CHANGED
|
@@ -11,7 +11,7 @@ namespace srpc {
|
|
|
11
11
|
class Packet;
|
|
12
12
|
class CallStart;
|
|
13
13
|
class CallData;
|
|
14
|
-
}
|
|
14
|
+
} // namespace srpc
|
|
15
15
|
|
|
16
16
|
namespace starpc {
|
|
17
17
|
|
|
@@ -21,39 +21,36 @@ using CloseHandler = std::function<void(Error close_err)>;
|
|
|
21
21
|
|
|
22
22
|
// PacketHandler handles a packet.
|
|
23
23
|
// Matches Go PacketHandler in packet.go
|
|
24
|
-
using PacketHandler = std::function<Error(const srpc::Packet&
|
|
24
|
+
using PacketHandler = std::function<Error(const srpc::Packet &pkt)>;
|
|
25
25
|
|
|
26
26
|
// PacketDataHandler handles a packet before it is parsed.
|
|
27
27
|
// Matches Go PacketDataHandler in packet.go
|
|
28
|
-
using PacketDataHandler = std::function<Error(const std::string&
|
|
28
|
+
using PacketDataHandler = std::function<Error(const std::string &data)>;
|
|
29
29
|
|
|
30
30
|
// NewPacketDataHandler wraps a PacketHandler with a decoding step.
|
|
31
31
|
PacketDataHandler NewPacketDataHandler(PacketHandler handler);
|
|
32
32
|
|
|
33
33
|
// Validate performs cursory validation of a Packet.
|
|
34
|
-
Error ValidatePacket(const srpc::Packet&
|
|
34
|
+
Error ValidatePacket(const srpc::Packet &pkt);
|
|
35
35
|
|
|
36
36
|
// Validate performs cursory validation of a CallStart.
|
|
37
|
-
Error ValidateCallStart(const srpc::CallStart&
|
|
37
|
+
Error ValidateCallStart(const srpc::CallStart &pkt);
|
|
38
38
|
|
|
39
39
|
// Validate performs cursory validation of a CallData.
|
|
40
|
-
Error ValidateCallData(const srpc::CallData&
|
|
40
|
+
Error ValidateCallData(const srpc::CallData &pkt);
|
|
41
41
|
|
|
42
42
|
// NewCallStartPacket constructs a new CallStart packet.
|
|
43
|
-
std::unique_ptr<srpc::Packet> NewCallStartPacket(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
bool data_is_zero);
|
|
43
|
+
std::unique_ptr<srpc::Packet> NewCallStartPacket(const std::string &service,
|
|
44
|
+
const std::string &method,
|
|
45
|
+
const std::string &data,
|
|
46
|
+
bool data_is_zero);
|
|
48
47
|
|
|
49
48
|
// NewCallDataPacket constructs a new CallData packet.
|
|
50
|
-
std::unique_ptr<srpc::Packet> NewCallDataPacket(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
bool complete,
|
|
54
|
-
Error err);
|
|
49
|
+
std::unique_ptr<srpc::Packet> NewCallDataPacket(const std::string &data,
|
|
50
|
+
bool data_is_zero,
|
|
51
|
+
bool complete, Error err);
|
|
55
52
|
|
|
56
53
|
// NewCallCancelPacket constructs a new CallCancel packet with cancel.
|
|
57
54
|
std::unique_ptr<srpc::Packet> NewCallCancelPacket();
|
|
58
55
|
|
|
59
|
-
}
|
|
56
|
+
} // namespace starpc
|