Commit 08af39dc by Sheng

Handled ERRNO_CONNRESET

parent f9a66a9e
import io import io
import logging import logging
import os.path import os.path
import socket
import traceback import traceback
import uuid import uuid
import weakref import weakref
...@@ -9,7 +8,9 @@ import paramiko ...@@ -9,7 +8,9 @@ import paramiko
import tornado.web import tornado.web
import tornado.websocket import tornado.websocket
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tornado.iostream import _ERRNO_CONNRESET
from tornado.options import define, options, parse_command_line from tornado.options import define, options, parse_command_line
from tornado.util import errno_from_exception
define('address', default='127.0.0.1', help='listen address') define('address', default='127.0.0.1', help='listen address')
...@@ -56,31 +57,42 @@ class Worker(object): ...@@ -56,31 +57,42 @@ class Worker(object):
def on_read(self): def on_read(self):
logging.debug('worker {} on read'.format(self.id)) logging.debug('worker {} on read'.format(self.id))
data = self.chan.recv(BUF_SIZE)
logging.debug('"{}" from {}'.format(data, self.dst_addr))
if not data:
self.close()
return
logging.debug('"{}" to {}'.format(data, self.handler.src_addr))
try: try:
self.handler.write_message(data) data = self.chan.recv(BUF_SIZE)
except tornado.websocket.WebSocketClosedError: except (OSError, IOError) as e:
self.close() logging.error(e)
if errno_from_exception(e) in _ERRNO_CONNRESET:
self.close()
else:
logging.debug('"{}" from {}'.format(data, self.dst_addr))
if not data:
self.close()
return
logging.debug('"{}" to {}'.format(data, self.handler.src_addr))
try:
self.handler.write_message(data)
except tornado.websocket.WebSocketClosedError:
self.close()
def on_write(self): def on_write(self):
logging.debug('worker {} on write'.format(self.id)) logging.debug('worker {} on write'.format(self.id))
if not self.data_to_dst: if not self.data_to_dst:
return return
data = ''.join(self.data_to_dst) data = ''.join(self.data_to_dst)
self.data_to_dst = []
logging.debug('"{}" to {}'.format(data, self.dst_addr)) logging.debug('"{}" to {}'.format(data, self.dst_addr))
try: try:
sent = self.chan.send(data) sent = self.chan.send(data)
except socket.error as e: except (OSError, IOError) as e:
logging.error(e) logging.error(e)
self.close() if errno_from_exception(e) in _ERRNO_CONNRESET:
self.close()
else:
self.loop.update_handler(self.fd, IOLoop.WRITE)
else: else:
self.data_to_dst = []
data = data[sent:] data = data[sent:]
if data: if data:
self.data_to_dst.append(data) self.data_to_dst.append(data)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment