iFun Engine API  1.0.0-b2601
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-2015 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*/,
35  const Json &/*message*/)> MessageHandler;
36 
37 typedef function<void (const Ptr<Session> &/*session*/,
38  const Ptr<FunMessage> &/*message*/)> MessageHandler2;
39 
40 typedef function<void (const Ptr<Session> &session)> SessionRedirectedHandler;
41 
42 
43 class Network {
44  public:
45  static void Install(const SessionOpenedHandler &opened_handler,
46  const SessionClosedHandler &closed_handler,
47  size_t io_threads_size);
48 
49  static void RegisterTcpTransportHandler(
50  const TcpTransportAttachedHandler &tcp_attached_handler,
51  const TcpTransportDetachedHandler &tcp_detached_handler);
52 
53  static void Register(const string &message_type,
54  const MessageHandler &message_handler);
55  static void Register2(const string &message_type,
56  const MessageHandler2 &message_handler);
57  static void Register2(int32_t message_type,
58  const MessageHandler2 &message_handler);
59  static void Register2(const ProtobufExtensionIdentifier &message_type,
60  const MessageHandler2 &message_handler);
61 
62  static void RegisterRedirectionHandler(
63  const SessionRedirectedHandler &redirection_handler);
64 };
65 
66 
67 class Session {
68  public:
69  DECLARE_CLASS_PTR(Session);
70 
71  enum State {
72  kOpening,
73  kOpened,
74  kClosed,
75  kRedirecting,
76  };
77 
78  static Ptr<Session> Create();
79 
80  virtual ~Session();
81 
82  virtual void ConnectTcp(const string &ip, uint16_t port,
83  EncodingScheme encoding) = 0;
84  virtual void ConnectTcp(const boost::asio::ip::tcp::endpoint &endpoint,
85  EncodingScheme encoding) = 0;
86 
87  virtual void ConnectUdp(const string &ip, uint16_t port,
88  EncodingScheme encoding) = 0;
89  virtual void ConnectUdp(const boost::asio::ip::udp::endpoint &endpoint,
90  EncodingScheme encoding) = 0;
91 
92  virtual void ConnectHttp(const string &url, EncodingScheme encoding) = 0;
93  virtual void ConnectHttp(const string &ip, uint16_t port,
94  EncodingScheme encoding) = 0;
95  virtual void ConnectHttp(const boost::asio::ip::tcp::endpoint &endpoint,
96  EncodingScheme encoding) = 0;
97 
98  virtual const SessionId &id() const = 0;
99  virtual State state() const = 0;
100 
101  virtual bool IsTransportAttached() const = 0;
102  virtual bool IsTransportAttached(TransportProtocol protocol) const = 0;
103 
104  virtual void SendMessage(const string &message_type, const Json &message,
105  TransportProtocol protocol) = 0;
106 
107  virtual void SendMessage(const string &message_type,
108  const Ptr<FunMessage> &message,
109  TransportProtocol protocol) = 0;
110  virtual void SendMessage(int32_t message_type,
111  const Ptr<FunMessage> &message,
112  TransportProtocol protocol) = 0;
113  virtual void SendMessage(const ProtobufExtensionIdentifier &message_type,
114  const Ptr<FunMessage> &message,
115  TransportProtocol protocol) = 0;
116 
117  virtual void CloseTransport() = 0;
118  virtual void CloseTransport(TransportProtocol protocol) = 0;
119 
120  virtual void Close() = 0;
121 
122  virtual void SetContext(const Json &ctxt) = 0;
123  virtual Json &GetContext() = 0;
124  virtual const Json &GetContext() const = 0;
125  virtual boost::mutex &GetContextMutex() const = 0;
126  virtual operator boost::mutex &() const = 0;
127 
128  protected:
129  Session();
130 };
131 
132 } // namespace funtest
133 
134 } // namespace fun
135 
136 #endif // INCLUDE_FUNAPI_TEST_NETWORK_H_
Definition: network.h:67
Definition: session.h:561
Definition: network.h:43
Definition: json.h:27