iFun Engine API  1.0.0-b6053
Great Technology for Great Games
session.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_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 
24 #ifndef _WIN32
25 #include <cxxabi.h>
26 #endif // _WIN32
27 
28 
29 namespace fun {
30 
31 typedef Uuid SessionId;
32 
33 
34 enum TransportProtocol {
35  kDefaultProtocol = 0,
36  kTcp,
37  kUdp,
38  kHttp,
39  kWebSocket,
40 };
41 
42 
43 enum EncodingScheme {
44  kUnknownEncoding = 0,
45  kJsonEncoding,
46  kProtobufEncoding,
47 };
48 
49 
50 enum Encryption {
51  kDefaultEncryption = 100,
52  kDummyEncryption,
53  kIFunEngine1Encryption,
54  kIFunEngine2Encryption,
55  kChacha20Encryption,
56  kAes128Encryption,
57 };
58 
59 
68 };
69 
70 
72 
73 
74 class FUNAPI_DLL_VISIBILITY Session : private boost::noncopyable {
75  public:
77 
78  typedef boost::unordered_set<Ptr<Session> > SessionsSet;
79  typedef std::pair<MonotonicClock::Duration /*ping_time*/,
80  size_t /*sample_count*/> Ping;
81 
82  static Ptr<Session> Find(const SessionId &session_id);
83 
87  static SessionsSet FindWithTag(const string &key);
88 
92  static size_t CountWithTag(const string &key);
93 
99  static void BroadcastLocally(const string &message_type,
100  const Json &message,
101  Encryption encryption,
102  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
103 
109  static void BroadcastLocally(const string &message_type,
110  const Ptr<FunMessage> &message,
111  Encryption encryption,
112  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
113 
119  static void BroadcastLocally(int32_t message_type,
120  const Ptr<FunMessage> &message,
121  Encryption encryption,
122  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
123 
129  static void BroadcastLocally(const ProtobufExtensionIdentifier &message_type,
130  const Ptr<FunMessage> &message,
131  Encryption encryption,
132  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
133 
140  static void BroadcastGlobally(const string &message_type,
141  const Json &message,
142  const Rpc::Tag &server_tag,
143  Encryption encryption,
144  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
145 
152  static void BroadcastGlobally(const string &message_type,
153  const Ptr<FunMessage> &message,
154  const Rpc::Tag &server_tag,
155  Encryption encryption,
156  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
157 
164  static void BroadcastGlobally(int32_t message_type,
165  const Ptr<FunMessage> &message,
166  const Rpc::Tag &server_tag,
167  Encryption encryption,
168  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
169 
176  static void BroadcastGlobally(const ProtobufExtensionIdentifier &message_type,
177  const Ptr<FunMessage> &message,
178  const Rpc::Tag &server_tag,
179  Encryption encryption,
180  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
181 
188  static void BroadcastGlobally(const string &message_type,
189  const Json &message,
190  const Rpc::PeerMap &servers,
191  Encryption encryption,
192  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
193 
200  static void BroadcastGlobally(const string &message_type,
201  const Ptr<FunMessage> &message,
202  const Rpc::PeerMap &servers,
203  Encryption encryption,
204  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
205 
212  static void BroadcastGlobally(int32_t message_type,
213  const Ptr<FunMessage> &message,
214  const Rpc::PeerMap &servers,
215  Encryption encryption,
216  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
217 
224  static void BroadcastGlobally(const ProtobufExtensionIdentifier &message_type,
225  const Ptr<FunMessage> &message,
226  const Rpc::PeerMap &servers,
227  Encryption encryption,
228  TransportProtocol protocol) ASSERT_NO_ROLLBACK;
229 
230  virtual ~Session();
231 
232 
235  virtual bool IsRestored() const = 0;
236 
238  virtual const SessionId &id() const = 0;
239 
246  virtual bool GetRemoteEndPoint(TransportProtocol protocol,
247  string *ip,
248  uint16_t *port = NULL) const = 0;
249 
251  virtual const string &LastReceivedMessageType() const = 0;
252 
254  virtual const string &LastSentMessageType() const = 0;
255 
260  virtual bool GetLastReceiveTime(const string &message_type,
261  WallClock::Value *receive_time) const = 0;
262 
267  virtual bool GetLastSendTime(const string &message_type,
268  WallClock::Value *send_time) const = 0;
269 
272  virtual WallClock::Value GetExpirationTime() const = 0;
273 
275  //
277  virtual bool IsTransportAttached() const = 0;
278 
283  virtual bool IsTransportAttached(TransportProtocol protocol) const = 0;
284 
286  virtual void CloseTransport() = 0;
287 
292  virtual bool CloseTransport(TransportProtocol protocol) = 0;
293 
299  virtual void SendMessage(
300  const string &message_type,
301  Encryption encryption = kDefaultEncryption,
302  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
303 
310  virtual void SendMessage(
311  const string &message_type,
312  const Json &message,
313  Encryption encryption = kDefaultEncryption,
314  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
315 
322  virtual void SendMessage(
323  const string &message_type,
324  const Ptr<FunMessage> &message,
325  Encryption encryption = kDefaultEncryption,
326  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
327 
334  virtual void SendMessage(
335  int32_t message_type,
336  const Ptr<FunMessage> &message,
337  Encryption encryption = kDefaultEncryption,
338  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
339 
340 
348  virtual void SendMessage(
349  const ProtobufExtensionIdentifier &message_type,
350  const Ptr<FunMessage> &message,
351  Encryption encryption = kDefaultEncryption,
352  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
353 
359  virtual void SendBackMessage(
360  const Json &message,
361  Encryption encryption = kDefaultEncryption,
362  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
363 
369  virtual void SendBackMessage(
370  const Ptr<FunMessage> &message,
371  Encryption encryption = kDefaultEncryption,
372  TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
373 
375  virtual void Close() = 0;
376 
378  //
380  virtual bool IsOpened() const = 0;
381 
384  virtual void Tag(const string &tag) = 0;
385 
388  virtual void Untag(const string &tag) = 0;
389 
393  virtual bool HasTag(const string &tag) const = 0;
394 
397  virtual std::set<string> GetTags() const = 0;
398 
404  // @param ctxt Json instance containting a context.
405  virtual void SetContext(const Json &ctxt) = 0;
406 
411  virtual const Json &GetContext() const = 0;
412 
417  virtual Json &GetContext() = 0;
418 
421  virtual void AddToContext(const string &key, const string &value) = 0;
422  virtual void AddToContext(const string &key, const int64_t &value) = 0;
423  virtual bool GetFromContext(const string &key, string *ret) const = 0;
424  virtual bool GetFromContext(const string &key, int64_t *ret) const = 0;
425  virtual bool DeleteFromContext(const string &key) = 0;
426 
432  virtual boost::mutex &GetContextMutex() = 0;
433  virtual operator boost::mutex &() const = 0;
434 
440  virtual void LockContext() = 0;
441  virtual void UnlockContext() = 0;
442 
445  virtual void SetPingSamplingInterval(size_t seconds) = 0;
446  virtual void SetPingSamplingInterval(size_t seconds,
447  TransportProtocol protocol) = 0;
448 
451  virtual size_t GetPingSamplingInterval() const = 0;
452  virtual size_t GetPingSamplingInterval(TransportProtocol protocol) const = 0;
453 
457  virtual void SetPingTimeout(size_t seconds) = 0;
458  virtual void SetPingTimeout(size_t seconds, TransportProtocol protocol) = 0;
459 
462  virtual size_t GetPingTimeout() const = 0;
463  virtual size_t GetPingTimeout(TransportProtocol protocol) const = 0;
464 
467  virtual Ping GetPing() const = 0;
468  virtual Ping GetPing(TransportProtocol protocol) const = 0;
469 
470  protected:
471  Session();
472 };
473 
474 
475 FUNAPI_DLL_VISIBILITY void SetTransport(TransportProtocol protocol);
476 
477 FUNAPI_DLL_VISIBILITY
478 void SetTransport(TransportProtocol protocol, const string &msgtype);
479 
480 
482 typedef function<void (const Ptr<Session> &/*session*/)> SessionOpenedHandler;
483 
485 typedef function<void (const Ptr<Session> &/*session*/)>
487 
489 typedef function<void (const Ptr<Session> &/*session*/, SessionCloseReason)>
491 
493 typedef function<void (const Ptr<Session> &/*session*/)>
495 
497 typedef function<void (const Ptr<Session> &/*session*/)>
499 
501 typedef function<void (const Ptr<Session> &/*session*/)>
503 
505 typedef function<void (const Ptr<Session> &/*session*/)>
507 
509 typedef function<void (const Ptr<Session> &/*session*/)> SessionRestoredHandler;
510 
512 typedef function<void (const Ptr<Session> &/*session*/,
513  const Json &/*message*/)> MessageHandler;
514 
516 typedef function<void (const Ptr<Session> &/*session*/,
517  const Ptr<FunMessage> &/*message*/)> MessageHandler2;
518 
519 
520 class FUNAPI_DLL_VISIBILITY HandlerRegistry {
521  public:
522  static void Install();
523  static void Install2(const SessionOpenedHandler &opened_handler,
524  const SessionClosedHandler2 &closed_handler);
525  static void Uninstall();
526 
527  static void RegisterTcpTransportAttachedHandler(
528  const TcpTransportAttachedHandler &tcp_attached_handler);
529 
530  static void RegisterTcpTransportDetachedHandler(
531  const TcpTransportDetachedHandler &tcp_detached_handler);
532 
533  static void RegisterWebSocketTransportAttachedHandler(
534  const WebSocketTransportAttachedHandler &websocket_attached_handler);
535 
536  static void RegisterWebSocketTransportDetachedHandler(
537  const WebSocketTransportDetachedHandler &websocket_detached_handler);
538 
539  static void RegisterSessionRestoredHandler(
540  const SessionRestoredHandler &restored_handler);
541 
542  static void Register(const string &message_type,
543  const MessageHandler &message_handler);
544  static void Register(const string &message_type,
545  const MessageHandler &message_handler,
546  const JsonSchema &message_schema);
547  static void Register2(const string &message_type,
548  const MessageHandler2 &message_handler);
549  static void Register2(int32_t message_type,
550  const MessageHandler2 &message_handler);
551  static void Register2(const ProtobufExtensionIdentifier &message_type,
552  const MessageHandler2 &message_handler);
553 
555  // Belows have been deprecated.
556  // Consider using alternatives.
557  DEPRECATED(static void Install(const SessionOpenedHandler &opened_handler, \
558  const SessionClosedHandler &closed_handler));
559 };
560 
561 
562 typedef function<void(const Ptr<Session> &/*session*/,
563  const Ptr<const FunMessage> &/*message*/,
564  const string &/*message_type*/,
565  size_t message_size)> ProtobufMessageSendHook;
566 typedef function<void(const Ptr<Session> &/*session*/,
567  const Json &/*message*/,
568  const string &/*message_type*/,
569  size_t message_size)> JsonMessageSendHook;
570 
571 
572 FUNAPI_DLL_VISIBILITY
573 void InstallProtobufMessageSendHook(const ProtobufMessageSendHook &hook);
574 
575 FUNAPI_DLL_VISIBILITY
576 void InstallJsonMessageSendHook(const JsonMessageSendHook &hook);
577 
578 
579 typedef function<bool(
580  const Ptr<Session> &/*session*/,
581  const Ptr<const FunMessage> &/*message*/,
582  const string &/*message type*/)> ProtobufPreMessageHandlerHook;
583 typedef function<void(
584  const Ptr<Session> &/*session*/,
585  const Ptr<const FunMessage> &/*message*/,
586  const string &/*message type*/)> ProtobufPostMessageHandlerHook;
587 typedef function<bool(
588  const Ptr<Session> &/*session*/,
589  const Ptr<const FunMessage> &/*message*/,
590  const int32_t /*message_type*/)> ProtobufPreMessageHandlerHook2;
591 typedef function<void(
592  const Ptr<Session> &/*session*/,
593  const Ptr<const FunMessage> &/*message*/,
594  const int32_t /*message_type*/)> ProtobufPostMessageHandlerHook2;
595 typedef function<bool(
596  const Ptr<Session> &/*session*/,
597  const Json &/*message*/,
598  const string & /*message type*/)> JsonPreMessageHandlerHook;
599 typedef function<void(
600  const Ptr<Session> &/*session*/,
601  const Json &/*message*/,
602  const string &/*message type*/)> JsonPostMessageHandlerHook;
603 
604 
605 FUNAPI_DLL_VISIBILITY void InstallProtobufMessageHandlerHook(
606  const ProtobufPreMessageHandlerHook &protobuf_pre_message_handler_hook,
607  const ProtobufPostMessageHandlerHook &protobuf_post_message_handler_hook);
608 
609 FUNAPI_DLL_VISIBILITY void InstallProtobufMessageHandlerHook2(
610  const ProtobufPreMessageHandlerHook2 &protobuf_pre_message_handler_hook,
611  const ProtobufPostMessageHandlerHook2 &protobuf_post_message_handler_hook);
612 
613 FUNAPI_DLL_VISIBILITY void InstallJsonMessageHandlerHook(
614  const JsonPreMessageHandlerHook &json_pre_message_handler_hook,
615  const JsonPostMessageHandlerHook &json_post_message_handler_hook);
616 
617 // Overrides 'session_timeout_in_second' value in MANIFEST.json.
618 // if the 'timeout_in_ms' value is 0, timeout will be disabled
619 // for the specified session.
620 FUNAPI_DLL_VISIBILITY void OverrideSessionTimeout(
621  const Ptr<Session> &session, const int64_t timeout_in_ms);
622 
623 
624 class FUNAPI_DLL_VISIBILITY ProtobufExtensionIdentifier {
625  public:
626  template <typename ExtendeeType, typename TraitsType,
627  ::google::protobuf::internal::FieldType field_type, bool is_packed>
629  const ::google::protobuf::internal::ExtensionIdentifier<ExtendeeType,
630  TraitsType, field_type, is_packed> &extension_identifier) {
631  field_tag_number_ = extension_identifier.number();
632  ExtractFieldTagName(typeid(TraitsType).name());
633  }
634 
635  inline int32_t field_tag_number() const { return field_tag_number_; }
636  inline string field_tag_name() const { return field_tag_name_; }
637 
638  private:
639  void ExtractFieldTagName(const string &type_name);
640  int32_t field_tag_number_;
641  string field_tag_name_;
642 };
643 
644 
645 FUNAPI_DLL_VISIBILITY inline bool operator==(
646  const int32_t lhs, const ProtobufExtensionIdentifier &rhs) {
647  return lhs == rhs.field_tag_number();
648 }
649 
650 
651 FUNAPI_DLL_VISIBILITY inline bool operator==(
652  const ProtobufExtensionIdentifier lhs, const int32_t &rhs) {
653  return lhs.field_tag_number() == rhs;
654 }
655 
656 } // namespace fun
657 
658 #endif // INCLUDE_FUNAPI_NETWORK_SESSION_H_
The session was idle for a long time.
Definition: session.h:63
HTTP.
Definition: http_util.h:56
function< void(const Ptr< Session > &)> SessionRestoredHandler
Handler to be notified on session restoration.
Definition: session.h:509
function< void(const Ptr< Session > &)> WebSocketTransportDetachedHandler
Handler to be notified on Websocket close if the session uses Websocket.
Definition: session.h:506
The session id is unknown.
Definition: session.h:64
SessionCloseReason
Enumeration to identify why a session has closed.
Definition: session.h:61
Definition: session.h:624
Server called Session::Close().
Definition: session.h:62
function< void(const Ptr< Session > &)> TcpTransportAttachedHandler
Handler to to noticed on TCP connect if the session uses TCP.
Definition: session.h:494
Client redirected to another server.
Definition: session.h:67
Definition: session.h:520
Definition: json_schema.h:20
function< void(const Ptr< Session > &, const Ptr< FunMessage > &)> MessageHandler2
Protobuf message handler type.
Definition: session.h:517
function< void(const Ptr< Session > &, const Json &)> MessageHandler
JSON message handler type.
Definition: session.h:513
function< void(const Ptr< Session > &)> SessionOpenedHandler
Handler to be notified on session creation.
Definition: session.h:482
function< void(const Ptr< Session > &)> SessionClosedHandler
Handler to be notified on session close. This one ignores the close reason.
Definition: session.h:486
Client requested to close the session.
Definition: session.h:66
Definition: json.h:18
Definition: session.h:74
function< void(const Ptr< Session > &)> TcpTransportDetachedHandler
Handler to be notified on TCP close if the session uses TCP.
Definition: session.h:498
boost::uuids::uuid Uuid
UUID type used throughout Funapi.
Definition: types.h:56
int64_t Duration
duration value in usec.
Definition: monotonic_clock.h:31
Definition: json.h:27
Event timeout occured.
Definition: session.h:65
function< void(const Ptr< Session > &, SessionCloseReason)> SessionClosedHandler2
Handler to be notified on session close. This one ignores the close reason.
Definition: session.h:490
#define DECLARE_CLASS_PTR(CLS)
Utility macro to forward-declare smart pointer types for a given class.
Definition: types.h:89
function< void(const Ptr< Session > &)> WebSocketTransportAttachedHandler
Handler to to noticed on Websocket connect if the session uses Websocket.
Definition: session.h:502