net wheel bug, onlyproxy function and more...

This commit is contained in:
2020-04-03 15:19:41 +08:00
parent b447b65c10
commit 55c6163e18
4 changed files with 79 additions and 22 deletions

View File

@@ -4,5 +4,6 @@
"listen_ip": "127.0.0.1",
"listen_num": 39,
"buffsize": 4096,
"proxy": false
"proxy": false,
"onlyproxy": false
}

View File

@@ -30,7 +30,7 @@ BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096)
ID = jsondata.try_to_read_jsondata('id', 'unknown_id')
class Datapack:
def __init__(self, method='post', app='all', version='msw/0.1', head=None, body=b'',
file=None, gen_flag=True):
file=None, gen_flag=True, delete=False):
self.id = ID
if head is None:
head = {}
@@ -38,6 +38,7 @@ class Datapack:
self.head['id'] = self.id
self.method = method
self.file = file
self.delete = delete
self.app = app
self.version = version
self.body = body
@@ -93,6 +94,7 @@ class Datapack:
ndp = copy.deepcopy(self)
ndp.app = ndp.head['from']
ndp.method = 'reply'
ndp.delete = False
if not self.head['id'] == ID: # net package
ndp.head['to'] = self.head['id']
ndp.head['id'] = ID

View File

@@ -24,6 +24,7 @@ class Ffmpeg_controller:
self.server = None
self.conver_task_queue = queue.Queue()
self.org_filename = None
self.concat = True
self.conver_task_thread = threading.Thread(target=self.conver_task_func, args=())
self.conver_task_thread.start()
@@ -39,7 +40,18 @@ class Ffmpeg_controller:
dp = receive_queue.get()
if dp.method == 'post' and dp.body == b'start': # config ffmpeg is server or client
if dp.head.get('concat'):
if dp.head['concat'] == 'true':
self.concat = True
elif dp.head['concat'] == 'false':
self.concat = False
else:
print('unknown concat value')
continue
self.org_filename = dp.head['filename']
if self.concat:
ndp = dp.reply()
ndp.body = 'Spliting file %s' % dp.head['filename']
ndp.body = ndp.body.encode()
@@ -52,6 +64,7 @@ class Ffmpeg_controller:
self.run_as_server()
if self.concat:
# concat all file
filelist = os.listdir('res/ffmpeg_finished')
if 'filelist.txt' in filelist:
@@ -63,7 +76,11 @@ class Ffmpeg_controller:
subprocess.check_output('ffmpeg -f concat -i res/ffmpeg_finished/filelist.txt \
-c copy -y ' + object_filename, shell=True)
print('All process finished at ' + object_filename)
for file in filelist:
os.remove('res/ffmpeg_finished/' + file)
os.remove('res/ffmpeg_finished/filelist.txt')
print('All process finished')
elif dp.method == 'post' and dp.body == b'enable': # clinet mode
self.status = 1
@@ -176,10 +193,13 @@ class Ffmpeg_controller:
os.system('ffmpeg -i ' + filename + ' -c:a libopus -ab 64k \
-c:v libx265 -s 1280x720 -y ' + output_filename)
os.remove(filename)
ndp = dp.reply()
ndp.head['filename'] = output_filename
ndp.head['old_filename'] = filename
ndp.method = 'file'
ndp.delete = True
send_queue.put(ndp)
self.send_request()

View File

@@ -15,6 +15,9 @@ receive_queue = receive_queues[__name__]
BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096)
ID = jsondata.try_to_read_jsondata('id', 'Unknown_ID')
RETRYSLEEP = 5
MYPROXY = jsondata.try_to_read_jsondata('proxy', False)
ONLYPROXY = jsondata.try_to_read_jsondata('onlyproxy', False)
def main():
network_controller = Network_controller()
@@ -23,6 +26,9 @@ def main():
class Network_controller: # manage id and connection
def __init__(self):
if ONLYPROXY and not MYPROXY:
print('config failed because you set onlyproxy true but proxy false')
return
self.send_queue = queue.Queue()
self.id_dict = {}
self.lock = threading.Lock()
@@ -153,6 +159,12 @@ class Network_controller: # manage id and connection
ip, port = connection.conn.getpeername()
port = int(connection.listen_port)
connection_list.append((ip, port))
for addr in self.conflist:
if not addr in connection_list:
connection_list.append(addr)
for addr in self.conflist_pass:
if not addr in connection_list:
connection_list.append(addr)
data_dict['mht'] = connection_list
data_dict['proxy'] = self.proxydict
@@ -209,6 +221,13 @@ class Network_controller: # manage id and connection
elif not to:
print('not to', dp)
elif ONLYPROXY and not to == MYPROXY:
if dp.head['to']:
dp.head['to'] = to + dp.head['to']
else:
dp.head['to'] = to
self.send_to_id(MYPROXY, dp)
else:
self.send_to_id(to, dp)
@@ -349,7 +368,7 @@ class Connection:
self.conn.close()
return
self.netowrk_controller.set_connection(self,)
self.netowrk_controller.set_connection(self)
self.thread_send = threading.Thread(target=self.send_func, args=(), daemon=True)
self.thread_send.start()
@@ -467,6 +486,13 @@ class Connection:
#print('you connect to your self')
return 4, dp.head.get('flag')
if ONLYPROXY and not self.id == MYPROXY: # refuce not proxy connection
return 5, dp.head.get('flag')
if dp.head.get('onlyuseproxy'):
if not dp.head['onlyuseproxy'] == ID:
return 6, dp.head.get('flag')
if not self.positive:
self.send_id()
@@ -476,6 +502,8 @@ class Connection:
def send_id(self):
dp = Datapack(head={'from': __name__})
dp.app = 'handshake'
if ONLYPROXY:
dp.head['onlyuseproxy'] = MYPROXY
dp.head['listen_port'] = str(jsondata.try_to_read_jsondata('listen_port', 3900))
dp.encode()
self.conn.sendall(dp.encode_data)
@@ -497,8 +525,14 @@ class Connection:
self.conn.sendall(data)
except Exception as e:
print('Failed to send file %s %s: %s' % (dp.head['filename'], type(e), str(e)), dp)
if dp.head.get('to'):
dp.head['to'] = self.id + '&' + dp.head['to']
else:
dp.head['to'] = self.id
self.netowrk_controller.wheel_queue.put(dp)
break
if dp.delete:
os.remove(dp.head['filename'])
print('Send file %s finished' % dp.head['filename'], dp)