forked from gmallard/stompngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
285 lines (267 loc) · 6.56 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// Copyright © 2011-2018 Guy M. Allard
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package stompngo
import (
"bufio"
"bytes"
"net"
// "bytes"
"strconv"
"time"
)
/*
Write data to logical network writer. Writer will take care of the output wire data.
If the underlying connection goes bad and writer give up working, the closed ssdc chan
will make sure write action aware that happens.
*/
func (c *Connection) writeWireData(wd wiredata) error {
select {
case c.output <- wd:
case <-c.ssdc:
return ECONBAD
}
return nil
}
/*
Logical network writer. Read wiredata structures from the communication
channel, and put the frame on the wire.
*/
func (c *Connection) writer() {
writerLoop:
for {
select {
case d := <-c.output:
c.log("WTR_WIREWRITE start")
c.wireWrite(d)
logLock.Lock()
if c.logger != nil {
c.logx("WTR_WIREWRITE COMPLETE", d.frame.Command, d.frame.Headers,
HexData(d.frame.Body))
}
logLock.Unlock()
if d.frame.Command == DISCONNECT {
break writerLoop // we are done with this connection
}
case _ = <-c.ssdc:
c.log("WTR_WIREWRITE shutdown S received")
break writerLoop
case _ = <-c.wtrsdc:
c.log("WTR_WIREWRITE shutdown W received")
break writerLoop
}
} // of for
//
c.connLock.Lock()
c.connected = false
c.connLock.Unlock()
c.sysAbort()
c.log("WTR_SHUTDOWN", time.Now())
}
/*
Connection logical write.
*/
func (c *Connection) wireWrite(d wiredata) {
f := &d.frame
// fmt.Printf("WWD01 f:[%v]\n", f)
switch f.Command {
case "\n": // HeartBeat frame
if c.dld.wde && c.dld.wds {
_ = c.netconn.SetWriteDeadline(time.Now().Add(c.dld.wdld))
}
_, e := c.wtr.WriteString(f.Command)
if e != nil {
if e.(net.Error).Timeout() {
if c.dld.dns {
c.dld.dlnotify(e, true)
}
}
d.errchan <- e
return
}
default: // Other frames
if e := f.writeFrame(c.wtr, c); e != nil {
d.errchan <- e
return
}
if e := c.wtr.Flush(); e != nil {
d.errchan <- e
return
}
}
if e := c.wtr.Flush(); e != nil {
d.errchan <- e
return
}
//
if c.hbd != nil {
c.hbd.sdl.Lock()
c.hbd.ls = time.Now().UnixNano() // Latest good send
c.hbd.sdl.Unlock()
}
c.mets.tfw++ // Frame written count
c.mets.tbw += f.Size(false) // Bytes written count
//
d.errchan <- nil
return
}
/*
Physical frame write to the wire.
*/
func (f *Frame) writeFrame(w *bufio.Writer, c *Connection) error {
var sctok bool
// Content type. Always add it if the client does not suppress and does not
// supply it.
_, sctok = f.Headers.Contains(HK_SUPPRESS_CT)
if !sctok {
if _, ctok := f.Headers.Contains(HK_CONTENT_TYPE); !ctok {
f.Headers = append(f.Headers, HK_CONTENT_TYPE,
DFLT_CONTENT_TYPE)
}
}
var sclok bool
// Content length - Always add it if client does not suppress it and
// does not supply it.
_, sclok = f.Headers.Contains(HK_SUPPRESS_CL)
if !sclok {
if _, clok := f.Headers.Contains(HK_CONTENT_LENGTH); !clok {
f.Headers = append(f.Headers, HK_CONTENT_LENGTH, strconv.Itoa(len(f.Body)))
}
}
// Encode the headers if needed
if c.Protocol() > SPL_10 && f.Command != CONNECT {
for i := 0; i < len(f.Headers); i += 2 {
f.Headers[i] = encode(f.Headers[i])
f.Headers[i+1] = encode(f.Headers[i+1])
}
}
if sclok {
nz := bytes.IndexByte(f.Body, 0)
// fmt.Printf("WDBG41 ok:%v\n", nz)
if nz == 0 {
f.Body = []byte{}
// fmt.Printf("WDBG42 body:%v bodystring: %v\n", f.Body, string(f.Body))
} else if nz > 0 {
f.Body = f.Body[0:nz]
// fmt.Printf("WDBG43 body:%v bodystring: %v\n", f.Body, string(f.Body))
}
}
if c.dld.wde && c.dld.wds {
_ = c.netconn.SetWriteDeadline(time.Now().Add(c.dld.wdld))
}
// Writes start
// Write the frame Command
_, e := w.WriteString(f.Command + "\n")
if c.checkWriteError(e) != nil {
return e
}
// fmt.Println("WRCMD", f.Command)
// Write the frame Headers
for i := 0; i < len(f.Headers); i += 2 {
if c.dld.wde && c.dld.wds {
_ = c.netconn.SetWriteDeadline(time.Now().Add(c.dld.wdld))
}
_, e := w.WriteString(f.Headers[i] + ":" + f.Headers[i+1] + "\n")
if c.checkWriteError(e) != nil {
return e
}
// fmt.Println("WRHDR", f.Headers[i]+":"+f.Headers[i+1]+"\n")
}
// Write the last Header LF
if c.dld.wde && c.dld.wds {
_ = c.netconn.SetWriteDeadline(time.Now().Add(c.dld.wdld))
}
e = w.WriteByte('\n')
if c.checkWriteError(e) != nil {
return e
}
// fmt.Printf("WDBG40 ok:%v\n", sclok)
// Write the body
if len(f.Body) != 0 { // Foolish to write 0 length data
// fmt.Println("WRBDY", f.Body)
e := c.writeBody(f)
if c.checkWriteError(e) != nil {
return e
}
}
if c.dld.wde && c.dld.wds {
_ = c.netconn.SetWriteDeadline(time.Now().Add(c.dld.wdld))
}
e = w.WriteByte(0)
if c.checkWriteError(e) != nil {
return e
}
// End of write loop - set no deadline
if c.dld.wde {
_ = c.netconn.SetWriteDeadline(c.dld.t0)
}
return nil
}
func (c *Connection) checkWriteError(e error) error {
if e == nil {
return e
}
ne, ok := e.(net.Error)
if !ok {
return e
}
if ne.Timeout() {
if c.dld.dns {
c.log("invoking write deadline callback 1")
c.dld.dlnotify(e, true)
}
}
return e
}
func (c *Connection) writeBody(f *Frame) error {
// fmt.Printf("WDBG99 body:%v bodystring: %v\n", f.Body, string(f.Body))
var n = 0
var e error
for {
if c.dld.wde && c.dld.wds {
_ = c.netconn.SetWriteDeadline(time.Now().Add(c.dld.wdld))
}
n, e = c.wtr.Write(f.Body)
if n == len(f.Body) {
return e
}
c.log("SHORT WRITE", n, len(f.Body))
if n == 0 { // Zero bytes would mean something is seriously wrong.
return e
}
if !c.dld.rfsw {
return e
}
if c.dld.wde && c.dld.wds && c.dld.dns && isErrorTimeout(e) {
c.log("invoking write deadline callback 2")
c.dld.dlnotify(e, true)
}
// *Any* error from a bufio.Writer is *not* recoverable. See code in
// bufio.go to understand this. We get a new writer here, to clear any
// error condition.
c.wtr = bufio.NewWriter(c.netconn) // Create new writer
f.Body = f.Body[n:]
}
}
func isErrorTimeout(e error) bool {
if e == nil {
return false
}
_, ok := e.(net.Error)
if !ok {
return false
}
return true
}