Debian Codegraph @

Decoder.go

raw symbols SCIP
1 /*
2 * Copyright (c) 2014 by Farsight Security, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package framestream
18
19 import (
20 "io"
21 "time"
22 )
23
24 // DecoderOptions specifies configuration for a framestream Decoder.
25 type DecoderOptions struct {
26 // MaxPayloadSize is the largest frame size accepted by the Decoder.
27 //
28 // If the Frame Streams Writer sends a frame in excess of this size,
29 // Decode() will return the error ErrDataFrameTooLarge. The Decoder
30 // attempts to recover from this error, so calls to Decode() after
31 // receiving this error may succeed.
32 MaxPayloadSize uint32
33 // The ContentType expected by the Decoder. May be left unset for no
34 // content negotiation. If the Writer requests a different content type,
35 // NewDecoder() will return ErrContentTypeMismatch.
36 ContentType []byte
37 // If Bidirectional is true, the underlying io.Reader must be an
38 // io.ReadWriter, and the Decoder will engage in a bidirectional
39 // handshake with its peer to establish content type and communicate
40 // shutdown.
41 Bidirectional bool
42 // Timeout gives the timeout for reading the initial handshake messages
43 // from the peer and writing response messages if Bidirectional. It is
44 // only effective for underlying Readers satisfying net.Conn.
45 Timeout time.Duration
46 }
47
48 // A Decoder decodes Frame Streams frames read from an underlying io.Reader.
49 //
50 // It is provided for compatibility. Use Reader instead.
51 type Decoder struct {
52 buf []byte
53 r *Reader
54 }
55
56 // NewDecoder returns a Decoder using the given io.Reader and options.
57 func NewDecoder(r io.Reader, opt *DecoderOptions) (*Decoder, error) {
58 if opt == nil {
59 opt = &DecoderOptions{}
60 }
61 if opt.MaxPayloadSize == 0 {
62 opt.MaxPayloadSize = DEFAULT_MAX_PAYLOAD_SIZE
63 }
64 ropt := &ReaderOptions{
65 Bidirectional: opt.Bidirectional,
66 Timeout: opt.Timeout,
67 }
68 if opt.ContentType != nil {
69 ropt.ContentTypes = append(ropt.ContentTypes, opt.ContentType)
70 }
71 dr, err := NewReader(r, ropt)
72 if err != nil {
73 return nil, err
74 }
75 dec := &Decoder{
76 buf: make([]byte, opt.MaxPayloadSize),
77 r: dr,
78 }
79 return dec, nil
80 }
81
82 // Decode returns the data from a Frame Streams data frame. The slice returned
83 // is valid until the next call to Decode.
84 func (dec *Decoder) Decode() (frameData []byte, err error) {
85 n, err := dec.r.ReadFrame(dec.buf)
86 if err != nil {
87 return nil, err
88 }
89 return dec.buf[:n], nil
90 }