rewrote irc core to use raw sockets instead of asynchat
This commit is contained in:
parent
8322df1c93
commit
f204e42197
43
core/irc.py
43
core/irc.py
|
@ -2,8 +2,6 @@ import sys
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
import thread
|
import thread
|
||||||
import asyncore
|
|
||||||
import asynchat
|
|
||||||
import Queue
|
import Queue
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,41 +14,38 @@ def decode(txt):
|
||||||
return txt.decode('utf-8', 'ignore')
|
return txt.decode('utf-8', 'ignore')
|
||||||
|
|
||||||
|
|
||||||
class crlf_tcp(asynchat.async_chat):
|
class crlf_tcp(object):
|
||||||
"Handles tcp connections that consist of utf-8 lines ending with crlf"
|
"Handles tcp connections that consist of utf-8 lines ending with crlf"
|
||||||
|
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port):
|
||||||
asynchat.async_chat.__init__(self)
|
self.ibuffer = ""
|
||||||
self.set_terminator('\r\n')
|
|
||||||
self.buffer = ""
|
|
||||||
self.obuffer = ""
|
self.obuffer = ""
|
||||||
self.oqueue = Queue.Queue() #where we stick things that need to be sent
|
self.oqueue = Queue.Queue() # lines to be sent out
|
||||||
self.iqueue = Queue.Queue() #where we stick things that were received
|
self.iqueue = Queue.Queue() # lines that were received
|
||||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.socket = socket.socket(socket.AF_INET, socket.TCP_NODELAY)
|
||||||
self.socket.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 0)
|
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.connect((self.host, self.port))
|
self.socket.connect((self.host, self.port))
|
||||||
asyncore.loop()
|
thread.start_new_thread(self.recv_loop, ())
|
||||||
|
thread.start_new_thread(self.send_loop, ())
|
||||||
|
|
||||||
def handle_connect(self):
|
def recv_loop(self):
|
||||||
thread.start_new_thread(self.queue_read_loop, ())
|
while True:
|
||||||
|
self.ibuffer += self.socket.recv(4096)
|
||||||
|
while '\r\n' in self.ibuffer:
|
||||||
|
line, self.ibuffer = self.ibuffer.split('\r\n', 1)
|
||||||
|
self.iqueue.put(decode(line))
|
||||||
|
|
||||||
def queue_read_loop(self):
|
def send_loop(self):
|
||||||
while True:
|
while True:
|
||||||
line = self.oqueue.get().splitlines()[0][:500]
|
line = self.oqueue.get().splitlines()[0][:500]
|
||||||
print ">>> %r" % line
|
print ">>> %r" % line
|
||||||
self.push(line.encode('utf-8', 'replace') + '\r\n')
|
self.obuffer += line.encode('utf-8', 'replace') + '\r\n'
|
||||||
|
while self.obuffer:
|
||||||
def collect_incoming_data(self, data):
|
sent = self.socket.send(self.obuffer)
|
||||||
self.buffer += data
|
self.obuffer = self.obuffer[sent:]
|
||||||
|
|
||||||
def found_terminator(self):
|
|
||||||
line = self.buffer
|
|
||||||
self.iqueue.put(decode(line))
|
|
||||||
self.buffer = ''
|
|
||||||
|
|
||||||
irc_prefix_rem = re.compile(r'(.*?) (.*?) (.*)').match
|
irc_prefix_rem = re.compile(r'(.*?) (.*?) (.*)').match
|
||||||
irc_noprefix_rem = re.compile(r'()(.*?) (.*)').match
|
irc_noprefix_rem = re.compile(r'()(.*?) (.*)').match
|
||||||
|
|
Loading…
Reference in New Issue