-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponent1.fs
More file actions
50 lines (49 loc) · 1.73 KB
/
Component1.fs
File metadata and controls
50 lines (49 loc) · 1.73 KB
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
namespace CommonLib
[<AutoOpen>]
module Core =
let MAX_PACKET_SIZE = (1024*1024)
//p.Dispose()
//From common.h
type Daemon =
|Meta = 0
|Backend = 1
|Disk = 2
|User = 3
|Page = 4
|Log = 5
|Cli = 6
|Pattern = 7
type PayloadVerb =
|Get = 0
|GotPage = 1
|GotFile = 2
|Res404 = 3
|GotPost = 4
type Packet =
{payloadsize : int; payloadverb : PayloadVerb; payload : byte[]}
member x.ToByteArray() = Array.concat [|System.BitConverter.GetBytes(x.payloadsize);System.BitConverter.GetBytes(x.payloadverb|>int);x.payload|]
let read (p:System.IO.Pipes.NamedPipeServerStream) =
p.WaitForConnection()
let b = Array.zeroCreate MAX_PACKET_SIZE
p.Read(b,0,8)|>ignore
let payloadsize = System.BitConverter.ToInt32(b,0)
p.Read(b,8,MAX_PACKET_SIZE-8)|>ignore
{
payloadsize = payloadsize;
payloadverb = System.BitConverter.ToInt32(b,4) |> enum;
payload = if payloadsize = 0 then [||] else b.[8..7+payloadsize]
}
let write (p:System.IO.Pipes.NamedPipeServerStream) (packet:Packet) =
p.WaitForConnection()
let b = packet.ToByteArray()
p.Write(b,0,b.Length)|>ignore
let rec flush (p:System.IO.Pipes.NamedPipeServerStream) = ()
//try
//let rec inner i =
//Async.RunSynchronously(async{p.ReadByte()},10)|>ignore
//inner(i+1)
//flush(p)
//inner 0
//with |_ -> ()
//p.CopyTo(System.IO.Stream.Null)
//Async.RunSynchronously(async{p.AsyncRead(MAX_PACKET_SIZE)|>Async.Ignore|>Async.RunSynchronously;flush(p)},1000)