4 ms·
The huge range of solutions that developers with different backgrounds will come up with is evidenced by the fact that when I read this... The code is pretty s
by userbinator 9y ago
The huge range of solutions that developers with different backgrounds will come up with is evidenced by the fact that when I read this...
The code is pretty straightforward. It opens up a socket to the host, then for each motion update it creates a MotionData value, sets the properties on it, encodes it into JSON and sends it to the script running in Blender. It reads any data the host sends and discards it.
...my first thought was why JSON? I'd be curious to know the reasoning, since if I wanted to do this same task, I'd just send the values directly as binary --- 4-byte floats seem the natural choice here, since that's the representation both sides ultimately want. Also, this protocol is clearly unidirectional, so there's no need to even bother with the other direction.
- verandaguy 9y agoWhy not? It's a straightforward format that's widely adopted with easy-to-use libraries, and the data's tagged (with keys) so you get a bit of extra information when you're debugging. It's a decent choice when you're building either a prototype, or not building something that's supposed to be extremely performance-oriented, in which case, I totally agree, a binary protocol would be more appropriate.
- userbinator 9y agoEvery layer of abstraction introduces additional complexity, and more significantly, possible error cases. Observe how the given code has a path to handle JSON encoding/decoding errors. If you use a plain binary protocol, that's not even a consideration. I know it may be a contrived example, but what happens if you try to send it several MB of JSON, well-formed or otherwise? Examining the code more closely, I notice that it only reads up to 4K at a time, and there is no message framing, despite using stream-oriented TCP. It's going to get very confused if it gets more or less than one exact JSON "message" per socket read. If you modify it so it does do framing, then the question above still holds... what happens if it runs out of memory, etc...? All questions which are completely avoided by a simple "read 4 numbers from the socket and set positions" protocol. Code in which error cases simply cannot occur, has no need to be afraid of even a fuzzer feeding it input. KISS, YAGNI. (The larger point I'm making is that simplicity is often surprisingly easy to apply, yet rather undervalued in these times of preferring more abstraction. Of all the easily-exploited IoT out there, I bet the majority of them would not be so if some simplicity had been applied to their design.)
- jakobegger 9y agoIt would probably also be better to use UDP instead of TCP -> you want to send updates as fast as possible, and if a packet is dropped you don’t want to waste network capacity to resend it — just send the next packet. Then again, TCP is a lot easier to work with, since you get an error on the client side if you made a mistake. With UDP there is no way to know if the packet was received.
- flohofwoe 9y agoIf you have a Python script at the other end JSON is nicer to decode and debug, and for such a low amount of data the few bytes overhead doesn't really matter. Also, once you go with binary encoding, using a fixed-point integer format is almost always the better choice over sending float numbers. Depending on the needed spatial resolution (which isn't much in this example), you can reduce message size by using 8-bit or 16-bit fixed-point integers, or if you need more precision than float in the same space, a 32-bit fixed-point number.