forked from gmallard/stompngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
160 lines (146 loc) · 3.26 KB
/
reader.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
//
// Copyright © 2011-2014 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 (
"strconv"
"strings"
"time"
)
/*
Logical network reader.
Read STOMP frames from the connection, create MessageData
structures from the received data, and push the MessageData to the client.
*/
func (c *Connection) reader() {
//
q := false // Shutdown indicator
for {
f, e := c.readFrame()
if e != nil {
f.Headers = append(f.Headers, "connection_read_error", e.Error())
md := MessageData{Message(f), e}
c.handleReadError(md)
break
}
if f.Command == "" && q {
break
}
m := Message(f)
c.mets.tfr += 1 // Total frames read
// Headers already decoded
c.mets.tbr += m.Size(false) // Total bytes read
d := MessageData{m, e}
if sid, ok := f.Headers.Contains("subscription"); ok {
c.subsLock.Lock()
c.subs[sid] <- d
c.subsLock.Unlock()
} else {
c.input <- d
}
c.log("RECEIVE", m.Command, m.Headers)
select {
case q = <-c.rsd:
default:
}
if q {
break
}
}
c.log("reader shutdown", time.Now())
}
/*
Physical frame reader.
This parses a single STOMP frame from data off of the wire, and
returns a Frame, with a possible error.
Note: this functionality could hang or exhibit other erroneous behavior
if running against a non-compliant STOMP server.
*/
func (c *Connection) readFrame() (f Frame, e error) {
f = Frame{"", Headers{}, NULLBUFF}
// Read f.Command or line ends (maybe heartbeats)
for {
s, e := c.rdr.ReadString('\n')
if s == "" {
return f, e
}
if e != nil {
return f, e
}
if c.hbd != nil {
c.hbd.lr = time.Now().UnixNano() // Latest good read
}
f.Command = s[0 : len(s)-1]
if s != "\n" {
break
}
// c.log("read slash n")
}
// Validate the command
if _, ok := validCmds[f.Command]; !ok {
return f, EINVBCMD
}
// Read f.Headers
for {
s, e := c.rdr.ReadString('\n')
if e != nil {
return f, e
}
if c.hbd != nil {
c.hbd.lr = time.Now().UnixNano() // Latest good read
}
if s == "\n" {
break
}
s = s[0 : len(s)-1]
p := strings.SplitN(s, ":", 2)
if len(p) != 2 {
return f, EUNKHDR
}
if c.Protocol() != SPL_10 {
p[0] = decode(p[0])
p[1] = decode(p[1])
}
f.Headers = append(f.Headers, p[0], p[1])
}
//
e = checkHeaders(f.Headers, c.Protocol())
if e != nil {
return f, e
}
// Read f.Body
if v, ok := f.Headers.Contains("content-length"); ok {
l, e := strconv.Atoi(strings.TrimSpace(v))
if e != nil {
return f, e
}
if l == 0 {
f.Body, e = readUntilNul(c.rdr)
} else {
f.Body, e = readBody(c.rdr, l)
}
} else {
// content-length not present
f.Body, e = readUntilNul(c.rdr)
}
if e != nil {
return f, e
}
if c.hbd != nil {
c.hbd.lr = time.Now().UnixNano() // Latest good read
}
//
return f, e
}