~ubuntu-branches/ubuntu/hardy/pymsn/hardy-proposed

« back to all changes in this revision

Viewing changes to pymsn/gnet/io/ssl_socket.py

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville, Sjoerd Simons, Laurent Bigonville, Jonny Lamb
  • Date: 2008-01-17 18:23:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080117182314-lwymmpnk2ut3rvr1
Tags: 0.3.1-0ubuntu1
[ Sjoerd Simons ]
* debian/rules: remove dh_python, it's no longer needed

[ Laurent Bigonville ]
* New upstream release (0.3.1)
* debian/control:
  - Add myself as an Uploaders
  - Add python:Provides for binary package
  - Add python-ctypes and python-crypto to build-deps/deps
* debian/rules: remove binary-install rule
* Add watch file
* remove pycompat file, not needed anymore
* Modify Maintainer value to match the DebianMaintainerField
  specification.

[ Jonny Lamb ]
* Added python-adns to build-deps/deps.
* Added python-pyopenssl to build-deps/deps.
* Updated copyright.
* Upped Standards-Version to 3.7.3.
* Added "XS-Dm-Upload-Allowed: yes" under the request of Sjoerd Simons.
* Added myself to Uploaders.
* Added Homepage to control.
* Added Vcs-Bzr to control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright (C) 2005  Ole André Vadla Ravnås <oleavr@gmail.com>
 
4
# Copyright (C) 2006-2007  Ali Sabil <ali.sabil@gmail.com>
 
5
# Copyright (C) 2007  Johann Prieur <johann.prieur@gmail.com>
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
#
 
21
from pymsn.gnet.constants import *
 
22
from iochannel import GIOChannelClient
 
23
 
 
24
import gobject
 
25
import socket
 
26
import OpenSSL.SSL as OpenSSL
 
27
 
 
28
__all__ = ['SSLSocketClient']
 
29
 
 
30
class SSLSocketClient(GIOChannelClient):
 
31
    """Asynchronous Socket client class.
 
32
        
 
33
        @sort: __init__, open, send, close
 
34
        @undocumented: do_*, _watch_*, __io_*, _connect_done_handler
 
35
 
 
36
        @since: 0.1"""
 
37
    
 
38
    def __init__(self, host, port, domain=AF_INET, type=SOCK_STREAM):
 
39
        GIOChannelClient.__init__(self, host, port, domain, type)
 
40
    
 
41
    def _pre_open(self, sock=None):
 
42
        if sock is None:
 
43
            sock = socket.socket(self._domain, self._type)
 
44
            try:
 
45
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
 
46
            except AttributeError:
 
47
                pass
 
48
        context = OpenSSL.Context(OpenSSL.SSLv23_METHOD)
 
49
        ssl_sock = OpenSSL.Connection(context, sock)
 
50
        GIOChannelClient._pre_open(self, ssl_sock)
 
51
    
 
52
    def _post_open(self):
 
53
        GIOChannelClient._post_open(self)
 
54
        if self._transport.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) == 0:
 
55
            self._watch_set_cond(gobject.IO_IN | gobject.IO_PRI | gobject.IO_OUT |
 
56
                               gobject.IO_ERR | gobject.IO_HUP)
 
57
        else:
 
58
            self.emit("error", IoError.CONNECTION_FAILED)
 
59
            self._status = IoStatus.CLOSED
 
60
        return False
 
61
    
 
62
    def _io_channel_handler(self, chan, cond):
 
63
        if self._status == IoStatus.CLOSED:
 
64
            return False
 
65
        if self._status == IoStatus.OPENING:
 
66
            try:
 
67
                self._transport.do_handshake()
 
68
            except (OpenSSL.WantX509LookupError,
 
69
                    OpenSSL.WantReadError, OpenSSL.WantWriteError):
 
70
                return True
 
71
            except (OpenSSL.ZeroReturnError, OpenSSL.SysCallError):
 
72
                self.emit("error", IoError.SSL_CONNECTION_FAILED)
 
73
                self.close()
 
74
                return False
 
75
            else:
 
76
                self._status = IoStatus.OPEN
 
77
        elif self._status == IoStatus.OPEN:
 
78
            if cond & (gobject.IO_IN | gobject.IO_PRI):
 
79
                try:
 
80
                    buf = self._transport.recv(2048)
 
81
                except (OpenSSL.WantX509LookupError,
 
82
                        OpenSSL.WantReadError, OpenSSL.WantWriteError):
 
83
                    return True
 
84
                except (OpenSSL.ZeroReturnError, OpenSSL.SysCallError):
 
85
                    self.close()
 
86
                    return False
 
87
                self.emit("received", buf, len(buf))
 
88
 
 
89
            if cond & (gobject.IO_ERR | gobject.IO_HUP):
 
90
                self.close()
 
91
                return False
 
92
 
 
93
            if cond & gobject.IO_OUT:
 
94
                if len(self._outgoing_queue) > 0: # send next item
 
95
                    item = self._outgoing_queue[0]
 
96
                    try:
 
97
                        ret = self._transport.send(item.read())
 
98
                    except (OpenSSL.WantX509LookupError,
 
99
                            OpenSSL.WantReadError, OpenSSL.WantWriteError):
 
100
                        return True
 
101
                    except (OpenSSL.ZeroReturnError, OpenSSL.SysCallError):
 
102
                        self.close()
 
103
                        return False
 
104
                    item.sent(ret)
 
105
                    if item.is_complete(): # sent item
 
106
                        self.emit("sent", item.buffer, item.size)
 
107
                        item.callback()
 
108
                        del self._outgoing_queue[0]
 
109
                        del item
 
110
                    if len(self._outgoing_queue) == 0:
 
111
                        self._watch_remove_cond(gobject.IO_OUT)
 
112
                else:
 
113
                    self._watch_remove_cond(gobject.IO_OUT)
 
114
 
 
115
        return True
 
116
 
 
117
gobject.type_register(SSLSocketClient)