Skip to content

Instantly share code, notes, and snippets.

@jpalmer
Created August 22, 2014 00:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jpalmer/d7149e84301b9e2586c6 to your computer and use it in GitHub Desktop.
let udpClientListeningController =
Agent.Start(fun inbox ->
let rec loop (runningClients:UdpClient seq) = async {
let! msg = inbox.Receive()
let closeRunningClients () = runningClients |> Seq.iter (fun client -> client.ProtectedClose())
match msg with
| ListenForSearchResponsesAndNotifyAdverts (port, ipAddresses) ->
closeRunningClients ()
let uniAndMulticastUdpClients = seq {
let portIPPairs = ipAddresses |> Seq.zip (seq { yield port })
yield! portIPPairs |> Seq.map createUnicastClient
yield! portIPPairs |> Seq.map (snd >> createMulticastClient)
}
//Start em up.
uniAndMulticastUdpClients
|> Seq.iter (udpClientReceiveLoopAsync >> Async.Start)
return! loop uniAndMulticastUdpClients
| StopListening ->
closeRunningClients ()
return! loop Seq.empty
}
loop Seq.empty
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment