This commit is contained in:
End 2020-08-09 14:38:41 +02:00
parent c1888e1cc3
commit fa9fa1746c
3 changed files with 12 additions and 8 deletions

View file

@ -2,12 +2,12 @@
import pdb import pdb
from mitmproxy import ctx from mitmproxy import ctx
import threading import threading
from queue import Queue from queue import Queue, Empty
import time import time
import zmq import zmq
import json import json
NO_MSG {"msg": None} NO_MSG = {"msg": None}
INIT_MSG = {"msg": "init"} INIT_MSG = {"msg": "init"}
ACK_MSG = {"msg": "ack"} ACK_MSG = {"msg": "ack"}
PING_MSG = {"msg": "ping"} PING_MSG = {"msg": "ping"}
@ -30,7 +30,7 @@ def get_msg(socket):
if msg: if msg:
return json.loads(msg) return json.loads(msg)
except json.JSONDecodeError: except json.JSONDecodeError:
print("malformed message received '{msg}'") print(f"malformed message received {msg}")
return NO_MSG return NO_MSG
@ -68,7 +68,10 @@ def networking(q):
timer = time.monotonic() timer = time.monotonic()
if not a: if not a:
try:
a = q.get(block=False) a = q.get(block=False)
except Empty:
pass
if a: if a:
send_msg(a, socket) send_msg(a, socket)
msg = get_msg(socket) msg = get_msg(socket)

View file

@ -20,11 +20,11 @@ void NetworkThread::process() {
while(true){ while(true){
bool connected = false; bool connected = false;
while(!connected) { while(!connected) {
sock.send(zmq::str_buffer("{'type': 'init'}"), zmq::send_flags::dontwait); sock.send(zmq::buffer(INIT_MSG_STR.c_str(), INIT_MSG_STR.length()), zmq::send_flags::dontwait);
zmq::message_t msg; zmq::message_t msg;
const auto ret = sock.recv(msg, zmq::recv_flags::dontwait); const auto ret = sock.recv(msg, zmq::recv_flags::dontwait);
if(ret) { if(ret) {
if(msg.to_string() == "{'type': 'ack'}") { if(msg.to_string() == ACK_MSG_STR) {
connected = true; connected = true;
} }
} }

View file

@ -3,7 +3,8 @@
#include <QObject> #include <QObject>
#include <includes.h> #include <includes.h>
const auto INIT_MSG = R"({"msg": "init"})"_json; const std::string INIT_MSG_STR = R"({"msg": "init"})";
const std::string ACK_MSG_STR = R"({"msg": "ack"})";
class NetworkThread : public QObject class NetworkThread : public QObject
{ {