Skip to content

Commit

Permalink
daemon sends back session id or error number
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatao Li committed Nov 18, 2020
1 parent 9080244 commit 157737c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
35 changes: 22 additions & 13 deletions daemon.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ let attachSession id svrpipe =
| true, ({ server = None } as s) ->
let ns = {s with server = Some svrpipe}
sessions.[id] <- ns
Some ns
| _ -> None
Ok ns
| _ -> Error -2

let newSession nvim stderrenc args svrpipe =
let myid = sessionId
Expand All @@ -74,15 +74,16 @@ let newSession nvim stderrenc args svrpipe =
sessionId <- sessionId + 1
sessions.[myid] <- session
proc.Start() |> ignore
Some session
Ok session


let attachFirstSession svrpipe =
sessions |> Seq.tryFind (fun kv -> kv.Value.server.IsNone)
>>= fun kv ->
>>= (fun kv ->
let ns = {kv.Value with server = Some svrpipe}
sessions.[kv.Key] <- ns
Some ns
Some ns)
|> function | Some ns -> Ok ns | None -> Error -1

let serveSession (session: Session) =
async {
Expand Down Expand Up @@ -130,11 +131,13 @@ let serve nvim stderrenc (pipe: NamedPipeServerStream) =
| AttachFirst _ -> attachFirstSession pipe

match session with
| None ->
trace "Session unavailable for request %A" request
| Error errno ->
trace "Session unavailable for request %A, errno=%d" request errno
do! fromInt32LE errno |> readonlymemory |> write pipe
return()
| Some session ->
| Ok session ->
trace "Request %A is attaching to session %d" request session.id
do! fromInt32LE session.id |> readonlymemory |> write pipe
do! serveSession session
finally
try
Expand Down Expand Up @@ -165,8 +168,14 @@ let fvrConnect (stdin: Stream) (verb: FVimRemoteVerb) =
verb
|> Json.serialize
|> Text.Encoding.UTF8.GetBytes
let len = fromInt32LE payload.Length
stdin.Write(FVR_MAGIC, 0, FVR_MAGIC.Length)
stdin.Write(len, 0, len.Length)
stdin.Write(payload, 0, payload.Length)
stdin.Flush()
let intbuf = fromInt32LE payload.Length
try
stdin.Write(FVR_MAGIC, 0, FVR_MAGIC.Length)
stdin.Write(intbuf, 0, intbuf.Length)
stdin.Write(payload, 0, payload.Length)
stdin.Flush()
Async.StartAsTask(read stdin (intbuf.AsMemory())).Wait()
toInt32LE intbuf
with ex ->
trace "%O" ex
-10
10 changes: 8 additions & 2 deletions neovim.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,21 @@ type Nvim() =
let pipe = new System.IO.Pipes.NamedPipeClientStream(".", name, IO.Pipes.PipeDirection.InOut, IO.Pipes.PipeOptions.Asynchronous, TokenImpersonationLevel.Impersonation)
pipe.Connect()
trace "Connected, sending session request..."
fvrConnect pipe verb
let id = fvrConnect pipe verb
if id < 0 then
pipe.Dispose()
failwithf "Remote daemon closed the connection with error code %d" id
RemoteSession pipe
| FVimRemote(pipe, Remote(prog, args), verb, _) ->
let pname = Option.defaultValue defaultDaemonName pipe
let paddr = pipeaddrUnix pname
trace "Connecting to remote fvr session '%s'" paddr
let proc = newProcess prog (args @ [paddr]) Text.Encoding.UTF8
proc.Start() |> ignore
fvrConnect proc.StandardInput.BaseStream verb
let id = fvrConnect proc.StandardInput.BaseStream verb
if id < 0 then
proc.Kill()
failwithf "Remote daemon closed the connection with error code %d" id
TunneledSession proc

member this.start opts =
Expand Down

0 comments on commit 157737c

Please sign in to comment.