diff options
Diffstat (limited to 'lib/base')
| -rw-r--r-- | lib/base/console.cpp | 44 | ||||
| -rw-r--r-- | lib/base/console.h | 5 | ||||
| -rw-r--r-- | lib/base/ebase.cpp | 42 | ||||
| -rw-r--r-- | lib/base/ebase.h | 8 | ||||
| -rw-r--r-- | lib/base/message.h | 20 |
5 files changed, 66 insertions, 53 deletions
diff --git a/lib/base/console.cpp b/lib/base/console.cpp index 830855fc..a12cb5e2 100644 --- a/lib/base/console.cpp +++ b/lib/base/console.cpp @@ -55,8 +55,10 @@ int bidirpipe(int pfd[], const char *cmd , const char * const argv[], const char return(pid); } +DEFINE_REF(eConsoleAppContainer); + eConsoleAppContainer::eConsoleAppContainer() -:pid(-1), killstate(0), in(0), out(0), err(0) +:pid(-1), killstate(0) { for (int i=0; i < 3; ++i) { @@ -109,12 +111,15 @@ int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[] ::fcntl(fd[1], F_SETFL, O_NONBLOCK); ::fcntl(fd[2], F_SETFL, O_NONBLOCK); - in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup ); - out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false); - err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority ); + in = eSocketNotifier::create(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup ); + out = eSocketNotifier::create(eApp, fd[1], eSocketNotifier::Write, false); + err = eSocketNotifier::create(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority ); CONNECT(in->activated, eConsoleAppContainer::readyRead); CONNECT(out->activated, eConsoleAppContainer::readyWrite); CONNECT(err->activated, eConsoleAppContainer::readyErrRead); + in->m_clients.push_back(this); + out->m_clients.push_back(this); + err->m_clients.push_back(this); return 0; } @@ -143,10 +148,9 @@ void eConsoleAppContainer::kill() outbuf.pop(); delete [] d.data; } - delete in; - delete out; - delete err; - in=out=err=0; + in = 0; + out = 0; + err = 0; for (int i=0; i < 3; ++i) { @@ -209,6 +213,7 @@ void eConsoleAppContainer::closePipes() outbuf.pop(); delete [] d.data; } + in = 0; out = 0; err = 0; pid = -1; } @@ -390,23 +395,23 @@ static PyGetSetDef eConsolePy_getseters[] = { static int eConsolePy_traverse(eConsolePy *self, visitproc visit, void *arg) { - PyObject *obj = self->cont->dataAvail.get(true); + PyObject *obj = self->cont->dataAvail.getSteal(); if (obj) { Py_VISIT(obj); } - obj = self->cont->stdoutAvail.get(true); + obj = self->cont->stdoutAvail.getSteal(); if (obj) { Py_VISIT(obj); } - obj = self->cont->stderrAvail.get(true); + obj = self->cont->stderrAvail.getSteal(); if (obj) { Py_VISIT(obj); } - obj = self->cont->dataSent.get(true); + obj = self->cont->dataSent.getSteal(); if (obj) { Py_VISIT(obj); } - obj = self->cont->appClosed.get(true); + obj = self->cont->appClosed.getSteal(); if (obj) { Py_VISIT(obj); } @@ -416,23 +421,23 @@ eConsolePy_traverse(eConsolePy *self, visitproc visit, void *arg) static int eConsolePy_clear(eConsolePy *self) { - PyObject *obj = self->cont->dataAvail.get(true); + PyObject *obj = self->cont->dataAvail.getSteal(true); if (obj) { Py_CLEAR(obj); } - obj = self->cont->stdoutAvail.get(true); + obj = self->cont->stdoutAvail.getSteal(true); if (obj) { Py_CLEAR(obj); } - obj = self->cont->stderrAvail.get(true); + obj = self->cont->stderrAvail.getSteal(true); if (obj) { Py_CLEAR(obj); } - obj = self->cont->dataSent.get(true); + obj = self->cont->dataSent.getSteal(true); if (obj) { Py_CLEAR(obj); } - obj = self->cont->appClosed.get(true); + obj = self->cont->appClosed.getSteal(true); if (obj) { Py_CLEAR(obj); } @@ -445,7 +450,7 @@ eConsolePy_dealloc(eConsolePy* self) if (self->in_weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) self); eConsolePy_clear(self); - delete self->cont; + self->cont->Release(); self->ob_type->tp_free((PyObject*)self); } @@ -454,6 +459,7 @@ eConsolePy_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { eConsolePy *self = (eConsolePy *)type->tp_alloc(type, 0); self->cont = new eConsoleAppContainer(); + self->cont->AddRef(); self->in_weakreflist = NULL; return (PyObject *)self; } diff --git a/lib/base/console.h b/lib/base/console.h index a5712bb7..e730b40e 100644 --- a/lib/base/console.h +++ b/lib/base/console.h @@ -18,15 +18,16 @@ struct queue_data int dataSent; }; -class eConsoleAppContainer: public Object +class eConsoleAppContainer: public Object, public iObject { + DECLARE_REF(eConsoleAppContainer); int fd[3]; int filefd[3]; int pid; int killstate; std::string m_cwd; std::queue<struct queue_data> outbuf; - eSocketNotifier *in, *out, *err; + ePtr<eSocketNotifier> in, out, err; void readyRead(int what); void readyErrRead(int what); void readyWrite(int what); diff --git a/lib/base/ebase.cpp b/lib/base/ebase.cpp index e7708633..a66d1958 100644 --- a/lib/base/ebase.cpp +++ b/lib/base/ebase.cpp @@ -8,9 +8,11 @@ #include <lib/base/elock.h> #include <lib/gdi/grc.h> +DEFINE_REF(eSocketNotifier); + eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested) { - if (startnow) + if (startnow) start(); } @@ -31,9 +33,10 @@ void eSocketNotifier::start() void eSocketNotifier::stop() { if (state) + { + state=0; context.removeSocketNotifier(this); - - state=0; + } } // timer @@ -131,8 +134,13 @@ void eMainloop::removeSocketNotifier(eSocketNotifier *sn) int fd = sn->getFD(); std::map<int,eSocketNotifier*>::iterator i(notifiers.find(fd)); if (i != notifiers.end()) - return notifiers.erase(i); - eFatal("removed socket notifier which is not present"); + { + notifiers.erase(i); + return; + } + for (i = notifiers.begin(); i != notifiers.end(); ++i) + eDebug("fd=%d, sn=%d", i->second->getFD(), (void*)i->second); + eFatal("removed socket notifier which is not present, fd=%d", fd); } int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePyObject additional) @@ -175,6 +183,7 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy // build the poll aray pollfd pfd[fdcount]; // make new pollfd array std::map<int,eSocketNotifier*>::iterator it = notifiers.begin(); + int i=0; for (; i < nativecount; ++i, ++it) { @@ -228,9 +237,13 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy if (it != notifiers.end() && it->second->state == 1) // added and in poll { - int req = it->second->getRequested(); - if (pfd[i].revents & req) - it->second->activate(pfd[i].revents & req); + eSocketNotifier *sn = it->second; + int req = sn->getRequested(); + if (pfd[i].revents & req) { + sn->AddRef(); + sn->activate(pfd[i].revents & req); + sn->Release(); + } pfd[i].revents &= ~req; } if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL)) @@ -368,7 +381,7 @@ struct eTimerPy static int eTimerPy_traverse(eTimerPy *self, visitproc visit, void *arg) { - PyObject *obj = self->tm->timeout.get(true); + PyObject *obj = self->tm->timeout.getSteal(); if (obj) { Py_VISIT(obj); } @@ -378,7 +391,7 @@ eTimerPy_traverse(eTimerPy *self, visitproc visit, void *arg) static int eTimerPy_clear(eTimerPy *self) { - PyObject *obj = self->tm->timeout.get(true); + PyObject *obj = self->tm->timeout.getSteal(true); if (obj) Py_CLEAR(obj); return 0; @@ -575,7 +588,7 @@ struct eSocketNotifierPy static int eSocketNotifierPy_traverse(eSocketNotifierPy *self, visitproc visit, void *arg) { - PyObject *obj = self->sn->activated.get(true); + PyObject *obj = self->sn->activated.getSteal(); if (obj) Py_VISIT(obj); return 0; @@ -584,7 +597,7 @@ eSocketNotifierPy_traverse(eSocketNotifierPy *self, visitproc visit, void *arg) static int eSocketNotifierPy_clear(eSocketNotifierPy *self) { - PyObject *obj = self->sn->activated.get(true); + PyObject *obj = self->sn->activated.getSteal(true); if (obj) Py_CLEAR(obj); return 0; @@ -596,7 +609,7 @@ eSocketNotifierPy_dealloc(eSocketNotifierPy* self) if (self->in_weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) self); eSocketNotifierPy_clear(self); - delete self->sn; + self->sn->Release(); self->ob_type->tp_free((PyObject*)self); } @@ -620,7 +633,8 @@ eSocketNotifierPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } else if (size < 2 || !PyArg_ParseTuple(args, "ii", &fd, &req)) return NULL; - self->sn = new eSocketNotifier(eApp, fd, req, immediate_start); + self->sn = eSocketNotifier::create(eApp, fd, req, immediate_start); + self->sn->AddRef(); self->in_weakreflist = NULL; return (PyObject *)self; } diff --git a/lib/base/ebase.h b/lib/base/ebase.h index 10b46120..cb676d0b 100644 --- a/lib/base/ebase.h +++ b/lib/base/ebase.h @@ -142,8 +142,9 @@ class eMainloop; * This class emits the signal \c eSocketNotifier::activate whenever the * event specified by \c req is available. */ -class eSocketNotifier +class eSocketNotifier: iObject { + DECLARE_REF(eSocketNotifier); friend class eMainloop; public: enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP }; @@ -153,6 +154,7 @@ private: int state; int requested; // requested events (POLLIN, ...) void activate(int what) { /*emit*/ activated(what); } + eSocketNotifier(eMainloop *context, int fd, int req, bool startnow); public: /** * \brief Constructs a eSocketNotifier. @@ -161,7 +163,7 @@ public: * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports. * \param startnow Specifies if the socketnotifier should start immediately. */ - eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true); + static eSocketNotifier* create(eMainloop *context, int fd, int req, bool startnow=true) { return new eSocketNotifier(context, fd, req, startnow); } ~eSocketNotifier(); PSignal1<void, int> activated; @@ -173,6 +175,8 @@ public: int getFD() { return fd; } int getRequested() { return requested; } void setRequested(int req) { requested=req; } + + eSmartPtrList<iObject> m_clients; }; #endif diff --git a/lib/base/message.h b/lib/base/message.h index 038fd55d..6e9eb07c 100644 --- a/lib/base/message.h +++ b/lib/base/message.h @@ -39,7 +39,7 @@ protected: template<class T> class eFixedMessagePump: private eMessagePump, public Object { - eSocketNotifier *sn; + ePtr<eSocketNotifier> sn; void do_recv(int) { T msg; @@ -54,15 +54,10 @@ public: } eFixedMessagePump(eMainloop *context, int mt): eMessagePump(mt) { - sn=new eSocketNotifier(context, getOutputFD(), eSocketNotifier::Read); + sn=eSocketNotifier::create(context, getOutputFD(), eSocketNotifier::Read); CONNECT(sn->activated, eFixedMessagePump<T>::do_recv); sn->start(); } - ~eFixedMessagePump() - { - delete sn; - sn=0; - } void start() { if (sn) sn->start(); } void stop() { if (sn) sn->stop(); } }; @@ -70,7 +65,7 @@ public: class ePythonMessagePump: public eMessagePump, public Object { - eSocketNotifier *sn; + ePtr<eSocketNotifier> sn; void do_recv(int) { int msg; @@ -86,17 +81,10 @@ public: ePythonMessagePump() :eMessagePump(1) { - eDebug("add python messagepump %p", this); - sn=new eSocketNotifier(eApp, getOutputFD(), eSocketNotifier::Read); + sn=eSocketNotifier::create(eApp, getOutputFD(), eSocketNotifier::Read); CONNECT(sn->activated, ePythonMessagePump::do_recv); sn->start(); } - ~ePythonMessagePump() - { - eDebug("remove python messagepump %p", this); - delete sn; - sn=0; - } void start() { if (sn) sn->start(); } void stop() { if (sn) sn->stop(); } }; |
