| 1 | ### -*-pyrex-*- |
| 2 | ### |
| 3 | ### Packet buffering |
| 4 | ### |
| 5 | ### (c) 2005 Straylight/Edgeware |
| 6 | ### |
| 7 | |
| 8 | ###----- Licensing notice --------------------------------------------------- |
| 9 | ### |
| 10 | ### This file is part of the Python interface to mLib. |
| 11 | ### |
| 12 | ### mLib/Python is free software; you can redistribute it and/or modify |
| 13 | ### it under the terms of the GNU General Public License as published by |
| 14 | ### the Free Software Foundation; either version 2 of the License, or |
| 15 | ### (at your option) any later version. |
| 16 | ### |
| 17 | ### mLib/Python is distributed in the hope that it will be useful, |
| 18 | ### but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ### GNU General Public License for more details. |
| 21 | ### |
| 22 | ### You should have received a copy of the GNU General Public License |
| 23 | ### along with mLib/Python; if not, write to the Free Software Foundation, |
| 24 | ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | |
| 26 | cdef class PacketBuffer: |
| 27 | """ |
| 28 | PacketBuffer([packetproc = None], [eofproc = None]) |
| 29 | |
| 30 | Split an incoming stream into packets. |
| 31 | """ |
| 32 | cdef pkbuf pk |
| 33 | cdef _packet |
| 34 | cdef _eof |
| 35 | def __cinit__(me, packetproc = None, eofproc = None, *hunoz, **hukairz): |
| 36 | pkbuf_init(&me.pk, _pkfunc, <void *>me) |
| 37 | me._packet = _checkcallable(packetproc, 'packet proc') |
| 38 | me._eof = _checkcallable(eofproc, 'eof proc') |
| 39 | def __dealloc__(me): |
| 40 | pkbuf_destroy(&me.pk) |
| 41 | property activep: |
| 42 | """PK.activep -> BOOL: is the buffer still active?""" |
| 43 | def __get__(me): |
| 44 | return _tobool(me.pk.f & PKBUF_ENABLE) |
| 45 | property want: |
| 46 | """PK.want -> INT: size of next packet to return""" |
| 47 | def __get__(me): |
| 48 | return me.pk.want |
| 49 | def __set__(me, want): |
| 50 | if want <= 0: |
| 51 | raise TypeError, 'want must be positive' |
| 52 | pkbuf_want(&me.pk, pk) |
| 53 | property packetproc: |
| 54 | """PK.packetproc -> FUNC: call FUNC(PACKET) on each packet""" |
| 55 | def __get__(me): |
| 56 | return me._packet |
| 57 | def __set__(me, proc): |
| 58 | me._line = _checkcallable(proc, 'packet proc') |
| 59 | def __del__(me): |
| 60 | me._line = None |
| 61 | property eofproc: |
| 62 | """PK.eofproc -> FUNC: call FUNC() at end-of-file""" |
| 63 | def __get__(me): |
| 64 | return me._eof |
| 65 | def __set__(me, proc): |
| 66 | me._eof = _checkcallable(proc, 'eof proc') |
| 67 | def __del__(me): |
| 68 | me._eof = None |
| 69 | def enable(me): |
| 70 | """PK.enable(): enable the buffer, allowing packets to be emitted""" |
| 71 | if me.pk.f & PKBUF_ENABLE: |
| 72 | raise ValueError, 'already enabled' |
| 73 | me.pk.f = me.pk.f | PKBUF_ENABLE |
| 74 | me.enabled() |
| 75 | return me |
| 76 | def disable(me): |
| 77 | """PK.disable(): disable the buffer, suspending packet emission""" |
| 78 | if not (me.pk.f & PKBUF_ENABLE): |
| 79 | raise ValueError, 'already disabled' |
| 80 | me.pk.f = me.pk.f & ~PKBUF_ENABLE |
| 81 | me.disabled() |
| 82 | return me |
| 83 | def close(me): |
| 84 | """PK.close(): report the end of the input stream""" |
| 85 | if not (me.pk.f & PKBUF_ENABLE): |
| 86 | raise ValueError, 'buffer disabled' |
| 87 | pkbuf_close(&me.pk) |
| 88 | return me |
| 89 | property free: |
| 90 | """PK.free -> INT: amount of space remaining in buffer""" |
| 91 | def __get__(me): |
| 92 | cdef unsigned char *p |
| 93 | return pkbuf_free(&me.pk, &p) |
| 94 | def flush(me, str): |
| 95 | """PK.flush(STR) -> insert STR into the buffer and emit packets""" |
| 96 | cdef Py_ssize_t len |
| 97 | cdef unsigned char *p |
| 98 | cdef unsigned char *q |
| 99 | cdef size_t n |
| 100 | PyObject_AsReadBuffer(str, <cvp *>&p, &len) |
| 101 | while len > 0: |
| 102 | n = pkbuf_free(&me.pk, &q) |
| 103 | if n > len: |
| 104 | n = len |
| 105 | memcpy(q, p, n) |
| 106 | p = p + n |
| 107 | len = len - n |
| 108 | if not (me.pk.f & PKBUF_ENABLE): |
| 109 | break |
| 110 | pkbuf_flush(&me.pk, q, n) |
| 111 | return PyString_FromStringAndSize(<char *>p, len) |
| 112 | def enabled(me): |
| 113 | """PK.enabled(): called when buffer is enabled""" |
| 114 | pass |
| 115 | def disabled(me): |
| 116 | """PK.disabled(): called when buffer is disabled""" |
| 117 | pass |
| 118 | def packet(me, pk): |
| 119 | """PK.packet(PACKET): called for each completed packet""" |
| 120 | return _maybecall(me._packet, (pk,)) |
| 121 | def eof(me): |
| 122 | """PK.eof(): called at end-of-file""" |
| 123 | return _maybecall(me._eof, ()) |
| 124 | |
| 125 | cdef void _pkfunc(unsigned char *p, size_t n, pkbuf *pk, |
| 126 | size_t *keep, void *arg): |
| 127 | cdef PacketBuffer pb |
| 128 | cdef void *rp |
| 129 | cdef Py_ssize_t rn |
| 130 | pb = <PacketBuffer>arg |
| 131 | if p is NULL: |
| 132 | pb.eof() |
| 133 | else: |
| 134 | r = pb.packet(PyString_FromStringAndSize(<char *>p, n)) |
| 135 | if r is not None: |
| 136 | PyObject_AsReadBuffer(r, <cvp *>&rp, &rn) |
| 137 | if rn > n: |
| 138 | raise ValueError, 'remaining buffer too large' |
| 139 | if rn: |
| 140 | memcpy(p + n - rn, rp, rn) |
| 141 | keep[0] = rn |
| 142 | |
| 143 | ###----- That's all, folks -------------------------------------------------- |