iFun Engine API  1.0.0-b3630
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
session.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2017 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_NETWORK_SESSION_H_
10 #define INCLUDE_FUNAPI_NETWORK_SESSION_H_
11 
12 #include <boost/noncopyable.hpp>
13 #include <boost/unordered_set.hpp>
14 #include <funapi/common/json.h>
17 #include <funapi/network/fun_message.pb.h>
19 #include <funapi/time/wall_clock.h>
20 #include <funapi/types.h>
21 
22 #include <vector>
23 #include <cxxabi.h>
24 
25 
26 namespace fun {
27 
28 typedef Uuid SessionId;
29 
30 
31 enum TransportProtocol {
32  kDefaultProtocol = 0,
33  kTcp,
34  kUdp,
35  kHttp,
36  kWebSocket,
37 };
38 
39 
40 enum EncodingScheme {
41  kUnknownEncoding = 0,
42  kJsonEncoding,
43  kProtobufEncoding,
44 };
45 
46 
47 enum Encryption {
48  kDefaultEncryption = 100,
49  kDummyEncryption,
50  kIFunEngine1Encryption,
51  kIFunEngine2Encryption,
52  kChacha20Encryption,
53  kAes128Encryption,
54 };
55 
56 
58 enum SessionCloseReason {
59  kClosedForServerDid = 0,
60  kClosedForIdle,
61  kClosedForUnknownSessionId,
62 };
63 
64 
65 class ProtobufExtensionIdentifier;
66 
67 
68 class Session : private boost::noncopyable {
69  public:
70  DECLARE_CLASS_PTR(Session);
71 
72  typedef boost::unordered_set<Ptr<Session> > SessionsSet;
73  typedef std::pair<MonotonicClock::Duration /*ping_time*/,
74  size_t /*sample_count*/> Ping;
75 
76  static Ptr<Session> Find(const SessionId &session_id);
77 
81  static SessionsSet FindWithTag(const string &key);
82 
88  static void BroadcastLocally(const string &message_type,
89  const Json &message,
90  Encryption encryption,
91  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
92 
98  static void BroadcastLocally(const string &message_type,
99  const Ptr<FunMessage> &message,
100  Encryption encryption,
101  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
102 
108  static void BroadcastLocally(int32_t message_type,
109  const Ptr<FunMessage> &message,
110  Encryption encryption,
111  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
112 
118  static void BroadcastLocally(const ProtobufExtensionIdentifier &message_type,
119  const Ptr<FunMessage> &message,
120  Encryption encryption,
121  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
122 
129  static void BroadcastGlobally(const string &message_type,
130  const Json &message,
131  const Rpc::Tag &server_tag,
132  Encryption encryption,
133  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
134 
141  static void BroadcastGlobally(const string &message_type,
142  const Ptr<FunMessage> &message,
143  const Rpc::Tag &server_tag,
144  Encryption encryption,
145  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
146 
153  static void BroadcastGlobally(int32_t message_type,
154  const Ptr<FunMessage> &message,
155  const Rpc::Tag &server_tag,
156  Encryption encryption,
157  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
158 
165  static void BroadcastGlobally(const ProtobufExtensionIdentifier &message_type,
166  const Ptr<FunMessage> &message,
167  const Rpc::Tag &server_tag,
168  Encryption encryption,
169  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
170 
177  static void BroadcastGlobally(const string &message_type,
178  const Json &message,
179  const Rpc::PeerMap &servers,
180  Encryption encryption,
181  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
182 
189  static void BroadcastGlobally(const string &message_type,
190  const Ptr<FunMessage> &message,
191  const Rpc::PeerMap &servers,
192  Encryption encryption,
193  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
194 
201  static void BroadcastGlobally(int32_t message_type,
202  const Ptr<FunMessage> &message,
203  const Rpc::PeerMap &servers,
204  Encryption encryption,
205  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
206 
213  static void BroadcastGlobally(const ProtobufExtensionIdentifier &message_type,
214  const Ptr<FunMessage> &message,
215  const Rpc::PeerMap &servers,
216  Encryption encryption,
217  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
218 
219  virtual ~Session();
220 
221 
224  virtual bool IsRestored() const = 0;
225 
227  virtual const SessionId &id() const = 0;
228 
235  virtual bool GetRemoteEndPoint(TransportProtocol protocol,
236  string *ip,
237  uint16_t *port = NULL) const = 0;
238 
240  virtual const string &LastReceivedMessageType() const = 0;
241 
243  virtual const string &LastSentMessageType() const = 0;
244 
249  virtual bool GetLastReceiveTime(const string &message_type,
250  WallClock::Value *receive_time) const = 0;
251 
256  virtual bool GetLastSendTime(const string &message_type,
257  WallClock::Value *send_time) const = 0;
258 
261  virtual WallClock::Value GetExpirationTime() const = 0;
262 
264  //
266  virtual bool IsTransportAttached() const = 0;
267 
272  virtual bool IsTransportAttached(TransportProtocol protocol) const = 0;
273 
275  virtual void CloseTransport() = 0;
276 
281  virtual bool CloseTransport(TransportProtocol protocol) = 0;
282 
288  virtual void SendMessage(
289  const string &message_type,
290  Encryption encryption = kDefaultEncryption,
291  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
292 
299  virtual void SendMessage(
300  const string &message_type,
301  const Json &message,
302  Encryption encryption = kDefaultEncryption,
303  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
304 
311  virtual void SendMessage(
312  const string &message_type,
313  const Ptr<FunMessage> &message,
314  Encryption encryption = kDefaultEncryption,
315  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
316 
323  virtual void SendMessage(
324  int32_t message_type,
325  const Ptr<FunMessage> &message,
326  Encryption encryption = kDefaultEncryption,
327  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
328 
329 
337  virtual void SendMessage(
338  const ProtobufExtensionIdentifier &message_type,
339  const Ptr<FunMessage> &message,
340  Encryption encryption = kDefaultEncryption,
341  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
342 
348  virtual void SendBackMessage(
349  const Json &message,
350  Encryption encryption = kDefaultEncryption,
351  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
352 
358  virtual void SendBackMessage(
359  const Ptr<FunMessage> &message,
360  Encryption encryption = kDefaultEncryption,
361  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
362 
364  virtual void Close() = 0;
365 
367  //
369  virtual bool IsOpened() const = 0;
370 
373  virtual void Tag(const string &tag) = 0;
374 
377  virtual void Untag(const string &tag) = 0;
378 
382  virtual bool HasTag(const string &tag) const = 0;
383 
386  virtual std::set<string> GetTags() const = 0;
387 
393  // @param ctxt Json instance containting a context.
394  virtual void SetContext(const Json &ctxt) = 0;
395 
400  virtual const Json &GetContext() const = 0;
401 
406  virtual Json &GetContext() = 0;
407 
410  virtual void AddToContext(const string &key, const string &value) = 0;
411  virtual void AddToContext(const string &key, const int64_t &value) = 0;
412  virtual bool GetFromContext(const string &key, string *ret) const = 0;
413  virtual bool GetFromContext(const string &key, int64_t *ret) const = 0;
414  virtual bool DeleteFromContext(const string &key) = 0;
415 
421  virtual boost::mutex &GetContextMutex() = 0;
422  virtual operator boost::mutex &() const = 0;
423 
429  virtual void LockContext() = 0;
430  virtual void UnlockContext() = 0;
431 
434  virtual void SetPingSamplingInterval(size_t seconds) = 0;
435 
439  virtual void SetPingTimeout(size_t seconds) = 0;
440 
443  virtual Ping GetPing() const = 0;
444 
445  protected:
446  Session();
447 };
448 
449 
450 void SetTransport(TransportProtocol protocol);
451 void SetTransport(TransportProtocol protocol, const string &msgtype);
452 
453 
455 typedef function<void (const Ptr<Session> &/*session*/)> SessionOpenedHandler;
456 
458 typedef function<void (const Ptr<Session> &/*session*/)>
459  SessionClosedHandler;
460 
462 typedef function<void (const Ptr<Session> &/*session*/, SessionCloseReason)>
463  SessionClosedHandler2;
464 
466 typedef function<void (const Ptr<Session> &/*session*/)>
467  TcpTransportAttachedHandler;
468 
470 typedef function<void (const Ptr<Session> &/*session*/)>
471  TcpTransportDetachedHandler;
472 
474 typedef function<void (const Ptr<Session> &/*session*/)> SessionRestoredHandler;
475 
477 typedef function<void (const Ptr<Session> &/*session*/,
478  const Json &/*message*/)> MessageHandler;
479 
481 typedef function<void (const Ptr<Session> &/*session*/,
482  const Ptr<FunMessage> &/*message*/)> MessageHandler2;
483 
484 
486  public:
487  static void Install();
488  static void Install2(const SessionOpenedHandler &opened_handler,
489  const SessionClosedHandler2 &closed_handler);
490  static void Uninstall();
491 
492  static void RegisterTcpTransportAttachedHandler(
493  const TcpTransportAttachedHandler &tcp_attached_handler);
494 
495  static void RegisterTcpTransportDetachedHandler(
496  const TcpTransportDetachedHandler &tcp_detached_handler);
497 
498  static void RegisterSessionRestoredHandler(
499  const SessionRestoredHandler &restored_handler);
500 
501  static void Register(const string &message_type,
502  const MessageHandler &message_handler);
503  static void Register(const string &message_type,
504  const MessageHandler &message_handler,
505  const JsonSchema &message_schema);
506  static void Register2(const string &message_type,
507  const MessageHandler2 &message_handler);
508  static void Register2(int32_t message_type,
509  const MessageHandler2 &message_handler);
510  static void Register2(const ProtobufExtensionIdentifier &message_type,
511  const MessageHandler2 &message_handler);
512 
514  // Belows have been deprecated.
515  // Consider using alternatives.
516  DEPRECATED(static void Install(const SessionOpenedHandler &opened_handler, \
517  const SessionClosedHandler &closed_handler));
518 };
519 
520 
521 typedef function<void(const Ptr<Session> &/*session*/,
522  const Ptr<const FunMessage> &/*message*/,
523  const string &/*message_type*/,
524  size_t message_size)> ProtobufMessageSendHook;
525 typedef function<void(const Ptr<Session> &/*session*/,
526  const Json &/*message*/,
527  const string &/*message_type*/,
528  size_t message_size)> JsonMessageSendHook;
529 
530 
531 void InstallProtobufMessageSendHook(const ProtobufMessageSendHook &hook);
532 void InstallJsonMessageSendHook(const JsonMessageSendHook &hook);
533 
534 
535 typedef function<bool(
536  const Ptr<Session> &/*session*/,
537  const Ptr<const FunMessage> &/*message*/,
538  const string &/*message type*/)> ProtobufPreMessageHandlerHook;
539 typedef function<void(
540  const Ptr<Session> &/*session*/,
541  const Ptr<const FunMessage> &/*message*/,
542  const string &/*message type*/)> ProtobufPostMessageHandlerHook;
543 typedef function<bool(
544  const Ptr<Session> &/*session*/,
545  const Ptr<const FunMessage> &/*message*/,
546  const int32_t /*message_type*/)> ProtobufPreMessageHandlerHook2;
547 typedef function<void(
548  const Ptr<Session> &/*session*/,
549  const Ptr<const FunMessage> &/*message*/,
550  const int32_t /*message_type*/)> ProtobufPostMessageHandlerHook2;
551 typedef function<bool(
552  const Ptr<Session> &/*session*/,
553  const Json &/*message*/,
554  const string & /*message type*/)> JsonPreMessageHandlerHook;
555 typedef function<void(
556  const Ptr<Session> &/*session*/,
557  const Json &/*message*/,
558  const string &/*message type*/)> JsonPostMessageHandlerHook;
559 
560 
561 void InstallProtobufMessageHandlerHook(
562  const ProtobufPreMessageHandlerHook &protobuf_pre_message_handler_hook,
563  const ProtobufPostMessageHandlerHook &protobuf_post_message_handler_hook);
564 void InstallProtobufMessageHandlerHook2(
565  const ProtobufPreMessageHandlerHook2 &protobuf_pre_message_handler_hook,
566  const ProtobufPostMessageHandlerHook2 &protobuf_post_message_handler_hook);
567 void InstallJsonMessageHandlerHook(
568  const JsonPreMessageHandlerHook &json_pre_message_handler_hook,
569  const JsonPostMessageHandlerHook &json_post_message_handler_hook);
570 
571 
573  public:
574  template <typename ExtendeeType, typename TraitsType,
575  ::google::protobuf::internal::FieldType field_type, bool is_packed>
577  const ::google::protobuf::internal::ExtensionIdentifier<ExtendeeType,
578  TraitsType, field_type, is_packed> &extension_identifier) {
579  field_tag_number_ = extension_identifier.number();
580  ExtractFieldTagName(typeid(TraitsType).name());
581  }
582 
583  inline int32_t field_tag_number() const { return field_tag_number_; }
584  inline string field_tag_name() const { return field_tag_name_; }
585 
586  private:
587  void ExtractFieldTagName(const string &type_name);
588  int32_t field_tag_number_;
589  string field_tag_name_;
590 };
591 
592 
593 inline bool operator==(
594  const int32_t lhs, const ProtobufExtensionIdentifier &rhs) {
595  return lhs == rhs.field_tag_number();
596 }
597 
598 
599 inline bool operator==(
600  const ProtobufExtensionIdentifier lhs, const int32_t &rhs) {
601  return lhs.field_tag_number() == rhs;
602 }
603 
604 } // namespace fun
605 
606 #endif // INCLUDE_FUNAPI_NETWORK_SESSION_H_
virtual void CloseTransport()=0
Closes all the attached transport.
virtual bool GetLastSendTime(const string &message_type, WallClock::Value *send_time) const =0
Gets the last time sent a specified message.
virtual void SetContext(const Json &ctxt)=0
Associate the session with the given user-prvided context.
virtual void Untag(const string &tag)=0
Untags the session.
static void BroadcastLocally(const string &message_type, const Json &message, Encryption encryption, TransportProtocol protocol) ASSERT_NO_ROLLBACK
Broadcasts a Json message to all the local sessions.
virtual void SetPingTimeout(size_t seconds)=0
Sets ping timeout.
static void BroadcastGlobally(const string &message_type, const Json &message, const Rpc::Tag &server_tag, Encryption encryption, TransportProtocol protocol) ASSERT_NO_ROLLBACK
Broadcasts a Json message to all the sessions of the given RPC servers.
virtual bool GetLastReceiveTime(const string &message_type, WallClock::Value *receive_time) const =0
Gets the last time received specified message.
virtual bool GetRemoteEndPoint(TransportProtocol protocol, string *ip, uint16_t *port=NULL) const =0
Gets a remote end point with the given protocol.
virtual void SendBackMessage(const Json &message, Encryption encryption=kDefaultEncryption, TransportProtocol protocol=kDefaultProtocol)=0ASSERT_NO_ROLLBACK
Send out given message in JSON with the last-seen message type.
virtual void Tag(const string &tag)=0
Tags the session.
Definition: session.h:572
virtual void LockContext()=0
Locks/Unlocks mutex of the session.
virtual bool IsRestored() const =0
Checks if the session is restored session or not.
virtual const Json & GetContext() const =0
Returns the associated context.
Definition: session.h:485
Definition: json_schema.h:20
virtual boost::mutex & GetContextMutex()=0
Returns mutex of the session for per-session synchronization.
virtual const SessionId & id() const =0
virtual bool IsTransportAttached() const =0
Checks if there is any transport attached to this session.
virtual bool IsOpened() const =0
Checks if the session is opened.
Definition: session.h:68
virtual void AddToContext(const string &key, const string &value)=0
Shortcuts to access the context.
int64_t Duration
duration value in usec.
Definition: monotonic_clock.h:25
Definition: json.h:27
static SessionsSet FindWithTag(const string &key)
Finds a session with the given key.
virtual bool HasTag(const string &tag) const =0
Checks if the session has the tag key.
virtual const string & LastReceivedMessageType() const =0
virtual std::set< string > GetTags() const =0
Returns tags associated with the session.
virtual void Close()=0
Closes the session explicitly.
virtual const string & LastSentMessageType() const =0
virtual void SetPingSamplingInterval(size_t seconds)=0
Sets ping sampling interval.
virtual WallClock::Value GetExpirationTime() const =0
Returns a time when the session gets expired.
virtual void SendMessage(const string &message_type, Encryption encryption=kDefaultEncryption, TransportProtocol protocol=kDefaultProtocol)=0ASSERT_NO_ROLLBACK
Sends out given message.
virtual Ping GetPing() const =0
Gets round-trip time to the client associated with the session.