5 # Selecting packet-buffer
7 # (c) 2005 Straylight/Edgeware
10 #----- Licensing notice -----------------------------------------------------
12 # This file is part of the Python interface to mLib.
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.
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.
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.
28 cdef class SelPacketBuffer:
32 def __new__(me, fd, packetproc = None, eofproc = None, *hunoz, **hukairz):
33 selpk_init(&me.p, &_sel, _getfd(fd), _selpkfunc, <void *>me)
35 me._packet = _checkcallable(packetproc, 'packet proc')
36 me._eof = _checkcallable(eofproc, 'eof proc')
41 return _to_bool(me.p.pk.f & PKBUF_ENABLE)
50 raise TypeError, 'size must be positive'
55 def __set__(me, proc):
56 me._line = _checkcallable(proc, 'line proc')
62 def __set__(me, proc):
63 me._eof = _checkcallable(proc, 'eof proc')
67 if me.p.pk.f & PKBUF_ENABLE:
68 raise ValueError, 'already enabled'
73 if not (me.p.pk.f & PKBUF_ENABLE):
74 raise ValueError, 'already disabled'
83 return _maybecall(me._packet, (pk,))
85 return _maybecall(me._eof, ())
87 cdef void _selpkfunc(unsigned char *p, size_t n, pkbuf *pk,
88 size_t *keep, void *arg):
89 cdef SelPacketBuffer pb
92 pb = <SelPacketBuffer>arg
96 r = pb.packet(PyString_FromStringAndSize(<char *>p, n))
98 PyObject_AsReadBuffer(r, &rp, &rn)
100 raise ValueError, 'remaining buffer too large'
102 memcpy(p + n - rn, rp, rn)
105 #----- That's all, folks ----------------------------------------------------