mncc_sock.py: throw / raise exceptions from MnccSocket

Calling sys.exit() from MnccSocket's constructor could lead to
a situation when the main thread becomes "dead", while its child
threads are still working. Let's throw the exceptions towards
the caller, so it would be possible to handle them there.

Change-Id: I485eb86c69e74693eeae89723de27e376585f1da
changes/58/11158/1
Vadim Yanitskiy 4 years ago
parent c43f321e6e
commit 1e3f0aaf6b
  1. 16
      mncc_sock.py

@ -10,7 +10,6 @@
# option, any later version.
import socket
import sys
import os
import mncc
import ctypes
@ -122,11 +121,7 @@ class MnccSocket(MnccSocketBase):
super(MnccSocketBase, self).__init__()
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
print('connecting to %s' % address)
try:
self.sock.connect(address)
except socket.error as errmsg:
sys.stderr.write("%s\n" % errmsg)
sys.exit(1)
self.sock.connect(address)
# Check the HELLO message
self.check_hello()
@ -137,15 +132,13 @@ class MnccSocket(MnccSocketBase):
# Match expected message type
if msg.msg_type != mncc.MNCC_SOCKET_HELLO:
sys.stderr.write('Received an unknown (!= MNCC_SOCKET_HELLO) '
raise AssertionError('Received an unknown (!= MNCC_SOCKET_HELLO) '
'message: %s\n' % msg)
sys.exit(1)
# Match expected protocol version
if msg.version != mncc.MNCC_SOCK_VERSION:
sys.stderr.write('MNCC protocol version mismatch '
raise AssertionError('MNCC protocol version mismatch '
'(0x%04x vs 0x%04x)\n' % (msg.version, mncc.MNCC_SOCK_VERSION))
sys.exit(1)
# Match expected message sizes / offsets
if (msg.mncc_size != ctypes.sizeof(mncc.gsm_mncc) or
@ -154,8 +147,7 @@ class MnccSocket(MnccSocketBase):
msg.signal_offset != mncc.gsm_mncc.signal.offset or
msg.emergency_offset != mncc.gsm_mncc.emergency.offset or
msg.lchan_type_offset != mncc.gsm_mncc.lchan_type.offset):
sys.stderr.write('MNCC message alignment mismatch\n')
sys.exit(1)
raise AssertionError('MNCC message alignment mismatch\n')
print('Received %s' % msg)

Loading…
Cancel
Save