chiark / gitweb /
Release 1.1.1.
[mLib-python] / pkbuf.pyx
CommitLineData
5b1830f3
MW
1### -*-pyrex-*-
2###
3### Packet buffering
4###
5### (c) 2005 Straylight/Edgeware
6###
579d0169 7
5b1830f3
MW
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.
579d0169 25
26cdef class PacketBuffer:
addc0c37
MW
27 """
28 PacketBuffer([packetproc = None], [eofproc = None])
29
30 Split an incoming stream into packets.
31 """
579d0169 32 cdef pkbuf pk
33 cdef _packet
34 cdef _eof
376ad06d 35 def __cinit__(me, packetproc = None, eofproc = None, *hunoz, **hukairz):
579d0169 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:
addc0c37 42 """PK.activep -> BOOL: is the buffer still active?"""
579d0169 43 def __get__(me):
44 return _tobool(me.pk.f & PKBUF_ENABLE)
45 property want:
addc0c37 46 """PK.want -> INT: size of next packet to return"""
579d0169 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:
addc0c37 54 """PK.packetproc -> FUNC: call FUNC(PACKET) on each packet"""
579d0169 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:
addc0c37 62 """PK.eofproc -> FUNC: call FUNC() at end-of-file"""
579d0169 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):
addc0c37 70 """PK.enable(): enable the buffer, allowing packets to be emitted"""
579d0169 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):
addc0c37 77 """PK.disable(): disable the buffer, suspending packet emission"""
579d0169 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):
addc0c37 84 """PK.close(): report the end of the input stream"""
579d0169 85 if not (me.pk.f & PKBUF_ENABLE):
86 raise ValueError, 'buffer disabled'
87 pkbuf_close(&me.pk)
88 return me
89 property free:
addc0c37 90 """PK.free -> INT: amount of space remaining in buffer"""
579d0169 91 def __get__(me):
92 cdef unsigned char *p
93 return pkbuf_free(&me.pk, &p)
94 def flush(me, str):
addc0c37 95 """PK.flush(STR) -> insert STR into the buffer and emit packets"""
78911cdb 96 cdef Py_ssize_t len
579d0169 97 cdef unsigned char *p
98 cdef unsigned char *q
99 cdef size_t n
704500e1 100 PyObject_AsReadBuffer(str, <cvp *>&p, &len)
579d0169 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):
addc0c37 113 """PK.enabled(): called when buffer is enabled"""
579d0169 114 pass
115 def disabled(me):
addc0c37 116 """PK.disabled(): called when buffer is disabled"""
579d0169 117 pass
118 def packet(me, pk):
addc0c37 119 """PK.packet(PACKET): called for each completed packet"""
579d0169 120 return _maybecall(me._packet, (pk,))
121 def eof(me):
addc0c37 122 """PK.eof(): called at end-of-file"""
579d0169 123 return _maybecall(me._eof, ())
124
125cdef void _pkfunc(unsigned char *p, size_t n, pkbuf *pk,
126 size_t *keep, void *arg):
127 cdef PacketBuffer pb
128 cdef void *rp
78911cdb 129 cdef Py_ssize_t rn
579d0169 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:
704500e1 136 PyObject_AsReadBuffer(r, <cvp *>&rp, &rn)
579d0169 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
5b1830f3 143###----- That's all, folks --------------------------------------------------