2017-05-25 23:04:33 +02:00
|
|
|
import Queue
|
2017-05-24 16:51:49 +02:00
|
|
|
import time
|
|
|
|
|
2017-03-10 23:11:57 +01:00
|
|
|
import asyncore_pollchoose as asyncore
|
2017-05-24 16:51:49 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
2017-02-08 14:19:02 +01:00
|
|
|
|
2017-01-10 21:20:49 +01:00
|
|
|
class AdvancedDispatcher(asyncore.dispatcher):
|
2017-03-11 11:12:08 +01:00
|
|
|
_buf_len = 2097152 # 2MB
|
2017-01-10 21:20:49 +01:00
|
|
|
|
2017-03-20 18:32:26 +01:00
|
|
|
def __init__(self, sock=None):
|
2017-03-10 23:11:57 +01:00
|
|
|
if not hasattr(self, '_map'):
|
2017-03-20 18:32:26 +01:00
|
|
|
asyncore.dispatcher.__init__(self, sock)
|
2017-03-10 23:11:57 +01:00
|
|
|
self.read_buf = b""
|
|
|
|
self.write_buf = b""
|
2017-05-25 23:04:33 +02:00
|
|
|
self.writeQueue = Queue.Queue()
|
|
|
|
self.receiveQueue = Queue.Queue()
|
2017-01-10 21:20:49 +01:00
|
|
|
self.state = "init"
|
2017-05-24 16:51:49 +02:00
|
|
|
self.lastTx = time.time()
|
2017-05-25 14:59:18 +02:00
|
|
|
self.sentBytes = 0
|
|
|
|
self.receivedBytes = 0
|
2017-01-10 21:20:49 +01:00
|
|
|
|
2017-02-08 14:19:02 +01:00
|
|
|
def slice_write_buf(self, length=0):
|
2017-04-16 18:27:15 +02:00
|
|
|
if length > 0:
|
2017-05-25 23:04:33 +02:00
|
|
|
self.write_buf = self.write_buf[length:]
|
2017-04-16 18:27:15 +02:00
|
|
|
|
|
|
|
def slice_read_buf(self, length=0):
|
|
|
|
if length > 0:
|
|
|
|
self.read_buf = self.read_buf[length:]
|
2017-01-10 21:20:49 +01:00
|
|
|
|
2017-02-08 14:19:02 +01:00
|
|
|
def read_buf_sufficient(self, length=0):
|
2017-01-10 21:20:49 +01:00
|
|
|
if len(self.read_buf) < length:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def process(self):
|
2017-04-16 18:27:15 +02:00
|
|
|
if self.state not in ["init", "tls_handshake"] and len(self.read_buf) == 0:
|
2017-01-10 21:20:49 +01:00
|
|
|
return
|
2017-05-27 19:09:21 +02:00
|
|
|
if not self.connected:
|
|
|
|
return
|
|
|
|
maxLoop = 20
|
|
|
|
while maxLoop > 0:
|
2017-01-10 21:20:49 +01:00
|
|
|
try:
|
2017-05-24 16:51:49 +02:00
|
|
|
# print "Trying to handle state \"%s\"" % (self.state)
|
2017-01-10 21:20:49 +01:00
|
|
|
if getattr(self, "state_" + str(self.state))() is False:
|
|
|
|
break
|
|
|
|
except AttributeError:
|
|
|
|
# missing state
|
|
|
|
raise
|
2017-05-27 19:09:21 +02:00
|
|
|
maxLoop -= 1
|
2017-01-10 21:20:49 +01:00
|
|
|
|
2017-04-16 18:27:15 +02:00
|
|
|
def set_state(self, state, length=0):
|
2017-01-10 21:20:49 +01:00
|
|
|
self.slice_read_buf(length)
|
|
|
|
self.state = state
|
|
|
|
|
|
|
|
def writable(self):
|
2017-05-25 23:04:33 +02:00
|
|
|
return self.connecting or len(self.write_buf) > 0 or not self.writeQueue.empty()
|
2017-01-10 21:20:49 +01:00
|
|
|
|
|
|
|
def readable(self):
|
2017-03-10 23:11:57 +01:00
|
|
|
return self.connecting or len(self.read_buf) < AdvancedDispatcher._buf_len
|
2017-01-10 21:20:49 +01:00
|
|
|
|
|
|
|
def handle_read(self):
|
2017-05-24 16:51:49 +02:00
|
|
|
self.lastTx = time.time()
|
|
|
|
if asyncore.maxDownloadRate > 0:
|
|
|
|
newData = self.recv(asyncore.downloadChunk)
|
|
|
|
asyncore.downloadBucket -= len(newData)
|
|
|
|
else:
|
2017-05-25 14:59:18 +02:00
|
|
|
newData = self.recv(AdvancedDispatcher._buf_len)
|
|
|
|
self.receivedBytes += len(newData)
|
|
|
|
asyncore.updateReceived(len(newData))
|
|
|
|
self.read_buf += newData
|
2017-01-10 21:20:49 +01:00
|
|
|
self.process()
|
|
|
|
|
|
|
|
def handle_write(self):
|
2017-05-24 16:51:49 +02:00
|
|
|
self.lastTx = time.time()
|
|
|
|
if asyncore.maxUploadRate > 0:
|
2017-05-25 23:04:33 +02:00
|
|
|
bufSize = asyncore.uploadChunk
|
2017-05-24 16:51:49 +02:00
|
|
|
else:
|
2017-05-25 23:04:33 +02:00
|
|
|
bufSize = self._buf_len
|
|
|
|
while len(self.write_buf) < bufSize:
|
|
|
|
try:
|
|
|
|
self.write_buf += self.writeQueue.get(False)
|
2017-05-27 19:39:19 +02:00
|
|
|
self.writeQueue.task_done()
|
2017-05-25 23:04:33 +02:00
|
|
|
except Queue.Empty:
|
|
|
|
break
|
|
|
|
if len(self.write_buf) > 0:
|
|
|
|
written = self.send(self.write_buf[0:bufSize])
|
|
|
|
asyncore.uploadBucket -= written
|
|
|
|
asyncore.updateSent(written)
|
|
|
|
self.sentBytes += written
|
|
|
|
self.slice_write_buf(written)
|
2017-03-10 23:11:57 +01:00
|
|
|
|
|
|
|
def handle_connect(self):
|
2017-05-24 16:51:49 +02:00
|
|
|
self.lastTx = time.time()
|
2017-03-10 23:11:57 +01:00
|
|
|
self.process()
|
2017-05-24 16:51:49 +02:00
|
|
|
|
2017-05-25 23:04:33 +02:00
|
|
|
def state_close(self):
|
|
|
|
pass
|
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
def close(self):
|
|
|
|
self.read_buf = b""
|
|
|
|
self.write_buf = b""
|
|
|
|
self.state = "shutdown"
|
2017-05-27 19:09:21 +02:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.writeQueue.get(False)
|
2017-05-27 19:39:19 +02:00
|
|
|
self.writeQueue.task_done()
|
2017-05-27 19:09:21 +02:00
|
|
|
except Queue.Empty:
|
|
|
|
break
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.receiveQueue.get(False)
|
2017-05-27 19:39:19 +02:00
|
|
|
self.receiveQueue.task_done()
|
2017-05-27 19:09:21 +02:00
|
|
|
except Queue.Empty:
|
|
|
|
break
|
2017-05-24 16:51:49 +02:00
|
|
|
asyncore.dispatcher.close(self)
|