-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.py
66 lines (51 loc) · 1.88 KB
/
connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import asyncio
import ssl
class Connection:
"""A TCP connection over the IRC protocol."""
CONNECT_TIMEOUT = 10
MAX_LEN = 451
def __init__(self, hostname, port, useSSL, eventloop=None):
self.hostname = hostname
self.port = port
self.ssl = useSSL
self.reader = None
self.writer = None
self.eventloop = eventloop or asyncio.new_event_loop()
async def connect(self):
"""Connect to target."""
if self.ssl:
ssl_context = ssl.create_default_context()
(self.reader, self.writer) = await asyncio.open_connection(
host=self.hostname,
port=self.port,
ssl=self.ssl,
)
async def disconnect(self):
"""Disconnect from target."""
if not self.connected:
return
self.writer.close()
self.reader = None
self.writer = None
self.stop()
@property
def connected(self):
"""Whether this connection is... connected to something."""
return self.reader is not None and self.writer is not None
def stop(self):
"""Stop event loop."""
#self.eventloop.call_soon(self.eventloop.stop)
async def send(self, data):
"""Add data to send queue."""
if len(data) > self.MAX_LEN:
prefix_index = data.index(':')
prefix = data[0:prefix_index+1]
msg = data[prefix_index+1:]
to_send = [ msg[i:i+(self.MAX_LEN-len(prefix))] for i in range(0, len(msg), self.MAX_LEN-len(prefix)) ]
for line in to_send:
self.writer.write(bytes(prefix + line + "\r\n", "UTF-8"))
else:
self.writer.write(bytes(data + "\r\n", "UTF-8"))
await self.writer.drain()
async def recv(self, *, timeout=None):
return await asyncio.wait_for(self.reader.readline(), timeout=timeout)