-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_type.go
214 lines (158 loc) · 3.81 KB
/
io_type.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
package io
import (
"bufio"
"context"
"github.com/injoyai/base/bytes"
"io"
"time"
)
type Bytes = Message
type Message = bytes.Entity
type TimeoutWriter interface {
WriteWithTimeout(p []byte, timeout time.Duration) (int, error)
}
type AnyWriter interface {
WriteAny(any interface{}) (int, error)
}
type AnyWriterClosed interface {
AnyWriter
Closed
}
type Runner interface {
Run() error
Running() bool
}
// Closed 是否已关闭
type Closed interface {
Closed() bool
}
type Closer2 interface {
Closer
Closed
}
// Publisher 发布者
type Publisher interface {
Publish(topic string, p []byte) error
}
type Listener interface {
Closer
Accept() (ReadWriteCloser, string, error)
Addr() string
}
// ListenFunc 监听函数
type ListenFunc func() (Listener, error)
// DialFunc 连接函数
type DialFunc func(ctx context.Context) (ReadWriteCloser, string, error)
// OptionClient 客户端选项
type OptionClient func(c *Client)
// OptionServer 服务端选项
type OptionServer func(s *Server)
//=================================Key=================================
// ReadFunc 读取函数
type ReadFunc func(p []byte) (int, error)
func (this ReadFunc) Read(p []byte) (int, error) { return this(p) }
// WriteFunc 写入函数
type WriteFunc func(p []byte) (int, error)
func (this WriteFunc) Write(p []byte) (int, error) { return this(p) }
// CloseFunc 关闭函数
type CloseFunc func() error
func (this CloseFunc) Close() error { return this() }
type Key string
func (this *Key) GetKey() string { return string(*this) }
func (this *Key) SetKey(key string) { *this = Key(key) }
//=================
// MustChan chan []byte 实现 io.Writer,必须等到写入成功为止
type MustChan chan []byte
func (this MustChan) Write(p []byte) (int, error) {
this <- p
return len(p), nil
}
// TryChan chan []byte 实现 io.Writer,尝试写入,不管是否成功
type TryChan chan []byte
func (this TryChan) Write(p []byte) (int, error) {
select {
case this <- p:
return len(p), nil
default:
return 0, nil
}
}
//====================
// Count 统计写入字节数量
type Count struct {
io.Writer
count int64
}
func (this *Count) Count() int64 {
return this.count
}
func (this *Count) Write(p []byte) (int, error) {
n, err := this.Writer.Write(p)
this.count += int64(n)
return n, err
}
/*
*/
type mReader struct {
buf *bufio.Reader
read func(buf *bufio.Reader) ([]byte, error)
}
func (this *mReader) ReadMessage() ([]byte, error) {
return this.read(this.buf)
}
type aReader struct {
buf *bufio.Reader
read func(buf *bufio.Reader) ([]byte, error)
}
func (this *aReader) ReadAck() (Acker, error) {
bs, err := this.read(this.buf)
return Ack(bs), err
}
//=============================
// MultiCloser 多个关闭合并
func MultiCloser(closer ...Closer) Closer {
return &multiCloser{closer: closer}
}
type multiCloser struct {
closer []Closer
}
func (this *multiCloser) Close() (err error) {
for _, v := range this.closer {
if er := v.Close(); er != nil {
err = er
}
}
return
}
//=============================
// PublisherToWriter Publisher to Writer
func PublisherToWriter(p Publisher, topic string) Writer {
return &publishToWriter{topic: topic, Publisher: p}
}
type publishToWriter struct {
topic string
Publisher
}
func (this *publishToWriter) Write(p []byte) (int, error) {
err := this.Publisher.Publish(this.topic, p)
return len(p), err
}
//=============================
func NewReadWriter(r Reader, w Writer) ReadWriteCloser {
return &readWriter{Reader: r, Writer: w}
}
type readWriter struct {
Reader
Writer
}
func (this *readWriter) Close() error { return nil }
//=============================
type Plan struct {
Index int //操作次数
Current int64 //当前数量
Total int64 //总数量
Bytes []byte //字节内容
}
type BytesReader interface {
ReadBytes() ([]byte, error)
}