SmartFoxServer 2X C++ API
|
SmartFox is the main class of the SmartFoxServer 2X API. More...
#include <SmartFox.h>
Inherits Sfs2X::Core::IDispatchable, and enable_shared_from_this< SmartFox >.
Public Member Functions | |
SmartFox () | |
Creates a new SmartFox instance. More... | |
SmartFox (bool debug) | |
Creates a new SmartFox instance. More... | |
void | Dispose () |
Dispose a SmartFox instance. More... | |
void | SetClientDetails (boost::shared_ptr< string > platformId, boost::shared_ptr< string > version) |
Allows to specify custom client details that will be used to gather statistics about the client platform via the SFS2X Analytics Module. By default the generic "Unity" label is used as platform, without specifying the version. More... | |
void | EnableLagMonitor (bool enabled, long int interval, long int queueSize) |
Enables the automatic realtime monitoring of the lag between the client and the server (round robin). When turned on, the SFSEvent.PING_PONG event type is dispatched continuously, providing the average of the last ten measured lag values. More... | |
void | EnableLagMonitor (bool enabled) |
EnableLagMonitor(bool, long int, long int) More... | |
void | EnableLagMonitor (bool enabled, long int interval) |
EnableLagMonitor(bool, long int, long int) More... | |
bool | IsConnected () |
Indicates whether the client is connected to the server or not. More... | |
boost::shared_ptr< string > | Version () |
Return the API current version More... | |
boost::shared_ptr< string > | HttpUploadURI () |
Returns the HTTP URI that can be used to upload files to SmartFoxServer 2X, using regular HTTP POST. More... | |
boost::shared_ptr< ConfigData > | Config () |
Get the current client configuration. It can return null if the configuration was not loaded More... | |
bool | UseBlueBox () |
Indicates whether the client should attempt a tunnelled http connection through the BlueBox in case a socket connection can't be established. More... | |
bool | ForceIPv6 () |
Indicates whether the client should attempt an IPv6 connection More... | |
boost::shared_ptr< string > | ConnectionMode () |
Return the current connection mode which can be socket or http. More... | |
long int | CompressionThreshold () |
Returns the current compression threshold. More... | |
long int | MaxMessageSize () |
Returns the maximum size of messages allowed by the server. More... | |
boost::shared_ptr< Room > | GetRoomById (long int id) |
Retrieves a Room object from its id. More... | |
boost::shared_ptr< Room > | GetRoomByName (string name) |
Retrieves a Room object from its name. More... | |
boost::shared_ptr< vector< boost::shared_ptr< Room > > > | GetRoomListFromGroup (string groupId) |
Retrieves the list of Rooms which are part of the specified Room Group. More... | |
void | KillConnection () |
Simulates an abrupt disconnection from the server. This method should be used for testing and simulations only, otherwise use the Disconnect() method. More... | |
void | Connect (string host, long int port) |
Establishes a connection between the client and a SmartFoxServer 2X instance. More... | |
void | Connect () |
| |
void | Connect (string host) |
| |
void | Connect (boost::shared_ptr< ConfigData > cfg) |
Establishes a connection between the client and a SmartFoxServer 2X instance using a configuration object.
| |
void | Disconnect () |
Closes the connection between the client and the SmartFoxServer 2X instance. More... | |
void | ValidateConfig (boost::shared_ptr< ConfigData > cfg) |
Validate a configuration object More... | |
boost::shared_ptr< string > | GetIPAddress (string host, long int port) |
Get the IP Address for a given host name More... | |
bool | Debug () |
Indicates whether the client-server messages debug is enabled or not. If set to More... | |
boost::shared_ptr< string > | CurrentIp () |
Returns the IP address of the SmartFoxServer 2X instance to which the client is connected. More... | |
long int | CurrentPort () |
Returns the TCP port of the SmartFoxServer 2X instance to which the client is connected. More... | |
boost::shared_ptr< string > | CurrentZone () |
Returns the Zone currently in use, if the user is already logged in. More... | |
boost::shared_ptr< User > | MySelf () |
Returns the User object representing the client when connected to a SmartFoxServer 2X instance. More... | |
boost::shared_ptr< Sfs2X::Logging::Logger > | Logger () |
Returns a reference to the internal Logger instance used by SmartFoxServer 2X. More... | |
boost::shared_ptr< Room > | LastJoinedRoom () |
Returns the object representing the last Room joined by the client, if any. More... | |
boost::shared_ptr< vector< boost::shared_ptr< Room > > > | JoinedRooms () |
Returns a list of Room objects representing the Rooms currently joined by the client. More... | |
boost::shared_ptr< vector< boost::shared_ptr< Room > > > | RoomList () |
Returns a list of Room objects representing the Rooms currently "watched" by the client. More... | |
boost::shared_ptr< IRoomManager > | RoomManager () |
Returns a reference to the Room Manager. More... | |
boost::shared_ptr< IUserManager > | UserManager () |
Returns a reference to the User Manager. More... | |
boost::shared_ptr< IBuddyManager > | BuddyManager () |
Returns a reference to the Buddy Manager. More... | |
bool | UdpAvailable () |
Indicates whether the UPD protocol is available or not in the current runtime. More... | |
bool | UdpInited () |
Indicates whether the UDP handshake has been performed successfully or not. More... | |
void | InitUDP () |
InitUDP(string*, long int) More... | |
void | InitUDP (string udpHost) |
InitUDP(string*, long int) More... | |
void | InitUDP (string udpHost, short int udpPort) |
IInitializes the UDP protocol by performing an handshake with the server. More... | |
boost::shared_ptr< string > | SessionToken () |
Return the unique session token. More... | |
void | DispatchEvent (boost::shared_ptr< BaseEvent > evt) |
Dispatch an event More... | |
bool | ThreadSafeMode () |
When true (default) sets the API to be run with an event queue that needs to be processed from the client More... | |
void | Send (boost::shared_ptr< IRequest > request) |
Sends a request to the server. More... | |
void | LoadConfig (string filePath, bool connectOnSuccess) |
Loads the client configuration file. More... | |
void | LoadConfig (string filePath) |
LoadConfig(string*, bool) More... | |
void | LoadConfig (bool connectOnSuccess) |
LoadConfig(string*, bool) More... | |
void | LoadConfig () |
LoadConfig(string*, bool) More... | |
void | AddLogListener (Sfs2X::Logging::LogLevel logLevel, boost::shared_ptr< EventListenerDelegate > eventListener) |
Register delegate method for log message callbacks More... | |
void | HttpPort (unsigned short int httpPort) |
Sets the http port to use for BlueBox connections More... | |
void | ProcessEvents () |
Tells the API to process all event queues and execute the delegate callbacks More... | |
void | AddEventListener (boost::shared_ptr< string > eventType, boost::shared_ptr< EventListenerDelegate > listener) |
Adds a delegate to a given API event type that will be used for callbacks More... | |
void | RemoveEventListener (boost::shared_ptr< string > eventType, boost::shared_ptr< EventListenerDelegate > listener) |
Removes a delegate registration from a given registered event More... | |
void | RemoveAllEventListeners () |
Removed all event listeners More... | |
void | HandleReconnectionFailure () |
<exclude> | |
void | InitCrypto () |
Initializes the connection cryptography to protect all client-server communications with standard TLS protocol. More... | |
SmartFox is the main class of the SmartFoxServer 2X API.
This class is responsible for connecting the client to a SmartFoxServer instance and for dispatching all asynchronous events. Developers always interact with SmartFoxServer through this class.
NOTE: in the provided examples, sfs
always indicates a SmartFox instance.
Author: The gotoAndPlay() Team
http://www.smartfoxserver.com
http://www.gotoandplay.it
Sfs2X::SmartFox::SmartFox | ( | ) |
Creates a new SmartFox instance.
The following example instantiates the SmartFox class without enabling the debug messages:
Sfs2X::SmartFox::SmartFox | ( | bool | debug | ) |
void Sfs2X::SmartFox::AddEventListener | ( | boost::shared_ptr< string > | eventType, |
boost::shared_ptr< EventListenerDelegate > | listener | ||
) |
Adds a delegate to a given API event type that will be used for callbacks
eventType | The SFSEvent event to get callbacks on |
listener | The delegate method to register |
void Sfs2X::SmartFox::AddLogListener | ( | Sfs2X::Logging::LogLevel | logLevel, |
boost::shared_ptr< EventListenerDelegate > | eventListener | ||
) |
Register delegate method for log message callbacks
Calling this method is just like calling the Logger.AddEventListener method directly, but in addition the Logger.EnableEventDispatching property is automatically set to true
.
C# API only
logLevel | A LogLevel |
eventListener | A EventListenerDelegate |
boost::shared_ptr< IBuddyManager > Sfs2X::SmartFox::BuddyManager | ( | ) |
Returns a reference to the Buddy Manager.
This manager is used internally by the SmartFoxServer 2X API; the reference returned by this property gives access to the buddies list, allowing interaction with Buddy and BuddyVariable objects and access to user properties in the Buddy List system.
long int Sfs2X::SmartFox::CompressionThreshold | ( | ) |
Returns the current compression threshold.
This value represents the maximum message size (in bytes) before the protocol compression is activated.
It is determined by the server configuration.
boost::shared_ptr< ConfigData > Sfs2X::SmartFox::Config | ( | ) |
Get the current client configuration. It can return null if the configuration was not loaded
void Sfs2X::SmartFox::Connect | ( | string | host, |
long int | port | ||
) |
Establishes a connection between the client and a SmartFoxServer 2X instance.
If no argument is passed, the client will use the settings loaded via loadConfig() method.
The client usually connects to a SmartFoxServer instance through a socket connection. In case a socket connection can't be established, and the UseBlueBox property is set to true , a tunnelled http connection through the BlueBox module is attempted. When a successful connection is established, the ConnectionMode property can be used to check the current connection mode.
host | The address of the server to connect to. |
port | The TCP port to connect to. |
ArgumentException | If an invalid host/address or port is passed, and it can't be found in the loaded settings. |
The following example connects to a local SmartFoxServer 2X instance:
void Sfs2X::SmartFox::Connect | ( | ) |
void Sfs2X::SmartFox::Connect | ( | string | host | ) |
void Sfs2X::SmartFox::Connect | ( | boost::shared_ptr< ConfigData > | cfg | ) |
Establishes a connection between the client and a SmartFoxServer 2X instance using a configuration object.
cfg | The client configuration object. |
The following example connects to a local SmartFoxServer 2X instance:
boost::shared_ptr< string > Sfs2X::SmartFox::ConnectionMode | ( | ) |
Return the current connection mode which can be socket or http.
The connection mode.
The following example shows the connection mode:
boost::shared_ptr< string > Sfs2X::SmartFox::CurrentIp | ( | ) |
Returns the IP address of the SmartFoxServer 2X instance to which the client is connected.
long int Sfs2X::SmartFox::CurrentPort | ( | ) |
Returns the TCP port of the SmartFoxServer 2X instance to which the client is connected.
boost::shared_ptr< string > Sfs2X::SmartFox::CurrentZone | ( | ) |
Returns the Zone currently in use, if the user is already logged in.
bool Sfs2X::SmartFox::Debug | ( | ) |
Indicates whether the client-server messages debug is enabled or not.
If set to
, detailed debugging informations for all the incoming and outgoing messages are provided.
Debugging can be enabled when instantiating the SmartFox class too.
true
if debug; otherwise, false
.
void Sfs2X::SmartFox::Disconnect | ( | ) |
Closes the connection between the client and the SmartFoxServer 2X instance.
NOTE: methods, like this, that act on the connection with SmartFoxServer 2X cannot be called in the context of an event listener.
void Sfs2X::SmartFox::DispatchEvent | ( | boost::shared_ptr< BaseEvent > | evt | ) |
Dispatch an event
void Sfs2X::SmartFox::Dispose | ( | ) |
void Sfs2X::SmartFox::EnableLagMonitor | ( | bool | enabled, |
long int | interval, | ||
long int | queueSize | ||
) |
Enables the automatic realtime monitoring of the lag between the client and the server (round robin).
When turned on, the SFSEvent.PING_PONG event type is dispatched continuously, providing the average of the last ten measured lag values.
<remark> The lag monitoring is automatically halted when the user logs out of a Zone or gets disconnected.
NOTE: the lag monitoring can be enabled after the login has been performed successfully only.
</remark>
enabled | The lag monitoring status: true false |
interval | An optional amount of seconds to pause between each query (recommended 3-4s) |
queueSize | The amount of values stored temporarily and used to calculate the average lag |
void Sfs2X::SmartFox::EnableLagMonitor | ( | bool | enabled | ) |
void Sfs2X::SmartFox::EnableLagMonitor | ( | bool | enabled, |
long int | interval | ||
) |
bool Sfs2X::SmartFox::ForceIPv6 | ( | ) |
Indicates whether the client should attempt an IPv6 connection
NOTE: this property must be set before the connect() method is called.
boost::shared_ptr< string > Sfs2X::SmartFox::GetIPAddress | ( | string | host, |
long int | port | ||
) |
Get the IP Address for a given host name
If provided an ip address instead of an host name, returns the address itself
host | The host name. |
port | The host TCP port. |
boost::shared_ptr< Room > Sfs2X::SmartFox::GetRoomById | ( | long int | id | ) |
Retrieves a Room object from its id.
NOTE: the same object is returned by the IRoomManager.GetRoomById() method, accessible through the RoomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
id | The id of the Room. |
The following example retrieves a Room object and writes its name:
boost::shared_ptr< Room > Sfs2X::SmartFox::GetRoomByName | ( | string | name | ) |
Retrieves a Room object from its name.
NOTE: the same object is returned by the IRoomManager.GetRoomByName() method, accessible through the RoomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
name | The name of the Room. |
The following example retrieves a Room object and writes its id:
boost::shared_ptr< vector< boost::shared_ptr< Room > > > Sfs2X::SmartFox::GetRoomListFromGroup | ( | string | groupId | ) |
Retrieves the list of Rooms which are part of the specified Room Group.
NOTE: the same list is returned by the IRoomManager.GetRoomListFromGroup() method, accessible through the RoomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
groupId | The name of the Group. |
void Sfs2X::SmartFox::HttpPort | ( | unsigned short int | httpPort | ) |
Sets the http port to use for BlueBox connections
Can only be set prior to connecting. Once connected this value has no effect
This example shows how to set the HTTP Port
boost::shared_ptr< string > Sfs2X::SmartFox::HttpUploadURI | ( | ) |
Returns the HTTP URI that can be used to upload files to SmartFoxServer 2X, using regular HTTP POST.
For more details on how to use this functionality, see the Upload File
tutorial.
NOTE: this property returns null
if no API configuration has been set or the current user is not already logged in the server.
void SmartFox::InitCrypto | ( | ) |
Initializes the connection cryptography to protect all client-server communications with standard TLS protocol.
This method must be called right after a successful connection, before the login is performed.
Once the encryption initialization process is successfully completed, all of the server's data will be encrypted using standard AES 128-bit algorithm, with a secure key served over HTTPS.
The following example initializes the encrypted communication:
void Sfs2X::SmartFox::InitUDP | ( | ) |
InitUDP(string*, long int)
void Sfs2X::SmartFox::InitUDP | ( | string | udpHost | ) |
InitUDP(string*, long int)
void Sfs2X::SmartFox::InitUDP | ( | string | udpHost, |
short int | udpPort | ||
) |
IInitializes the UDP protocol by performing an handshake with the server.
This method needs to be called only once. It can be executed at any moment provided that you already started connection to the server.
After a successful initialization you can send UDP extension request at any moment.
If udpHost or udpPort arguments are not passed, the client will use the settings loaded via LoadConfig() method.
The MTU or Maximum Transmission Unit, represents the largest amount of bytes that can be sent at once before packet fragmentation occurs.
Since the UDP protocol uses a "nothing-or-all" appraoch to the transmission, it is important to keep in mind that on average a size of 1100-1200 bytes is probably the maximum you can reach. If you exceed the MTU size the data will be "lost in hyperspace" (the internet).
Another interesting matter is that there's no fixed size for the MTU, each operating system uses a slighlty different size.
Because of this we suggest a conservative data size of 1000-1200 bytes per packet to avoid packet loss.
The SFS2X protocol compression allows to send 2-3KBytes of uncompressed data which usually is squeezed down to a size of ~1000 bytes.
If you have larger data to send we suggest to organize it in smaller chunks so that they don't exceed the suggested MTU size.
More details about the MTU are found here: http://en.wikipedia.org/wiki/Maximum_transmission_unit
The following example initializes the UDP communication, sends a request to an Extension and handles the related events:
udpHost | The IP address of the server to connect to. |
udpPort | The UDP port to connect to. |
bool Sfs2X::SmartFox::IsConnected | ( | ) |
Indicates whether the client is connected to the server or not.
The following example checks the connection status:
boost::shared_ptr< vector< boost::shared_ptr< Room > > > Sfs2X::SmartFox::JoinedRooms | ( | ) |
Returns a list of Room objects representing the Rooms currently joined by the client.
NOTE: the same list is returned by the IRoomManager.GetJoinedRooms() method, accessible through the RoomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
void Sfs2X::SmartFox::KillConnection | ( | ) |
Simulates an abrupt disconnection from the server.
This method should be used for testing and simulations only, otherwise use the Disconnect() method.
NOTE: methods, like this, that act on the connection with SmartFoxServer 2X cannot be called in the context of an event listener.
boost::shared_ptr< Room > Sfs2X::SmartFox::LastJoinedRoom | ( | ) |
Returns the object representing the last Room joined by the client, if any.
This property is
if no Room was joined.
NOTE: setting the LastJoinedRoom property manually can disrupt the API functioning. Use the JoinRoomRequest request to join a new Room instead.
void Sfs2X::SmartFox::LoadConfig | ( | string | filePath, |
bool | connectOnSuccess | ||
) |
Loads the client configuration file.
The SmartFox instance can be configured through an external xml configuration file loaded at run-time.
By default, the loadConfig method loads a file named "sfs-config.xml", placed in the same folder of the application file.
If the AutoConnect parameter is set to true
, On loading completion the Connect(string*, long int) method is automatically called by the API, otherwise the SFSEvent.CONFIG_LOAD_SUCCESS event is dispatched.
In case of loading error, the SFSEvent.CONFIG_LOAD_FAILURE event id fired.
NOTE: the SmartFoxClient configuration file (client-side) should not be confused with the SmartFoxServer configuration file (server-side).
The external xml configuration file has the following structure; ip, port, udpIp, udpPort and zone parameters are mandatory, all other parameters are optional.
The following example shows how to load an external configuration file.
filePath | Filename of the external XML configuration, including its path relative to the folder of the application file. |
connectOnSuccess | A flag indicating if the connection to SmartFoxServer must be attempted upon configuration loading completion. |
void Sfs2X::SmartFox::LoadConfig | ( | string | filePath | ) |
LoadConfig(string*, bool)
void Sfs2X::SmartFox::LoadConfig | ( | bool | connectOnSuccess | ) |
LoadConfig(string*, bool)
void Sfs2X::SmartFox::LoadConfig | ( | ) |
LoadConfig(string*, bool)
boost::shared_ptr< Sfs2X::Logging::Logger > Sfs2X::SmartFox::Logger | ( | ) |
Returns a reference to the internal Logger instance used by SmartFoxServer 2X.
long int Sfs2X::SmartFox::MaxMessageSize | ( | ) |
Returns the maximum size of messages allowed by the server.
Any request exceeding this size will not be sent.
The value is determined by the server configuration.
boost::shared_ptr< User > Sfs2X::SmartFox::MySelf | ( | ) |
Returns the User object representing the client when connected to a SmartFoxServer 2X instance.
This object is generated upon successful login only, so it is
if login was not performed yet.
NOTE: setting the MySelf property manually can disrupt the API functioning.
void Sfs2X::SmartFox::ProcessEvents | ( | ) |
Tells the API to process all event queues and execute the delegate callbacks
This has to be done from the client to maintain thread safety
void Sfs2X::SmartFox::RemoveAllEventListeners | ( | ) |
Removed all event listeners
Note: log delegates need to be removed separately
void Sfs2X::SmartFox::RemoveEventListener | ( | boost::shared_ptr< string > | eventType, |
boost::shared_ptr< EventListenerDelegate > | listener | ||
) |
Removes a delegate registration from a given registered event
eventType | |
listener |
boost::shared_ptr< vector< boost::shared_ptr< Room > > > Sfs2X::SmartFox::RoomList | ( | ) |
Returns a list of Room objects representing the Rooms currently "watched" by the client.
The list contains all the Rooms that are currently joined and all the Rooms belonging to the Room Groups that have been subscribed.
NOTE 1: at login time, the client automatically subscribes all the Room Groups specified in the Zone's Default Room Groups setting.
NOTE 2: the same list is returned by the IRoomManager.GetRoomList() method, accessible through the RoomManager getter; this was replicated on the SmartFox class for handy access due to its usually frequent usage.
boost::shared_ptr< IRoomManager > Sfs2X::SmartFox::RoomManager | ( | ) |
Returns a reference to the Room Manager.
This manager is used internally by the SmartFoxServer 2X API; the reference returned by this property gives access to the Rooms list and Groups, allowing interaction with Room objects.
void Sfs2X::SmartFox::Send | ( | boost::shared_ptr< IRequest > | request | ) |
Sends a request to the server.
All the available request objects can be found in the requests namespace.
request | a request object |
The following example sends a login request:
The following example sends a login request:
The following example creates an object containing some parameters and sends it to the server-side Extension.
boost::shared_ptr< string > Sfs2X::SmartFox::SessionToken | ( | ) |
Return the unique session token.
The Session token is a String sent by the server to the client after the initial handshake
void Sfs2X::SmartFox::SetClientDetails | ( | boost::shared_ptr< string > | platformId, |
boost::shared_ptr< string > | version | ||
) |
Allows to specify custom client details that will be used to gather statistics about the client platform via the SFS2X Analytics Module. By default the generic "Unity" label is used as platform, without specifying the version.
<remark> This method must be called before the connection is started.
The length of the two strings combined must be < 512 characters. </remark>
platformId | The id of the runtime platform: for example "Unity WebPlayer" or "iOS". |
version | An optional version of the runtime platform: for example "2.0.0". |
bool Sfs2X::SmartFox::ThreadSafeMode | ( | ) |
When true (default) sets the API to be run with an event queue that needs to be processed from the client
When run in Unity, this has to be true. Otherwise you will get random crashes.
bool Sfs2X::SmartFox::UdpAvailable | ( | ) |
Indicates whether the UPD protocol is available or not in the current runtime.
UPD Protocol is available for Unity always and will always return true. Added for compatibility reasons with other APIs
Using the UDP protocol in an application requires that a handshake is performed between client and server. By default This is NOT done by the API to avoid allocating resources that might never be used
In order to activate the UDP support, the InitUDP() method must be invoked explicitly.
bool Sfs2X::SmartFox::UdpInited | ( | ) |
Indicates whether the UDP handshake has been performed successfully or not.
bool Sfs2X::SmartFox::UseBlueBox | ( | ) |
Indicates whether the client should attempt a tunnelled http connection through the BlueBox in case a socket connection can't be established.
NOTE: this property must be set before the connect() method is called.
Also, after a connection is established, this property does not return the current connection mode (socket or http); for this purpose use the ConnectionMode property.
boost::shared_ptr< IUserManager > Sfs2X::SmartFox::UserManager | ( | ) |
Returns a reference to the User Manager.
This manager is used internally by the SmartFoxServer 2X API; the reference returned by this property gives access to the users list, allowing interaction with User objects.
void Sfs2X::SmartFox::ValidateConfig | ( | boost::shared_ptr< ConfigData > | cfg | ) |
Validate a configuration object
cfg | The configuration object to validate. |
boost::shared_ptr< string > Sfs2X::SmartFox::Version | ( | ) |
Return the API current version