SmartFoxServer 2X C++ API
Sfs2X::SmartFox Class Reference

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< ConfigDataConfig ()
 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 ()
 
See also
Connect(string*, long int)
More...
 
void Connect (string host)
 
See also
Connect(string*, long int)
More...
 
void Connect (boost::shared_ptr< ConfigData > cfg)
 Establishes a connection between the client and a SmartFoxServer 2X instance using a configuration object.

See also
Connect(string*, long int)
More...
 
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< UserMySelf ()
 Returns the User object representing the client when connected to a SmartFoxServer 2X instance. More...
 
boost::shared_ptr< Sfs2X::Logging::LoggerLogger ()
 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< IRoomManagerRoomManager ()
 Returns a reference to the Room Manager. More...
 
boost::shared_ptr< IUserManagerUserManager ()
 Returns a reference to the User Manager. More...
 
boost::shared_ptr< IBuddyManagerBuddyManager ()
 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...
 

Detailed Description

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

Constructor & Destructor Documentation

◆ SmartFox() [1/2]

Sfs2X::SmartFox::SmartFox ( )

Creates a new SmartFox instance.

The following example instantiates the SmartFox class without enabling the debug messages:

boost::shared_ptr<Sfs2X::SmartFox> ptrSmartFox (new Sfs2X::SmartFox());

◆ SmartFox() [2/2]

Sfs2X::SmartFox::SmartFox ( bool  debug)

Creates a new SmartFox instance.

<overloads>

The following example instantiates the SmartFox class while enabling the debug messages:

boost::shared_ptr<Sfs2X::SmartFox> ptrSmartFox (new Sfs2X::SmartFox(true));

</overloads>

Parameters
debugIf
true
, the SmartFoxServer API debug messages are logged.

Member Function Documentation

◆ AddEventListener()

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

Parameters
eventTypeThe SFSEvent event to get callbacks on
listenerThe delegate method to register
boost::shared_ptr<Sfs2X::SmartFox> ptrSmartFox;
ptrSmartFox = boost::shared_ptr<Sfs2X::SmartFox>(new Sfs2X::SmartFox());
ptrSmartFox->AddEventListener(SFSEvent::CONNECTION, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMyClass::OnConnection, (unsigned long long)this)));
static void OnConnection(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
void CMyClass::OnConnection(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
{
// Get the pointer to my class
CMyClass* instance = (CMyClass*)ptrContext;
boost::shared_ptr<map<string, boost::shared_ptr<void>>> ptrEventParams = ptrEvent->Params();
boost::shared_ptr<void> ptrEventParamValueSuccess = (*ptrEventParams)["success"];
boost::shared_ptr<bool> ptrValue = (boost::static_pointer_cast<bool>)(ptrEventParamValueSuccess);
if (*ptrValue == true)
{
// Connection was established
}
else
{
// Connection failed
}
}
See also
SFSEvent

◆ AddLogListener()

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

Parameters
logLevelA LogLevel
eventListenerA EventListenerDelegate
See also
Sfs2X.Logging.Logger.AddEventListener

◆ BuddyManager()

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.

See also
SFSBuddyManager

◆ CompressionThreshold()

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.

◆ Config()

boost::shared_ptr< ConfigData > Sfs2X::SmartFox::Config ( )

Get the current client configuration. It can return null if the configuration was not loaded

See also
LoadConfig(string, bool)

◆ Connect() [1/4]

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.

Parameters
hostThe address of the server to connect to.
portThe TCP port to connect to.
Exceptions
ArgumentExceptionIf 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:

ptrSmartFox->Connect("127.0.0.1", 9933);
See also
LoadConfig(string,bool), UseBlueBox, ConnectionMode, Disconnect()

◆ Connect() [2/4]

void Sfs2X::SmartFox::Connect ( )

See also
Connect(string*, long int)

◆ Connect() [3/4]

void Sfs2X::SmartFox::Connect ( string  host)

See also
Connect(string*, long int)

◆ Connect() [4/4]

void Sfs2X::SmartFox::Connect ( boost::shared_ptr< ConfigData cfg)

Establishes a connection between the client and a SmartFoxServer 2X instance using a configuration object.

See also
Connect(string*, long int)

Parameters
cfgThe client configuration object.

The following example connects to a local SmartFoxServer 2X instance:

boost::shared_ptr<ConfigData> cfgData = m_ptrSmartFox->Config();
cfgData->Host("127.0.0.1");
cfgData->Port(9933);
cfgData->Zone("BasicExamples");
cfgData->ForceIPv6(false);
ptrSmartFox->Connect(cfgData);
See also
ConfigData, UseBlueBox, ConnectionMode, Disconnect(), SFSEvent.CONNECTION

◆ ConnectionMode()

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:

string message = "Connection mode: " + *(ptrSmartFox->ConnectionMode());

◆ CurrentIp()

boost::shared_ptr< string > Sfs2X::SmartFox::CurrentIp ( )

Returns the IP address of the SmartFoxServer 2X instance to which the client is connected.

See also
Connect(string, int), LoadConfig(string, bool)

◆ CurrentPort()

long int Sfs2X::SmartFox::CurrentPort ( )

Returns the TCP port of the SmartFoxServer 2X instance to which the client is connected.

See also
Connect(string, int), LoadConfig(string, bool)

◆ CurrentZone()

boost::shared_ptr< string > Sfs2X::SmartFox::CurrentZone ( )

Returns the Zone currently in use, if the user is already logged in.

See also
LoadConfig(string, bool), LoginRequest

◆ Debug()

bool Sfs2X::SmartFox::Debug ( )

Indicates whether the client-server messages debug is enabled or not.
If set to

true

, 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.

◆ Disconnect()

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.

See also
Connect(string*, long int)

◆ DispatchEvent()

void Sfs2X::SmartFox::DispatchEvent ( boost::shared_ptr< BaseEvent evt)

Dispatch an event

◆ Dispose()

void Sfs2X::SmartFox::Dispose ( )

Dispose a SmartFox instance.

<overloads>

<remark> This method must be called once to dispose all resources used by a SmartFox instance that is exiting.
</remark>

</overloads>

◆ EnableLagMonitor() [1/3]

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>

Parameters
enabledThe lag monitoring status:
true
to start the monitoring,
false
to stop it.
intervalAn optional amount of seconds to pause between each query (recommended 3-4s)
queueSizeThe amount of values stored temporarily and used to calculate the average lag
See also
SFSEvent

◆ EnableLagMonitor() [2/3]

void Sfs2X::SmartFox::EnableLagMonitor ( bool  enabled)

◆ EnableLagMonitor() [3/3]

void Sfs2X::SmartFox::EnableLagMonitor ( bool  enabled,
long int  interval 
)

◆ ForceIPv6()

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.

See also
ConnectionMode, LoadConfig(string*, bool)

◆ GetIPAddress()

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

Parameters
hostThe host name.
portThe host TCP port.

◆ GetRoomById()

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.

Parameters
idThe id of the Room.
Returns
An object representing the requested Room;
NULL
if no Room object with the passed id exists in the Rooms list.

The following example retrieves a Room object and writes its name:

long int roomId = 3;
boost::shared_ptr<Room> room = ptrSmartFox->GetRoomById(roomId);
string message = "The name of Room is " + *(room->Name());
See also
GetRoomByName(string), RoomList, Entities.Managers.SFSRoomManager

◆ GetRoomByName()

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.

Parameters
nameThe name of the Room.
Returns
An object representing the requested Room;
NULL
if no Room object with the passed name exists in the Rooms list.

The following example retrieves a Room object and writes its id:

string roomName = "The Lobby";
boost::shared_ptr<Room> room = ptrSmartFox->GetRoomByName(roomName);
char buffer[512];
sprintf (buffer, "%ld", room->Id());
string message = "The ID of Room " + roomName + " is " + *(new string(buffer));
See also
Entities.Managers.SFSRoomManager, RoomList, SFSRoom

◆ GetRoomListFromGroup()

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.

Parameters
groupIdThe name of the Group.
Returns
The list of Room objects belonging to the passed Group.
See also
Entities.Managers.SFSRoomManager, SFSRoom

◆ HttpPort()

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

ptrSmartFox->HttpPort(80);
ptrSmartFox->Connect(ip, port);

◆ HttpUploadURI()

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.

◆ InitCrypto()

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 SomeMethod() {
boost::shared_ptr<Sfs2X::SmartFox> sfs (new Sfs2X::SmartFox(true));
sfs->AddEventListener(SFSEvent::CONNECTION, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnConnection, (unsigned long long)this)));
sfs->AddEventListener(SFSEvent::CRYPTO_INIT, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnEncryptionInitialized, (unsigned long long)this)));
sfs->Connect("mysecuredomain.com", 9933);
}
void OnConnection(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent) {
boost::shared_ptr<map<string, boost::shared_ptr<void>>> ptrEventParams = ptrEvent->Params();
boost::shared_ptr<void> ptrEventParamValueSuccess = (*ptrEventParams)["success"];
boost::shared_ptr<bool> ptrErrorMessage = ((boost::static_pointer_cast<bool>))(ptrEventParamValueSuccess);
if (*ptrErrorMessage == true)
{
// Connection was established
// Initialize encrypted connection
sfs->InitCrypto();
}
else
{
// Connection failed
}
}
void OnEncryptionInitialized(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent) {
boost::shared_ptr<map<string, boost::shared_ptr<void>>> ptrEventParams = ptrEvent->Params();
boost::shared_ptr<void> ptrEventParamValueSuccess = (*ptrEventParams)["success"];
boost::shared_ptr<bool> ptrErrorMessage = ((boost::static_pointer_cast<bool>))(ptrEventParamValueSuccess);
if (*ptrErrorMessage == true)
{
// Do login
boost::shared_ptr<IRequest> request (new LoginRequest("FozzieTheBear", "", "SimpleChat"));
sfs->Send(request);
}
else
{
// Encryption initialization failed
boost::shared_ptr<void> ptrEventParamValueErrorMessage = (*ptrEventParams)["errorMessage"];
boost::shared_ptr<string> ptrErrorMessage = ((boost::static_pointer_cast<string>))(ptrEventParamValueErrorMessage);
CString csErrorMessage;
csErrorMessage.Format(_T("Encryption initialization failed. Caused by: %s"), CString(ptrErrorMessage->c_str()));
}
}
See also
SFSEvent.CRYPTO_INIT

◆ InitUDP() [1/3]

void Sfs2X::SmartFox::InitUDP ( )

InitUDP(string*, long int)

◆ InitUDP() [2/3]

void Sfs2X::SmartFox::InitUDP ( string  udpHost)

InitUDP(string*, long int)

◆ InitUDP() [3/3]

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.

MTU Note:

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:

ptrSmartFox->AddEventListener(SFSEvent::UPD_INIT, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMyClass::OnUDPInit, (unsigned long long)this)));
ptrSmartFox->InitUDP();
static void OnUDPInit(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
void CMyClass::OnUDPInit(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
{
// Get the pointer to my class
CMyClass* instance = (CMyClass*)ptrContext;
boost::shared_ptr<map<string, boost::shared_ptr<void>>> ptrEventParams = ptrEvent->Params();
boost::shared_ptr<void> ptrEventParamValueSuccess = (*ptrEventParams)["success"];
if ((*((boost::static_pointer_cast<bool>)(ptrEventParamValueSuccess))) == true)
{
// Init OK: Execute an extension call via UDP
boost::shared_ptr<ISFSObject> parameters (new SFSObject());
boost::shared_ptr<IRequest> request (new ExtensionRequest("udpTest", parameters, boost::shared_ptr<Room>(), true));
ptrSmartFox->Send(request);
}
else
{
// Sorry, UDP is not available. Initialization failed
}
}
Parameters
udpHostThe IP address of the server to connect to.
udpPortThe UDP port to connect to.
See also
SFSEvent.UDP_INIT

◆ IsConnected()

bool Sfs2X::SmartFox::IsConnected ( )

Indicates whether the client is connected to the server or not.

The following example checks the connection status:

string status = ptrSmartFox->IsConnected() == true ? "yes" : "no";
string message = "Am I connected? " + status;

◆ JoinedRooms()

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.

See also
JoinRoomRequest, RoomManager, Room, LastJoinedRoom

◆ KillConnection()

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.

See also
Disconnect

◆ LastJoinedRoom()

boost::shared_ptr< Room > Sfs2X::SmartFox::LastJoinedRoom ( )

Returns the object representing the last Room joined by the client, if any.

This property is

NULL

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.

See also
JoinRoomRequest

◆ LoadConfig() [1/4]

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.

<SmartFoxConfig>
<ip>127.0.0.1</ip>
<port>9933</port>
<udpIp>127.0.0.1</ip>
<udpPort>9933</port>
<zone>simpleChat</zone>
<debug>true</debug>
<httpPort>8080</httpPort>
<httpsPort>8443</httpsPort>
<useBlueBox>true</useBlueBox>
<blueBoxPollingRate>750</blueBoxPollingRate>
<forceIPv6>false</forceIPv6>
</SmartFoxConfig>

The following example shows how to load an external configuration file.

ptrSmartFox->AddEventListener(SFSEvent::CONFIG_LOAD_SUCCESS, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMyClass::OnConfigLoadSuccess, (unsigned long long)this)));
ptrSmartFox->AddEventListener(SFSEvent::CONFIG_LOAD_FAILURE, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMyClass::OnConfigLoadFailure, (unsigned long long)this)));
ptrSmartFox->LoadConfig(".\\Configuration\\testEnvironmentConfig.xml", true);
static void OnConfigLoadSuccess(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
static void OnConfigLoadFailure(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);
void CMyClass::OnConfigLoadSuccess(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
{
// Config file loaded, connection in progress
}
void CMyClass::OnConfigLoadFailure(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
{
// Failed loading config file
}
Parameters
filePathFilename of the external XML configuration, including its path relative to the folder of the application file.
connectOnSuccessA flag indicating if the connection to SmartFoxServer must be attempted upon configuration loading completion.
See also
SFSEvent.CONFIG_LOAD_SUCCESS, SFSEvent.CONFIG_LOAD_FAILURE

◆ LoadConfig() [2/4]

void Sfs2X::SmartFox::LoadConfig ( string  filePath)

LoadConfig(string*, bool)

◆ LoadConfig() [3/4]

void Sfs2X::SmartFox::LoadConfig ( bool  connectOnSuccess)

LoadConfig(string*, bool)

◆ LoadConfig() [4/4]

void Sfs2X::SmartFox::LoadConfig ( )

LoadConfig(string*, bool)

◆ Logger()

boost::shared_ptr< Sfs2X::Logging::Logger > Sfs2X::SmartFox::Logger ( )

Returns a reference to the internal Logger instance used by SmartFoxServer 2X.

◆ MaxMessageSize()

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.

◆ MySelf()

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

NULL

if login was not performed yet.

NOTE: setting the MySelf property manually can disrupt the API functioning.

See also
Sfs2X.Entities.User.IsItMe, LoginRequest

◆ ProcessEvents()

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 FixedUpdate() {
ptrSmartFox->ProcessEvents();
}

◆ RemoveAllEventListeners()

void Sfs2X::SmartFox::RemoveAllEventListeners ( )

Removed all event listeners

Note: log delegates need to be removed separately

◆ RemoveEventListener()

void Sfs2X::SmartFox::RemoveEventListener ( boost::shared_ptr< string >  eventType,
boost::shared_ptr< EventListenerDelegate >  listener 
)

Removes a delegate registration from a given registered event

Parameters
eventType
listener

◆ RoomList()

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.

See also
RoomManager, JoinRoomRequest, SubscribeRoomGroupRequest, UnsubscribeRoomGroupRequest

◆ RoomManager()

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.

See also
SFSRoomManager

◆ Send()

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.

Parameters
requesta request object

The following example sends a login request:

boost::shared_ptr<IRequest> request (new LoginRequest("FozzieTheBear", "KermitPass", "TheMuppetZone"));
ptrSmartFox->Send(request);

The following example sends a login request:

boost::shared_ptr<IRequest> request (new JoinRoomRequest("Lobby"));
ptrSmartFox->Send(request);

The following example creates an object containing some parameters and sends it to the server-side Extension.

boost::shared_ptr<ISFSObject> parameters = SFSObject.NewInstance();
boost::shared_ptr<long int> value (new long int());
*value = 10;
parameters->SetInt("x", value);
value = boost::shared_ptr<long int>(new long int());
*value = 10;
parameters->SetInt("y", value);
boost::shared_ptr<IRequest> request (new ExtensionRequest("setPosition", parameters));
ptrSmartFox->Send(request);

◆ SessionToken()

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

◆ SetClientDetails()

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>

Parameters
platformIdThe id of the runtime platform: for example "Unity WebPlayer" or "iOS".
versionAn optional version of the runtime platform: for example "2.0.0".

◆ ThreadSafeMode()

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.

See also
ProcessEvents

◆ UdpAvailable()

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.

See also
InitUDP(string, int)

◆ UdpInited()

bool Sfs2X::SmartFox::UdpInited ( )

Indicates whether the UDP handshake has been performed successfully or not.

See also
InitUDP(string, int)

◆ UseBlueBox()

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.

See also
ConnectionMode, LoadConfig(string*, bool)

◆ UserManager()

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.

See also
SFSUserManager

◆ ValidateConfig()

void Sfs2X::SmartFox::ValidateConfig ( boost::shared_ptr< ConfigData cfg)

Validate a configuration object

Parameters
cfgThe configuration object to validate.

◆ Version()

boost::shared_ptr< string > Sfs2X::SmartFox::Version ( )

Return the API current version