-
Notifications
You must be signed in to change notification settings - Fork 2
/
FSock.fsx
executable file
·71 lines (69 loc) · 2.42 KB
/
FSock.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#load "../src/FSock/Collections.fs"
open FSock
#load "../src/FSock/Definitions.fs"
open FSock
#load "../src/FSock/Messaging.fs"
open FSock
#load "../src/FSock/Sockets.fs"
open FSock
#load "../src/FSock/FSock.fs"
#load "Testing.fsx"
#nowarn "40"
open System
open System.Collections
open System.Collections.Concurrent
let all () =
let port = 8091
let errors = ConcurrentBag()
let domain = Testing.bytesDomain
let log x = Printf.ksprintf (fun msg -> lock stdout (fun () -> stdout.WriteLine(msg))) x
let received = ConcurrentQueue()
let report prefix e =
log "%s :: %O" prefix e
errors.Add(e)
let client =
async {
let! cli =
FSock.Client.AsyncConnect(fun cfg ->
cfg.Report <- fun e -> report "client" e
cfg.Port <- port)
do!
async {
for msg in domain do
do! cli.Connection.AsyncSendMessage(msg)
let! rep = cli.Connection.AsyncReceiveMessage()
if rep <> Array.append msg msg then
report "client" (exn "Turnaround failure")
}
|> cli.Connection.AsyncWrap
return ()
}
let serverDone = FSock.Future.Create()
let server =
FSock.Server.Start(fun cfg ->
cfg.Report <- fun e -> report "server" e
cfg.Port <- port
cfg.OnConnect <- fun conn ->
async {
for i in 1 .. domain.Length do
let! msg = conn.AsyncReceiveMessage()
do! conn.AsyncSendMessage(Array.append msg msg)
do received.Enqueue(msg)
return serverDone.Set(())
}
|> conn.AsyncWrap
|> Async.Start)
let wrap main =
async { try return! main with e -> return errors.Add(e) }
Async.Parallel [| client; serverDone.Future.AsyncAwait() |]
|> Async.Ignore
|> wrap
|> Async.RunSynchronously
if received.ToArray() <> domain then
failwithf "FSock: did not receive the sent messages"
if errors.Count > 0 then
for e in errors do
log "%O" e
failwithf "FSock: encountered errors"
server.AsyncStop() |> Async.RunSynchronously
log "FSock: OK"