Changeset d45adcf for skype


Ignore:
Timestamp:
2010-12-24T12:21:16Z (13 years ago)
Author:
unknown <pcfeb0009@…>
Branches:
master
Children:
1130561
Parents:
9c51166
Message:

Add locking to ensure only one thread accesses the socket

The Skype API calls the OnNotify callback in a separate thread: see
http://skype4py.sourceforge.net/doc/html/Skype4Py.utils.EventHandlingBase-class.html

Tested informally (chatting with another person for > 15 min)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • skype/skyped.py

    r9c51166 rd45adcf  
    3434import ssl
    3535import select
     36import threading
    3637
    3738__version__ = "0.1.1"
     
    5152
    5253sys.excepthook = eh
     54
     55def wait_for_lock(lock, timeout_to_print, timeout, msg):
     56        start = time.time()
     57        locked = lock.acquire(0)
     58        while not(locked):
     59                time.sleep(0.5)
     60                if timeout_to_print and (time.time() - timeout_to_print > start):
     61                        dprint("%s: Waited %f seconds" % \
     62                                        (msg, time.time() - start))
     63                        timeout_to_print = False
     64                if timeout and (time.time() - timeout > start):
     65                        dprint("%s: Waited %f seconds, giving up" % \
     66                                        (msg, time.time() - start))
     67                        return False
     68                locked = lock.acquire(0)
     69        return True
    5370
    5471def input_handler(fd):
     
    6178                return True
    6279        else:
    63                 try:
    64                         input = fd.recv(1024)
    65                 except Exception, s:
    66                         dprint("Warning, receiving 1024 bytes failed (%s)." % s)
    67                         fd.close()
    68                         options.conn = False
    69                         return False
    70                 for i in input.split("\n"):
    71                         skype.send(i.strip())
     80                if wait_for_lock(options.lock, 3, 10, "input_handler"):
     81                        try:
     82                                        input = fd.recv(1024)
     83                                        options.lock.release()
     84                        except Exception, s:
     85                                dprint("Warning, receiving 1024 bytes failed (%s)." % s)
     86                                fd.close()
     87                                options.conn = False
     88                                options.lock.release()
     89                                return False
     90                        for i in input.split("\n"):
     91                                skype.send(i.strip())
    7292                return True
    7393
     
    85105        done = False
    86106        while (not done) and (count < 10):
    87                 try:
    88                         sock.send(txt)
    89                         done = True
    90                 except Exception, s:
    91                         count += 1
    92                         dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count))
    93                         time.sleep(1)
     107                if wait_for_lock(options.lock, 3, 10, "socket send"):
     108                        try:
     109                                sock.send(txt)
     110                                options.lock.release()
     111                                done = True
     112                        except Exception, s:
     113                                options.lock.release()
     114                                count += 1
     115                                dprint("Warning, sending '%s' failed (%s). count=%d" % (txt, s, count))
     116                                time.sleep(1)
    94117        if not done:
    95118                if options.conn: options.conn.close()
     
    123146def listener(sock, skype):
    124147        global options
     148        if not(wait_for_lock(options.lock, 3, 10, "listener")): return False
    125149        rawsock, addr = sock.accept()
    126150        options.conn = ssl.wrap_socket(rawsock,
     
    133157                        options.conn.handshake()
    134158                except Exception:
     159                        options.lock.release()
    135160                        dprint("Warning, handshake failed, closing connection.")
    136161                        return False
     
    147172                options.conn.close()
    148173                options.conn = False
     174                options.lock.release()
    149175                return False
    150176        if ret == 2:
    151177                dprint("Username and password OK.")
    152178                options.conn.send("PASSWORD OK\n")
     179                options.lock.release()
    153180                serverloop(options, skype)
    154181                return True
     
    158185                options.conn.close()
    159186                options.conn = False
     187                options.lock.release()
    160188                return False
    161189
     
    384412        while 1:
    385413                options.conn = False
     414                options.lock = threading.Lock()
    386415                server(options.host, options.port, skype)
Note: See TracChangeset for help on using the changeset viewer.