Giter Site home page Giter Site logo

Comments (4)

jangko avatar jangko commented on August 27, 2024

this is not msgpack4nim problem, it's stdlib problem.
why not just read them all into a string?

from msgpack4nim.

demotomohiro avatar demotomohiro commented on August 27, 2024

why not just read them all into a string?

I want to make a program that repeats sending a request message to neovim and receiving a response using msgpack-rpc through pipe.
As neovim process is running after it sent msgpack-rpc response message, there is no EOF after the message from neovim's stdout.
Also there is no '\n' or specific byte sequence that tells me end of response.
That means I cannot use proc readLine(s: Stream): TaintedString or proc readAll(s: Stream): string in Stream module.
They block even if they read whole response message.

According to spec of msgpack and msgpack-rpc, it seems there is no way to get size of a whole message by reading only first fixed size bytes.
Even if I knew that I got the array with 4 elements, it can contain different type of elements.
That means I cannot use proc readData(s: Stream; buffer: pointer; bufLen: int): int or proc readDataStr(s: Stream; buffer: var string; slice: Slice[int]): int in Stream module.
That means I cannot read 1 message from Stream and put it to string without parsing response message.

That is why I wanted to pass output stream of neovim process to proc stringify[ByteStream](s: ByteStream, zz: ByteStream) in msgpack4nim.nim, but peek procs are unavailable on output stream.

I have created new issue about it on Nim repository.
nim-lang/Nim#14906

from msgpack4nim.

demotomohiro avatar demotomohiro commented on August 27, 2024

I added peekableOutputStream proc with this PR so that I can peek output pipe stream of a process.
nim-lang/Nim#14949

I still don't think I can take message as string from pipe while the process is running.
So I need to use proc stringify[ByteStream](s: ByteStream, zz: ByteStream) instead of proc stringify*(data: string): string.
Could you make it being exported?
I will send PR that add * to that symbol if you want.

Here is working sample code to run neovim and send neovim command from Nim.
This code requires devel Nim and msgpack4nim exports proc stringify[ByteStream](s: ByteStream, zz: ByteStream).

import osproc, streams
import msgpack4nim, macros

macro writeRPCReq[ByteStream](
                              s: ByteStream;
                              methodName: string;
                              id: int;
                              args: varargs[typed]) =
  let
    argsLen = args.len.newLit
  result = quote do:
    `s`.pack_array 4
    `s`.pack_type 0
    `s`.pack_type `id`
    `s`.pack_type `methodName`
    `s`.pack_array `argsLen`
 
  for a in args:
    result.add newCall(bindSym"pack", s, a)

proc main =
  var
    p = startProcess("nvim", args = ["--embed"], options = {poUsePath})
    inStrm = p.inputStream
    outStrm = p.peekableOutputStream
    stdoutStrm = stdout.newFileStream

  var msgId = 0
  for l in stdin.lines:
    if not p.running:
      break
    inStrm.writeRPCReq("nvim_command_output", msgId, l)
    inStrm.flush()
    outStrm.stringify stdoutStrm.Stream
    echo ""
    inc msgId
  p.close()

main()

from msgpack4nim.

demotomohiro avatar demotomohiro commented on August 27, 2024

Thank you!

from msgpack4nim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.