Created
November 13, 2014 23:56
-
-
Save zapu/1e2803e00dc7519ab1cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SocketPolicyServer | |
type TcpListener = System.Net.Sockets.TcpListener | |
type IPAddress = System.Net.IPAddress | |
type HttpListenerRequest = System.Net.HttpListenerRequest | |
open System.Threading.Tasks | |
open System.Threading | |
let StartPolicyServer (gamePort:int) (policyServerPort:int) = | |
let PolicyString = | |
[| | |
"<?xml version=\"1.0\"?>" | |
"<cross-domain-policy>" | |
" <allow-access-from domain=\"*\" to-ports=\"" + (string gamePort) + "\"/>" | |
"</cross-domain-policy>" | |
|] |> String.concat "" | |
let responseBuffer = System.Text.Encoding.UTF8.GetBytes(PolicyString) | |
let PolicyRequestString = "<policy-file-request/>" | |
let listener = new TcpListener(IPAddress.Any, policyServerPort) | |
let prefix = "http://localhost:" + (string policyServerPort) + "/" | |
listener.Start() | |
let task = async { | |
while true do | |
let! client = listener.AcceptTcpClientAsync() |> Async.AwaitTask | |
let task = async { | |
let stream = client.GetStream() | |
use cts = new CancellationTokenSource() | |
let timer:(Async<byte[] option>) = async { | |
do! Async.Sleep(5000) | |
return None | |
} | |
let task = async { | |
let! buffer = stream.AsyncRead PolicyRequestString.Length | |
return Some buffer | |
} | |
let tasks = ([|task; timer|] |> Seq.map Async.StartAsTask) | |
let! maybeBuffer = (Task.WhenAny tasks |> Async.AwaitTask) | |
match maybeBuffer.Result with | |
| Some _ -> | |
printfn "Win" | |
do! stream.AsyncWrite responseBuffer | |
| None -> | |
printfn "Lose" | |
client.Close() | |
} | |
Async.Start task | |
} | |
(listener, task) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment