Debian Codegraph @

Writer.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 "bufio"
21 "encoding/binary"
22 "io"
23 "time"
24 )
25
26 type WriterOptions struct {
27 // The ContentTypes available to be written to the Writer. May be
28 // left unset for no content negotiation. If the Reader requests a
29 // disjoint set of content types, NewEncoder() will return
30 // ErrContentTypeMismatch.
31 ContentTypes [][]byte
32 // If Bidirectional is true, the underlying io.Writer must be an
33 // io.ReadWriter, and the Writer will engage in a bidirectional
34 // handshake with its peer to establish content type and communicate
35 // shutdown.
36 Bidirectional bool
37 // Timeout gives the timeout for writing both control and data frames,
38 // and for reading responses to control frames sent. It is only
39 // effective for underlying Writers satisfying net.Conn.
40 Timeout time.Duration
41 }
42
43 // A Writer writes data frames to a Frame Streams file or connection.
44 type Writer struct {
45 contentType []byte
46 w *bufio.Writer
47 r *bufio.Reader
48 opt WriterOptions
49 buf []byte
50 }
51
52 // NewWriter returns a Frame Streams Writer using the given io.Writer and options.
53 func NewWriter(w io.Writer, opt *WriterOptions) (writer *Writer, err error) {
54 if opt == nil {
55 opt = &WriterOptions{}
56 }
57 w = timeoutWriter(w, opt)
58 writer = &Writer{
59 w: bufio.NewWriter(w),
60 opt: *opt,
61 }
62
63 if len(opt.ContentTypes) > 0 {
64 writer.contentType = opt.ContentTypes[0]
65 }
66
67 if opt.Bidirectional {
68 r, ok := w.(io.Reader)
69 if !ok {
70 return nil, ErrType
71 }
72 writer.r = bufio.NewReader(r)
73 ready := ControlReady
74 ready.SetContentTypes(opt.ContentTypes)
75 if err = ready.EncodeFlush(writer.w); err != nil {
76 return
77 }
78
79 var accept ControlFrame
80 if err = accept.DecodeTypeEscape(writer.r, CONTROL_ACCEPT); err != nil {
81 return
82 }
83
84 if t, ok := accept.ChooseContentType(opt.ContentTypes); ok {
85 writer.contentType = t
86 } else {
87 return nil, ErrContentTypeMismatch
88 }
89 }
90
91 // Write the start control frame.
92 start := ControlStart
93 start.SetContentType(writer.contentType)
94 err = start.EncodeFlush(writer.w)
95 if err != nil {
96 return
97 }
98
99 return
100 }
101
102 // ContentType returns the content type negotiated with Reader.
103 func (w *Writer) ContentType() []byte {
104 return w.contentType
105 }
106
107 // Close shuts down the Frame Streams stream by writing a CONTROL_STOP message.
108 // If the Writer is Bidirectional, Close will wait for an acknowledgement
109 // (CONTROL_FINISH) from its peer.
110 func (w *Writer) Close() (err error) {
111 err = ControlStop.EncodeFlush(w.w)
112 if err != nil || !w.opt.Bidirectional {
113 return
114 }
115
116 var finish ControlFrame
117 return finish.DecodeTypeEscape(w.r, CONTROL_FINISH)
118 }
119
120 // WriteFrame writes the given frame to the underlying io.Writer with Frame Streams
121 // framing.
122 func (w *Writer) WriteFrame(frame []byte) (n int, err error) {
123 err = binary.Write(w.w, binary.BigEndian, uint32(len(frame)))
124 if err != nil {
125 return
126 }
127 return w.w.Write(frame)
128 }
129
130 // Flush ensures that any buffered data frames are written to the underlying
131 // io.Writer.
132 func (w *Writer) Flush() error {
133 return w.w.Flush()
134 }