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

Debugged AsyncServer

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