ReadControlMessage reads a control message from the reader
(r io.Reader)
| 47 | |
| 48 | // ReadControlMessage reads a control message from the reader |
| 49 | func ReadControlMessage(r io.Reader) (opcode byte, nonce uint64, err error) { |
| 50 | opcode, err = readByte(r) |
| 51 | if err != nil { |
| 52 | return 0, 0, fmt.Errorf("failed to read opcode: %w", err) |
| 53 | } |
| 54 | |
| 55 | switch opcode { |
| 56 | case OpPing, OpPong: |
| 57 | nonce, err = readUint64(r) |
| 58 | if err != nil { |
| 59 | return opcode, 0, fmt.Errorf("failed to read nonce: %w", err) |
| 60 | } |
| 61 | case OpShutdown: |
| 62 | // No additional data for shutdown |
| 63 | default: |
| 64 | return opcode, 0, fmt.Errorf("unknown opcode: %02x", opcode) |
| 65 | } |
| 66 | |
| 67 | return opcode, nonce, nil |
| 68 | } |
| 69 | |
| 70 | // Helper functions for reading/writing |
| 71 | func writeByte(w io.Writer, b byte) error { |