Hi!

I am trying to build a protocol on top of streams. Currently, I am doing
something like this using a duplex stream:

#+begin_src js
function MyProtocol(options) {
  stream.Duplex.call(this, { objectMode: true,
                             allowHalfOpen: false });
  var tlsStream = tls.connect(options);
  this.inRStream = new stream.PassThrough({ objectMode: true });
  this.inRStream
    .pipe(encode1)
    .on('error', propagateError)
    .pipe(encode2)
    .on('error', propagateError)
    .pipe(encode3)
    .on('error', propagateError);
  this.outWStream = tlsStream
    .on('error', propagateError)
    .pipe(decode3)
    .on('error', propagateError)
    .pipe(decode2)
    .on('error', propagateError)
    .pipe(decode1);
}
util.inherits(MyProtocol, stream.Duplex);

MyProtocol.prototype._write = function(chunk, enc, done) {
  this.inRStream.write(chunk, enc, done);
};

MyProtocol.prototype._read = function(size) {
  var self = this;

  function readable() {
    var chunk;
    while (null !== (chunk = self.outWStream.read(size))) {
      if (!self.push(chunk)) break;
    }
    self.outWStream.removeListener('end', end);
  }

  function end() {
    self.push(null);
    self.outWStream.removeListener('readable', readable);
  }

  self.outWStream
    .once('readable', readable)
    .once('end', end);
};
#+end_src

The decode/encode stuff are transformers. They all are instanciated with
`allowHalfOpen` set to false.

This works fine. Errors are propragated as expected.

However, I don't understand how to handle some edge cases like a stuck
connection or the inability to establish a connection for the server. In
those cases, I would like to be able to toss away the whole stream. I
could call .end() on it but there would be pending events that would
stay forever. Maybe I would get additionnal error events in the process.

How those exceptional conditions on stream should be handled?
-- 
Make sure all variables are initialised before use.
            - The Elements of Programming Style (Kernighan & Plauger)

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to