| 1 | # -*-pyrex-*- |
| 2 | # |
| 3 | # $Id$ |
| 4 | # |
| 5 | # Selecting packet-buffer |
| 6 | # |
| 7 | # (c) 2005 Straylight/Edgeware |
| 8 | # |
| 9 | |
| 10 | #----- Licensing notice ----------------------------------------------------- |
| 11 | # |
| 12 | # This file is part of the Python interface to mLib. |
| 13 | # |
| 14 | # mLib/Python is free software; you can redistribute it and/or modify |
| 15 | # it under the terms of the GNU General Public License as published by |
| 16 | # the Free Software Foundation; either version 2 of the License, or |
| 17 | # (at your option) any later version. |
| 18 | # |
| 19 | # mLib/Python is distributed in the hope that it will be useful, |
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | # GNU General Public License for more details. |
| 23 | # |
| 24 | # You should have received a copy of the GNU General Public License |
| 25 | # along with mLib/Python; if not, write to the Free Software Foundation, |
| 26 | # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | |
| 28 | cdef class SelPacketBuffer: |
| 29 | cdef selpk p |
| 30 | cdef _packet |
| 31 | cdef _eof |
| 32 | def __new__(me, fd, packetproc = None, eofproc = None, *hunoz, **hukairz): |
| 33 | selpk_init(&me.p, &_sel, _getfd(fd), _selpkfunc, <void *>me) |
| 34 | selpk_disable(&me.p) |
| 35 | me._packet = _checkcallable(packetproc, 'packet proc') |
| 36 | me._eof = _checkcallable(eofproc, 'eof proc') |
| 37 | def __dealloc__(me): |
| 38 | selpk_destroy(&me.p) |
| 39 | property activep: |
| 40 | def __get__(me): |
| 41 | return _to_bool(me.p.pk.f & PKBUF_ENABLE) |
| 42 | property fd: |
| 43 | def __get__(me): |
| 44 | return me.p.reader.fd |
| 45 | property want: |
| 46 | def __get__(me): |
| 47 | return me.p.pk.want |
| 48 | def __set__(me, n): |
| 49 | if n <= 0: |
| 50 | raise TypeError, 'size must be positive' |
| 51 | selpk_want(&me.p, n) |
| 52 | property lineproc: |
| 53 | def __get__(me): |
| 54 | return me._line |
| 55 | def __set__(me, proc): |
| 56 | me._line = _checkcallable(proc, 'line proc') |
| 57 | def __del__(me): |
| 58 | me._line = None |
| 59 | property eofproc: |
| 60 | def __get__(me): |
| 61 | return me._eof |
| 62 | def __set__(me, proc): |
| 63 | me._eof = _checkcallable(proc, 'eof proc') |
| 64 | def __del__(me): |
| 65 | me._eof = None |
| 66 | def enable(me): |
| 67 | if me.p.pk.f & PKBUF_ENABLE: |
| 68 | raise ValueError, 'already enabled' |
| 69 | selpk_enable(&me.p) |
| 70 | me.enabled() |
| 71 | return me |
| 72 | def disable(me): |
| 73 | if not (me.p.pk.f & PKBUF_ENABLE): |
| 74 | raise ValueError, 'already disabled' |
| 75 | selpk_disable(&me.p) |
| 76 | me.disabled() |
| 77 | return me |
| 78 | def enabled(me): |
| 79 | pass |
| 80 | def disabled(me): |
| 81 | pass |
| 82 | def packet(me, pk): |
| 83 | return _maybecall(me._packet, (pk,)) |
| 84 | def eof(me): |
| 85 | return _maybecall(me._eof, ()) |
| 86 | |
| 87 | cdef void _selpkfunc2(unsigned char *p, size_t n, pkbuf *pk, |
| 88 | size_t *keep, void *arg): |
| 89 | cdef SelPacketBuffer pb |
| 90 | cdef void *rp |
| 91 | cdef int rn |
| 92 | pb = <SelPacketBuffer>arg |
| 93 | if p is NULL: |
| 94 | pb.eof() |
| 95 | else: |
| 96 | r = pb.packet(PyString_FromStringAndSize(<char *>p, n)) |
| 97 | if r is not None: |
| 98 | PyObject_AsReadBuffer(r, &rp, &rn) |
| 99 | if rn > n: |
| 100 | raise ValueError, 'remaining buffer too large' |
| 101 | if rn: |
| 102 | memcpy(p + n - rn, rp, rn) |
| 103 | keep[0] = rn |
| 104 | cdef void _selpkfunc(unsigned char *p, size_t n, pkbuf *pk, |
| 105 | size_t *keep, void *arg): |
| 106 | PyEval_AcquireLock() |
| 107 | _selpkfunc2(p, n, pk, keep, arg) |
| 108 | PyEval_ReleaseLock() |
| 109 | |
| 110 | #----- That's all, folks ---------------------------------------------------- |