crossdomain policy hell

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

Moderators: Lapo, Bax

jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

crossdomain policy hell

Postby jamieyg3 » 13 Jul 2012, 01:26

Hi

I'm trying to change the smartfox port on my remote server to port 443... this works fine in standalone builds... as soon as i switch to web player i run into problems...

In fact, i've tried a few different ports (443, 80, 9932) and they all give problems, port 9933 gives no problem at all.

Here is my client code:

Code: Select all

if (Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(SERVER_PORT), 500))
    Debug.Log("good!");
else
    Debug.Log("bad!");
smartfox = new SmartFox(true);
smartfox.Connect(SERVER_IP, SERVER_PORT);


in my config file on the server:

Code: Select all

<socket address="x.x.x.x" port="443" type="TCP"/>
<socket address="x.x.x.x" port="9933" type="TCP"/>
<socket address="x.x.x.x" port="80" type="TCP"/>
<socket address="x.x.x.x" port="9932" type="TCP"/>


When the server starts up it shows this:

Code: Select all

20:10:21,209 INFO  [main] v2.SmartFoxServer     - Listening Sockets: { x.x.x.x:443, (Tcp) } { x.x.x.x:9933, (Tcp) } { x.x.x.x:80, (Tcp) } { x.x.x.x:9932, (Tcp) }

=======================================================
Client:
-----------------------------------------------------------------
When I connect to port 9933, everything works fine as expected...
------------------------------------------------------------------
When I connect to port 9932 or port 80 (output is the same for both ports)
server output:

Code: Select all

20:12:30,696 DEBUG [SocketReader] protocol.SFSIoHandler     - Handling Flash Policy request
20:12:30,776 DEBUG [SocketReader] v2.SmartFoxServer     - []


client output:

Code: Select all

good!
UnityEngine.Debug:Log(Object)

[SFS DEBUG] TCPSocketLayer: General exception on connection: Unable to connect, as no valid crossdomain policy was found   at System.Net.Sockets.Socket.Connect_internal (IntPtr sock, System.Net.SocketAddress sa, System.Int32& error, Boolean requireSocketPolicyFile) [0x00000] in <filename unknown>:0
  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
UnityEngine.Debug:Log(Object)
GameManager:OnDebugMessage(BaseEvent) (at Assets/Scripts/GameManager.cs:65)
Sfs2X.Core.EventDispatcher:DispatchEvent(BaseEvent)
Sfs2X.SmartFox:ProcessEvents()
GameManager:FixedUpdate() (at Assets/Scripts/GameManager.cs:137)

------------------------------------------------------------------
When I try on port 443:

Server output: Nothing!
Client Output:

Code: Select all

bad!
UnityEngine.Debug:Log(Object)


[SFS DEBUG] TCPSocketLayer: General exception on connection: Unable to connect, as no valid crossdomain policy was found   at System.Net.Sockets.Socket.Connect_internal (IntPtr sock, System.Net.SocketAddress sa, System.Int32& error, Boolean requireSocketPolicyFile) [0x00000] in <filename unknown>:0
  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
UnityEngine.Debug:Log(Object)
GameManager:OnDebugMessage(BaseEvent) (at Assets/Scripts/GameManager.cs:65)
Sfs2X.Core.EventDispatcher:DispatchEvent(BaseEvent)
Sfs2X.SmartFox:ProcessEvents()
GameManager:FixedUpdate() (at Assets/Scripts/GameManager.cs:137)


I've spent all day trying to get this to work... ive tried on 2 completely different linux servers (one with redhat and cpanel, one with centos and no cpanel).....

please help me fix this problem its very frustrating. Why does port 9933 work so easily, while other ports (even 9932) cause so much problems?

Thanks.
User avatar
Lapo
Site Admin
Posts: 21139
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: crossdomain policy hell

Postby Lapo » 13 Jul 2012, 19:36

What is the content of the policy file please?
Lapo
--
gotoAndPlay()
...addicted to flash games
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Re: crossdomain policy hell

Postby jamieyg3 » 13 Jul 2012, 20:00

I'm just using a standard one i think

Code: Select all

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy

  SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">

<cross-domain-policy>

  <allow-access-from domain="*" />

</cross-domain-policy>
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Re: crossdomain policy hell

Postby jamieyg3 » 13 Jul 2012, 20:10

is there a way to check my crossdomain policy? i just went to the website root... i dont know how to check it any other way
User avatar
Lapo
Site Admin
Posts: 21139
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: crossdomain policy hell

Postby Lapo » 14 Jul 2012, 07:28

No the policy file is found under the SFS2X/config/ folder. Typically it will look like this:

Code: Select all

<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <!-- MY POLICY -->
   <site-control permitted-cross-domain-policies="all"/>

   <!-- Instead of setting to-ports="*", administrators can use ranges and commas -->
   <!-- This will allow access to ports 123, 456, 457, and 458 -->
   <allow-access-from domain="*" to-ports="9933" />
</cross-domain-policy>

And it would explain why it fails with all ports but 9933

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Re: crossdomain policy hell

Postby jamieyg3 » 15 Jul 2012, 20:56

Lapo wrote:No the policy file is found under the SFS2X/config/ folder. Typically it will look like this:

Code: Select all

<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <!-- MY POLICY -->
   <site-control permitted-cross-domain-policies="all"/>

   <!-- Instead of setting to-ports="*", administrators can use ranges and commas -->
   <!-- This will allow access to ports 123, 456, 457, and 458 -->
   <allow-access-from domain="*" to-ports="9933" />
</cross-domain-policy>

And it would explain why it fails with all ports but 9933

Hope it helps


Hi, thanks yes that did help a bit.

It did not seem to work for port 443 but all other ports are working now... I guess i can put it on port 80, thats a good port to use right?

However, if i set the remote server to be on port 9932, and then block port 9932 on my local router... it does not connect with bluebox and gives these policy file errors:


Code: Select all

[SFS DEBUG] TCPSocketLayer: General exception on connection: Unable to connect, as no valid crossdomain policy was found   at System.Net.Sockets.Socket.Connect_internal (IntPtr sock, System.Net.SocketAddress sa, System.Int32& error, Boolean requireSocketPolicyFile) [0x00000] in <filename unknown>:0
  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
UnityEngine.Debug:Log(Object)
GameManager:OnDebugMessage(BaseEvent) (at Assets/Scripts/GameManager.cs:65)
Sfs2X.Core.EventDispatcher:DispatchEvent(BaseEvent)
Sfs2X.SmartFox:ProcessEvents()
GameManager:FixedUpdate() (at Assets/Scripts/GameManager.cs:137)



[SFS DEBUG] [ BB-Connect ]: http://server-ip:8080/BlueBox/BlueBox.do
UnityEngine.Debug:Log(Object)
GameManager:OnDebugMessage(BaseEvent) (at Assets/Scripts/GameManager.cs:65)
Sfs2X.Core.EventDispatcher:DispatchEvent(BaseEvent)
Sfs2X.SmartFox:ProcessEvents()
GameManager:FixedUpdate() (at Assets/Scripts/GameManager.cs:137)


[SFS DEBUG] [ BB-Send ]: null|connect|null
UnityEngine.Debug:Log(Object)
GameManager:OnDebugMessage(BaseEvent) (at Assets/Scripts/GameManager.cs:65)
Sfs2X.Core.EventDispatcher:DispatchEvent(BaseEvent)
Sfs2X.SmartFox:ProcessEvents()
GameManager:FixedUpdate() (at Assets/Scripts/GameManager.cs:137)


[SFS DEBUG] ## BlueBox Error: Http error creating http connection: System.Security.SecurityException: Unable to connect, as no valid crossdomain policy was found
  at System.Net.Sockets.Socket.Connect_internal (IntPtr sock, System.Net.SocketAddress sa, System.Int32& error, Boolean requireSocketPolicyFile) [0x00000] in <filename unknown>:0
  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.Http.SFSWebClient.UploadValuesAsync (System.Uri uri, System.String paramName, System.String encodedData) [0x00000] in <filename unknown>:0
UnityEngine.Debug:Log(Object)
GameManager:OnDebugMessage(BaseEvent) (at Assets/Scripts/GameManager.cs:65)
Sfs2X.Core.EventDispatcher:DispatchEvent(BaseEvent)
Sfs2X.SmartFox:ProcessEvents()
GameManager:FixedUpdate() (at Assets/Scripts/GameManager.cs:137)



Thanks for the help so far, and I hope you can help get the bluebox working, i think it will be the last roadblock.

Thanks.
Jake-GR
Posts: 59
Joined: 28 Dec 2011, 22:52

Re: crossdomain policy hell

Postby Jake-GR » 15 Jul 2012, 21:43

You wouldnt want to use port 80, as that is for the HTTP protocol (i.e. web page), and could possibly cause issues. It sounds like you are running this from home, and some ISP companies will actually block port 80 requests, to restrict hosting websites from home.
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Re: crossdomain policy hell

Postby jamieyg3 » 15 Jul 2012, 21:52

I'm not running it from home

I turned off apache on the remote server, port 80 seems to work fine. (port 443 does not for some reason).

port 80 seems to work but bluebox is not working, that is the current problem.

Ideally i'd like to run the server on port 443 and bluebox on port 80... but if i can't do that then i will run the server on port 80 and bluebox on port 8080.

Thanks.
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Re: crossdomain policy hell

Postby jamieyg3 » 15 Jul 2012, 22:05

Also, if i switch unity to PC Standalone build everything works..

PC Standalone build:
- connecting to port 443 (and all other ports) works fine...
- also Bluebox works fine in standalone mode...

it is only the web player that is giving me problems... Web Player Build:
- can't connect to port 443 (port 80 seems to work though now after i changed the crossdomain file)
- i can't connect to Bluebox.

Here is my SFS2X/config/crossdomain.xml file

Code: Select all

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <!-- This is a master-policy file. site-control can be all,none,master-only -->
   <site-control permitted-cross-domain-policies="all"/>

   <!-- Instead of setting to-ports="*", administrators can use ranges and commas -->
   <!-- This will allow access to ports 123, 456, 457, and 458 -->
   <allow-access-from domain="*" to-ports="*" />
</cross-domain-policy>


Thanks.
Jake-GR
Posts: 59
Joined: 28 Dec 2011, 22:52

Re: crossdomain policy hell

Postby Jake-GR » 15 Jul 2012, 22:35

It may have to do with just the WebPlayer because that is designed to run through a browser, where port 80 is HTTP but is publicly open.
Im not that familiar using different ports, but just have an idea that 443 doesnt work because its dedicated for HTTPS, and the browser might not be able to use that without a valid SSL certificate?
Again I'm just throwing out ideas, as i use the (SmartFox) defaults of 8080 for Admin/BlueBox, 9933 for Server, and 843 as Flash Domain Policy... and everything works 100% when running a WebPlayer build.
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Re: crossdomain policy hell

Postby jamieyg3 » 15 Jul 2012, 23:12

I was able to run some more tests and my problem seems to be with Security.PrefetchSocketPolicy

This is pretty confusing but i'll try to explain it...

SFS is listening on 9932, 80, and 443... bluebox is on 8080...

The server is in a remote location... my firewall blocks port 9932...

--------------------------------------------
TEST 1

Here is my code:

Code: Select all

public const int SERVER_PORT = 80;
if (Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(8080), 500))
                Debug.Log("bluebox policy good!");
            else
                Debug.Log("bluebox policy bad!");

output:

Code: Select all

bluebox policy bad!


Result: so, it can't get a policy file from bluebox... no connection to the server because there is no policy file
---------------------------------------------
TEST 2

Here is my code:

Code: Select all

public const int SERVER_PORT = 80;
if (Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(SERVER_PORT), 500))
                Debug.Log("good!");
            else
                Debug.Log("bad!");
            if (Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(8080), 500))
                Debug.Log("bluebox policy good!");
            else
                Debug.Log("bluebox policy bad!");


and here's the output:

Code: Select all

good!
bluebox policy good!


Result: Wait a sec?? now it is getting the policy file from port 8080...... but only if i get a policy file from port 80 first?? Connects to the server just fine because there is a policy file
---------------------------------------------
TEST 3

Here is my code:

Code: Select all

public const int SERVER_PORT = 443;
if (Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(80), 500))
                Debug.Log("good!");
            else
                Debug.Log("bad!");
            if (Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(8080), 500))
                Debug.Log("bluebox policy good!");
            else
                Debug.Log("bluebox policy bad!");


and here's the output:

Code: Select all

good!
bluebox policy good!


Result: I am now connecting to port 443, just like i wanted... i got the policy file from port 80 though
--------------------------------------------
TEST 4

Here is my code:

Code: Select all

public const int SERVER_PORT = 443;
if (Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(SERVER_PORT), 500))
                Debug.Log("bluebox policy good!");
            else
                Debug.Log("bluebox policy bad!");

output:

Code: Select all

bluebox policy bad!


Result: so, it can't get a policy file from port 443...... why? Can't connect to server with no policy file
--------------------------------------------
TEST 5

Here is my code:

Code: Select all

public const int SERVER_PORT = 9932;
if (Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(80), 500))
                Debug.Log("good!");
            else
                Debug.Log("bad!");
            if (Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(8080), 500))
                Debug.Log("bluebox policy good!");
            else
                Debug.Log("bluebox policy bad!");


and here's the output:

Code: Select all

good!
bluebox policy good!


Result: Port 9932 is blocked...... but because i got the policy file from port 80 I am now able to connect with Bluebox!
--------------------------------------------

So....... I guess my problem is the policy file..... it won't get the policy file if i do this: Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(8080), 500)............. it does not like getting the policy file from bluebox...

if the port im trying to connect to is blocked, how would I get the policy file??

if i'm trying to connect to port 443 and its blocked... Security.PrefetchSocketPolicy(SERVER_IP, Convert.ToInt32(443), 500) would also fail...... which means bluebox would fail....... I hope that makes sense...

I hope you can help solve this problem.

Thanks.
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Re: crossdomain policy hell

Postby jamieyg3 » 15 Jul 2012, 23:35

Just some extra info in case its needed:

telnet <ip> 80:

Code: Select all

Escape character is '^]'.
<request-policy-file />
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <!-- This is a master-policy file. site-control can be all,none,master-only -->
   <site-control permitted-cross-domain-policies="all"/>

   <!-- Instead of setting to-ports="*", administrators can use ranges and commas -->
   <!-- This will allow access to ports 123, 456, 457, and 458 -->
   <allow-access-from domain="*" to-ports="*" />
</cross-domain-policy>


telnet <ip> 443:

Code: Select all

Escape character is '^]'.
<request-policy-file />
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <!-- This is a master-policy file. site-control can be all,none,master-only -->
   <site-control permitted-cross-domain-policies="all"/>

   <!-- Instead of setting to-ports="*", administrators can use ranges and commas -->
   <!-- This will allow access to ports 123, 456, 457, and 458 -->
   <allow-access-from domain="*" to-ports="*" />
</cross-domain-policy>


telnet <ip> 8080:

Code: Select all

Escape character is '^]'.
<request-policy-file />
Connection closed by foreign host.
User avatar
Lapo
Site Admin
Posts: 21139
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: crossdomain policy hell

Postby Lapo » 16 Jul 2012, 07:40

The request to the BlueBox fails because your not using regular HTTP: you must pass a valid HTTP GET request.
In fact if you point the browser to your bluebox add:port you will obtain it.
Default example:

Code: Select all

http://localhost:8080/crossdomain.xml

The webplayer is supposed to do that correctly. I will ask Thomas Lund to take a look, since he knows the dirty secrets of the Unity Player :)
Lapo

--

gotoAndPlay()

...addicted to flash games
jamieyg3
Posts: 84
Joined: 25 Sep 2008, 16:01

Re: crossdomain policy hell

Postby jamieyg3 » 16 Jul 2012, 16:57

Lapo wrote:The request to the BlueBox fails because your not using regular HTTP: you must pass a valid HTTP GET request.
In fact if you point the browser to your bluebox add:port you will obtain it.
Default example:

Code: Select all

http://localhost:8080/crossdomain.xml

The webplayer is supposed to do that correctly. I will ask Thomas Lund to take a look, since he knows the dirty secrets of the Unity Player :)


yes http://<server-ip>:8080/crossdomain.xml shows it correctly, but not in the webplayer.

I hope this can be fixed soon.

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

Re: crossdomain policy hell

Postby ThomasLund » 19 Jul 2012, 08:21

Hey,

Not an expert on this matter. Unfortunately.

And even more unfortunately is that this is all not SFS client related, but purely Unity webplayer on one side and SFS server on the other. And if the SFS server is sending out the policy file correctly on the different ports, then your best bet is to contact Unity support (good luck....).

Some of the important things to note:
- Unity policy file request before ANY kind of networking
- make sure the network equipment in between doesnt filter on a given port (try using telnet like you did)
- when testing from editor remember to set the fake policy url thingie in the editor preferences
- read any hints here: http://docs.unity3d.com/Documentation/M ... ndbox.html

You can also try to rule out the SFS server by using a small custom policy server that you have full control over.

Here is one I've used before for testing.

Code: Select all

// Socket Policy Server (sockpol)
//
// Copyright (C) 2009 Novell, Inc (http://www.novell.com)
//
// Based on XSP source code (ApplicationServer.cs and XSPWebSource.cs)
// Authors:
//   Gonzalo Paniagua Javier (gonzalo@ximian.com)
//   Lluis Sanchez Gual (lluis@ximian.com)
//
// Copyright (c) Copyright 2002-2007 Novell, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

class SocketPolicyServer {

   const string PolicyFileRequest = "<policy-file-request/>";
   static byte[] request = Encoding.UTF8.GetBytes (PolicyFileRequest);
   private byte[] policy;

   private Socket listen_socket;
   private Thread runner;

   private AsyncCallback accept_cb;

   class Request {
      public Request (Socket s)
      {
         Socket = s;
         // the only answer to a single request (so it's always the same length)
         Buffer = new byte [request.Length];
         Length = 0;
      }

      public Socket Socket { get; private set; }
      public byte[] Buffer { get; set; }
      public int Length { get; set; }
   }

   public SocketPolicyServer (string xml)
   {
      // transform the policy to a byte array (a single time)
      policy = Encoding.UTF8.GetBytes (xml);
   }

   public int Start ()
   {
      try {
         listen_socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         listen_socket.Bind (new IPEndPoint (IPAddress.Any, 843));
         listen_socket.Listen (500);
         listen_socket.Blocking = false;
      }
      catch (SocketException se) {
         // Most common mistake: port 843 is not user accessible on unix-like operating systems
         if (se.SocketErrorCode == SocketError.AccessDenied) {
            Console.WriteLine ("NOTE: must be run as root since the server listen to port 843");
            return 5;
         } else {
            Console.WriteLine (se);
            return 6;
         }
      }

      runner = new Thread (new ThreadStart (RunServer));
      runner.Start ();
      return 0;
   }

   void RunServer ()
   {
      accept_cb = new AsyncCallback (OnAccept);
      listen_socket.BeginAccept (accept_cb, null);

      while (true) // Just sleep until we're aborted.
         Thread.Sleep (1000000);
   }

   void OnAccept (IAsyncResult ar)
   {
      Console.WriteLine("incoming connection");
      Socket accepted = null;
      try {
         accepted = listen_socket.EndAccept (ar);
      } catch {
      } finally {
         listen_socket.BeginAccept (accept_cb, null);
      }

      if (accepted == null)
         return;

      accepted.Blocking = true;

      Request request = new Request (accepted);
      accepted.BeginReceive (request.Buffer, 0, request.Length, SocketFlags.None, new AsyncCallback (OnReceive), request);
   }

   void OnReceive (IAsyncResult ar)
   {
      Request r = (ar.AsyncState as Request);
      Socket socket = r.Socket;
      try {
         r.Length += socket.EndReceive (ar);

         // compare incoming data with expected request
         for (int i=0; i < r.Length; i++) {
            if (r.Buffer [i] != request [i]) {
               // invalid request, close socket
               socket.Close ();
               return;
            }
         }

         if (r.Length == request.Length) {
            Console.WriteLine("got policy request, sending response");
            // request complete, send policy
            socket.BeginSend (policy, 0, policy.Length, SocketFlags.None, new AsyncCallback (OnSend), socket);
         } else {
            // continue reading from socket
            socket.BeginReceive (r.Buffer, r.Length, request.Length - r.Length, SocketFlags.None,
               new AsyncCallback (OnReceive), ar.AsyncState);
         }
      } catch {
         // if anything goes wrong we stop our connection by closing the socket
         socket.Close ();
      }
        }

   void OnSend (IAsyncResult ar)
        {
      Socket socket = (ar.AsyncState as Socket);
      try {
         socket.EndSend (ar);
      } catch {
         // whatever happens we close the socket
      } finally {
         socket.Close ();
      }
   }

   public void Stop ()
   {
      runner.Abort ();
      listen_socket.Close ();
   }

   const string AllPolicy =

@"<?xml version='1.0'?>
<cross-domain-policy>
        <allow-access-from domain=""*"" to-ports=""*"" />
</cross-domain-policy>";

   const string LocalPolicy =

@"<?xml version='1.0'?>
<cross-domain-policy>
   <allow-access-from domain=""*"" to-ports=""4500-4550"" />
</cross-domain-policy>";

   static int Main (string[] args)
   {
      if (args.Length == 0) {
         Console.WriteLine ("sockpol [--all | --range | --file policy]");
         Console.WriteLine ("\t--all   Allow access on every port)");
         Console.WriteLine ("\t--range   Allow access on portrange 4500-4550)");
         return 1;
      }

      string policy = null;
      switch (args [0]) {
      case "--all":
         policy = AllPolicy;
         break;
      case "--local":
         policy = LocalPolicy;
         break;
      case "--file":
         if (args.Length < 2) {
            Console.WriteLine ("Missing policy file name after '--file'.");
            return 2;
         }
         string filename = args [1];
         if (!File.Exists (filename)) {
            Console.WriteLine ("Could not find policy file '{0}'.", filename);
            return 3;
         }
         using (StreamReader sr = new StreamReader (filename)) {
            policy = sr.ReadToEnd ();
         }
         break;
      default:
         Console.WriteLine ("Unknown option '{0}'.", args [0]);
         return 4;
      }

      SocketPolicyServer server = new SocketPolicyServer (policy);
      int result = server.Start ();
      if (result != 0)
         return result;

      Console.WriteLine ("Hit Return to stop the server.");
      Console.ReadLine ();
      server.Stop ();
      return 0;
   }
}

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 3 guests