Protocol wrapping?

Marc Lehmann schmorp at schmorp.de
Mon Jan 16 22:08:14 CET 2012


On Fri, Jan 13, 2012 at 01:48:25PM +0100, Anton Berezin <tobez at tobez.org> wrote:
> To me this sounds like a job for a (rather thin) wrapper around
> AnyEvent::Handle.

If I understand you correctly, what you would ideally want is some kind
of filter, or, better yet, the ability to add filter layers between the
AE::Handle api and the low-level socket.

That is indeed something that I am pondering about for quite a while, and
something that AE::Handle wasn't designed for, and something I didn't want
to implement for speed reasons. One way that I might implement it is by
simply conencting one handle to a socketpair and use another handle on it
(probably just what you are imagining as a solution). This would be rather
slow, but the overhead would only occur when it's used, which is rare (at
the moment). I do think that solution would be quite clean though, even more
so than having a layer mechanism inside AE::Handle itself.

> This seems to work reasonably well (the code is below), but
> I am wondering whether this is a sane way of doing such things?

It's uncommon for sure, but I think it's very clean. The reason you
don't see this thing often is that going through the kernel is quite the
overhead, but clean... clean it is.

> I am pretty new to AnyEvent, so I would not be surprized if I
> missed a better, more idiomatic/less ugly way of achieving this.

Not really, if you must do it transparently.

Another way to do it without the overhead, but less flexible, is to implement
user-defined read/write types, e.g.:

   sub Blabla::Telnet::anyevent_read_type {
      my ($hdl, $cb, @ergs) = @_;
      ...
   }

   $handle->push_read (Blabla::Telnet => ...

And do the necessary stripping inside these. That is less flexible, but
probably easier to use.

Yet another way would be to subclass AnyEvent::Handle and provide your own
push_read/push_write methods.

So.... nothing really wrong with your approach, if it works for you.

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\



More information about the anyevent mailing list