-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.fs
90 lines (74 loc) · 2.33 KB
/
Utils.fs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*)
module Longboat.Utils
open System.Net.Sockets
open System.Text
open System.Threading.Tasks
open System.Runtime.InteropServices
///Converts a local .NET string to a UTF-8 byte array for network use.
///Also replaces Unix-style LF line endings with the CRLF used by Gopher and HTTP.
let netstring (str: string) =
Encoding.UTF8.GetBytes (str.ReplaceLineEndings("\r\n"))
///Converts a UTF-8 byte array to a local .NET string.
///Also replaces Gopher or HTTP CRLF line endings with Unix-style LF.
let localstring (bytes: byte[]) =
(Encoding.UTF8.GetString bytes).ReplaceLineEndings("\n")
///Formats the given string as a Gopher information(i) element.
let gopherinfo str =
sprintf "i%s\t\t(NULL)\t0\n" str
///Formats the given string as a Gopher error(3) element.
let gophererr str =
sprintf "3%s\t\t(NULL)\t0\n" str
let pathcmp (str1: string) (str2: string) =
let stripslash (x: string) = if x.EndsWith '/' && x.Length <> 1 then x[..x.Length - 2] else x
let path1 =
str1.Trim()
|> stripslash
let path2 =
str2.Trim()
|> stripslash
path1 = path2
type platform =
| Linux
| FreeBSD
| MacOS
| Windows
| Unknown
let getCurrentPlatform () =
if RuntimeInformation.IsOSPlatform OSPlatform.Linux then
Linux
elif RuntimeInformation.IsOSPlatform OSPlatform.FreeBSD then
FreeBSD
elif RuntimeInformation.IsOSPlatform OSPlatform.OSX then
MacOS
elif RuntimeInformation.IsOSPlatform OSPlatform.Windows then
Windows
else
Unknown
let inline waittask (t: Task) =
t
|> Async.AwaitTask
|> Async.RunSynchronously
let waitvtask (t: ValueTask) =
t.AsTask()
|> Async.AwaitTask
|> Async.RunSynchronously
let waitreturn t =
t
|> Async.AwaitTask
|> Async.RunSynchronously
let waitvreturn (t: ValueTask<'a>) =
t.AsTask()
|> Async.AwaitTask
|> Async.RunSynchronously
let trywrite(stream: NetworkStream)(buffer: byte[]): bool =
try
stream.WriteAsync(buffer).AsTask()
|> Async.AwaitTask
|> Async.RunSynchronously
true
with
| _ -> false