@@ -94,9 +94,9 @@ def onUiInitDone(self):
9494
9595 def onWidget (self ):
9696 serialSetting = QWidget ()
97- serialSettingsLayout = QGridLayout ()
97+ self . serialSettingsLayout = QGridLayout ()
9898 protocolLabel = QLabel (_ ("Protocol" ))
99- modeLabel = QLabel (_ ("Mode" ))
99+ self . modeLabel = QLabel (_ ("Mode" ))
100100 self .targetLabel = QLabel (_ ("Target" ))
101101 self .targetCombobox = ComboBox ()
102102 self .targetCombobox .setEditable (True )
@@ -126,21 +126,21 @@ def onWidget(self):
126126 self .autoReconnect = QCheckBox ()
127127 self .autoReconnectIntervalEdit = QLineEdit ("1.0" )
128128 self .serialOpenCloseButton = QPushButton (_ ("OPEN" ))
129- serialSettingsLayout .addWidget (protocolLabel ,0 ,0 )
130- serialSettingsLayout .addWidget (protocolWidget , 0 , 1 , 1 , 2 )
131- serialSettingsLayout .addWidget (modeLabel , 1 , 0 )
132- serialSettingsLayout .addWidget (modeWidget , 1 , 1 , 1 , 2 )
133- serialSettingsLayout .addWidget (self .targetLabel , 2 , 0 )
134- serialSettingsLayout .addWidget (self .targetCombobox , 2 , 1 , 1 , 2 )
135- serialSettingsLayout .addWidget (self .portLabel , 3 , 0 )
136- serialSettingsLayout .addWidget (self .porttEdit , 3 , 1 , 1 , 2 )
137- serialSettingsLayout .addWidget (self .clientsCombobox , 4 , 0 , 1 , 2 )
138- serialSettingsLayout .addWidget (self .disconnetClientBtn , 4 , 2 , 1 , 1 )
139- serialSettingsLayout .addWidget (self .autoReconnetLable , 5 , 0 , 1 , 1 )
140- serialSettingsLayout .addWidget (self .autoReconnect , 5 , 1 , 1 , 1 )
141- serialSettingsLayout .addWidget (self .autoReconnectIntervalEdit , 5 , 2 , 1 , 1 )
142- serialSettingsLayout .addWidget (self .serialOpenCloseButton , 6 , 0 , 1 , 3 )
143- serialSetting .setLayout (serialSettingsLayout )
129+ self . serialSettingsLayout .addWidget (protocolLabel ,0 ,0 )
130+ self . serialSettingsLayout .addWidget (protocolWidget , 0 , 1 , 1 , 2 )
131+ self . serialSettingsLayout .addWidget (self . modeLabel , 1 , 0 )
132+ self . serialSettingsLayout .addWidget (modeWidget , 1 , 1 , 1 , 2 )
133+ self . serialSettingsLayout .addWidget (self .targetLabel , 2 , 0 )
134+ self . serialSettingsLayout .addWidget (self .targetCombobox , 2 , 1 , 1 , 2 )
135+ self . serialSettingsLayout .addWidget (self .portLabel , 3 , 0 )
136+ self . serialSettingsLayout .addWidget (self .porttEdit , 3 , 1 , 1 , 2 )
137+ self . serialSettingsLayout .addWidget (self .clientsCombobox , 4 , 0 , 1 , 2 )
138+ self . serialSettingsLayout .addWidget (self .disconnetClientBtn , 4 , 2 , 1 , 1 )
139+ self . serialSettingsLayout .addWidget (self .autoReconnetLable , 5 , 0 , 1 , 1 )
140+ self . serialSettingsLayout .addWidget (self .autoReconnect , 5 , 1 , 1 , 1 )
141+ self . serialSettingsLayout .addWidget (self .autoReconnectIntervalEdit , 5 , 2 , 1 , 1 )
142+ self . serialSettingsLayout .addWidget (self .serialOpenCloseButton , 6 , 0 , 1 , 3 )
143+ serialSetting .setLayout (self . serialSettingsLayout )
144144 self .widgetConfMap ["protocol" ] = self .protoclTcpRadioBtn
145145 self .widgetConfMap ["mode" ] = self .modeClientRadioBtn
146146 self .widgetConfMap ["target" ] = self .targetCombobox
@@ -157,6 +157,8 @@ def initEvet(self):
157157 self .showSwitchSignal .connect (self .showSwitch )
158158 self .updateTargetSignal .connect (self .updateTarget )
159159 self .updateClientsSignal .connect (self .updateClients )
160+ self .protoclTcpRadioBtn .clicked .connect (lambda : self .changeProtocol ("tcp" ))
161+ self .protoclUdpRadioBtn .clicked .connect (lambda : self .changeProtocol ("udp" ))
160162 self .modeServerRadioBtn .clicked .connect (lambda : self .changeMode ("server" ))
161163 self .modeClientRadioBtn .clicked .connect (lambda : self .changeMode ("client" ))
162164 self .clientsCombobox .currentIndexChanged .connect (self .serverModeClientChanged )
@@ -165,6 +167,31 @@ def initEvet(self):
165167 self .autoReconnectIntervalEdit .textChanged .connect (lambda : self .setVar ("auto_reconnect_interval" ))
166168 self .targetCombobox .currentTextChanged .connect (self .onTargetChanged )
167169
170+ def changeProtocol (self , protocol , init = False ):
171+ if init or protocol != self .config ["protocol" ]:
172+ if self .isConnected ():
173+ self .openCloseSerial ()
174+ if protocol == "tcp" :
175+ self .modeClientRadioBtn .show ()
176+ self .modeServerRadioBtn .show ()
177+ self .modeLabel .show ()
178+ self .changeMode (self .config ["mode" ], init = True )
179+ else :
180+ self .targetCombobox .show ()
181+ self .targetLabel .show ()
182+ self .porttEdit .show ()
183+ self .portLabel .show ()
184+ self .clientsCombobox .hide ()
185+ self .disconnetClientBtn .hide ()
186+ self .autoReconnect .hide ()
187+ self .autoReconnectIntervalEdit .hide ()
188+ self .autoReconnetLable .hide ()
189+ self .modeClientRadioBtn .hide ()
190+ self .modeServerRadioBtn .hide ()
191+ self .modeLabel .hide ()
192+ self .widget .adjustSize ()
193+ self .config ["protocol" ] = protocol
194+
168195 def changeMode (self , mode , init = False ):
169196 if init or mode != self .config ["mode" ]:
170197 if self .isConnected ():
@@ -189,6 +216,7 @@ def changeMode(self, mode, init=False):
189216 self .autoReconnect .show ()
190217 self .autoReconnectIntervalEdit .show ()
191218 self .autoReconnetLable .show ()
219+ self .widget .adjustSize ()
192220 self .config ["mode" ] = mode
193221
194222 def onTargetChanged (self ):
@@ -312,29 +340,38 @@ def openCloseSerialProcess(self):
312340 self .showSwitchSignal .emit (self .status )
313341 else :
314342 try :
315- if self .config ["mode" ] == "client" :
316- print ("-- connect" )
317- target = self .checkTarget (self .config ["target" ][0 ])
318- if not target :
319- raise Exception (_ ("Target error" + ": " + self .config ["target" ]))
320- print ("-- connect" , target )
321- self .conn = socket .socket ()
322- self .conn .connect (target )
343+ if self .config ["protocol" ] == "tcp" :
344+ if self .config ["mode" ] == "client" :
345+ print ("-- connect" )
346+ target = self .checkTarget (self .config ["target" ][0 ])
347+ if not target :
348+ raise Exception (_ ("Target error" ) + ": " + self .config ["target" ][0 ])
349+ print ("-- connect" , target )
350+ self .conn = socket .socket ()
351+ self .conn .connect (target )
352+ self .status = ConnectionStatus .CONNECTED
353+ print ("-- connect success" )
354+ self .receiveProcess = threading .Thread (target = self .receiveDataProcess , args = (self .conn , ))
355+ self .receiveProcess .setDaemon (True )
356+ self .receiveProcess .start ()
357+ else :
358+ print ("-- server mode, wait client connect" )
359+ self .conn = socket .socket ()
360+ self .conn .setsockopt (socket .SOL_SOCKET ,socket .SO_REUSEADDR ,1 )
361+ self .conn .bind (("0.0.0.0" , self .config ["port" ]))
362+ self .conn .listen (100 )
363+ self .status = ConnectionStatus .CONNECTED
364+ self .waitClentsProcess = threading .Thread (target = self .waitClientsProcess )
365+ self .waitClentsProcess .setDaemon (True )
366+ self .waitClentsProcess .start ()
367+ else :
368+ print ("-- UPD protocol" )
369+ self .conn = socket .socket (type = socket .SOCK_DGRAM )
370+ self .conn .bind (("0.0.0.0" , self .config ["port" ]))
323371 self .status = ConnectionStatus .CONNECTED
324- print ("-- connect success" )
325372 self .receiveProcess = threading .Thread (target = self .receiveDataProcess , args = (self .conn , ))
326373 self .receiveProcess .setDaemon (True )
327374 self .receiveProcess .start ()
328- else :
329- print ("-- server mode, wait client connect" )
330- self .conn = socket .socket ()
331- self .conn .setsockopt (socket .SOL_SOCKET ,socket .SO_REUSEADDR ,1 )
332- self .conn .bind (("0.0.0.0" , self .config ["port" ]))
333- self .conn .listen (100 )
334- self .status = ConnectionStatus .CONNECTED
335- self .waitClentsProcess = threading .Thread (target = self .waitClientsProcess )
336- self .waitClentsProcess .setDaemon (True )
337- self .waitClentsProcess .start ()
338375 self .onConnectionStatus .emit (self .status , "" )
339376 self .showSwitchSignal .emit (self .status )
340377 except Exception as e :
@@ -361,12 +398,10 @@ def checkTarget(self, target):
361398 target = target [:- 1 ]
362399 _host = re .match ('http(.*)://(.*)' , target )
363400 if _host :
364- print (_host )
365401 s , target = _host .groups ()
366402 host = target
367403 _host = re .match ('(.*):(\d*)' , target )
368404 if _host :
369- print (_host )
370405 host , port = _host .groups ()
371406 port = int (port )
372407 if host .endswith ("/" ):
@@ -412,17 +447,19 @@ def waitClientsProcess(self):
412447 t .setDaemon (True )
413448 t .start ()
414449
450+
415451 def receiveDataProcess (self , conn , remote_addr :tuple = None ):
416452 waitingReconnect = False
417453 buffer = b''
418454 t = 0
419455 conn .settimeout (0.1 )
456+ protocolIsTcp = self .config ["protocol" ] == "tcp"
420457 while self .status != ConnectionStatus .CLOSED :
421458 if waitingReconnect :
422459 try :
423460 target = self .checkTarget (self .config ["target" ][0 ])
424461 if not target :
425- raise Exception (_ ("Target error" + ": " + self .config ["target" ]) )
462+ raise Exception (_ ("Target error" ) + ": " + self .config ["target" ][ 0 ] )
426463 conn = socket .socket ()
427464 conn .connect (target )
428465 conn .settimeout (0.1 )
@@ -439,7 +476,10 @@ def receiveDataProcess(self, conn, remote_addr:tuple = None):
439476 try :
440477 # length = max(1, self.conn.in_waiting)
441478 try :
442- data = conn .recv (4096 )
479+ if protocolIsTcp :
480+ data = conn .recv (4096 )
481+ else :
482+ data , target = conn .recvfrom (4096 )
443483 if data == b'' : # closed by peer(peer send FIN, now we can close this connection)
444484 if buffer :
445485 self .onReceived (buffer )
@@ -462,7 +502,7 @@ def receiveDataProcess(self, conn, remote_addr:tuple = None):
462502 print ("-- recv error:" , e , type (e ))
463503 if not self .config ["auto_reconnect" ]:
464504 over = False
465- if self .config ["mode" ] == "server" :
505+ if self .config ["protocol" ] == "tcp" and self . config [ " mode" ] == "server" :
466506 self .onConnectionStatus .emit (ConnectionStatus .CLOSED , _ ("Connection" ) + f' { remote_addr [0 ]} :{ remote_addr [1 ]} ' + _ ("closed!" ))
467507 over = True
468508 else :
@@ -497,14 +537,20 @@ def receiveDataProcess(self, conn, remote_addr:tuple = None):
497537
498538 def send (self , data : bytes ):
499539 if self .conn :
500- if self .config ["mode" ] == "client" :
501- self .conn .sendall (data )
502- else :
503- if not self .serverModeSelectedClient :
504- for addr , conn in self .serverModeClientsConns .items ():
505- conn .sendall (data )
540+ if self .config ["protocol" ] == "tcp" :
541+ if self .config ["mode" ] == "client" :
542+ self .conn .sendall (data )
506543 else :
507- self .serverModeClientsConns [self .serverModeSelectedClient ].sendall (data )
544+ if not self .serverModeSelectedClient :
545+ for addr , conn in self .serverModeClientsConns .items ():
546+ conn .sendall (data )
547+ else :
548+ self .serverModeClientsConns [self .serverModeSelectedClient ].sendall (data )
549+ else :
550+ target = self .checkTarget (self .config ["target" ][0 ])
551+ if not target :
552+ self .hintSignal .emit ("error" , _ ("Target error" ), _ ("Target error" ) + ": " + self .config ["target" ])
553+ self .conn .sendto (data , target )
508554
509555 def isConnected (self ):
510556 return self .status == ConnectionStatus .CONNECTED
0 commit comments