Debian Codegraph @

Encoder.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 // EncoderOptions specifies configuration for an Encoder
25 type EncoderOptions struct {
26 // The ContentType of the data sent by the Encoder. May be left unset
27 // for no content negotiation. If the Reader requests a different
28 // content type, NewEncoder() will return ErrContentTypeMismatch.
29 ContentType []byte
30 // If Bidirectional is true, the underlying io.Writer must be an
31 // io.ReadWriter, and the Encoder will engage in a bidirectional
32 // handshake with its peer to establish content type and communicate
33 // shutdown.
34 Bidirectional bool
35 // Timeout gives the timeout for writing both control and data frames,
36 // and for reading responses to control frames sent. It is only
37 // effective for underlying Writers satisfying net.Conn.
38 Timeout time.Duration
39 }
40
41 // An Encoder sends data frames over a FrameStream Writer.
42 //
43 // Encoder is provided for compatibility, use Writer instead.
44 type Encoder struct {
45 *Writer
46 }
47
48 // NewEncoder creates an Encoder writing to the given io.Writer with the given
49 // EncoderOptions.
50 func NewEncoder(w io.Writer, opt *EncoderOptions) (enc *Encoder, err error) {
51 if opt == nil {
52 opt = &EncoderOptions{}
53 }
54 wopt := &WriterOptions{
55 Bidirectional: opt.Bidirectional,
56 Timeout: opt.Timeout,
57 }
58 if opt.ContentType != nil {
59 wopt.ContentTypes = append(wopt.ContentTypes, opt.ContentType)
60 }
61 writer, err := NewWriter(w, wopt)
62 if err != nil {
63 return nil, err
64 }
65 return &Encoder{Writer: writer}, nil
66 }
67
68 func (e *Encoder) Write(frame []byte) (int, error) {
69 return e.WriteFrame(frame)
70 }