forked from gmallard/stompngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
134 lines (124 loc) · 2.67 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
//
// Copyright © 2011-2012 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 (
"io"
"strconv"
"strings"
"time"
)
// Logical network reader. Read frames from the connection, create MessageData
// structures from the receivedd data, and push the MessageData to the client.
func (c *Connection) reader() {
//
q := false
gf := func() {
q = <-c.rsd
}
go gf()
for {
f, e := c.readFrame()
if e != nil {
if e == io.EOF {
break
}
c.input <- MessageData{Message{"", Headers{}, NULLBUFF}, e}
break
}
if f.Command == "" && q {
break
}
d := MessageData{Message{f.Command, f.Headers, f.Body}, e}
if sid, ok := f.Headers.Contains("subscription"); ok {
c.subsLock.Lock()
c.subs[sid] <- d
c.subsLock.Unlock()
} else {
c.input <- d
}
if q {
break
}
}
}
// Physical frame reader. This slurps a single frame off of the wire.
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")
}
// 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)
k := p[0]
v := p[1]
if c.protocol != SPL_10 && f.Command != CONNECTED {
k = decode(k)
v = decode(v)
}
f.Headers = append(f.Headers, k, v)
}
// 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
}