Main server loop. Waits for a client connection, then processes commands in a loop. Handles all supported commands: set filename, configure stream, enable/disable stream, read/write audio, close server. Sends responses or audio data as appropriate.
(self)
| 563 | logger.error(f"_writeAudio: error writing audio data: {e}") |
| 564 | |
| 565 | def run(self): |
| 566 | """ |
| 567 | Main server loop. |
| 568 | |
| 569 | Waits for a client connection, then processes commands in a loop. |
| 570 | Handles all supported commands: set filename, configure stream, enable/disable stream, read/write audio, close server. |
| 571 | Sends responses or audio data as appropriate. |
| 572 | """ |
| 573 | logger.info("Audio server started") |
| 574 | |
| 575 | try: |
| 576 | conn = self.listener.accept() |
| 577 | logger.info(f'Connection accepted {self.listener.address}') |
| 578 | except Exception: |
| 579 | logger.error("Connection not accepted") |
| 580 | return |
| 581 | |
| 582 | while True: |
| 583 | try: |
| 584 | recv = conn.recv() |
| 585 | except EOFError: |
| 586 | return |
| 587 | |
| 588 | cmd = recv[0] # Command |
| 589 | payload = recv[1:] # Payload |
| 590 | |
| 591 | if cmd == self.SET_MODE: |
| 592 | current_mode = self._setMode(payload[0]) |
| 593 | conn.send(current_mode) |
| 594 | |
| 595 | elif cmd == self.SET_DEVICE: |
| 596 | device_valid = self._setDevice(payload[0]) |
| 597 | conn.send(device_valid) |
| 598 | |
| 599 | elif cmd == self.SET_FILENAME: |
| 600 | filename_valid = self._setFilename(payload[0], payload[1]) |
| 601 | conn.send(filename_valid) |
| 602 | |
| 603 | elif cmd == self.STREAM_CONFIGURE: |
| 604 | configuration_valid = self._configureStream(payload[0], payload[1], payload[2]) |
| 605 | conn.send(configuration_valid) |
| 606 | |
| 607 | elif cmd == self.STREAM_ENABLE: |
| 608 | self._enableStream() |
| 609 | conn.send(self.active) |
| 610 | |
| 611 | elif cmd == self.STREAM_DISABLE: |
| 612 | self._disableStream() |
| 613 | conn.send(self.active) |
| 614 | |
| 615 | elif cmd == self.AUDIO_READ: |
| 616 | size = payload[0] |
| 617 | audio_data = self._readAudio(size) |
| 618 | conn.send_bytes(audio_data) |
| 619 | conn.send(self.eos) |
| 620 | |
| 621 | elif cmd == self.AUDIO_WRITE: |
| 622 | audio_data = conn.recv_bytes() |
no test coverage detected