Comments (4)
this is not msgpack4nim problem, it's stdlib problem.
why not just read them all into a string?
from msgpack4nim.
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.
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.
Thank you!
from msgpack4nim.
Related Issues (20)
- Error: undeclared field: 'data' HOT 9
- using nim-serialization framework
- implement compile time test
- Support unpacking object variants HOT 2
- msgpack_obj_to_map deserialisation raises on first non-string value that is not present on obj HOT 1
- please tag a release in github HOT 3
- Unable to create helper proc HOT 2
- Package 'msgpack4nim' has an incorrect structure HOT 1
- Defining pack_type and unpack_type for templated type does not call the specialized version HOT 1
- [Question] Reading from a file HOT 2
- Add the implementation to the msgpack website HOT 3
- Can't use `pack` from inside a template in another module without import msgpack4nim HOT 3
- Exception for int64 when a field is DateTime HOT 3
- Warnings when unpacking to an object with range properties HOT 1
- unpack_items_imp uses same address
- Make MsgStream.writeData public, please HOT 2
- Unpack a seq of ref Object
- Performance decreased between `head` vs `v0.3.1` HOT 1
- {.gcsafe.} pragma is broken with 0.4.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgpack4nim.