9 #ifndef INCLUDE_FUNAPI_NETWORK_SESSION_H_ 10 #define INCLUDE_FUNAPI_NETWORK_SESSION_H_ 12 #include <boost/noncopyable.hpp> 13 #include <boost/unordered_set.hpp> 17 #include <funapi/network/fun_message.pb.h> 31 typedef Uuid SessionId;
34 enum TransportProtocol {
51 kDefaultEncryption = 100,
53 kIFunEngine1Encryption,
54 kIFunEngine2Encryption,
74 class FUNAPI_DLL_VISIBILITY
Session :
private boost::noncopyable {
78 typedef boost::unordered_set<Ptr<Session> > SessionsSet;
82 static Ptr<Session> Find(
const SessionId &session_id);
87 static SessionsSet FindWithTag(
const string &key);
92 static size_t CountWithTag(
const string &key);
99 static void BroadcastLocally(
const string &message_type,
101 Encryption encryption,
102 TransportProtocol protocol) ASSERT_NO_ROLLBACK;
109 static void BroadcastLocally(
const string &message_type,
110 const Ptr<FunMessage> &message,
111 Encryption encryption,
112 TransportProtocol protocol) ASSERT_NO_ROLLBACK;
119 static void BroadcastLocally(int32_t message_type,
120 const Ptr<FunMessage> &message,
121 Encryption encryption,
122 TransportProtocol protocol) ASSERT_NO_ROLLBACK;
130 const Ptr<FunMessage> &message,
131 Encryption encryption,
132 TransportProtocol protocol) ASSERT_NO_ROLLBACK;
140 static void BroadcastGlobally(
const string &message_type,
142 const Rpc::Tag &server_tag,
143 Encryption encryption,
144 TransportProtocol protocol) ASSERT_NO_ROLLBACK;
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;
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;
177 const Ptr<FunMessage> &message,
178 const Rpc::Tag &server_tag,
179 Encryption encryption,
180 TransportProtocol protocol) ASSERT_NO_ROLLBACK;
188 static void BroadcastGlobally(
const string &message_type,
190 const Rpc::PeerMap &servers,
191 Encryption encryption,
192 TransportProtocol protocol) ASSERT_NO_ROLLBACK;
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;
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;
225 const Ptr<FunMessage> &message,
226 const Rpc::PeerMap &servers,
227 Encryption encryption,
228 TransportProtocol protocol) ASSERT_NO_ROLLBACK;
235 virtual bool IsRestored()
const = 0;
238 virtual const SessionId &id()
const = 0;
246 virtual bool GetRemoteEndPoint(TransportProtocol protocol,
248 uint16_t *port = NULL)
const = 0;
251 virtual const string &LastReceivedMessageType()
const = 0;
254 virtual const string &LastSentMessageType()
const = 0;
260 virtual bool GetLastReceiveTime(
const string &message_type,
261 WallClock::Value *receive_time)
const = 0;
267 virtual bool GetLastSendTime(
const string &message_type,
268 WallClock::Value *send_time)
const = 0;
272 virtual WallClock::Value GetExpirationTime()
const = 0;
277 virtual bool IsTransportAttached()
const = 0;
283 virtual bool IsTransportAttached(TransportProtocol protocol)
const = 0;
286 virtual void CloseTransport() = 0;
292 virtual bool CloseTransport(TransportProtocol protocol) = 0;
299 virtual void SendMessage(
300 const string &message_type,
301 Encryption encryption = kDefaultEncryption,
302 TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
310 virtual void SendMessage(
311 const string &message_type,
313 Encryption encryption = kDefaultEncryption,
314 TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
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;
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;
348 virtual void SendMessage(
350 const Ptr<FunMessage> &message,
351 Encryption encryption = kDefaultEncryption,
352 TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
359 virtual void SendBackMessage(
361 Encryption encryption = kDefaultEncryption,
362 TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
369 virtual void SendBackMessage(
370 const Ptr<FunMessage> &message,
371 Encryption encryption = kDefaultEncryption,
372 TransportProtocol protocol = kDefaultProtocol) = 0 ASSERT_NO_ROLLBACK;
375 virtual void Close() = 0;
380 virtual bool IsOpened()
const = 0;
384 virtual void Tag(
const string &tag) = 0;
388 virtual void Untag(
const string &tag) = 0;
393 virtual bool HasTag(
const string &tag)
const = 0;
397 virtual std::set<string> GetTags()
const = 0;
405 virtual void SetContext(
const Json &ctxt) = 0;
411 virtual const Json &GetContext()
const = 0;
417 virtual Json &GetContext() = 0;
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;
432 virtual boost::mutex &GetContextMutex() = 0;
433 virtual operator boost::mutex &()
const = 0;
440 virtual void LockContext() = 0;
441 virtual void UnlockContext() = 0;
445 virtual void SetPingSamplingInterval(
size_t seconds) = 0;
446 virtual void SetPingSamplingInterval(
size_t seconds,
447 TransportProtocol protocol) = 0;
451 virtual size_t GetPingSamplingInterval()
const = 0;
452 virtual size_t GetPingSamplingInterval(TransportProtocol protocol)
const = 0;
457 virtual void SetPingTimeout(
size_t seconds) = 0;
458 virtual void SetPingTimeout(
size_t seconds, TransportProtocol protocol) = 0;
462 virtual size_t GetPingTimeout()
const = 0;
463 virtual size_t GetPingTimeout(TransportProtocol protocol)
const = 0;
467 virtual Ping GetPing()
const = 0;
468 virtual Ping GetPing(TransportProtocol protocol)
const = 0;
475 FUNAPI_DLL_VISIBILITY
void SetTransport(TransportProtocol protocol);
477 FUNAPI_DLL_VISIBILITY
478 void SetTransport(TransportProtocol protocol,
const string &msgtype);
485 typedef function<void (const Ptr<Session> &)>
493 typedef function<void (const Ptr<Session> &)>
497 typedef function<void (const Ptr<Session> &)>
501 typedef function<void (const Ptr<Session> &)>
505 typedef function<void (const Ptr<Session> &)>
512 typedef function<void (const Ptr<Session> &,
516 typedef function<void (const Ptr<Session> &,
522 static void Install();
525 static void Uninstall();
527 static void RegisterTcpTransportAttachedHandler(
530 static void RegisterTcpTransportDetachedHandler(
533 static void RegisterWebSocketTransportAttachedHandler(
536 static void RegisterWebSocketTransportDetachedHandler(
539 static void RegisterSessionRestoredHandler(
542 static void Register(
const string &message_type,
544 static void Register(
const string &message_type,
547 static void Register2(
const string &message_type,
549 static void Register2(int32_t message_type,
562 typedef function<void(const Ptr<Session> &,
563 const Ptr<const FunMessage> &,
565 size_t message_size)> ProtobufMessageSendHook;
566 typedef function<void(const Ptr<Session> &,
569 size_t message_size)> JsonMessageSendHook;
572 FUNAPI_DLL_VISIBILITY
573 void InstallProtobufMessageSendHook(
const ProtobufMessageSendHook &hook);
575 FUNAPI_DLL_VISIBILITY
576 void InstallJsonMessageSendHook(
const JsonMessageSendHook &hook);
579 typedef function<bool(
580 const Ptr<Session> &,
581 const Ptr<const FunMessage> &,
582 const string &)> ProtobufPreMessageHandlerHook;
583 typedef function<void(
584 const Ptr<Session> &,
585 const Ptr<const FunMessage> &,
586 const string &)> ProtobufPostMessageHandlerHook;
587 typedef function<bool(
588 const Ptr<Session> &,
589 const Ptr<const FunMessage> &,
590 const int32_t )> ProtobufPreMessageHandlerHook2;
591 typedef function<void(
592 const Ptr<Session> &,
593 const Ptr<const FunMessage> &,
594 const int32_t )> ProtobufPostMessageHandlerHook2;
595 typedef function<bool(
596 const Ptr<Session> &,
598 const string & )> JsonPreMessageHandlerHook;
599 typedef function<void(
600 const Ptr<Session> &,
602 const string &)> JsonPostMessageHandlerHook;
605 FUNAPI_DLL_VISIBILITY
void InstallProtobufMessageHandlerHook(
606 const ProtobufPreMessageHandlerHook &protobuf_pre_message_handler_hook,
607 const ProtobufPostMessageHandlerHook &protobuf_post_message_handler_hook);
609 FUNAPI_DLL_VISIBILITY
void InstallProtobufMessageHandlerHook2(
610 const ProtobufPreMessageHandlerHook2 &protobuf_pre_message_handler_hook,
611 const ProtobufPostMessageHandlerHook2 &protobuf_post_message_handler_hook);
613 FUNAPI_DLL_VISIBILITY
void InstallJsonMessageHandlerHook(
614 const JsonPreMessageHandlerHook &json_pre_message_handler_hook,
615 const JsonPostMessageHandlerHook &json_post_message_handler_hook);
620 FUNAPI_DLL_VISIBILITY
void OverrideSessionTimeout(
621 const Ptr<Session> &session,
const int64_t timeout_in_ms);
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());
635 inline int32_t field_tag_number()
const {
return field_tag_number_; }
636 inline string field_tag_name()
const {
return field_tag_name_; }
639 void ExtractFieldTagName(
const string &type_name);
640 int32_t field_tag_number_;
641 string field_tag_name_;
645 FUNAPI_DLL_VISIBILITY
inline bool operator==(
647 return lhs == rhs.field_tag_number();
651 FUNAPI_DLL_VISIBILITY
inline bool operator==(
653 return lhs.field_tag_number() == rhs;
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
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
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