mirror of
https://github.com/DJ2LS/FreeDATA
synced 2024-05-14 08:04:33 +00:00
socket timeout
close the socket if timeout is reached
This commit is contained in:
parent
ef3edb2f7a
commit
37146b3081
2 changed files with 20 additions and 5 deletions
20
sock.py
20
sock.py
|
@ -21,22 +21,34 @@ import helpers
|
||||||
class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
|
class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
while 1:
|
|
||||||
|
# loop through socket buffer until timeout is reached. then close buffer
|
||||||
|
socketTimeout = time.time() + 3
|
||||||
|
while socketTimeout > time.time():
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
encoding = 'utf-8'
|
encoding = 'utf-8'
|
||||||
#data = str(self.request.recv(1024), 'utf-8')
|
#data = str(self.request.recv(1024), 'utf-8')
|
||||||
|
|
||||||
data = bytes()
|
data = bytes()
|
||||||
while True:
|
|
||||||
chunk = self.request.recv(8192) # .strip()
|
# we need to loop through buffer until end of chunk is reached or timeout occured
|
||||||
|
while True and socketTimeout > time.time():
|
||||||
|
chunk = self.request.recv(1024) # .strip()
|
||||||
data += chunk
|
data += chunk
|
||||||
if chunk.endswith(b'\n'):
|
if chunk.endswith(b'\n'):
|
||||||
break
|
break
|
||||||
data = data[:-1] # remove b'\n'
|
data = data[:-1] # remove b'\n'
|
||||||
data = str(data, 'utf-8')
|
data = str(data, 'utf-8')
|
||||||
|
|
||||||
|
if len(data) > 0:
|
||||||
|
socketTimeout = time.time() + static.SOCKET_TIMEOUT
|
||||||
|
|
||||||
# convert data to json object
|
# convert data to json object
|
||||||
|
# we need to do some error handling in case of socket timeout
|
||||||
|
try:
|
||||||
received_json = json.loads(data)
|
received_json = json.loads(data)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
print(received_json)
|
print(received_json)
|
||||||
|
|
||||||
# GET COMMANDS
|
# GET COMMANDS
|
||||||
|
@ -217,6 +229,8 @@ class CMDTCPRequestHandler(socketserver.BaseRequestHandler):
|
||||||
if received_json["type"] == 'SET' and received_json["command"] == 'DEL_RX_BUFFER':
|
if received_json["type"] == 'SET' and received_json["command"] == 'DEL_RX_BUFFER':
|
||||||
static.RX_BUFFER = []
|
static.RX_BUFFER = []
|
||||||
|
|
||||||
|
print("sock timeout...")
|
||||||
|
|
||||||
def start_cmd_socket():
|
def start_cmd_socket():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -19,8 +19,9 @@ DXGRID = b''
|
||||||
# ---------------------------------
|
# ---------------------------------
|
||||||
|
|
||||||
# Server Defaults
|
# Server Defaults
|
||||||
HOST = "localhost"
|
HOST = "0.0.0.0"
|
||||||
PORT = 3000
|
PORT = 3000
|
||||||
|
SOCKET_TIMEOUT = 3 # seconds
|
||||||
# ---------------------------------
|
# ---------------------------------
|
||||||
|
|
||||||
# HAMLIB DEFAULTS
|
# HAMLIB DEFAULTS
|
||||||
|
|
Loading…
Reference in a new issue