Files
shadowsocks/shadowsocks/eventloop.py

242 lines
6.9 KiB
Python
Raw Normal View History

2014-04-23 16:31:17 +08:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 clowwindy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# from ssloop
# https://github.com/clowwindy/ssloop
2014-05-30 16:28:44 +08:00
import os
2014-06-01 13:42:48 +08:00
import socket
2014-04-23 16:31:17 +08:00
import select
2014-06-01 19:09:52 +08:00
import errno
import logging
2014-04-23 16:31:17 +08:00
from collections import defaultdict
2014-04-24 12:34:31 +08:00
__all__ = ['EventLoop', 'POLL_NULL', 'POLL_IN', 'POLL_OUT', 'POLL_ERR',
2014-06-01 16:21:33 +08:00
'POLL_HUP', 'POLL_NVAL', 'EVENT_NAMES']
2014-04-23 17:12:07 +08:00
2014-04-24 12:34:31 +08:00
POLL_NULL = 0x00
POLL_IN = 0x01
POLL_OUT = 0x04
POLL_ERR = 0x08
POLL_HUP = 0x10
POLL_NVAL = 0x20
2014-04-23 16:31:17 +08:00
2014-06-01 16:21:33 +08:00
EVENT_NAMES = {
POLL_NULL: 'POLL_NULL',
POLL_IN: 'POLL_IN',
POLL_OUT: 'POLL_OUT',
POLL_ERR: 'POLL_ERR',
POLL_HUP: 'POLL_HUP',
POLL_NVAL: 'POLL_NVAL',
}
2014-04-23 16:31:17 +08:00
class EpollLoop(object):
def __init__(self):
self._epoll = select.epoll()
def poll(self, timeout):
return self._epoll.poll(timeout)
def add_fd(self, fd, mode):
self._epoll.register(fd, mode)
def remove_fd(self, fd):
self._epoll.unregister(fd)
def modify_fd(self, fd, mode):
self._epoll.modify(fd, mode)
class KqueueLoop(object):
MAX_EVENTS = 1024
def __init__(self):
self._kqueue = select.kqueue()
self._fds = {}
def _control(self, fd, mode, flags):
events = []
2014-04-24 12:34:31 +08:00
if mode & POLL_IN:
2014-04-23 16:31:17 +08:00
events.append(select.kevent(fd, select.KQ_FILTER_READ, flags))
2014-04-24 12:34:31 +08:00
if mode & POLL_OUT:
2014-04-23 16:31:17 +08:00
events.append(select.kevent(fd, select.KQ_FILTER_WRITE, flags))
for e in events:
self._kqueue.control([e], 0)
def poll(self, timeout):
if timeout < 0:
timeout = None # kqueue behaviour
events = self._kqueue.control(None, KqueueLoop.MAX_EVENTS, timeout)
2014-04-24 12:34:31 +08:00
results = defaultdict(lambda: POLL_NULL)
2014-04-23 16:31:17 +08:00
for e in events:
fd = e.ident
if e.filter == select.KQ_FILTER_READ:
2014-04-24 12:34:31 +08:00
results[fd] |= POLL_IN
2014-04-23 16:31:17 +08:00
elif e.filter == select.KQ_FILTER_WRITE:
2014-04-24 12:34:31 +08:00
results[fd] |= POLL_OUT
2014-04-23 16:31:17 +08:00
return results.iteritems()
def add_fd(self, fd, mode):
self._fds[fd] = mode
self._control(fd, mode, select.KQ_EV_ADD)
def remove_fd(self, fd):
self._control(fd, self._fds[fd], select.KQ_EV_DELETE)
del self._fds[fd]
def modify_fd(self, fd, mode):
self.remove_fd(fd)
self.add_fd(fd, mode)
class SelectLoop(object):
def __init__(self):
self._r_list = set()
self._w_list = set()
self._x_list = set()
def poll(self, timeout):
r, w, x = select.select(self._r_list, self._w_list, self._x_list,
timeout)
2014-04-24 12:34:31 +08:00
results = defaultdict(lambda: POLL_NULL)
for p in [(r, POLL_IN), (w, POLL_OUT), (x, POLL_ERR)]:
2014-04-23 16:31:17 +08:00
for fd in p[0]:
results[fd] |= p[1]
return results.items()
def add_fd(self, fd, mode):
2014-04-24 12:34:31 +08:00
if mode & POLL_IN:
2014-04-23 16:31:17 +08:00
self._r_list.add(fd)
2014-04-24 12:34:31 +08:00
if mode & POLL_OUT:
2014-04-23 16:31:17 +08:00
self._w_list.add(fd)
2014-04-24 12:34:31 +08:00
if mode & POLL_ERR:
2014-04-23 16:31:17 +08:00
self._x_list.add(fd)
def remove_fd(self, fd):
if fd in self._r_list:
self._r_list.remove(fd)
if fd in self._w_list:
self._w_list.remove(fd)
if fd in self._x_list:
self._x_list.remove(fd)
def modify_fd(self, fd, mode):
self.remove_fd(fd)
self.add_fd(fd, mode)
2014-04-23 17:12:07 +08:00
class EventLoop(object):
def __init__(self):
if hasattr(select, 'epoll'):
self._impl = EpollLoop()
2014-06-01 19:09:52 +08:00
model = 'epoll'
2014-04-23 17:12:07 +08:00
elif hasattr(select, 'kqueue'):
self._impl = KqueueLoop()
2014-06-01 19:09:52 +08:00
model = 'kqueue'
2014-04-23 17:12:07 +08:00
elif hasattr(select, 'select'):
self._impl = SelectLoop()
2014-06-01 19:09:52 +08:00
model = 'select'
2014-04-23 17:12:07 +08:00
else:
raise Exception('can not find any available functions in select '
'package')
2014-04-23 19:27:38 +08:00
self._fd_to_f = {}
2014-06-01 19:09:52 +08:00
self._handlers = []
self.stopping = False
logging.debug('using event model: %s', model)
2014-06-01 16:21:33 +08:00
2014-04-23 17:12:07 +08:00
def poll(self, timeout=None):
events = self._impl.poll(timeout)
2014-06-01 19:09:52 +08:00
return [(self._fd_to_f[fd], fd, event) for fd, event in events]
2014-04-23 17:12:07 +08:00
def add(self, f, mode):
fd = f.fileno()
2014-04-23 19:27:38 +08:00
self._fd_to_f[fd] = f
2014-04-23 17:12:07 +08:00
self._impl.add_fd(fd, mode)
def remove(self, f):
fd = f.fileno()
2014-04-23 19:27:38 +08:00
self._fd_to_f[fd] = None
2014-04-23 17:12:07 +08:00
self._impl.remove_fd(fd)
def modify(self, f, mode):
fd = f.fileno()
self._impl.modify_fd(fd, mode)
2014-04-24 12:34:31 +08:00
2014-06-01 19:09:52 +08:00
def add_handler(self, handler):
self._handlers.append(handler)
def run(self):
while not self.stopping:
try:
events = self.poll(1)
except (OSError, IOError) as e:
if errno_from_exception(e) == errno.EPIPE:
# Happens when the client closes the connection
2014-06-19 09:20:04 +08:00
logging.error('poll:%s', e)
2014-06-01 19:09:52 +08:00
continue
else:
2014-06-19 09:20:04 +08:00
logging.error('poll:%s', e)
2014-06-06 22:57:57 +08:00
import traceback
traceback.print_exc()
2014-06-01 19:09:52 +08:00
continue
for handler in self._handlers:
# TODO when there are a lot of handlers
2014-06-06 22:52:02 +08:00
try:
handler(events)
except (OSError, IOError) as e:
logging.error(e)
2014-06-06 22:57:57 +08:00
import traceback
traceback.print_exc()
2014-06-01 19:09:52 +08:00
2014-04-24 12:34:31 +08:00
# from tornado
def errno_from_exception(e):
"""Provides the errno from an Exception object.
There are cases that the errno attribute was not set so we pull
the errno out of the args but if someone instatiates an Exception
without any args you will get a tuple error. So this function
abstracts all that behavior to give you a safe way to get the
errno.
"""
if hasattr(e, 'errno'):
return e.errno
elif e.args:
return e.args[0]
else:
return None
2014-05-30 16:28:44 +08:00
# from tornado
def get_sock_error(sock):
2014-06-02 18:16:24 +08:00
error_number = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
return socket.error(error_number, os.strerror(error_number))