home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / NatCheck.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  3.1 KB  |  110 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from cStringIO import StringIO
  5. from socket import error as socketerror
  6. protocol_name = 'BitTorrent protocol'
  7.  
  8. class NatCheck:
  9.     
  10.     def __init__(self, resultfunc, downloadid, peerid, ip, port, rawserver):
  11.         self.resultfunc = resultfunc
  12.         self.downloadid = downloadid
  13.         self.peerid = peerid
  14.         self.ip = ip
  15.         self.port = port
  16.         self.closed = False
  17.         self.buffer = StringIO()
  18.         self.next_len = 1
  19.         self.next_func = self.read_header_len
  20.         
  21.         try:
  22.             self.connection = rawserver.start_connection((ip, port), self)
  23.             self.connection.write(chr(len(protocol_name)) + protocol_name + chr(0) * 8 + downloadid)
  24.         except socketerror:
  25.             self.answer(False)
  26.         except IOError:
  27.             self.answer(False)
  28.  
  29.  
  30.     
  31.     def answer(self, result):
  32.         self.closed = True
  33.         
  34.         try:
  35.             self.connection.close()
  36.         except AttributeError:
  37.             pass
  38.  
  39.         self.resultfunc(result, self.downloadid, self.peerid, self.ip, self.port)
  40.  
  41.     
  42.     def read_header_len(self, s):
  43.         if ord(s) != len(protocol_name):
  44.             return None
  45.         
  46.         return (len(protocol_name), self.read_header)
  47.  
  48.     
  49.     def read_header(self, s):
  50.         if s != protocol_name:
  51.             return None
  52.         
  53.         return (8, self.read_reserved)
  54.  
  55.     
  56.     def read_reserved(self, s):
  57.         return (20, self.read_download_id)
  58.  
  59.     
  60.     def read_download_id(self, s):
  61.         if s != self.downloadid:
  62.             return None
  63.         
  64.         return (20, self.read_peer_id)
  65.  
  66.     
  67.     def read_peer_id(self, s):
  68.         if s != self.peerid:
  69.             return None
  70.         
  71.         self.answer(True)
  72.  
  73.     
  74.     def data_came_in(self, connection, s):
  75.         while True:
  76.             if self.closed:
  77.                 return None
  78.             
  79.             i = self.next_len - self.buffer.tell()
  80.             if i > len(s):
  81.                 self.buffer.write(s)
  82.                 return None
  83.             
  84.             self.buffer.write(s[:i])
  85.             s = s[i:]
  86.             m = self.buffer.getvalue()
  87.             self.buffer.reset()
  88.             self.buffer.truncate()
  89.             x = self.next_func(m)
  90.             if x is None:
  91.                 if not self.closed:
  92.                     self.answer(False)
  93.                 
  94.                 return None
  95.             
  96.             (self.next_len, self.next_func) = x
  97.  
  98.     
  99.     def connection_lost(self, connection):
  100.         if not self.closed:
  101.             self.closed = True
  102.             self.resultfunc(False, self.downloadid, self.peerid, self.ip, self.port)
  103.         
  104.  
  105.     
  106.     def connection_flushed(self, connection):
  107.         pass
  108.  
  109.  
  110.