Extract protocol encoding/decoding

Updates #2