Skip to content

Commit

Permalink
Merge pull request #158 from liZe/master
Browse files Browse the repository at this point in the history
Add a connection factory for the async client
  • Loading branch information
jaraco committed Mar 29, 2019
2 parents 2e2997c + b0589a0 commit c546cb1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
17 changes: 10 additions & 7 deletions irc/client_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import threading
import logging

from . import connection
from .client import ServerConnection, ServerNotConnectedError, Reactor,\
SimpleIRCClient, Event, _ping_ponger

Expand Down Expand Up @@ -99,8 +100,8 @@ class AioConnection(ServerConnection):
protocol_class = IrcProtocol

async def connect(
self, server, port, nickname,
password=None, username=None, ircname=None
self, server, port, nickname, password=None, username=None,
ircname=None, connect_factory=connection.AioFactory()
):
"""Connect/reconnect to a server.
Expand All @@ -113,6 +114,9 @@ async def connect(
* username - The username
* ircname - The IRC name ("realname")
* connect_factory - An async callable that takes the event loop and the
server address, and returns a connection (with a socket interface)
This function can be called to reconnect a closed connection.
Returns the AioProtocol instance (used for handling incoming
Expand All @@ -133,12 +137,11 @@ async def connect(
self.username = username or nickname
self.ircname = ircname or nickname
self.password = password
self.connect_factory = connect_factory

connection = self.reactor.loop.create_connection(
lambda: self.protocol_class(self, self.reactor.loop),
self.server,
self.port,
)
protocol_instance = self.protocol_class(self, self.reactor.loop)
connection = self.connect_factory(
protocol_instance, self.server_address)
transport, protocol = await connection

self.transport = transport
Expand Down
36 changes: 36 additions & 0 deletions irc/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,39 @@ def connect(self, server_address):
sock.connect(server_address)
return sock
__call__ = connect


class AioFactory:
"""
A class for creating async custom socket connections.
To create a simple connection:
server_address = ('localhost', 80)
Factory()(protocol_instance, server_address)
To create an SSL connection:
Factory(ssl=True)(protocol_instance, server_address)
To create an IPv6 connection:
Factory(ipv6=True)(protocol_instance, server_address)
Note that Factory doesn't save the state of the socket itself. The
caller must do that, as necessary. As a result, the Factory may be
re-used to create new connections with the same settings.
"""

family = socket.AF_INET

def __init__(self, **kwargs):
self.connection_args = kwargs

def connect(self, protocol_instance, server_address):
return protocol_instance.loop.create_connection(
lambda: protocol_instance, *server_address,
**self.connection_args)

__call__ = connect

0 comments on commit c546cb1

Please sign in to comment.