How to encode bidirectional duplex streams in NodeJS

In recent versions of NodeJS (v0.10.X since writing), the Streams API has gone through a nice redesign, and I would like to start using it now.

I want to wrap both input and output of a socket with an object that implements the protocol.

The so-called Duplex interface seems to be just any stream that can be read and written (like a socket).

It is unclear whether the duplex should look like A or B, or it does not matter.

+---+ +---+ -->| A |--> | |--> +---+ | B | | |<-- +---+ 

What is the correct code / interface structure for an object that has two records and two reads?

 +--------+ +----------+ +---- | r|-->|wr|-->|w | socket | | protocol | | rest of app | w|<--|rw|<--|r +--------+ +----------+ +---- 

The problem with the above diagram is that the protocol object requires two separate read methods and two write methods.

At the top of my head, I can make the protocol produce β€œleft” and β€œright” duplex objects, or β€œin” and β€œexit” from duplex objects (to crop them differently).

Are they one of the preferred methods or is there a better solution?

+6
source share
2 answers
  | app | +---------------+ ^ | | V +-----+ +-----+ | | | | +----------| |-| |-+ | protocol | .up | |.down| | +----------| |-| |-+ | | | | +-----+ +-----+ ^ | | V +---------------+ | socket | 

My solution was to create a protocol class that created Up Transform and Down Transform .

The protocol constructor passes the link (to itself) when constructing the Up and Down transformations. The _transform method in each of the up and down conversions can then call push on its own, and on the other Transform or both as needed. The general state may be stored in a Protocol object.

+4
source

Duplex flow is similar to your B diagram, at least for the user. A more complete representation of the flow would be the inclusion of a producer (source) with a consumer (user). See previous answer. Try not to think about how to read / write from a consumer perspective.

What you are doing is creating a thin layer above the socket for the protocol, so your design is correct:

  -------+ +----------+ +------ r|---->| r|---->| socket | | protocol | | rest of app w|<----| w|<----| -------+ +----------+ +------ 

You can use duplex or conversion for part of the protocol.

  +---------+--------+---------+ +------------------+ | _write->| | |r | Transform -> |r |-----------Duplex-----------| +------------------+ | | | <-_read |w | <- Transform |w +---------+--------+---------+ +------------------+ 

being your protocol-related processing of incoming / outgoing data using internal _read, _write. Or you can transform threads. You will pass the protocol to the socket and the socket to the protocol.

+1
source

Source: https://habr.com/ru/post/956078/


All Articles