Search Unity

Security.PrefetchSocketPolicy(serverName, serverPort, 500) fails...

Discussion in 'Multiplayer' started by antx, Sep 22, 2012.

  1. antx

    antx

    Joined:
    Feb 16, 2012
    Posts:
    28
    Hello,

    I have 2 projects on the same PC which both use Security.PrefetchSocketPolicy(serverName, serverPort, 500) as the first thing in their Start() function. One works, the other one does not. I don´t understand how this can be.

    - I created a new scene in my project (the one that does not work),
    - put an empty GameObject in its Hierarchy,
    - copied the C# script from the working project into the not working one,
    - and then attached the script to the GameObject.

    This scene has no connection to anything else in the project. When I start it in the editor: it does not work. Why?

    - The project name is diffenent and the location on the harddrive, (obviously)
    - There are more files in the project folder (and therefore project view) in the not working project (but non of them is being used in the test scene!).

    Other than that I don´t see any difference between the two projects but one works and the other one does not:

    That is the C# script for both:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections.Generic;
    5.  
    6. public class Co : MonoBehaviour {
    7.     #region setup variables
    8.     public static string serverName = "127.0.0.1";
    9.     public static int serverPort = 9933;
    10.     #endregion
    11.  
    12.     void Start () {
    13.         // In a webplayer (or editor in webplayer mode) we need to setup security policy negotiation with the server first
    14.         if (Application.isWebPlayer || Application.isEditor) {
    15.             if (!Security.PrefetchSocketPolicy(serverName, serverPort, 500)) {
    16.                 Debug.LogError("Security Exception. Policy file load failed!");
    17.             }
    18.             else {
    19.                 Debug.Log ("it works!!!");
    20.             }
    21.         }
    22.     }
    23. }
    24.  
     
  2. tcarr9

    tcarr9

    Joined:
    Oct 11, 2010
    Posts:
    63
    I use this type of IF in my .cs class and it works for editor, webplayer, and non-webplayer builds:
    Code (csharp):
    1.  
    2.                 #if UNITY_WEBPLAYER
    3.                 Security.PrefetchSocketPolicy(ip, port);
    4.                 #endif
    5.  
    Of course it won't work if the server can't be reached on that IP and port, or if the server doesn't serve a socket policy in the right form. It also won't work if you use a hostname instead of an IPv4 dotted IP address, so if there's a possibility of a hostname (as when users enter the IP manually) I process the input first:
    Code (csharp):
    1.  
    2.     private string getIP(string host)
    3.     {
    4.             return Dns.GetHostAddresses(host)[0].ToString();
    5.     }
    6.  
    Is it possible that your server might not be responding quickly enough? Perhaps you need a longer timeout. I don't bother to specify, so I get the default which is 3000.
     
  3. antx

    antx

    Joined:
    Feb 16, 2012
    Posts:
    28
    Thank you for your response. But this does not work either.

    The Problem is, that the same code on the same PC with the same server works in one project but not in another (all I do is open another project in Unity and run it).

    I tried timeout values up to 3000 with no success. I also tried your code, but in editor mode this is not even executed. It tried this then:
    Code (csharp):
    1.  
    2.         #if UNITY_EDITOR
    3.         if (!Security.PrefetchSocketPolicy(serverName, serverPort, 3000)) {
    4.             Debug.LogError("Security Exception. Policy file load failed!");
    5.         }
    6.         Debug.Log ("I get here");
    7.         #endif 
    8.  
    but it gives me the error message.

    I suspect that something in the unity project got messed up. I can't see any difference between the two projects other than there are more files in one of them.
    Can someone think of anything that I could look into? Is there any way to get some information out of this Policy thing on why it exactly failed?
     
  4. tcarr9

    tcarr9

    Joined:
    Oct 11, 2010
    Posts:
    63
    Try changing your IF to "#if UNITY_WEBPLAYER". The Prefetch line should only be used for webplayer - it will give you an error with any other kind of player.
     
  5. antx

    antx

    Joined:
    Feb 16, 2012
    Posts:
    28
    If I do this I can´t be certain if it works. The policy test won´t be executed. I would need to build a webplayer and there I don´t see my debug log to check whether it worked or not.

    Since it works in one project I don´t see why it shouldn´t work in another. Even in Editor mode.

    I need to be absolutely certain that it works. Right now this is completely random behavior that I do not understand. That is something I just can´t live with.

    Anyway, I appreciate you input here.
     
  6. tcarr9

    tcarr9

    Joined:
    Oct 11, 2010
    Posts:
    63
    The policy test will break your build if it's not a web player build. In my experience, if I have the build set to web player and I'm running from the editor, I need to have the policy test in there, and if I have the build set to some other type of player and I'm running from the editor, I need to have it NOT there. That particular IF statement works for me no matter what build I have.

    Hopefully eventually Unity will fix it so that the prefetch line doesn't break the non-webplayer builds. We can hope at least.
     
  7. antx

    antx

    Joined:
    Feb 16, 2012
    Posts:
    28
    Oh, here you said something. When ever I was building a test build for web I just did so and never had a problem until I started to use client-server stuff. I never noticed the "switch platform" button underneath the list of platforms. I had no idea that the editor could be put into a platform mode itself.

    Now with what you where saying here I started to look around for such setting and found this button. And that actually works! Now I also understand what you really meant with that IF statement. ;-)

    So thank you very, very much!