Ignore:
Timestamp:
06/16/12 20:00:37 (12 years ago)
Author:
atzm
Message:
  • improve performance
File:
1 edited

Legend:

Unmodified
Added
Removed
  • etherws/trunk/etherws.py

    r160 r162  
    6767 
    6868class TapHandler(DebugMixIn): 
     69    READ_SIZE = 65535 
     70 
    6971    def __init__(self, dev, debug=False): 
    7072        self._debug = debug 
     
    7274        self._tap = pytun.TunTapDevice(dev, pytun.IFF_TAP | pytun.IFF_NO_PI) 
    7375        self._tap.up() 
    74         self._glock = threading.Lock() 
     76        self._taplock = threading.Lock() 
    7577 
    7678    def fileno(self): 
    77         with self._glock: 
     79        with self._taplock: 
    7880            return self._tap.fileno() 
    7981 
    8082    def register_client(self, client): 
    81         with self._glock: 
    82             self._clients.append(client) 
     83        self._clients.append(client) 
    8384 
    8485    def unregister_client(self, client): 
    85         with self._glock: 
    86             self._clients.remove(client) 
     86        self._clients.remove(client) 
    8787 
    8888    def write(self, caller, message): 
    89         with self._glock: 
    90             clients = self._clients[:] 
    91  
    92             if caller is not self: 
    93                 clients.remove(caller) 
     89        clients = self._clients[:] 
     90 
     91        if caller is not self: 
     92            clients.remove(caller) 
     93 
     94            with self._taplock: 
    9495                self._tap.write(message) 
    9596 
    96             for c in clients: 
    97                 c.write_message(message, True) 
     97        for c in clients: 
     98            c.write_message(message, True) 
    9899 
    99100    def __call__(self, fd, events): 
    100         with self._glock: 
    101             data = self._tap.read(self._tap.mtu) 
    102         self.write(self, data) 
     101        buf = [] 
     102 
     103        while True: 
     104            with self._taplock: 
     105                data = self._tap.read(self.READ_SIZE) 
     106 
     107            if data: 
     108                buf.append(data) 
     109 
     110            if len(data) < self.READ_SIZE: 
     111                break 
     112 
     113        self.write(self, ''.join(buf)) 
    103114 
    104115 
Note: See TracChangeset for help on using the changeset viewer.