SFSEvent::UDP_INIT() not firing.

Post here your questions about the C++ API for SFS2X

Moderators: Lapo, Bax, MBagnati

Posts: 1
Joined: 18 Mar 2015, 13:53

SFSEvent::UDP_INIT() not firing.

Postby emomax » 18 Mar 2015, 15:09

Hi all!

I'm currently attempting to receive data via UDP from an extension I wrote which works with unity. It returns a few values (these are no issue atm, I get them properly without using UDP). I'm on Windows 8, using Visual Studio 2013.

Firstly I ran the SImple Chat example and it's from that I have gathered most of the knowledge about the API. From this I fashioned a simple CMD-example which calls upon an extension which returns the square of the int sent.
I now try to implement this with an extension I'm using with Unity - with Unity it works fine. I had the same errors there when I hadn't initiated UDP (by calling smartfox.initUdp()).

However, when attempting this with the c++ API nothing happens. The event i'm listening for, "SFS2X::INIT_UDP" doesn't seem to fire. I have tried calling this after connecting, after logging in and after joining a room.

What I try to do is the following:
1. Connect
2.1 Login with username
2.2 (Initiated UDP)
3. Join room
4. Sent extension request
5. Receive extension response.

What I do is to first add the eventlistener as such:

Code: Select all

// Retrieve info regarding UDP handshake
m_ptrSmartFox->AddEventListener(SFSEvent::UDP_INIT, boost::shared_ptr<EventListenerDelegate>(new EventListenerDelegate(MainFrame::OnUDPInit, (unsigned long long)this)));   

I then call the InitUDP method after logging in:

Code: Select all

put("Logged in!");
put("Attempt to init UDP transmitting..");

The OnUDPInit function looks like this:

Code: Select all

void MainFrame::OnUDPInit(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent) {
   // get pointer to main frame.
   MainFrame* ptrMainFrame = (MainFrame*)ptrContext;

   put("UDP response was received!");

   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) {
      put("UDP can be used!");
   else {
      put("UDP is not available.. Abort mission.");

The output I get from CMD is:

What's happening? Mainframe here..
SmartFoxServer connection initiated!
Connection established!
Request for logging in sent!
Logged in!
Attempt to init UDP transmitting..
UDP handshake request sent!

..and then nothing happens. The server outputs:

15:59:24,589 INFO [SocketReader] sessions.DefaultSessionManager - Session created: { Id: 22, Type: DEFAULT, Logged: No, IP: } on Server port: 9933 <---> 59853
15:59:24,620 INFO [SFSWorker:Sys:1] api.SFSApi - User login: { Zone: BasicExamples }, ( User Name: Emomax, Id: 18, Priv: 0, Sess: ) , Type: C++ API

What I've tried and/or noticed:
    - I have made sure that UDP is available by calling "ptrSmartFox->UdpAvailable()" which returns true.
    - I called "ptrSmartFox->UdpInited()" which return false. Also tried "while(!ptrSmartFox->UdpInited()) { put("UDP not inited!";)}" after calling init function which just outputs "UDP not inited!" repeatedly.
    - I have tried using "InitUDP()" with both "InitUDP('localhost')" and "InitUDP('localhost', 9933)".

Is this a problem with the "InitUDP()" method or am I doing something borked?

Thankful for all help and advice. /Max
User avatar
Site Admin
Posts: 19935
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSEvent::UDP_INIT() not firing.

Postby Lapo » 19 Mar 2015, 10:56

Thanks for reporting, it seems what you're doing is correct.
We're going to take a look and report back.

Just to double check, on your local SmartFoxServer you have configured the UDP port to 9933, right?
And your previous test in Unity reported UDP success using the same local SmartFoxServer instance, correct?

...addicted to flash games
Posts: 125
Joined: 12 Feb 2013, 10:57

Re: SFSEvent::UDP_INIT() not firing.

Postby MBagnati » 19 Mar 2015, 13:35

To investigate the issue, I have modified the SimpleChat example in this way:

1) Editing the MainFrm.h file to add the declaration of a new method

Code: Select all

   static void OnSmartFoxUdpInit(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent);

among private members of CMainFrm class.
This is the declaration of the event listener that will be bound to UDP_INIT event

2) Editing the MainFrm.cpp file
2.a) change method

void CMainFrame::InitializeSmartFox()

to add

Code: Select all

      m_ptrSmartFox->AddEventListener(SFSEvent::UDP_INIT, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxUdpInit, (unsigned long long)this)));

This statement subscribes the CMainFrame::OnSmartFoxUdpInit method as listener for the SFSEvent::UDP_INIT event
So, the updated CMainFrame::InitializeSmartFox method becomes

Code: Select all

void CMainFrame::InitializeSmartFox()
   // Initialize Smart Fox
   m_ptrSmartFox = boost::shared_ptr<Sfs2X::SmartFox>(new Sfs2X::SmartFox(true));

   // Add event listeners
   m_ptrSmartFox->AddEventListener(SFSEvent::CONNECTION, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxConnection, (unsigned long long)this)));
   m_ptrSmartFox->AddEventListener(SFSEvent::CONNECTION_LOST, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxConnectionLost, (unsigned long long)this)));
   m_ptrSmartFox->AddEventListener(SFSEvent::LOGIN, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxLogin, (unsigned long long)this)));
   m_ptrSmartFox->AddEventListener(SFSEvent::LOGIN_ERROR, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxLoginError, (unsigned long long)this)));
   m_ptrSmartFox->AddEventListener(SFSEvent::LOGOUT, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxLogout, (unsigned long long)this)));
   m_ptrSmartFox->AddEventListener(BitSwarmEvent::DISCONNECT, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxDisconnection, (unsigned long long)this)));
   m_ptrSmartFox->AddEventListener(SFSEvent::CONNECTION_RETRY, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxConnectionRetry, (unsigned long long)this)));
   m_ptrSmartFox->AddEventListener(SFSEvent::CONNECTION_RESUME, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxConnectionResume, (unsigned long long)this)));
   m_ptrSmartFox->AddEventListener(SFSEvent::UDP_INIT, boost::shared_ptr<EventListenerDelegate> (new EventListenerDelegate(CMainFrame::OnSmartFoxUdpInit, (unsigned long long)this)));

   m_ptrSmartFox->LoadConfig(".\\Configuration\\sfs-config.xml", true);

2.b) implement method CMainFrame::OnSmartFoxUdpInit that acts as listener for UDP_INIT event
I leave empty the method implemetation, because for my test is not important what it does. I want only check that method will be called as callback for udp initialization.

Code: Select all

// -------------------------------------------------------------------------
// OnSmartFoxUdpInit
// -------------------------------------------------------------------------
void CMainFrame::OnSmartFoxUdpInit(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
   // Get the pointer to Main Frame class
   CMainFrame* ptrMainFrame = (CMainFrame*)ptrContext;

   if (ptrMainFrame == NULL)

2.c) change the implementation of CMainFrame::OnSmartFoxLogin adding the udp initialization
OnSmartFoxLogin is where SimpleChat application receives the notification of a login phase completed with success.
Here I add a call to initialize udp

Code: Select all

   // Initialize UDP
   ptrMainFrame->m_ptrSmartFox->InitUDP("", (unsigned short)9933);

So, the updated CMainFrame::OnSmartFoxLogin method becomes

Code: Select all

// -------------------------------------------------------------------------
// OnSmartFoxLogin
// -------------------------------------------------------------------------
void CMainFrame::OnSmartFoxLogin(unsigned long long ptrContext, boost::shared_ptr<BaseEvent> ptrEvent)
   // Get the pointer to Main Frame class
   CMainFrame* ptrMainFrame = (CMainFrame*)ptrContext;

   if (ptrMainFrame == NULL)

   // Assign SmartFox connection to chat form

   // Notify chat form about logged in user
   // Initialize UDP
   ptrMainFrame->m_ptrSmartFox->InitUDP("", (unsigned short)9933);

   // Show chat form
   ptrMainFrame->SendMessage(WM_SHOWCHATVIEW, 0 ,0);

Finally I have checked by AdminTool that SmartFoxServer is configured to handle UDP connections.
Take a look at Socket Addresses in ServerConfiguration section.
Here there is the same address and port used as parameters for InitUDP

Please, can you compare your UDP initialization with this test?
Server Configuration
(59.72 KiB) Not downloaded yet

Return to “SFS2X C++ API”

Who is online

Users browsing this forum: No registered users and 1 guest