Added socketTimeout option to ensure that OS-level timeouts are handled by node-imap

Hi,

> Eventually the OS will detect the dead connection, but typically after a much longer period of time (although most non-Windows platforms allow you to tweak these kinds of settings). (Brian, #423)

It won't. I waited 10 hours, and they still weren't.

> If you have any good suggestions for solving the problem, I'm all ears. (Brian, #423)

Here is my pull request to fix the issue.
Basically, I added a "socketTimeout" option in the configuration. Note that sockets can be created in two ways: in the constructor, or in _starttls. I cover both cases.

This ensures that OS-level timeouts can be set. This basically means that if the socket remains silent for X amount of time, the exception will be raised.
Note that I wasn't sure how much cleaning up you want to do in this kind of situation. At the moment, I clean up "the lot":

    this._onSocketTimeout = function(){
      clearTimeout(self._tmrConn);
      clearTimeout(self._tmrAuth);
      clearTimeout(self._tmrKeepalive);
      self.state = 'disconnected';
      self.debug && self.debug('[connection] Socket timeout');

      var err = new Error('Socket timed out while talking to server');
      err.source = 'timeout';
      self.emit('error', err);
      socket.destroy();
    }

Not sure if I went overboard. I am also not sure if you want to set self.state in case of a timeout error -- I think it makes sense.

Solves #423, which I closed.

Hi,

Will apply fixes as soon as you tell me that you don't have any more notes!

Merc.
On 03/10/2014 8:45 PM, "Brian White" <notifications@github.com> wrote:

> In lib/Connection.js:
>
> > @@ -150,6 +150,21 @@ Connection.prototype.connect = function() {
> >    };
> >    this._sock.on('error', this._onError);
> >
> > +  this._onSocketTimeout = function(){
> > +    clearTimeout(self._tmrConn);
> > +    clearTimeout(self._tmrAuth);
> > +    clearTimeout(self._tmrKeepalive);
> > +    self.state = 'disconnected';
> > +    self.debug && self.debug('[connection] Socket timeout');
> > +
> > +    var err = new Error('Socket timed out while talking to server');
> > +    err.source = 'timeout';
>
> maybe make this a bit more specific, like 'socket-timeout'.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/mscdex/node-imap/pull/424/files#r18392667>.
>