iFun Engine API  1.0.0-b3776
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
network.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2018 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 Network {
50  public:
51  static void Install(const SessionOpenedHandler &opened_handler,
52  const SessionClosedHandler &closed_handler,
53  size_t io_threads_size);
54 
55  static void RegisterTcpTransportHandler(
56  const TcpTransportAttachedHandler &tcp_attached_handler,
57  const TcpTransportDetachedHandler &tcp_detached_handler);
58 
59  static void RegisterWebSocketTransportHandler(
60  const WebSocketTransportAttachedHandler &websocket_attached_handler,
61  const WebSocketTransportDetachedHandler &websocket_detached_handler);
62 
63  static void Register(const string &message_type,
64  const MessageHandler &message_handler);
65  static void Register2(const string &message_type,
66  const MessageHandler2 &message_handler);
67  static void Register2(int32_t message_type,
68  const MessageHandler2 &message_handler);
69  static void Register2(const ProtobufExtensionIdentifier &message_type,
70  const MessageHandler2 &message_handler);
71 
72  static void RegisterRedirectionHandler(
73  const SessionRedirectedHandler &redirection_handler);
74 };
75 
76 
77 class Session {
78  public:
79  DECLARE_CLASS_PTR(Session);
80 
81  enum State {
82  kOpening,
83  kOpened,
84  kClosed,
85  kRedirecting,
86  };
87 
88  static Ptr<Session> Create();
89 
90  virtual ~Session();
91 
92  virtual void ConnectTcp(const string &ip, uint16_t port,
93  EncodingScheme encoding, bool use_ssl = false) = 0;
94  virtual void ConnectTcp(const boost::asio::ip::tcp::endpoint &endpoint,
95  EncodingScheme encoding, bool use_ssl = false) = 0;
96 
97  virtual void ConnectUdp(const string &ip, uint16_t port,
98  EncodingScheme encoding) = 0;
99  virtual void ConnectUdp(const boost::asio::ip::udp::endpoint &endpoint,
100  EncodingScheme encoding) = 0;
101 
102  virtual void ConnectHttp(const string &url, EncodingScheme encoding) = 0;
103  virtual void ConnectHttp(const string &ip, uint16_t port,
104  EncodingScheme encoding) = 0;
105  virtual void ConnectHttp(const boost::asio::ip::tcp::endpoint &endpoint,
106  EncodingScheme encoding) = 0;
107 
108  virtual void ConnectWebSocket(const string &url, EncodingScheme encoding) = 0;
109  virtual void ConnectWebSocket(const string &ip, uint16_t port,
110  EncodingScheme encoding) = 0;
111  virtual void ConnectWebSocket(const boost::asio::ip::tcp::endpoint &endpoint,
112  EncodingScheme encoding) = 0;
113 
114  virtual const SessionId &id() const = 0;
115  virtual State state() const = 0;
116 
117  virtual bool IsTransportAttached() const = 0;
118  virtual bool IsTransportAttached(TransportProtocol protocol) const = 0;
119 
120  virtual void SendMessage(const string &message_type, const Json &message,
121  TransportProtocol protocol) = 0;
122 
123  virtual void SendMessage(const string &message_type,
124  const Ptr<FunMessage> &message,
125  TransportProtocol protocol) = 0;
126  virtual void SendMessage(int32_t message_type,
127  const Ptr<FunMessage> &message,
128  TransportProtocol protocol) = 0;
129  virtual void SendMessage(const ProtobufExtensionIdentifier &message_type,
130  const Ptr<FunMessage> &message,
131  TransportProtocol protocol) = 0;
132 
133  virtual void CloseTransport() = 0;
134  virtual void CloseTransport(TransportProtocol protocol) = 0;
135 
136  virtual void Close() = 0;
137 
138  virtual void SetContext(const Json &ctxt) = 0;
139  virtual Json &GetContext() = 0;
140  virtual const Json &GetContext() const = 0;
141  virtual boost::mutex &GetContextMutex() const = 0;
142  virtual operator boost::mutex &() const = 0;
143 
144  protected:
145  Session();
146 };
147 
148 } // namespace funtest
149 
150 } // namespace fun
151 
152 #endif // INCLUDE_FUNAPI_TEST_NETWORK_H_
Definition: network.h:77
Definition: session.h:586
Definition: network.h:49
Definition: json.h:27