Skip to content

Commit

Permalink
加上可能会影响效率的日志写入
Browse files Browse the repository at this point in the history
  • Loading branch information
woodyxiong committed Jun 2, 2018
1 parent 689ce29 commit 05c090d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pyc
.idea/
log.txt
5 changes: 2 additions & 3 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ def __init__(self, config):

def add_to_loop(self, loop):
if self._eventloop:
raise Exception("already add to loop")
raise Exception("controller already add to loop")
self._eventloop = loop
self._eventloop.add(self.server_socket, self)
print(self._eventloop._fdmap)

def handle_event(self, sock, event):
if sock == self.server_socket:
# 新客户端连接
conn = self.server_socket.accept()
handler = Tcprelay (self, conn, self._eventloop, self._config, self._fd_to_handlers)
Tcprelay(self, conn, self._eventloop, self._config, self._fd_to_handlers)



Expand Down
23 changes: 19 additions & 4 deletions eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: UTF-8 -*-

import select
import logging


class SelectLoop(object):
Expand All @@ -19,9 +20,13 @@ def poll(self, timeout=None):
return result

def register(self, fd):
if self._r_list.__contains__(fd):
logging.info("select的_r_list的"+str(fd)+"已经存在")
self._r_list.add(fd)

def unregister(self, fd):
if not self._r_list.__contains__(fd):
logging.info("select的_r_list的"+str(fd)+"为空")
self._r_list.remove(fd)


Expand All @@ -38,6 +43,8 @@ def __init__(self):
if hasattr(select, 'select'):
self._impl = SelectLoop()
self._model = 'select'
else:
raise Exception("Don't hava select model")
self._fdmap = {} #[fd](f,handler)

def poll(self):
Expand All @@ -52,13 +59,17 @@ def poll(self):
def stop(self):
self._isstopping = True

def add(self, f, handler):
fd = f.fileno()
self._fdmap[fd] = (f, handler)
def add(self, socket, handler):
fd = socket.fileno()
if self._fdmap.__contains__(fd):
logging.info("event映射的文件标识符", fd, "已存在")
self._fdmap[fd] = (socket, handler)
self._impl.register(fd)

def remove(self, f, handler):
fd = f.fileno()
if not self._fdmap[fd]:
logging.info("event映射的文件标识符", fd, "为空")
del self._fdmap[fd]
self._impl.unregister(fd)

Expand All @@ -67,20 +78,24 @@ def run(self):
while not self._isstopping:
try:
events = self.poll()
print("2333")
except Exception as e:
print(e)
logging.info(e)

# events(fileno,socket,1)
for fd, sock, event in events:
try:
if sock.fileno() < 0:
logging.info("文件标识符小于零"+str(sock.fileno()))
break
if self._fdmap[sock.fileno()][0]:
handler = self._fdmap[sock.fileno()][1]
else:
self.remove(sock.fileno())
logging.warning("未找到对应的句柄"+str(sock.fileno()))
break
# 转入控制器
handler.handle_event(sock, event)
except (OSError, IOError) as e:
logging.warning(e)
print(e)
10 changes: 8 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@
import shell
import controller
import eventloop
import logging

def main():
# 获取配置
config = shell.get_config()

# 设置日志 debug/info/warning/error/critical
logging.basicConfig(level=logging.INFO,
filename='./log.txt',
filemode='w',
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
try:
tcp_server = controller.Controller(config)
loop = eventloop.EventLoop()
tcp_server.add_to_loop(loop)
except Exception as e:
print(e)
sys.exit(0)
logging.error(e)
sys.exit(-1)

loop.run()

Expand Down

0 comments on commit 05c090d

Please sign in to comment.