refactor protocol parsing and dispatcher