This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload.go
295 lines (258 loc) · 6.45 KB
/
upload.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package dctk
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/aler9/go-dc/adc"
"github.com/aler9/go-dc/nmdc"
"github.com/aler9/dctk/pkg/log"
"github.com/aler9/dctk/pkg/protoadc"
"github.com/aler9/dctk/pkg/tiger"
)
var errorNoSlots = fmt.Errorf("no slots available")
type upload struct {
client *Client
terminateRequested bool
state string
pconn *peerConn
reader io.ReadCloser
isCompressed bool
query string
start uint64
length uint64
offset uint64
lastPrintTime time.Time
}
func (*upload) isTransfer() {}
func newUpload(client *Client,
pconn *peerConn,
reqQuery string,
reqStart uint64,
reqLength int64,
reqCompressed bool,
) bool {
u := &upload{
client: client,
state: "processing",
pconn: pconn,
query: reqQuery,
start: reqStart,
isCompressed: (!client.conf.PeerDisableCompression && reqCompressed),
}
log.Log(client.conf.LogLevel, log.LevelInfo, "[upload] [%s] request %s (s=%d l=%d)",
pconn.peer.Nick, dcReadableQuery(u.query), u.start, reqLength)
err := func() error {
// check available slots
if u.client.uploadSlotAvail <= 0 {
return errorNoSlots
}
// upload is file list
if u.query == "file files.xml.bz2" {
if u.start != 0 || reqLength != -1 {
return fmt.Errorf("filelist seeking is not supported")
}
u.reader = io.NopCloser(bytes.NewReader(u.client.fileList))
u.length = uint64(len(u.client.fileList))
return nil
}
if !strings.HasPrefix(u.query, "file TTH/") && !strings.HasPrefix(u.query, "tthl TTH/") {
return fmt.Errorf("invalid query")
}
// skip "file TTH/" or "tthl TTH/"
tthString := u.query[9:]
// upload is file by TTH or its tthl
tth, err := tiger.HashFromBase32(tthString)
if err != nil {
return err
}
sfile := func() (ret *shareFile) {
var scanDir func(dir *shareDirectory) bool
scanDir = func(dir *shareDirectory) bool {
for _, file := range dir.files {
if file.tth == tth {
ret = file
return true
}
}
for _, sdir := range dir.dirs {
if scanDir(sdir) {
return true
}
}
return false
}
for _, dir := range u.client.shareTree {
if scanDir(dir) {
break
}
}
return
}()
if sfile == nil {
return fmt.Errorf("file does not exists")
}
// upload is file tthl
if strings.HasPrefix(u.query, "tthl") {
if u.start != 0 || reqLength != -1 {
return fmt.Errorf("tthl seeking is not supported")
}
buf := bytes.NewBuffer(nil)
for _, leaf := range sfile.tthl {
buf.Write(leaf[:])
}
u.reader = io.NopCloser(buf)
u.length = uint64(buf.Len())
return nil
}
// open file
var f *os.File
f, err = os.Open(sfile.realPath)
if err != nil {
return err
}
// apply start
_, err = f.Seek(int64(u.start), 0)
if err != nil {
f.Close()
return err
}
maxLength := sfile.size - u.start
if reqLength != -1 {
// check required length
if uint64(reqLength) > maxLength {
f.Close()
return fmt.Errorf("length too big")
}
u.length = uint64(reqLength)
} else {
// set real length
u.length = maxLength
}
u.reader = f
return nil
}()
if err != nil {
log.Log(u.client.conf.LogLevel, log.LevelInfo, "[peer] cannot start upload: %s", err)
if err == errorNoSlots {
if u.client.protoIsAdc() {
u.pconn.conn.Write(&protoadc.AdcCStatus{ //nolint:govet
&adc.ClientPacket{},
&adc.Status{
Sev: adc.Recoverable,
Code: protoadc.AdcCodeSlotsFull,
Msg: "Slots full",
},
})
} else {
u.pconn.conn.Write(&nmdc.MaxedOut{})
}
} else {
if u.client.protoIsAdc() {
u.pconn.conn.Write(&protoadc.AdcCStatus{ //nolint:govet
&adc.ClientPacket{},
&adc.Status{
Sev: adc.Recoverable,
Code: protoadc.AdcCodeFileNotAvailable,
Msg: "File Not Available",
},
})
} else {
u.pconn.conn.Write(&nmdc.Error{Err: fmt.Errorf("File Not Available")})
}
}
return false
}
if u.client.protoIsAdc() {
queryParts := strings.Split(u.query, " ")
u.pconn.conn.Write(&protoadc.AdcCSendFile{ //nolint:govet
&adc.ClientPacket{},
&adc.GetResponse{
Type: queryParts[0],
Path: queryParts[1],
Start: int64(u.start),
Bytes: int64(u.length),
Compressed: u.isCompressed,
},
})
} else {
queryParts := strings.Split(u.query, " ")
u.pconn.conn.Write(&nmdc.ADCSnd{
ContentType: nmdc.String(queryParts[0]),
Identifier: nmdc.String(queryParts[1]),
Start: u.start,
Length: u.length,
Compressed: u.isCompressed,
})
}
client.transfers[u] = struct{}{}
u.client.uploadSlotAvail--
u.pconn.state = "delegated_upload"
u.pconn.transfer = u
return true
}
func (u *upload) Close() {
if u.terminateRequested {
return
}
u.terminateRequested = true
u.pconn.close()
}
func (u *upload) handleUpload() error {
u.pconn.conn.SetSyncMode(true)
if u.isCompressed {
u.pconn.conn.EnableWriterZlib()
}
u.lastPrintTime = time.Now()
buf := make([]byte, 1024*1024)
bufLength := uint64(len(buf))
for {
// apply length
maxLength := func() uint64 {
if (u.offset + bufLength) >= u.length {
return u.length - u.offset
}
return bufLength
}()
if maxLength == 0 {
break
}
n, err := u.reader.Read(buf[:maxLength])
if err != nil && err != io.EOF {
return err
}
u.offset += uint64(n)
err = u.pconn.conn.WriteSync(buf[:n])
if err != nil {
return err
}
since := time.Since(u.lastPrintTime)
if since >= (1 * time.Second) {
u.lastPrintTime = time.Now()
speed := float64(u.pconn.conn.PullWriteCounter()) / 1024 / (float64(since) / float64(time.Second))
log.Log(u.client.conf.LogLevel, log.LevelInfo, "[sent] %d/%d (%.1f KiB/s)", u.offset, u.length, speed)
}
}
if u.isCompressed {
u.pconn.conn.DisableWriterZlib()
}
u.pconn.conn.SetSyncMode(false)
return nil
}
func (u *upload) handleExit(err error) {
if !u.terminateRequested && err != nil {
log.Log(u.client.conf.LogLevel, log.LevelInfo, "ERR (upload) [%s]: %s", u.pconn.peer.Nick, err)
}
delete(u.client.transfers, u)
u.reader.Close()
u.client.uploadSlotAvail++
if err == nil {
log.Log(u.client.conf.LogLevel, log.LevelInfo, "[upload] [%s] finished %s (s=%d l=%d)",
u.pconn.peer.Nick, dcReadableQuery(u.query), u.start, u.length)
} else {
log.Log(u.client.conf.LogLevel, log.LevelInfo, "[upload] [%s] failed %s",
u.pconn.peer.Nick, dcReadableQuery(u.query))
}
}