Page 1 of 2

Unity 3, webplayer security model and SFS2X policy server

Posted: 30 Oct 2010, 11:14
by ThomasLund
Unity 3 introduces a new security model which is similar to Flash and Silverlight.

This is described in more detail here if you want to know more about it:
http://unity3d.com/support/documentatio ... ndbox.html

Practically you can choose between 2 solutions to server policy files:
- SFS2X build in policy serving
- Use the standalone policy server referenced in the Unity manual (or any other standalone policy server)

To use the SFS2X policy server you have to add this command to your game before ANY network call.

Security.PrefetchSocketPolicy(serverIP, serverPort);

Note: the examples distributed with SFS2X are made for Unity 2.6!! And they do not include the policy prefetch command!!!

/Thomas

Policy Prefetch

Posted: 18 Mar 2011, 08:32
by Sichuan
I dont understand where I should place this line in the lobby code:

Code: Select all

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using Sfs2X;
using Sfs2X.Core;
using Sfs2X.Entities;
using Sfs2X.Requests;
using Sfs2X.Logging;

public class LobbyGUI : MonoBehaviour
{
   public string serverName = "xxxxxxxxxx";
   public int serverPort = 9933;

   private SmartFox smartFox;
   private string zone = "SimpleChat";
   private string username = "";
   private string password = "";
   private string loginErrorMessage = "";
   private bool isLoggedIn;
   private bool isJoining = false;

   
   
   private string newMessage = "";
   private ArrayList messages = new ArrayList();
   // Locker to use for messages collection to ensure its cross-thread safety
   private System.Object messagesLocker = new System.Object();
   
   private Vector2 chatScrollPosition, userScrollPosition;

   private int roomSelection = 0;
   private string [] roomStrings;
   
   public GUISkin gSkin;
   
   private Room currentActiveRoom;
   
   void Start()
   {
      bool debug = true;
      
      if (SmartFoxConnection.IsInitialized)
      {
         smartFox = SmartFoxConnection.Connection;
      }
      else
      {
         smartFox = new SmartFox(debug);
      }
                  
      // Register callback delegate
      smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
      smartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
      smartFox.AddEventListener(SFSEvent.LOGIN, OnLogin);
      smartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
      smartFox.AddEventListener(SFSEvent.LOGOUT, OnLogout);
      smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
      smartFox.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);

      smartFox.AddLogListener(LogLevel.DEBUG, OnDebugMessage);
      
      smartFox.Connect(serverName, serverPort);
   }


Hope you can help me out :)

Posted: 18 Mar 2011, 09:43
by ThomasLund
> To use the SFS2X policy server you have to add this command to your game before ANY network call.

So I would add it right before you do

smartFox = new SmartFox(debug);

or maybe even better first line in Start

/Thomas

Posted: 18 Mar 2011, 12:47
by Sichuan
So I have it right here:

Code: Select all

void Start()
{
   Security.PrefetchSocketPolicy(serverName, serverPort);
   bool debug = true;
      
   if (SmartFoxConnection.IsInitialized)
   {
      smartFox = SmartFoxConnection.Connection;
   }
   else
   {
      smartFox = new SmartFox(debug);
   }


But once I run it I still get the debug telling me:

Code: Select all

[SFS DEBUG] TCPSocketLayer: General exception on connection: Thread was being aborted   at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.Socket.Connect (System.Net.IPAddress address, Int32 port) [0x00000] in <filename unknown>:0
  at Sfs2X.Core.Sockets.TCPSocketLayer.ConnectThread () [0x00000] in <filename unknown>:0

Posted: 18 Mar 2011, 12:53
by ThomasLund
More info required to help.

* Does it work in editor?
* Do you get security exceptions in the player.log/editor.log? (If not, then this is not a problem with policy file)
* More log file needed

/T

File

Posted: 18 Mar 2011, 13:24
by Sichuan
Hereby included a Unity 3.3 project which is basically the SFS2X Lobby example with the extra security line and the ip of my SFS2X server already entered. Whenever trying to run this, this will cause a bug report to show in the editor console. http://rapidshare.com/files/453162161/SFSLobby.zip

Posted: 18 Mar 2011, 20:05
by Scarpelius
There is a problem with your server.
I tested the project with my server (changed the server ip to my server) and is running ok.

Error

Posted: 18 Mar 2011, 20:29
by Sichuan
You didnt get any error in the console?

Posted: 18 Mar 2011, 20:40
by Scarpelius
No, just a lot of SFS Debug messages - info level, no warnings, no errors.

Posted: 18 Mar 2011, 20:47
by Sichuan

Code: Select all

[SFS DEBUG] TCPSocketLayer: General exception on connection: Thread was being aborted   at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.Socket.Connect (System.Net.IPAddress address, Int32 port) [0x00000] in <filename unknown>:0
  at Sfs2X.Core.Sockets.TCPSocketLayer.ConnectThread () [0x00000] in <filename unknown>:0


Thats what I get, it connects but it gives this error in the background.

Posted: 19 Mar 2011, 10:55
by ThomasLund
I'll re-iterate

-----

More info required to help.

* Does it work in editor?
* Do you get security exceptions in the player.log/editor.log? (If not, then this is not a problem with policy file)
* More log file needed

Posted: 28 Aug 2011, 07:52
by Ferz
You should read this:
http://unity3d.com/support/documentatio ... ndbox.html

And use something like this:

<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" to-ports="1200-10000"/>
</cross-domain-policy>"

Posted: 01 Sep 2011, 19:50
by matg
We finaly have solved this problem after some hours of testing:

Here is the result:

if(Security.PrefetchSocketPolicy(serverName,serverPort,200))
{
... do server connect
}

As you can see you have to check for a valid policy result and add a timeout value of 200 ms and thats it :)

works in unity editor and inside browser

Posted: 03 Oct 2011, 09:27
by vkalpias
Hello,

I also have an issue with this. I added this line Security.PrefetchSocketPolicy in the Start function and all works in the computer where I have smartfox installed. However when I try to connect with my laptop which is inside the network with an ip like 192.168.xxx.xxx nothing seems to happen.

What actually happens is that Security.PrefetchSocketPolicy returns false in my laptop but true in my server computer... I do not run any firewalls in either machine and there are no blocked ports in the network..

Any ideas?

Posted: 20 Oct 2011, 14:23
by ThomasLund
Hmmm - I dont have any ideas to why the prefetch would return false instead of true for local address in your end. Definitely if thats the case and it didnt return a policy - then the webplayer will fail.

Poke the Unity guys on the Unity forums - best advice I can give for now

/Thomas