Skip to content

Commit

Permalink
Revert "Update to messagepack 2.1"
Browse files Browse the repository at this point in the history
This reverts commit 2bd8cb7.
  • Loading branch information
yatli committed Apr 7, 2020
1 parent c4458b9 commit 104449f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 50 deletions.
42 changes: 36 additions & 6 deletions Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ open Avalonia.ReactiveUI
open System.Threading
open Avalonia.Controls.ApplicationLifetimes

open MessagePack
open MessagePack.Resolvers

open System
open System.IO
open getopt
open Shell
open common
open MessagePack.Formatters

// Avalonia configuration, don't remove; also used by visual designer.
[<CompiledName "BuildAvaloniaApp">]
Expand All @@ -26,19 +30,45 @@ let buildAvaloniaApp() =
.With(new MacOSPlatformOptions(ShowInDock=true))
.LogToDebug()

type MsgPackFormatter(resolver: IFormatterResolver) =
let m_formatter = resolver.GetFormatter<obj>()
interface IMessagePackFormatter<obj> with
member this.Serialize(bytes: byref<byte []>, offset: int, value: obj, formatterResolver: IFormatterResolver): int =
m_formatter.Serialize(&bytes, offset, value, formatterResolver)
member x.Deserialize(bytes: byte[] , offset: int, formatterResolver: IFormatterResolver , readSize: byref<int>) =
if MessagePackBinary.GetMessagePackType(bytes, offset) = MessagePackType.Extension then
let result = MessagePackBinary.ReadExtensionFormat(bytes, offset, &readSize)
if result.TypeCode = 1y then
let mutable _size = 0
m_formatter.Deserialize(result.Data, 0, formatterResolver, &_size)
else
m_formatter.Deserialize(bytes, offset, formatterResolver, &readSize)
else
m_formatter.Deserialize(bytes, offset, formatterResolver, &readSize)

type MsgPackResolver() =
static let s_formatter = box(MsgPackFormatter(MessagePack.Resolvers.StandardResolver.Instance))
static let s_resolver = MessagePack.Resolvers.StandardResolver.Instance
interface IFormatterResolver with
member x.GetFormatter<'a>() =
if typeof<'a> = typeof<obj> then
s_formatter :?> IMessagePackFormatter<'a>
else
s_resolver.GetFormatter<'a>()


[<EntryPoint>]
[<CompiledName "Main">]
let main(args: string[]) =

let _ = Thread.CurrentThread.TrySetApartmentState(ApartmentState.STA)

//CompositeResolver.RegisterAndSetAsDefault(
// MsgPackResolver()
//// ImmutableCollectionResolver.Instance,
//// FSharpResolver.Instance,
//// StandardResolver.Instance
//)
CompositeResolver.RegisterAndSetAsDefault(
MsgPackResolver()
// ImmutableCollectionResolver.Instance,
// FSharpResolver.Instance,
// StandardResolver.Instance
)

AppDomain.CurrentDomain.UnhandledException.Add(fun exArgs ->
let filename = Path.Combine(config.configdir, sprintf "fvim-crash-%s.txt" (DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")))
Expand Down
2 changes: 1 addition & 1 deletion fvim.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
</PackageReference>
<PackageReference Include="HarfBuzzSharp" Version="1.4.6.2" />
<PackageReference Include="HarfBuzzSharp.NativeAssets.Linux" Version="1.4.6.2" />
<PackageReference Include="MessagePack" Version="2.1.90" />
<PackageReference Include="MessagePack" Version="1.9.11" />
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" />
<PackageReference Include="NSubsys" Version="1.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
73 changes: 30 additions & 43 deletions neovim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ open System.Threading.Tasks
open System.Threading
open FSharp.Control.Reactive
open FSharp.Control.Tasks.V2.ContextSensitive
open MessagePack.Formatters

let inline private trace fmt = trace "neovim.process" fmt

Expand Down Expand Up @@ -111,47 +110,35 @@ type Nvim() =
| _ -> failwith ""

let read (ob: IObserver<obj>) (cancel: CancellationToken) =
let thread =
Thread(fun () ->
let task = task {
trace "%s" "begin read loop"
trace "%A" <| stdout.GetType().FullName
use reader = new MessagePackStreamReader(stdout)
let mutable ex = false
while not ex && not cancel.IsCancellationRequested do
try
let! msgpack = reader.ReadAsync(cancel)
if msgpack.HasValue then
let mutable v = msgpack.Value
let data = MessagePackSerializer.Deserialize<obj>(&v)
ob.OnNext(data)
else
ex <- true
with
| :? InvalidOperationException
| :? System.IO.IOException
| :? System.Net.Sockets.SocketException
| :? ObjectDisposedException
as _ex -> ex <- true

let ec = serverExitCode()
if ec.IsSome then
let code = ec.Value
trace "end read loop: process exited, code = %d" code
if code <> 0 then
m_cancelSrc.Cancel()
ob.OnNext([|box (Crash code)|])
else
ob.OnNext([|box Exit|])
else
trace "%s" "end read loop."
ob.OnNext([|box Exit|])
ob.OnCompleted()
}
task.Wait()
)
thread.Start()
Task.Factory.StartNew(fun () -> thread.Join())
Task.Factory.StartNew(fun () ->
trace "%s" "begin read loop"
let mutable ex = false
while not ex && not cancel.IsCancellationRequested do
try
let data = MessagePackSerializer.Deserialize<obj>(stdout, true)
ob.OnNext(data)
with
| :? InvalidOperationException
| :? System.IO.IOException
| :? System.Net.Sockets.SocketException
| :? ObjectDisposedException
as _ex -> ex <- true

let ec = serverExitCode()
if ec.IsSome then
let code = ec.Value
trace "end read loop: process exited, code = %d" code
if code <> 0 then
m_cancelSrc.Cancel()
ob.OnNext([|box (Crash code)|])
else
ob.OnNext([|box Exit|])
else
trace "%s" "end read loop."
ob.OnNext([|box Exit|])
ob.OnCompleted()

, cancel, TaskCreationOptions.LongRunning, TaskScheduler.Current)

let reply (id: int) (rsp: Response) = task {
let result, error =
Expand Down Expand Up @@ -225,7 +212,7 @@ type Nvim() =

let payload = mkparams4 0 myid ev.method ev.parameters
#if DEBUG
MessagePackSerializer.SerializeToJson(payload) |> trace "call: %d -> %s" myid
MessagePackSerializer.ToJson(payload) |> trace "call: %d -> %s" myid
#endif
do! MessagePackSerializer.SerializeAsync(stdin, payload)
do! stdin.FlushAsync()
Expand Down

0 comments on commit 104449f

Please sign in to comment.