SmartFoxServer 2X C++ API
C:/Projects/Software/Vss/Customer Archives/SmartFoxServer 2X/API Client/Cpp/trunk_api/SmartFox.h
1 // ===================================================================
2 //
3 // Description
4 // Contains the definition of SmartFox
5 //
6 // Revision history
7 // Date Description
8 // 30-Nov-2012 First version
9 //
10 // www.smartfoxserver.com
11 // (c) 2009-2016 gotoAndPlay()
12 // ===================================================================
13 #ifndef __SmartFox__
14 #define __SmartFox__
15 
16 // Forward class declaration
17 namespace Sfs2X {
18  class SmartFox;
19 } // namespace Sfs2X
20 
21 #if defined(_MSC_VER)
22 #pragma warning(disable:4786) // STL library: disable warning 4786; this warning is generated due to a Microsoft bug
23 #endif
24 #include <vector> // STL library: vector object
25 #include <string> // STL library: string object
26 using namespace std; // STL library: declare the STL namespace
27 
28 #include "Bitswarm/BitSwarmClient.h"
29 #include "Bitswarm/BitSwarmEvent.h"
30 #include "Util/EventDispatcher.h"
31 #include "Core/SFSIOHandler.h"
32 #include "Core/BaseEvent.h"
33 #include "Core/SFSEvent.h"
34 #include "Core/IDispatchable.h"
35 #include "Util/Common.h"
36 #include "Entities/Managers/IUserManager.h"
37 #include "Entities/Room.h"
38 #include "Entities/User.h"
39 #include "Entities/Managers/IBuddyManager.h"
40 #include "Entities/Managers/IRoomManager.h"
41 #include "Exceptions/SFSValidationError.h"
42 #include "FSM/FiniteStateMachine.h"
43 #include "Logging/Logger.h"
44 #include "Requests/IRequest.h"
45 #include "Util/ConfigData.h"
46 #include "Util/ConfigLoader.h"
47 #include "Util/LagMonitor.h"
48 
49 #include <boost/exception/exception.hpp>
50 #include <boost/exception/all.hpp>
51 #include <boost/asio.hpp> // Boost Asio header
52 #include <boost/shared_ptr.hpp> // Boost Asio shared pointer
53 #include <boost/enable_shared_from_this.hpp> // Boost shared_ptr for this
54 
55 using namespace Sfs2X::Bitswarm;
56 using namespace Sfs2X::Util;
57 using namespace Sfs2X::Entities;
58 using namespace Sfs2X::Entities::Data;
59 using namespace Sfs2X::Requests;
60 using namespace Sfs2X::Core;
61 using namespace Sfs2X::Logging;
62 using namespace Sfs2X::Exceptions;
63 using namespace Sfs2X::Entities::Managers;
64 
65 namespace Sfs2X {
66 
79  class DLLImportExport SmartFox : public IDispatchable, public boost::enable_shared_from_this<SmartFox>
80  {
81  public:
82 
83  // -------------------------------------------------------------------
84  // Public methods
85  // -------------------------------------------------------------------
86 
97  SmartFox();
98 
115  SmartFox(bool debug);
116 
117  virtual ~SmartFox();
118 
129  void Dispose();
130 
148  void SetClientDetails(boost::shared_ptr<string> platformId, boost::shared_ptr<string> version);
149 
150  boost::shared_ptr<BitSwarmClient> BitSwarm();
151  boost::shared_ptr<Sfs2X::Logging::Logger> Log();
152 
174  void EnableLagMonitor(bool enabled, long int interval, long int queueSize);
175 
179  void EnableLagMonitor(bool enabled);
180 
184  void EnableLagMonitor(bool enabled, long int interval);
185 
186  bool IsConnecting();
187 
188  boost::shared_ptr<BitSwarmClient> GetSocketEngine();
189 
190  boost::shared_ptr<Sfs2X::Util::LagMonitor> LagMonitor();
191 
202  bool IsConnected();
203 
207  boost::shared_ptr<string> Version();
208 
218  boost::shared_ptr<string> HttpUploadURI();
219 
225  boost::shared_ptr<ConfigData> Config();
226 
236  bool UseBlueBox();
237  void UseBlueBox(bool value);
238 
247  bool ForceIPv6();
248  void ForceIPv6(bool value);
249 
262  boost::shared_ptr<string> ConnectionMode();
263 
271  long int CompressionThreshold();
272 
280  long int MaxMessageSize();
281 
307  boost::shared_ptr<Room> GetRoomById(long int id);
308 
338  boost::shared_ptr<Room> GetRoomByName(string name);
339 
356  boost::shared_ptr<vector<boost::shared_ptr<Room> > > GetRoomListFromGroup(string groupId);
357 
366  void KillConnection();
367 
399  void Connect(string host, long int port);
400 
404  void Connect();
405 
409  void Connect(string host);
410 
436  void Connect(boost::shared_ptr<ConfigData> cfg);
437 
445  void Disconnect();
446 
454  void ValidateConfig(boost::shared_ptr<ConfigData> cfg);
455 
469  boost::shared_ptr<string> GetIPAddress(string host, long int port);
470 
479  bool Debug();
480  void Debug(bool value);
481 
488  boost::shared_ptr<string> CurrentIp();
489 
496  long int CurrentPort();
497 
504  boost::shared_ptr<string> CurrentZone();
505 
515  boost::shared_ptr<User> MySelf();
516  void MySelf(boost::shared_ptr<User> value);
517 
521  boost::shared_ptr<Sfs2X::Logging::Logger> Logger();
522 
533  boost::shared_ptr<Room> LastJoinedRoom();
534  void LastJoinedRoom(boost::shared_ptr<Room> value);
535 
547  boost::shared_ptr<vector<boost::shared_ptr<Room> > > JoinedRooms();
548 
562  boost::shared_ptr<vector<boost::shared_ptr<Room> > > RoomList();
563 
572  boost::shared_ptr<IRoomManager> RoomManager();
573 
582  boost::shared_ptr<IUserManager> UserManager();
583 
592  boost::shared_ptr<IBuddyManager> BuddyManager();
593 
606  bool UdpAvailable();
607 
612  bool UdpInited();
613 
617  void InitUDP();
618 
622  void InitUDP(string udpHost);
623 
683  void InitUDP(string udpHost, short int udpPort);
684 
685  bool IsJoining();
686  void IsJoining(bool value);
687 
694  boost::shared_ptr<string> SessionToken();
695 
699  void DispatchEvent(boost::shared_ptr<BaseEvent> evt);
700 
708  bool ThreadSafeMode();
709  void ThreadSafeMode(bool value);
710 
711  // No public docs for this, we use it internally for starting a reconnection
712  long int GetReconnectionSeconds();
713 
714  // No public docs for this, we use it internally for starting a reconnection
715  void SetReconnectionSeconds(long int seconds);
716 
761  void Send(boost::shared_ptr<IRequest> request);
762 
824  void LoadConfig(string filePath, bool connectOnSuccess);
825 
829  void LoadConfig(string filePath);
830 
834  void LoadConfig(bool connectOnSuccess);
835 
839  void LoadConfig();
840 
858  void AddLogListener(Sfs2X::Logging::LogLevel logLevel, boost::shared_ptr<EventListenerDelegate> eventListener);
859 
860  void AddJoinedRoom(boost::shared_ptr<Room> room);
861  void RemoveJoinedRoom(boost::shared_ptr<Room> room);
862 
863  //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
864  // Public handlers methods (used by SystemController)
865  //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
866  void HandleHandShake(boost::shared_ptr<BaseEvent> evt);
867 
868  void HandleLogin(boost::shared_ptr<BaseEvent> evt);
869 
870  void HandleClientDisconnection(boost::shared_ptr<string> reason);
871 
872  void HandleLogout();
873 
889  void HttpPort(unsigned short int httpPort);
890 
904  void ProcessEvents();
905 
906  // IDispatchable implementation
907 
941  void AddEventListener(boost::shared_ptr<string> eventType, boost::shared_ptr<EventListenerDelegate> listener);
942 
948  void RemoveEventListener (boost::shared_ptr<string> eventType, boost::shared_ptr<EventListenerDelegate> listener);
949 
956  void RemoveAllEventListeners();
957 
958  boost::shared_ptr<boost::asio::io_service> GetBoostService();
959 
961  void HandleReconnectionFailure();
962 
1028  void InitCrypto();
1029 
1030  // -------------------------------------------------------------------
1031  // Public members
1032  // -------------------------------------------------------------------
1033 
1034  protected:
1035 
1036  // -------------------------------------------------------------------
1037  // Protected methods
1038  // -------------------------------------------------------------------
1039 
1040  // -------------------------------------------------------------------
1041  // Protected members
1042  // -------------------------------------------------------------------
1043 
1044  private:
1045 
1046  // -------------------------------------------------------------------
1047  // Private methods
1048  // -------------------------------------------------------------------
1049 
1050  void Initialize();
1051  void Reset();
1052  void DisconnectConnection(long int timeout);
1053 
1054  void HandleConnectionProblem(boost::shared_ptr<BaseEvent> e);
1055  void SendHandshakeRequest(bool isReconnection);
1056  void EnqueueEvent(boost::shared_ptr<BaseEvent> evt);
1057  void OnDisconnectConnectionEvent(const boost::system::error_code& code);
1058  void CleanEventParams(boost::shared_ptr<BaseEvent> e);
1059 
1063  boost::shared_ptr<EventDispatcher> Dispatcher();
1064 
1065  //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1066  // Event handlers
1067  //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
1068 
1069  static void OnSocketConnect(unsigned long long context, boost::shared_ptr<BaseEvent> e);
1070  static void OnSocketClose(unsigned long long context, boost::shared_ptr<BaseEvent> e);
1071  static void OnSocketReconnectionTry(unsigned long long context, boost::shared_ptr<BaseEvent> e);
1072  static void OnSocketDataError(unsigned long long context, boost::shared_ptr<BaseEvent> e);
1073  static void OnSocketIOError(unsigned long long context, boost::shared_ptr<BaseEvent> e);
1074  static void OnSocketSecurityError(unsigned long long context, boost::shared_ptr<BaseEvent> e);
1075  static void OnConfigLoadSuccess(unsigned long long context, boost::shared_ptr<BaseEvent> e);
1076  static void OnConfigLoadFailure(unsigned long long context, boost::shared_ptr<BaseEvent> e);
1077 
1078  // -------------------------------------------------------------------
1079  // Private members
1080  // -------------------------------------------------------------------
1081 
1082  const static long int DEFAULT_HTTP_PORT = 8080;
1083  const static long int MAX_BB_CONNECT_ATTEMPTS = 3;
1084  const static char CLIENT_TYPE_SEPARATOR = ':';
1085 
1086  // Current version
1087  long int majVersion;
1088  long int minVersion;
1089  long int subVersion;
1090 
1091  boost::shared_ptr<BitSwarmClient> bitSwarm;
1092 
1093  // Stores the custom client details about the runtime platform
1094  boost::shared_ptr<string> clientDetails;
1095 
1096  boost::shared_ptr<Sfs2X::Util::LagMonitor> lagMonitor;
1097 
1098  // If true the client will fall back to BlueBox if no socket connection is available
1099  bool useBlueBox;
1100 
1101  // If true the client will use IPv6 for socket connection
1102  bool forceIPv6;
1103 
1104  // If true the client is in the middle of a join transaction
1105  bool isJoining;
1106 
1107  // References the client's User object
1108  boost::shared_ptr<User> mySelf;
1109 
1110  // A unique session token, sent by the server during the handshake
1111  boost::shared_ptr<string> sessionToken;
1112 
1113  // Last joined room
1114  boost::shared_ptr<Room> lastJoinedRoom;
1115 
1116  // The logger
1117  boost::shared_ptr<Sfs2X::Logging::Logger> log;
1118 
1119  // API initialization flag
1120  bool inited;
1121 
1122  // Protocol debug flag
1123  bool debug;
1124 
1125  // Thread safe mode. Once enabled, you have to run ProcessEvents manually to process the queued events in your thread.
1126  bool threadSafeMode;
1127 
1128  // Connection attempt flag
1129  bool isConnecting;
1130 
1131  // The global user manager
1132  boost::shared_ptr<IUserManager> userManager;
1133 
1134  // The global room manager
1135  boost::shared_ptr<IRoomManager> roomManager;
1136 
1137  // The global buddy manager
1138  boost::shared_ptr<IBuddyManager> buddyManager;
1139 
1140  boost::shared_ptr<ConfigData> config;
1141 
1142  // The name of the currently joined Zone
1143  boost::shared_ptr<string> currentZone;
1144 
1145  // When true -> starts the connection right after successful config loading
1146  bool autoConnectOnConfig;
1147 
1148  // Last ip address used for connection, used when falling back to BlueBox
1149  boost::shared_ptr<string> lastIpAddress;
1150 
1151  boost::shared_ptr<EventDispatcher> dispatcher;
1152 
1153  boost::shared_ptr<SynchronizationObject> eventsLocker;
1154 
1155  boost::shared_ptr<vector<boost::shared_ptr<BaseEvent> > > eventsQueue;
1156 
1157  long int bbConnectionAttempt;
1158 
1159  boost::shared_ptr<boost::asio::io_service> service;
1160  boost::shared_ptr<boost::asio::deadline_timer> disconnectTimer; // Boost Asio deadline timer
1161  };
1162 
1163 } // namespace Sfs2X
1164 
1165 #endif
Definition: SFSCodecError.cpp:14
The internal logger used by the SmartFoxServer 2X client API.
Definition: Logger.h:56
Definition: BaseEvent.cpp:14
Definition: Logger.cpp:14
STL namespace.
Definition: IBuddyManager.h:32
Definition: BaseController.cpp:14
Definition: AdminMessageRequest.cpp:14
Definition: BuddyOnlineState.h:15
SmartFox is the main class of the SmartFoxServer 2X API.
Definition: SmartFox.h:79
Definition: Buddy.h:30
Definition: SmartFox.cpp:24
Definition: ISFSArray.h:17