Skip to content

Commit

Permalink
#1411 - Change to JsonSerialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Jooseppi12 committed Jul 25, 2024
1 parent 6a7738b commit e6c77d1
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ nuget System.Net.Http 4.3.0
nuget FSharp.Core 6.0.0

nuget System.Text.RegularExpressions 4.3.0
nuget System.Text.Json 4.7.0
nuget System.Text.Json 8.0.4
nuget System.Security.Cryptography.Csp 4.3.0

nuget Microsoft.TypeScript.MSBuild 4.9.4
Expand Down
31 changes: 16 additions & 15 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ NUGET
Microsoft.Net.Http.Headers (>= 2.0)
Microsoft.AspNetCore.Routing.Abstractions (2.0)
Microsoft.AspNetCore.Http.Abstractions (>= 2.0)
Microsoft.Bcl.AsyncInterfaces (1.1) - restriction: || (&& (== net8.0) (>= monoandroid) (< netstandard2.0)) (&& (== net8.0) (>= monotouch)) (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (&& (== net8.0) (< netcoreapp2.1)) (&& (== net8.0) (< netcoreapp3.0)) (&& (== net8.0) (>= uap10.1)) (&& (== net8.0) (>= xamarinios)) (&& (== net8.0) (>= xamarinmac)) (&& (== net8.0) (>= xamarintvos)) (&& (== net8.0) (>= xamarinwatchos)) (== netstandard2.0)
System.Threading.Tasks.Extensions (>= 4.5.2) - restriction: || (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.1)) (== netstandard2.0)
Microsoft.Bcl.AsyncInterfaces (8.0) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< net6.0)) (== netstandard2.0)
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< netstandard2.1)) (== netstandard2.0)
Microsoft.Build.Framework (16.8)
System.Security.Permissions (>= 4.7)
Microsoft.Build.Utilities.Core (16.8)
Expand Down Expand Up @@ -267,7 +267,7 @@ NUGET
System.Runtime (>= 4.1)
System.Runtime.Extensions (>= 4.1)
System.Threading (>= 4.0.11)
System.Memory (4.5.4)
System.Memory (4.5.5)
System.Buffers (>= 4.5.1) - restriction: || (&& (== net8.0) (>= monotouch)) (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (&& (== net8.0) (< netstandard1.1)) (&& (== net8.0) (< netstandard2.0)) (&& (== net8.0) (>= xamarinios)) (&& (== net8.0) (>= xamarinmac)) (&& (== net8.0) (>= xamarintvos)) (&& (== net8.0) (>= xamarinwatchos)) (== netstandard2.0)
System.Numerics.Vectors (>= 4.4) - restriction: || (&& (== net8.0) (< netcoreapp2.0)) (== netstandard2.0)
System.Runtime.CompilerServices.Unsafe (>= 4.5.3) - restriction: || (&& (== net8.0) (>= monotouch)) (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (&& (== net8.0) (< netcoreapp2.1)) (&& (== net8.0) (< netstandard1.1)) (&& (== net8.0) (< netstandard2.0)) (&& (== net8.0) (>= uap10.1)) (&& (== net8.0) (>= xamarinios)) (&& (== net8.0) (>= xamarinmac)) (&& (== net8.0) (>= xamarintvos)) (&& (== net8.0) (>= xamarinwatchos)) (== netstandard2.0)
Expand Down Expand Up @@ -303,7 +303,7 @@ NUGET
Microsoft.NETCore.Targets (>= 1.1)
System.Runtime (>= 4.3)
System.Runtime.Handles (>= 4.3)
System.Numerics.Vectors (4.5) - restriction: || (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (== netstandard2.0)
System.Numerics.Vectors (4.5) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< netcoreapp2.0)) (== netstandard2.0)
System.ObjectModel (4.0.12)
System.Collections (>= 4.0.11)
System.Diagnostics.Debug (>= 4.0.11)
Expand Down Expand Up @@ -348,7 +348,7 @@ NUGET
System.Runtime (4.3)
Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1)
System.Runtime.CompilerServices.Unsafe (5.0)
System.Runtime.CompilerServices.Unsafe (6.0)
System.Runtime.Extensions (4.3)
Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1)
Expand Down Expand Up @@ -506,16 +506,17 @@ NUGET
Microsoft.NETCore.Targets (>= 1.0.1)
System.Runtime (>= 4.1)
System.Text.Encoding (>= 4.0.11)
System.Text.Encodings.Web (4.7)
System.Memory (>= 4.5.3) - restriction: || (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.1) (< netstandard2.1)) (&& (== net8.0) (>= uap10.1)) (== netstandard2.0)
System.Text.Json (4.7)
Microsoft.Bcl.AsyncInterfaces (>= 1.1) - restriction: || (&& (== net8.0) (>= monoandroid) (< netstandard2.0)) (&& (== net8.0) (>= monotouch)) (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (&& (== net8.0) (< netcoreapp2.1)) (&& (== net8.0) (< netcoreapp3.0)) (&& (== net8.0) (>= uap10.1)) (&& (== net8.0) (>= xamarinios)) (&& (== net8.0) (>= xamarinmac)) (&& (== net8.0) (>= xamarintvos)) (&& (== net8.0) (>= xamarinwatchos)) (== netstandard2.0)
System.Buffers (>= 4.5) - restriction: || (&& (== net8.0) (>= monoandroid) (< netstandard2.0)) (&& (== net8.0) (>= monotouch)) (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (&& (== net8.0) (>= xamarinios)) (&& (== net8.0) (>= xamarinmac)) (&& (== net8.0) (>= xamarintvos)) (&& (== net8.0) (>= xamarinwatchos)) (== netstandard2.0)
System.Memory (>= 4.5.3) - restriction: || (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (&& (== net8.0) (< netcoreapp2.1)) (&& (== net8.0) (>= uap10.1)) (== netstandard2.0)
System.Numerics.Vectors (>= 4.5) - restriction: || (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (== netstandard2.0)
System.Runtime.CompilerServices.Unsafe (>= 4.7) - restriction: || (&& (== net8.0) (>= monoandroid) (< netstandard2.0)) (&& (== net8.0) (>= monotouch)) (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (&& (== net8.0) (< netcoreapp2.1)) (&& (== net8.0) (< netcoreapp3.0)) (&& (== net8.0) (< netcoreapp3.1)) (&& (== net8.0) (>= uap10.1)) (&& (== net8.0) (>= xamarinios)) (&& (== net8.0) (>= xamarinmac)) (&& (== net8.0) (>= xamarintvos)) (&& (== net8.0) (>= xamarinwatchos)) (== netstandard2.0)
System.Text.Encodings.Web (>= 4.7) - restriction: || (&& (== net8.0) (>= monoandroid) (< netstandard2.0)) (&& (== net8.0) (>= monotouch)) (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (&& (== net8.0) (< netcoreapp2.1)) (&& (== net8.0) (< netcoreapp3.0)) (&& (== net8.0) (< netcoreapp3.1)) (&& (== net8.0) (>= uap10.1)) (&& (== net8.0) (>= xamarinios)) (&& (== net8.0) (>= xamarinmac)) (&& (== net8.0) (>= xamarintvos)) (&& (== net8.0) (>= xamarinwatchos)) (== netstandard2.0)
System.Threading.Tasks.Extensions (>= 4.5.2) - restriction: || (&& (== net8.0) (>= net461)) (&& (== net8.0) (< netcoreapp2.0)) (&& (== net8.0) (< netcoreapp2.1)) (&& (== net8.0) (>= uap10.1)) (== netstandard2.0)
System.Text.Encodings.Web (8.0)
System.Buffers (>= 4.5.1) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< net6.0)) (== netstandard2.0)
System.Memory (>= 4.5.5) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< net6.0)) (== netstandard2.0)
System.Runtime.CompilerServices.Unsafe (>= 6.0) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< net6.0)) (&& (== net8.0) (< net7.0)) (== netstandard2.0)
System.Text.Json (8.0.4)
Microsoft.Bcl.AsyncInterfaces (>= 8.0) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< net6.0)) (== netstandard2.0)
System.Buffers (>= 4.5.1) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< net6.0)) (== netstandard2.0)
System.Memory (>= 4.5.5) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< net6.0)) (== netstandard2.0)
System.Runtime.CompilerServices.Unsafe (>= 6.0) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< net6.0)) (&& (== net8.0) (< net7.0)) (== netstandard2.0)
System.Text.Encodings.Web (>= 8.0)
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net8.0) (>= net462)) (&& (== net8.0) (< net6.0)) (== netstandard2.0)
System.Text.RegularExpressions (4.3)
System.Collections (>= 4.3) - restriction: || (&& (== net8.0) (< netcoreapp1.1)) (== netstandard2.0)
System.Globalization (>= 4.3) - restriction: || (&& (== net8.0) (< netcoreapp1.1)) (== netstandard2.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
<ItemGroup>
<Compile Include="LoggerBase.fs" />
Expand Down
37 changes: 28 additions & 9 deletions src/compiler/WebSharper.Compiler/WsFscServiceCommon.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module WebSharper.Compiler.WsFscServiceCommon

open System.IO.Pipes
open System.Runtime.Serialization.Formatters.Binary
open System.Text

type ArgsType = {args: string array}

Expand All @@ -19,17 +19,36 @@ let hashPath (fullPath: string) =
let readingMessages (pipe: PipeStream) (handleMessage: obj -> Async<'a option>) =
let rec readingMessage() =
async {
let bf = new BinaryFormatter()
try
let deserializedMessage = bf.Deserialize(pipe)
let! finish = handleMessage deserializedMessage
match finish with
| Some _ -> return finish
| None -> return! readingMessage()
let readMessage () =
async {
let sb = new StringBuilder()
let mutable buffer = Array.zeroCreate<byte> 5
let bytesRead = pipe.Read(buffer, 0, buffer.Length)
sb.Append(System.Text.Encoding.UTF8.GetString(buffer)) |> ignore
buffer <- Array.zeroCreate<byte> 5
while (not pipe.IsMessageComplete) do
let bytesRead = pipe.Read(buffer, 0, buffer.Length)
sb.Append(System.Text.Encoding.UTF8.GetString(buffer)) |> ignore
buffer <- Array.zeroCreate<byte> 5
let res = sb.ToString()
return res.Replace("\x00", "")
}
let! byteArr = readMessage ()
match byteArr with
| "" | null ->
do! Async.Sleep 1000
return! readingMessage()
| _ ->
let deserializedMessage = System.Text.Json.JsonSerializer.Deserialize(byteArr)
let! finish = handleMessage deserializedMessage
match finish with
| Some _ -> return finish
| None -> return! readingMessage()
with
| :? System.Runtime.Serialization.SerializationException ->
| :? System.Runtime.Serialization.SerializationException as ex->
return None
| _ ->
| ex ->
return! readingMessage()
}
readingMessage ()
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/WebSharper.Compiler/paket.references
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Mono.Cecil
exclude Mono.Cecil.Rocks.dll
System.Runtime.Loader
FSharp.Core
FSharp.Core
System.Text.Json
20 changes: 9 additions & 11 deletions src/compiler/WebSharper.FSharp.Service/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ open System
open System.IO.Pipes
open System.Threading
open System.IO
open System.Runtime.Serialization.Formatters.Binary
open FSharp.Compiler.CodeAnalysis
open System.Runtime.Caching
open WebSharper.Compiler.WsFscServiceCommon
Expand Down Expand Up @@ -99,12 +98,11 @@ let startListening() =
let send (serverPipe: NamedPipeServerStream) paramPrint str = async {
let newMessage = paramPrint str
nLogger.Trace(sprintf "Server sends: %s" newMessage)
let bf = new BinaryFormatter()
use ms = new MemoryStream()
bf.Serialize(ms, newMessage)
ms.Flush()
ms.Position <- 0L
do! ms.CopyToAsync(serverPipe) |> Async.AwaitTask
let options = System.Text.Json.JsonSerializerOptions()
options.Encoder <-System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
let res = System.Text.Json.JsonSerializer.Serialize(newMessage, options)
let bytes = System.Text.Encoding.UTF8.GetBytes res
serverPipe.Write(bytes, 0, bytes.Length)
serverPipe.Flush()
}

Expand All @@ -126,7 +124,6 @@ let startListening() =
try
// read a message
let! (deserializedMessage: ArgsType, serverPipe: NamedPipeServerStream, token) = inbox.Receive()

let send = send serverPipe
let sendFinished = sendFinished serverPipe
// all compilation output goes through a logger. This in standalone mode goes to stdout and stderr
Expand Down Expand Up @@ -247,7 +244,7 @@ let startListening() =
try
let handleMessage (message: obj) =
async {
let message = message :?> ArgsType
let message = System.Text.Json.JsonSerializer.Deserialize<ArgsType>(string message)
if message.args.Length = 1 && message.args.[0].StartsWith "compile:" then
let project = message.args.[0].Substring(8)
match argsDict.TryGetValue project with
Expand Down Expand Up @@ -275,11 +272,12 @@ let startListening() =
let serverPipe = new NamedPipeServerStream(
pipeName, // name of the pipe,
PipeDirection.InOut, // diretcion of the pipe
-1, // max number of server instances
PipeTransmissionMode.Byte, // Transmissione Mode
NamedPipeServerStream.MaxAllowedServerInstances, // max number of server instances
PipeTransmissionMode.Message, // Transmissione Mode
PipeOptions.WriteThrough // the operation will not return the control until the write is completed
||| PipeOptions.Asynchronous)
do! serverPipe.WaitForConnectionAsync(token) |> Async.AwaitTask
serverPipe.ReadMode <- PipeTransmissionMode.Message
Async.Start (handOverPipe serverPipe token, token)
do! pipeListener token
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<Signed>False</Signed>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<AssemblyName>wsfscservice</AssemblyName>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
<RuntimeIdentifiers>win-x64;linux-x64;linux-musl-x64;osx-x64</RuntimeIdentifiers>
<RollForward>LatestMajor</RollForward>
<ApplicationIcon>../../../tools/WebSharper.ico</ApplicationIcon>
Expand Down
26 changes: 15 additions & 11 deletions src/compiler/WebSharper.FSharp/NamedPipeClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
// $end{copyright}
module WebSharper.FSharp.NamedPipeClient
#nowarn "44"
open System.Text


open System.Diagnostics
open System.IO.Pipes
open System.Runtime.Serialization.Formatters.Binary
open WebSharper.Compiler.WsFscServiceCommon
open System.IO

Expand Down Expand Up @@ -91,14 +92,15 @@ let sendCompileCommand args projectDir =
PipeDirection.InOut, // direction of the pipe
PipeOptions.WriteThrough // the operation will not return the control until the write is completed
||| PipeOptions.Asynchronous)
let Write (ms: MemoryStream) =
let Write (ms: byte []) =
if clientPipe.IsConnected && clientPipe.CanWrite then
let unexpectedFinishErrorCode = -12211
let write = async {
let printResponse (message: obj) =
async {
// messages on the service have n: e: or x: prefix for stdout stderr or error code kind of output
match message :?> string with
let jE = message :?> Json.JsonElement
match jE.GetString() with
| StdOut n ->
printfn "%s" n
return None
Expand All @@ -111,14 +113,16 @@ let sendCompileCommand args projectDir =
let unrecognizedMessageErrorCode = -13311
return unrecognizedMessageErrorCode |> Some
}
do! ms.CopyToAsync(clientPipe) |> Async.AwaitTask

clientPipe.Write(ms, 0, ms.Length)
clientPipe.Flush()
clientPipe.WaitForPipeDrain()
let! errorCode = readingMessages clientPipe printResponse
match errorCode with
| Some -12211 ->
return -12211
| Some x -> return x
| None ->
| None ->
return unexpectedFinishErrorCode
}
try
Expand All @@ -134,13 +138,13 @@ let sendCompileCommand args projectDir =



let bf = new BinaryFormatter();
use ms = new MemoryStream()
// args going binary serialized to the service.
let startCompileMessage: ArgsType = {args = args}
bf.Serialize(ms, startCompileMessage);
ms.Flush();
ms.Position <- 0L
clientPipe.Connect()
let returnCode = Write ms
clientPipe.ReadMode <- PipeTransmissionMode.Message
let options = System.Text.Json.JsonSerializerOptions()
options.Encoder <- Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
let res = System.Text.Json.JsonSerializer.Serialize(startCompileMessage, options)
let bytes = System.Text.Encoding.UTF8.GetBytes res
let returnCode = Write bytes
returnCode
1 change: 0 additions & 1 deletion src/compiler/WebSharper.FSharp/WebSharper.FSharp.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<Signed>False</Signed>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<FSharpTool>True</FSharpTool>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
<RuntimeIdentifiers>win-x64;linux-x64;linux-musl-x64;osx-x64</RuntimeIdentifiers>
<RollForward>LatestMajor</RollForward>
</PropertyGroup>
Expand Down

0 comments on commit e6c77d1

Please sign in to comment.