开发者

An elegant API for a C-based remote-control / daemon (IPC / RPC)?

开发者 https://www.devze.com 2023-01-23 00:57 出处:网络
I have a daemon (which must be written in C) which should have a remote like common media players do:

I have a daemon (which must be written in C) which should have a remote like common media players do:

mediaplayer-rc --enqueue /path/to/song.mp3

If mediaplayer-daemon isn't running, it is started by the remote controller. The remote will pass the message.

I took the approach that seemed the most intuitive to me:

  • The client application tries to connect() and starts the daemon if it can't
  • The client uses argp to parse the params into a struct
  • The client sends the struct through a socket
  • The server receives the struct and interprets this

I've got a simple demo implementation using libev and unix sockets on github.

My reasoning is th开发者_StackOverflow中文版at it seemed easier to write a client in C that I can call from other languages with their version of system() than to try to get another language to pack the struct correctly or to get the C library to parse some other format.

But I don't think that this is a particularly elegant solution.

Another possibility, would be to use JSON. The downshot being that using a JSON parser in vanilla C would probably be more complex by far than the args parser. The upshot being that JSON (or YAML) is in the standard libraries of just about every other language.

Any suggestions? Anybody know how songbird, gimp, itunes, and other apps with remotes handle this issue?


I can't recommend a ready-to-go library - Instead I suggest you look at Music Player Daemon. It uses a very simple text protocol to control the entire application remotely. There are several clients (with code) you can look at too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号