| 1 | ### -*-pyrex-*- |
| 2 | ### |
| 3 | ### Non-blocking connections |
| 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 SelConnect: |
| 27 | cdef conn c |
| 28 | cdef int _activep |
| 29 | cdef readonly object socket |
| 30 | cdef _connected |
| 31 | cdef _error |
| 32 | def __cinit__(me, sk, connectedproc = None, errorproc = None, |
| 33 | *hunoz, **hukairz): |
| 34 | conn_fd(&me.c, &_sel, sk.fileno(), _connfunc, <void *>me) |
| 35 | me._activep = 1 |
| 36 | me.socket = sk |
| 37 | me._connected = _checkcallable(connectedproc, 'connected proc') |
| 38 | me._error = _checkcallable(errorproc, 'error proc') |
| 39 | def __dealloc__(me): |
| 40 | if me._activep: |
| 41 | conn_kill(&me.c) |
| 42 | property activep: |
| 43 | def __get__(me): |
| 44 | return _tobool(me._activep) |
| 45 | property connectedproc: |
| 46 | def __get__(me): |
| 47 | return me._connected |
| 48 | def __set__(me, proc): |
| 49 | me._connected = _checkcallable(proc, 'connected proc') |
| 50 | def __del__(me): |
| 51 | me._connected = None |
| 52 | property errorproc: |
| 53 | def __get__(me): |
| 54 | return me._error |
| 55 | def __set__(me, proc): |
| 56 | me._error = _checkcallable(proc, 'error proc') |
| 57 | def __del__(me): |
| 58 | me._error = None |
| 59 | def kill(me): |
| 60 | if not me._activep: |
| 61 | raise ValueError, 'already dead' |
| 62 | conn_kill(&me.c); |
| 63 | me._dead() |
| 64 | return me |
| 65 | cdef _dead(me): |
| 66 | me._activep = 0 |
| 67 | me.dead() |
| 68 | def dead(me): |
| 69 | pass |
| 70 | def connected(me): |
| 71 | return _maybecall(me._connected, ()) |
| 72 | def error(me, errno, strerror): |
| 73 | return _maybecall(me._error, ()) |
| 74 | |
| 75 | cdef void _connfunc(int fd, void *arg): |
| 76 | cdef SelConnect c |
| 77 | c = <SelConnect>arg |
| 78 | c._dead() |
| 79 | if fd == -1: |
| 80 | c.socket = None |
| 81 | c.error(errno, strerror(errno)) |
| 82 | else: |
| 83 | c.connected() |
| 84 | |
| 85 | ###----- That's all, folks -------------------------------------------------- |