Unity 3, webplayer security model and SFS2X policy server

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Unity 3, webplayer security model and SFS2X policy server

Postby ThomasLund » 30 Oct 2010, 11:14

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
Sichuan
Posts: 13
Joined: 18 Mar 2011, 08:28

Policy Prefetch

Postby Sichuan » 18 Mar 2011, 08:32

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 :)
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 18 Mar 2011, 09:43

> 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
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
Sichuan
Posts: 13
Joined: 18 Mar 2011, 08:28

Postby Sichuan » 18 Mar 2011, 12:47

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
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 18 Mar 2011, 12:53

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
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
Sichuan
Posts: 13
Joined: 18 Mar 2011, 08:28

File

Postby Sichuan » 18 Mar 2011, 13:24

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
User avatar
Scarpelius
Posts: 24
Joined: 12 Mar 2011, 11:33

Postby Scarpelius » 18 Mar 2011, 20:05

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.
Sichuan
Posts: 13
Joined: 18 Mar 2011, 08:28

Error

Postby Sichuan » 18 Mar 2011, 20:29

You didnt get any error in the console?
User avatar
Scarpelius
Posts: 24
Joined: 12 Mar 2011, 11:33

Postby Scarpelius » 18 Mar 2011, 20:40

No, just a lot of SFS Debug messages - info level, no warnings, no errors.
Sichuan
Posts: 13
Joined: 18 Mar 2011, 08:28

Postby Sichuan » 18 Mar 2011, 20:47

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.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 19 Mar 2011, 10:55

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
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
Ferz
Posts: 21
Joined: 27 Aug 2011, 08:17

Postby Ferz » 28 Aug 2011, 07:52

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>"
matg
Posts: 1
Joined: 01 Sep 2011, 19:45

Postby matg » 01 Sep 2011, 19:50

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
vkalpias
Posts: 56
Joined: 03 Oct 2011, 09:24

Postby vkalpias » 03 Oct 2011, 09:27

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?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 20 Oct 2011, 14:23

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
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 35 guests