iFun Engine API  1.0.0-b6053
Great Technology for Great Games
network.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_TEST_NETWORK_H_
10 #define INCLUDE_FUNAPI_TEST_NETWORK_H_
11 
12 #include <boost/asio.hpp>
13 #include <funapi/network/session.h>
14 #include <funapi/types.h>
15 
16 namespace fun {
17 
18 namespace funtest {
19 
20 class Session;
21 
22 
23 typedef function<void(const Ptr<Session> &/*session*/)> SessionOpenedHandler;
24 
25 typedef function<void(const Ptr<Session> &/*session*/,
26  SessionCloseReason /*reason*/)> SessionClosedHandler;
27 
28 typedef function<void (const Ptr<Session> &/*session*/, bool /*success*/)>
29  TcpTransportAttachedHandler;
30 
31 typedef function<void (const Ptr<Session> &/*session*/)>
32  TcpTransportDetachedHandler;
33 
34 typedef function<void (const Ptr<Session> &/*session*/, bool /*success*/)>
35  WebSocketTransportAttachedHandler;
36 
37 typedef function<void (const Ptr<Session> &/*session*/)>
38  WebSocketTransportDetachedHandler;
39 
40 typedef function<void (const Ptr<Session> &/*session*/,
41  const Json &/*message*/)> MessageHandler;
42 
43 typedef function<void (const Ptr<Session> &/*session*/,
44  const Ptr<FunMessage> &/*message*/)> MessageHandler2;
45 
46 typedef function<void (const Ptr<Session> &session)> SessionRedirectedHandler;
47 
48 
49 class FUNAPI_DLL_VISIBILITY Network {
50  public:
51  struct FUNAPI_DLL_VISIBILITY Option {
52  struct FUNAPI_DLL_VISIBILITY Compression {
53  Compression() : type("none"), threshold(128) {
54  }
55  string type;
56  string dictionary;
57  size_t threshold;
58  };
59 
60  explicit Option(size_t io_threads_size = 4);
61 
62  size_t io_threads_size;
63  bool use_session_reliability;
64  bool send_session_id_only_once;
65  bool disable_tcp_nagle;
66 
67  Compression tcp_compression;
68  Compression udp_compression;
69  Compression websocket_compression;
70 
71  string server_encryption_public_key;
72  };
73 
74  static void Install(const SessionOpenedHandler &opened_handler,
75  const SessionClosedHandler &closed_handler,
76  const Option &option);
77 
78  static void RegisterTcpTransportHandler(
79  const TcpTransportAttachedHandler &tcp_attached_handler,
80  const TcpTransportDetachedHandler &tcp_detached_handler);
81 
82  static void RegisterWebSocketTransportHandler(
83  const WebSocketTransportAttachedHandler &websocket_attached_handler,
84  const WebSocketTransportDetachedHandler &websocket_detached_handler);
85 
86  static void Register(const string &message_type,
87  const MessageHandler &message_handler);
88  static void Register2(const string &message_type,
89  const MessageHandler2 &message_handler);
90  static void Register2(int32_t message_type,
91  const MessageHandler2 &message_handler);
92  static void Register2(const ProtobufExtensionIdentifier &message_type,
93  const MessageHandler2 &message_handler);
94 
95  static void RegisterRedirectionHandler(
96  const SessionRedirectedHandler &redirection_handler);
97 };
98 
99 
100 class FUNAPI_DLL_VISIBILITY Session {
101  public:
103 
104  enum State {
105  kOpening,
106  kOpened,
107  kClosed,
108  kRedirecting,
109  };
110 
111  static Ptr<Session> Create();
112 
113  virtual ~Session();
114 
115  virtual void ConnectTcp(const string &ip, uint16_t port,
116  EncodingScheme encoding, bool use_ssl = false) = 0;
117  virtual void ConnectTcp(const boost::asio::ip::tcp::endpoint &endpoint,
118  EncodingScheme encoding, bool use_ssl = false) = 0;
119 
120  virtual void ConnectUdp(const string &ip, uint16_t port,
121  EncodingScheme encoding) = 0;
122  virtual void ConnectUdp(const boost::asio::ip::udp::endpoint &endpoint,
123  EncodingScheme encoding) = 0;
124 
125  virtual void ConnectHttp(const string &url, EncodingScheme encoding) = 0;
126  virtual void ConnectHttp(const string &ip, uint16_t port,
127  EncodingScheme encoding) = 0;
128  virtual void ConnectHttp(const boost::asio::ip::tcp::endpoint &endpoint,
129  EncodingScheme encoding) = 0;
130 
131  virtual void ConnectWebSocket(const string &url, EncodingScheme encoding) = 0;
132  virtual void ConnectWebSocket(const string &ip, uint16_t port,
133  EncodingScheme encoding) = 0;
134  virtual void ConnectWebSocket(const boost::asio::ip::tcp::endpoint &endpoint,
135  EncodingScheme encoding) = 0;
136 
137  virtual const SessionId &id() const = 0;
138  virtual State state() const = 0;
139 
140  virtual bool IsTransportAttached() const = 0;
141  virtual bool IsTransportAttached(TransportProtocol protocol) const = 0;
142 
143  virtual bool IsRedirected() const = 0;
144 
145  virtual void SendMessage(const string &message_type, const Json &message,
146  TransportProtocol protocol) = 0;
147 
148  virtual void SendMessage(const string &message_type,
149  const Ptr<FunMessage> &message,
150  TransportProtocol protocol) = 0;
151  virtual void SendMessage(int32_t message_type,
152  const Ptr<FunMessage> &message,
153  TransportProtocol protocol) = 0;
154  virtual void SendMessage(const ProtobufExtensionIdentifier &message_type,
155  const Ptr<FunMessage> &message,
156  TransportProtocol protocol) = 0;
157 
158  virtual void SendMessage(const string &message_type, const Json &message,
159  Encryption encryption,
160  TransportProtocol protocol) = 0;
161 
162  virtual void SendMessage(const string &message_type,
163  const Ptr<FunMessage> &message,
164  Encryption encryption,
165  TransportProtocol protocol) = 0;
166  virtual void SendMessage(int32_t message_type,
167  const Ptr<FunMessage> &message,
168  Encryption encryption,
169  TransportProtocol protocol) = 0;
170  virtual void SendMessage(const ProtobufExtensionIdentifier &message_type,
171  const Ptr<FunMessage> &message,
172  Encryption encryption,
173  TransportProtocol protocol) = 0;
174 
175  virtual void CloseTransport() = 0;
176  virtual void CloseTransport(TransportProtocol protocol) = 0;
177 
178  virtual void Close() = 0;
179  virtual void CloseRequest() = 0;
180 
181  virtual void SetContext(const Json &ctxt) = 0;
182  virtual Json &GetContext() = 0;
183  virtual const Json &GetContext() const = 0;
184  virtual boost::mutex &GetContextMutex() const = 0;
185  virtual operator boost::mutex &() const = 0;
186 
187  protected:
188  Session();
189 };
190 
191 } // namespace funtest
192 
193 } // namespace fun
194 
195 #endif // INCLUDE_FUNAPI_TEST_NETWORK_H_
Definition: network.h:100
SessionCloseReason
Enumeration to identify why a session has closed.
Definition: session.h:61
Definition: session.h:624
Definition: json.h:18
Definition: network.h:49
Definition: json.h:27
Definition: network.h:51
#define DECLARE_CLASS_PTR(CLS)
Utility macro to forward-declare smart pointer types for a given class.
Definition: types.h:89