-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.go
176 lines (141 loc) · 3.65 KB
/
stream.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
// Copyright (c) 2010 William R. Conant, WillConant.com
// Use of this source code is governed by the MIT licence:
// http://www.opensource.org/licenses/mit-license.php
package msglite
import (
"strconv"
"os"
"io"
"bufio"
"strings"
)
type CommandStream struct {
reader *bufio.Reader
writer io.WriteCloser
closed bool
}
func (stream *CommandStream) ReadCommand() ([]string, os.Error) {
line, err := stream.reader.ReadString('\n')
if err != nil {
return nil, err
}
return strings.Fields(strings.TrimSpace(line)), nil
}
func (stream *CommandStream) ReadBody(bodyLen int) (string, os.Error) {
bodyBuf := make([]byte, bodyLen + 2)
_, err := io.ReadFull(stream.reader, bodyBuf)
if err != nil {
return "", err
}
if bodyBuf[bodyLen] != '\r' || bodyBuf[bodyLen+1] != '\n' {
return "", os.NewError("body must be followed by \\r\\n")
}
return string(bodyBuf[0:bodyLen]), nil
}
func (stream *CommandStream) ReadMessage() (*Message, os.Error) {
inCommand, err := stream.ReadCommand()
if inCommand[0] == timeoutCommandStr {
return nil, nil
} else if inCommand[0] != messageCommandStr {
return nil, os.NewError("invalid message from server")
}
params := inCommand[1:]
if len(params) < 3 || len(params) > 4 {
return nil, os.NewError("invalid message from server")
}
bodyLen, err := strconv.Atoi(params[0])
if err != nil {
return nil, os.NewError("invalid message from server")
}
timeout, err := strconv.Atoi64(params[1])
if err != nil {
return nil, os.NewError("invalid message from server")
}
toAddr := params[2]
var replyAddr string
if len(params) == 4 {
replyAddr = params[3]
}
var body string
if bodyLen > 0 {
body, err = stream.ReadBody(bodyLen)
if err != nil {
return nil, err
}
}
return &Message{toAddr, replyAddr, timeout, body, 0}, nil
}
func (stream *CommandStream) WriteCommand(command []string) os.Error {
_, err := io.WriteString(stream.writer, strings.Join(command, " ") + "\r\n")
return err
}
func (stream *CommandStream) WriteMessage(msg *Message) os.Error {
if msg == nil {
err := stream.WriteCommand([]string{timeoutCommandStr})
return err
}
command := make([]string, 5)
command[0] = messageCommandStr
command[1] = strconv.Itoa(len(msg.Body))
command[2] = strconv.Itoa64(msg.TimeoutSeconds)
command[3] = msg.ToAddress
if msg.ReplyAddress != "" {
command[4] = msg.ReplyAddress
} else {
command = command[0:4]
}
err := stream.WriteCommand(command)
if err != nil {
return err
}
if len(msg.Body) > 0 {
_, err = io.WriteString(stream.writer, msg.Body)
if err != nil {
return err
}
_, err = io.WriteString(stream.writer, "\r\n")
if err != nil {
return err
}
}
return nil
}
func (stream *CommandStream) WriteQuery(body string, timeoutSeconds int64, toAddress string) os.Error {
command := make([]string, 4)
command[0] = queryCommandStr
command[1] = strconv.Itoa(len(body))
command[2] = strconv.Itoa64(timeoutSeconds)
command[3] = toAddress
err := stream.WriteCommand(command)
if err != nil {
return err
}
if len(body) > 0 {
_, err = io.WriteString(stream.writer, body)
if err != nil {
return err
}
_, err = io.WriteString(stream.writer, "\r\n")
if err != nil {
return err
}
}
return nil
}
func (stream *CommandStream) Close() os.Error {
stream.closed = true
return stream.writer.Close()
}
func (stream *CommandStream) WriteError(err os.Error) {
stream.WriteCommand([]string{errorCommandStr, err.String()})
stream.Close()
}
func (stream *CommandStream) WriteQuit() os.Error {
err := stream.WriteCommand([]string{quitCommandStr})
if err != nil {
// we still close the stream
stream.Close()
return err
}
return stream.Close()
}