Commit 5af22f06 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Debugged AsyncServer

parent 553aa17f
...@@ -33,7 +33,7 @@ class AsyncConnection(Connection): ...@@ -33,7 +33,7 @@ class AsyncConnection(Connection):
'instead' % frame) 'instead' % frame)
def send(self, message, fragment_size=None, mask=False): def send(self, message, fragment_size=None, mask=False):
frames = self.message_to_frames(message, fragment_size, mask) frames = list(self.message_to_frames(message, fragment_size, mask))
for frame in frames[:-1]: for frame in frames[:-1]:
self.sock.queue_send(frame) self.sock.queue_send(frame)
...@@ -98,7 +98,7 @@ class AsyncServer(Server): ...@@ -98,7 +98,7 @@ class AsyncServer(Server):
@property @property
def clients(self): def clients(self):
return self.conns.itervalues() return self.conns.values()
def remove_client(self, client, code, reason): def remove_client(self, client, code, reason):
self.epoll.unregister(client.fno) self.epoll.unregister(client.fno)
...@@ -138,18 +138,10 @@ class AsyncServer(Server): ...@@ -138,18 +138,10 @@ class AsyncServer(Server):
except SocketClosed: except SocketClosed:
continue continue
except Exception as e: except Exception as e:
logging.error(format_exc(e)) logging.error(format_exc(e).rstrip())
continue continue
mask = 0 self.update_mask(conn)
if conn.sock.can_send():
mask |= EPOLLOUT
if conn.sock.can_recv():
mask |= EPOLLIN
self.epoll.modify(fileno, mask)
def run(self): def run(self):
try: try:
...@@ -162,8 +154,19 @@ class AsyncServer(Server): ...@@ -162,8 +154,19 @@ class AsyncServer(Server):
self.epoll.close() self.epoll.close()
self.sock.close() self.sock.close()
def update_mask(self, conn):
mask = 0
if conn.sock.can_send():
mask |= EPOLLOUT
if conn.sock.can_recv():
mask |= EPOLLIN
self.epoll.modify(conn.sock.fileno(), mask)
def onsend(self, client, message): def onsend(self, client, message):
logging.debug('Written "%s" to %s', message, client) return NotImplemented
class AsyncClient(Client, AsyncConnection): class AsyncClient(Client, AsyncConnection):
...@@ -174,8 +177,10 @@ class AsyncClient(Client, AsyncConnection): ...@@ -174,8 +177,10 @@ class AsyncClient(Client, AsyncConnection):
def send(self, message, fragment_size=None, mask=False): def send(self, message, fragment_size=None, mask=False):
logging.debug('Enqueueing %s to %s', message, self) logging.debug('Enqueueing %s to %s', message, self)
AsyncConnection.send(self, message, fragment_size, mask) AsyncConnection.send(self, message, fragment_size, mask)
self.server.update_mask(self)
def onsend(self, message): def onsend(self, message):
logging.debug('Finished sending %s to %s', message, self)
self.server.onsend(self, message) self.server.onsend(self, message)
......
...@@ -136,27 +136,22 @@ class Server(object): ...@@ -136,27 +136,22 @@ class Server(object):
self.onclose(client, code, reason) self.onclose(client, code, reason)
def onopen(self, client): def onopen(self, client):
logging.debug('Opened socket to %s', client) return NotImplemented
def onmessage(self, client, message): def onmessage(self, client, message):
logging.debug('Received %s from %s', message, client) return NotImplemented
def onping(self, client, payload): def onping(self, client, payload):
logging.debug('Sent ping "%s" to %s', payload, client) return NotImplemented
def onpong(self, client, payload): def onpong(self, client, payload):
logging.debug('Received pong "%s" from %s', payload, client) return NotImplemented
def onclose(self, client, code, reason): def onclose(self, client, code, reason):
msg = 'Closed socket to %s' % client return NotImplemented
if code is not None:
msg += ': [%d] %s' % (code, reason)
logging.debug(msg)
def onerror(self, client, e): def onerror(self, client, e):
logging.error(format_exc(e)) return NotImplemented
class Client(Connection): class Client(Connection):
...@@ -175,21 +170,32 @@ class Client(Connection): ...@@ -175,21 +170,32 @@ class Client(Connection):
Connection.send(self, message, fragment_size=fragment_size, mask=mask) Connection.send(self, message, fragment_size=fragment_size, mask=mask)
def onopen(self): def onopen(self):
logging.debug('Opened socket to %s', self)
self.server.onopen(self) self.server.onopen(self)
def onmessage(self, message): def onmessage(self, message):
logging.debug('Received %s from %s', message, self)
self.server.onmessage(self, message) self.server.onmessage(self, message)
def onping(self, payload): def onping(self, payload):
logging.debug('Sent ping "%s" to %s', payload, self)
self.server.onping(self, payload) self.server.onping(self, payload)
def onpong(self, payload): def onpong(self, payload):
logging.debug('Received pong "%s" from %s', payload, self)
self.server.onpong(self, payload) self.server.onpong(self, payload)
def onclose(self, code, reason): def onclose(self, code, reason):
msg = 'Closed socket to %s' % self
if code is not None:
msg += ': [%d] %s' % (code, reason)
logging.debug(msg)
self.server.remove_client(self, code, reason) self.server.remove_client(self, code, reason)
def onerror(self, e): def onerror(self, e):
logging.error(format_exc(e))
self.server.onerror(self, e) self.server.onerror(self, e)
......
...@@ -195,7 +195,6 @@ class websocket(object): ...@@ -195,7 +195,6 @@ class websocket(object):
nframes += 1 nframes += 1
if callback: if callback:
print 'write cb'
callback() callback()
else: else:
entry[1] -= nwritten entry[1] -= nwritten
......
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