Packets: extended description of bad_cast exception in Packet.as()
[senf.git] / senf / Scheduler / ReadHelper.ct
1 // $Id$
2 //
3 // Copyright (C) 2006 Stefan Bund <g0dil@berlios.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the
17 // Free Software Foundation, Inc.,
18 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 // Copyright (C) 2006
20
21 /** \file
22     \brief ReadHelper non-inline template implementation */
23
24 #include "ReadHelper.ih"
25
26 // Custom includes
27 #include <errno.h>
28 #include <boost/bind.hpp>
29 #include <senf/Utils/Exception.hh>
30 #include "Scheduler.hh"
31
32 #define prefix_
33 //-/////////////////////////////////////////////////////////////////////////////////////////////////
34
35 template <class Handle>
36 prefix_ senf::ReadHelper<Handle>::ReadHelper(Handle handle, std::string::size_type maxSize,
37                                                     InternalPredicate * predicate, Callback cb)
38     : handle_(handle),
39       fde_("senf::ReadHelper", boost::bind(&ReadHelper::dispatchProcess,ptr(this), handle, _1),
40            handle, senf::scheduler::FdEvent::EV_READ),
41       maxSize_(maxSize), predicate_(predicate), callback_(cb), errno_(0), complete_(false)
42 {
43     // Here we add a *static* member taking a *smart* pointer as first
44     // argument instead of a simple bound-member as callback to the
45     // scheduler. This ensures, that the refcount is at least 1 as
46     // long as the helper is registered with the scheduler.
47 }
48
49 template <class Handle>
50 prefix_ void senf::ReadHelper<Handle>::revoke()
51 {
52     ptr guard (this); // To ensure, 'this' is deleted only after this method terminates ...
53     fde_.disable();
54     fde_.action(0); // Remove smart pointer reference to this
55 }
56
57 template <class Handle>
58 prefix_ void
59 senf::ReadHelper<Handle>::dispatchProcess(ptr helper, Handle handle, int event)
60 {
61     // since we have a 'ptr' argument, the instance cannot be deleted
62     // before this method returns
63     helper->process(handle,event);
64 }
65
66 template <class Handle>
67 prefix_ void senf::ReadHelper<Handle>::process(Handle handle,int event)
68 {
69     try {
70         if (event != senf::scheduler::FdEvent::EV_READ)
71             throw SystemException(EPIPE SENF_EXC_DEBUGINFO);
72         std::string rcv;
73         handle.read(rcv, maxSize_ - data_.size());
74         data_.append(rcv);
75         std::string::size_type n = predicate_ ? (*predicate_)(data_) : std::string::npos;
76         if (n != std::string::npos || data_.size() >= maxSize_ || rcv.size() == 0) {
77             complete_ = true;
78             if (n < data_.size()) {
79                 tail_.assign(data_,n,std::string::npos);
80                 data_.erase(n);
81             }
82         }
83     }
84     catch (senf::SystemException const & ex) {
85         errno_ = ex.errorNumber();
86         done();
87         return;
88     }
89     if (complete_)
90         done();
91 }
92
93 template <class Handle>
94 prefix_ void senf::ReadHelper<Handle>::done()
95 {
96     revoke();
97     callback_(ptr(this));
98 }
99
100 template <class Handle>
101 template <class Predicate>
102 prefix_ std::string::size_type
103 senf::ReadHelper<Handle>::InternalPredicate::Dispatcher<Predicate>::
104 operator()(std::string const & data)
105 {
106     return predicate(data);
107 }
108
109 //-/////////////////////////////////////////////////////////////////////////////////////////////////
110 #undef prefix_
111
112 \f
113 // Local Variables:
114 // mode: c++
115 // fill-column: 100
116 // c-file-style: "senf"
117 // indent-tabs-mode: nil
118 // ispell-local-dictionary: "american"
119 // compile-command: "scons -u test"
120 // comment-column: 40
121 // End: