forked from byte-mug/fastnntp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
224 lines (201 loc) · 6.36 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
/*
MIT License
Copyright (c) 2017 Simon Schmidt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package fastnntp
import "fmt"
import "io"
import "sync"
//var pool_dotBuffer = sync.Pool{ New : func() interface{} { return make([]byte,5) }}
/*
A io.Writer-Wrapper, that enables dot-line ended content to be written.
After the final ".\r\n", any further content is discarded. The final ".\r\n" is
addedd, if the content didn't contain any.
*/
type DotWriter struct{
w io.Writer
state uint16
end bool
}
var pool_DotWriter = sync.Pool{ New : func() interface{} { return new(DotWriter) }}
func AcquireDotWriter() *DotWriter {
return pool_DotWriter.Get().(*DotWriter)
}
func (w *DotWriter) Release() {
// Remove the reference to it's writer to enable it to be GCed.
w.w = nil
pool_DotWriter.Put(w)
}
func (w *DotWriter) Reset(wr io.Writer) {
*w = DotWriter{ w:wr, state : nlDotNl_start, end : false }
}
func (w *DotWriter) Write(buf []byte) (int, error) {
// Do not write any further.
if w.end { return len(buf),nil }
// Put state into local variable. This enhances performance.
state := w.state
for i,b := range buf {
state = nlDotNl_transition(state,b)
if state==nlDotNl_end {
w.end = true
j := i+1
n,e := w.w.Write(buf[:j])
if n<j {
if e!=nil { e=io.EOF }
return n,e
}
return len(buf),e
}
}
// Put state back.
w.state = state
// Fully approved!
return w.w.Write(buf)
}
var dotCRLF = []byte(".\r\n")
var crlfDotCRLF = []byte("\r\n.\r\n")
func (w *DotWriter) Close() (e error) {
// short-cut:
if w.end { return }
if w.state == 0x0100 {
_,e = w.Write(dotCRLF)
}else{
_,e = w.Write(crlfDotCRLF)
}
return e
}
var pool_Overview = sync.Pool{ New : func() interface{} { return new(Overview) }}
type Overview struct{
buffer []byte
writer io.Writer
mode int
}
func (ov *Overview) reset(buffer []byte,writer io.Writer,mode int) {
ov.buffer = buffer
ov.writer = writer
ov.mode = mode
}
func (ov *Overview) release(){
ov.writer = nil
ov.buffer = nil
ov.mode = 0
pool_Overview.Put(ov)
}
func (ov *Overview) WriteEntry(num int64,subject, from, date, msgId, refs []byte, lng, lines int64) error {
out := ov.buffer
switch ov.mode {
case 0: // XOVER:
out = append(AppendUint(out,num),'\t')
out = append(append(out,subject...),'\t')
out = append(append(out,from...),'\t')
out = append(append(out,date...),'\t')
out = append(append(out,msgId...),'\t')
out = append(append(out,refs...),'\t')
out = append(AppendUint(out,lng),'\t')
out = append(AppendUint(out,lines),crlf...)
// XHDR:
case 1: out = append(append(out,subject...),crlf...)
case 2: out = append(append(out,from...),crlf...)
case 3: out = append(append(out,date...),crlf...)
case 4: out = append(append(out,msgId...),crlf...)
case 5: out = append(append(out,refs...),crlf...)
case 6: out = append(AppendUint(out,lng),crlf...)
case 7: out = append(AppendUint(out,lines),crlf...)
default:
panic(fmt.Sprint("invalid mode: ",ov.mode))
}
_,err := ov.writer.Write(out)
return err
}
type IOverview interface{
WriteEntry(num int64,subject, from, date, msgId, refs []byte, lng, lines int64) error
}
type ListActiveMode int
const(
LAM_Full = ListActiveMode(iota)
LAM_Active
LAM_Newsgroups
)
var pool_ListActive = sync.Pool{ New : func() interface{} { return new(ListActive) }}
type ListActive struct{
buffer []byte
writer io.Writer
mode ListActiveMode
wm *WildMat
}
func (ov *ListActive) match(group []byte) bool {
if ov.wm==nil { return true }
return ov.wm.Match(group)
}
func (ov *ListActive) reset(buffer []byte,writer io.Writer,mode ListActiveMode,wm *WildMat) {
ov.buffer = buffer
ov.writer = writer
ov.mode = mode
ov.wm = wm
}
func (ov *ListActive) release(){
ov.writer = nil
ov.buffer = nil
ov.mode = LAM_Active
ov.wm = nil
pool_ListActive.Put(ov)
}
func (ov *ListActive) GetListActiveMode() ListActiveMode { return ov.mode }
func (ov *ListActive) WriteActive(group []byte, high, low int64,status byte) error {
if ov.mode != LAM_Active { panic(fmt.Sprint("invalid mode: ",ov.mode)) }
/* Skip unwanted groups. */
if !ov.match(group) { return nil }
out := ov.buffer
out = append(out,group...)
out = append(out,' ')
out = AppendUint(out,high)
out = append(out,' ')
out = AppendUint(out,low)
out = append(out,' ',status,'\r','\n')
_,err := ov.writer.Write(out)
return err
}
func (ov *ListActive) WriteNewsgroups(group []byte,description []byte) error {
if ov.mode != LAM_Newsgroups { panic(fmt.Sprint("invalid mode: ",ov.mode)) }
/* Skip unwanted groups. */
if !ov.match(group) { return nil }
out := ov.buffer
out = append(out,group...)
out = append(out,'\t')
out = append(out,description...)
out = append(out,crlf...)
_,err := ov.writer.Write(out)
return err
}
func (ov *ListActive) WriteFullInfo(group []byte, high, low int64,status byte,description []byte) error {
switch ov.mode {
case LAM_Active:
return ov.WriteActive(group,high,low,status)
case LAM_Newsgroups:
return ov.WriteNewsgroups(group,description)
}
panic(fmt.Sprint("invalid mode: ",ov.mode))
}
type IListActive interface{
GetListActiveMode() ListActiveMode
// This function may be used if, and only if GetListActiveMode() returns LAM_Active
WriteActive(group []byte, high, low int64,status byte) error
// This function may be used if, and only if GetListActiveMode() returns LAM_Newsgroups
WriteNewsgroups(group []byte,description []byte) error
WriteFullInfo(group []byte, high, low int64,status byte,description []byte) error
}