iFun Engine API  1.0.0-b6053
Great Technology for Great Games
rpc.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2020 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 
9 #ifndef INCLUDE_FUNAPI_DISTRIBUTION_RPC_H_
10 #define INCLUDE_FUNAPI_DISTRIBUTION_RPC_H_
11 
12 #include <boost/asio.hpp>
13 #include <boost/enable_shared_from_this.hpp>
14 #include <boost/function.hpp>
15 #include <boost/noncopyable.hpp>
16 #include <funapi/distribution/fun_rpc_message.pb.h>
18 #include <funapi/types.h>
19 
20 #include <map>
21 #include <set>
22 
23 
24 namespace fun {
25 
27 class FUNAPI_DLL_VISIBILITY Rpc : public boost::enable_shared_from_this<Rpc>,
28  private boost::noncopyable {
29  public:
30  typedef Uuid Xid;
31  typedef Uuid PeerId;
32  typedef Uuid SerializationTag;
33  typedef boost::asio::ip::tcp::endpoint PeerEndpoint;
34  typedef std::map<PeerId, PeerEndpoint> PeerMap;
35  typedef string Tag;
36  typedef std::set<Tag> Tags;
37 
39  typedef boost::function<void(const Ptr<FunRpcMessage> & /*reply*/)> ReadyBack ASSERT_NO_ROLLBACK;
40 
42  typedef boost::function<void(const PeerId & /*new server*/)> ConnectHandler;
43 
45  typedef boost::function<void(const PeerId & /*server*/)> DisconnectHandler;
46 
48  typedef boost::function<
49  void(const PeerId & /*sender*/, const Xid & /*xid*/,
50  const Ptr<const FunRpcMessage> & /*request*/,
51  const ReadyBack & /*finisher*/)> Handler;
52 
53  typedef boost::function<
54  void(const PeerId & /*sender*/, const Xid & /*xid*/,
55  const Ptr<const FunRpcMessage> & /*request*/)> VoidReplyHandler;
56 
58  typedef boost::function<
59  void(const PeerId & /*sender*/, const Xid & /*xid*/,
60  const Ptr<const FunRpcMessage> & /*reply*/)> Callback;
61 
62  static const Xid kNullXid;
63  static const PeerId kNullPeerId;
64  static const SerializationTag kDefaultSerializationTag;
65 
68  static void AddTag(const Tag &tag);
69 
72  static void AddTags(const Tags &tags);
73 
76  static void RemoveTag(const Tag &tag);
77 
80  static void RemoveTags(const Tags &tags);
81 
85  static bool HasTag(const Tag &tag);
86 
88  static Json GetStatus();
89 
92  static void SetStatus(const Json &status);
93 
96  static PeerId GetSelfId();
97 
100  static PeerEndpoint GetSelfEndpoint();
101 
106  static size_t GetPeers(PeerMap *ret, bool include_self = false);
107 
113  static size_t GetPeersWithTag(PeerMap *ret, const Tag &tag,
114  bool include_self = false);
115 
120  static size_t GetPeerTags(Tags *ret, const PeerId &peer);
121 
128  static std::string GetPeerExternalHostname(const PeerId &peer);
129 
135  static boost::asio::ip::address GetPeerExternalIp(const PeerId &peer);
136 
142  static HardwareInfo::ProtocolPortMap GetPeerExternalPorts(const PeerId &peer);
143 
146  static bool IsExternalPeer(const PeerId &peer);
147 
150  static Json GetPeerStatus(const PeerId &peer);
151 
155  static bool IsPeerConnected(const PeerId &peer);
156 
158  static void RegisterConnectHandler(const ConnectHandler &h);
159 
161  static void RegisterDisconnectHandler(const DisconnectHandler &h);
162 
167  static void RegisterHandler(const string &type, const Handler &h);
168 
174  static void Call(
175  const PeerId &peer,
176  const Ptr<FunRpcMessage> &msg,
177  const Callback &callback,
178  const SerializationTag &tag = kDefaultSerializationTag)
180 
185  static void RegisterVoidReplyHandler(const string &type,
186  const VoidReplyHandler &h);
187 
192  static void Call(
193  const PeerId &peer,
194  const Ptr<FunRpcMessage> &msg,
195  const SerializationTag &tag = kDefaultSerializationTag)
196  ASSERT_NO_ROLLBACK;
197 };
198 
199 } // namespace fun
200 
201 #endif // INCLUDE_FUNAPI_DISTRIBUTION_RPC_H_
boost::function< void(const Ptr< FunRpcMessage > &)> ReadyBack ASSERT_NO_ROLLBACK
Functor to signal that a RPC server finished processing.
Definition: rpc.h:39
boost::function< void(const PeerId &)> DisconnectHandler
Functor to be invoked when a server disconnected.
Definition: rpc.h:45
Definition: json.h:18
boost::function< void(const PeerId &, const Xid &, const Ptr< const FunRpcMessage > &, const ReadyBack &)> Handler
Functor to be invoked when a RPC server receives RPC messages.
Definition: rpc.h:51
boost::uuids::uuid Uuid
UUID type used throughout Funapi.
Definition: types.h:56
boost::function< void(const PeerId &, const Xid &, const Ptr< const FunRpcMessage > &)> Callback
Functor to be invoked when a RPC client receives back a reply.
Definition: rpc.h:60
Definition: json.h:27
Funapi RPC interface.
Definition: rpc.h:27
boost::function< void(const PeerId &)> ConnectHandler
Functor to be invoked when a new server connected.
Definition: rpc.h:42