1// Copyright 2018 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5package osp 6 7// TODO(jophba): 8// - Read and write size prefixes 9 10import ( 11 "fmt" 12 "github.com/ugorji/go/codec" 13 "io" 14) 15 16const PresentationStartRequestTypeKey = 104 17 18// AKA presentation-start-request 19type PresentationStartRequest struct { 20 // Must be public to allow (de)serialization 21 RequestID uint64 `codec:"0"` 22 PresentationID string `codec:"1"` 23 URL string `codec:"2"` 24} 25 26// Read with type/size prefix 27func ReadMessage(r io.Reader) (interface{}, error) { 28 typeKey, err := ReadVaruint(r) 29 if err != nil { 30 return nil, err 31 } 32 33 switch typeKey { 34 case PresentationStartRequestTypeKey: 35 var msg PresentationStartRequest 36 h := &codec.CborHandle{} 37 dec := codec.NewDecoder(r, h) 38 err = dec.Decode(&msg) 39 if err != nil { 40 return nil, err 41 } 42 return msg, nil 43 default: 44 return nil, fmt.Errorf("Unknown type key: %d", typeKey) 45 } 46} 47 48// Write with type/size prefix 49func WriteMessage(msg interface{}, w io.Writer) error { 50 var typeKey uint64 51 switch msg.(type) { 52 case PresentationStartRequest: 53 typeKey = uint64(PresentationStartRequestTypeKey) 54 default: 55 return fmt.Errorf("Unknown message type: %v", msg) 56 } 57 58 err := WriteVaruint(typeKey, w) 59 if err != nil { 60 return err 61 } 62 63 h := &codec.CborHandle{} 64 enc := codec.NewEncoder(w, h) 65 return enc.Encode(msg) 66} 67