Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace asyncore with asyncio for better support #3264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions yowsup/layers/network/dispatcher/dispatcher_asyncio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from yowsup.layers.network.dispatcher.dispatcher import YowConnectionDispatcher
import asyncio
import logging
import socket
import traceback

logger = logging.getLogger(__name__)

class AsyncioConnectionDispatcher(YowConnectionDispatcher):
def __init__(self, connectionCallbacks):
super().__init__(connectionCallbacks)
self._connected = False
self.loop = asyncio.get_event_loop()
self.sock = None

async def _send_data(self, data):
if self._connected:
self.sock.sendall(data)
else:
logger.warning("Attempted to send %d bytes while still not connected", len(data))

def sendData(self, data):
asyncio.run(self._send_data(data))

async def _connect(self, host):
logger.debug("connect(%s)", str(host))
self.connectionCallbacks.onConnecting()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setblocking(False)
try:
await self.loop.sock_connect(self.sock, host)
self._connected = True
self.connectionCallbacks.onConnected()
except Exception as e:
logger.error("Connection failed: %s", str(e))
self.connectionCallbacks.onDisconnected()

def connect(self, host):
self.loop.run_until_complete(self._connect(host))

def handle_connect(self):
logger.debug("handle_connect")
if not self._connected:
self._connected = True
self.connectionCallbacks.onConnected()

def handle_close(self):
logger.debug("handle_close")
if self._connected and self.sock:
self.sock.close()
self._connected = False
self.connectionCallbacks.onDisconnected()

def handle_error(self):
logger.error(traceback.format_exc())
self.handle_close()

async def _handle_read(self):
if self._connected:
try:
data = await self.loop.sock_recv(self.sock, 1024)
self.connectionCallbacks.onRecvData(data)
except Exception as e:
logger.error("Read failed: %s", str(e))
self.handle_close()

def handle_read(self):
asyncio.run(self._handle_read())

def disconnect(self):
logger.debug("disconnect")
self.handle_close()
52 changes: 0 additions & 52 deletions yowsup/layers/network/dispatcher/dispatcher_asyncore.py

This file was deleted.

4 changes: 2 additions & 2 deletions yowsup/layers/network/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from yowsup.layers.network.dispatcher.dispatcher import ConnectionCallbacks
from yowsup.layers.network.dispatcher.dispatcher import YowConnectionDispatcher
from yowsup.layers.network.dispatcher.dispatcher_socket import SocketConnectionDispatcher
from yowsup.layers.network.dispatcher.dispatcher_asyncore import AsyncoreConnectionDispatcher
from yowsup.layers.network.dispatcher.dispatcher_asyncio import AsyncioConnectionDispatcher
import logging
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -42,7 +42,7 @@ def __init__(self):
def __create_dispatcher(self, dispatcher_type):
if dispatcher_type == self.DISPATCHER_ASYNCORE:
logger.debug("Created asyncore dispatcher")
return AsyncoreConnectionDispatcher(self)
return AsyncioConnectionDispatcher(self)
else:
logger.debug("Created socket dispatcher")
return SocketConnectionDispatcher(self)
Expand Down