Page 1 of 1

Occasionall "Cryptography cannot be initialized before connecting to SmartFoxServer!" in SFSEvent.CONNECTION handler

Posted: 14 Oct 2019, 16:04
by Jochanan
I have checked our logs and find out, that users are ocasionally getting
InvalidOperationException: Cryptography cannot be initialized before connecting to SmartFoxServer! exception iside SFSEvent.CONNECTION handler. I have checked the forum and it looks like, that this exception is thrown when caling InitCrypto() when IsConnected is set to false.

When i add additionall log, IsConnected property is set to true iside the handler.
What do you think it happened? How can we fix it?

I would only assume, that connection lost have been triggered before CONNECTION has been trigerred. Is it even possible? I thought that the handlers are called in serial manner based on the stacktrace:

Code: Select all

Sfs2X.Util.CryptoInitializerV2..ctor (Sfs2X.SmartFox sfs) (at <9bfe5727c1e148feb52fa1397dabf0b5>:0)
Sfs2X.SmartFox.InitCrypto () (at <9bfe5727c1e148feb52fa1397dabf0b5>:0)
xxx.Client.ServerConnection.OnConnection (Sfs2X.Core.BaseEvent evt) (at <23c0a572b4f74be0ad98fd09011f4a9d>:0)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt) (at <9bfe5727c1e148feb52fa1397dabf0b5>:0)
Rethrow as Exception: Error dispatching event connection: Cryptography cannot be initialized before connecting to SmartFoxServer!   at Sfs2X.Util.CryptoInitializerV2..ctor (Sfs2X.SmartFox sfs) (at <9bfe5727c1e148feb52fa1397dabf0b5>:0)
Sfs2X.SmartFox.InitCrypto () (at <9bfe5727c1e148feb52fa1397dabf0b5>:0)
xxx.Client.ServerConnection.OnConnection (Sfs2X.Core.BaseEvent evt) (at <23c0a572b4f74be0ad98fd09011f4a9d>:0)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt) (at <9bfe5727c1e148feb52fa1397dabf0b5>:0)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt) (at <9bfe5727c1e148feb52fa1397dabf0b5>:0)
Sfs2X.SmartFox.ProcessEvents () (at <9bfe5727c1e148feb52fa1397dabf0b5>:0)
xxx.Client.ServerConnection.Update () (at <23c0a572b4f74be0ad98fd09011f4a9d>:0)

Re: Occasionall "Cryptography cannot be initialized before connecting to SmartFoxServer!" in SFSEvent.CONNECTION handler

Posted: 15 Oct 2019, 07:06
by Lapo
Hi,
for starters, what client platform is you client?
What API version are you using?

The error you have mentioned ("Cryptography cannot be initialized before connecting to SmartFoxServer")... is thrown when a client is trying to start the encryption handshake without an active socket connection to the server.
This suggests that some client is connecting but probably loosing the connection very quickly and when the InitCrypto call is invoked the API complain about it.

The reasons for this can be multiple but essentially it sounds like the client is having network issues at connection time.
It would be interesting to know from your user's feedback if they can connect successfully with a 2nd attempt.

Unless this is happening very frequently it is expected to see a low % of failed connections (in the range of 0%...5%), especially from mobile devices.
Hope it helps

Re: Occasionall "Cryptography cannot be initialized before connecting to SmartFoxServer!" in SFSEvent.CONNECTION handler

Posted: 15 Oct 2019, 08:27
by Jochanan
So far it is Windows only,
c# API 1.7.12 (Unity3D)