-
Notifications
You must be signed in to change notification settings - Fork 39
/
tracker.go
82 lines (76 loc) · 1.74 KB
/
tracker.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
package fdfs_client
import (
"bytes"
"encoding/binary"
"fmt"
"net"
)
type trackerStorageInfo struct {
groupName string
ipAddr string
port int64
storePathIndex int8
}
type trackerTask struct {
header
//req
groupName string
remoteFilename string
//res
trackerStorageInfo
}
func (this *trackerTask) SendReq(conn net.Conn) error {
if this.groupName != "" {
this.pkgLen = int64(FDFS_GROUP_NAME_MAX_LEN + len(this.remoteFilename))
}
if err := this.SendHeader(conn); err != nil {
return err
}
if this.groupName != "" {
buffer := new(bytes.Buffer)
byteGroupName := []byte(this.groupName)
var bufferGroupName [16]byte
for i := 0; i < len(byteGroupName); i++ {
bufferGroupName[i] = byteGroupName[i]
}
buffer.Write(bufferGroupName[:])
buffer.WriteString(this.remoteFilename)
if _, err := conn.Write(buffer.Bytes()); err != nil {
return err
}
}
return nil
}
func (this *trackerTask) RecvRes(conn net.Conn) error {
if err := this.RecvHeader(conn); err != nil {
return fmt.Errorf("TrackerTask RecvHeader %v", err)
}
if this.pkgLen != 39 && this.pkgLen != 40 {
return fmt.Errorf("recvStorageInfo pkgLen %d invaild", this.pkgLen)
}
buf := make([]byte, this.pkgLen)
if _, err := conn.Read(buf); err != nil {
return err
}
buffer := bytes.NewBuffer(buf)
var err error
this.groupName, err = readCStrFromByteBuffer(buffer, 16)
if err != nil {
return err
}
this.ipAddr, err = readCStrFromByteBuffer(buffer, 15)
if err != nil {
return err
}
if err := binary.Read(buffer, binary.BigEndian, &this.port); err != nil {
return err
}
if this.pkgLen == 40 {
storePathIndex, err := buffer.ReadByte()
if err != nil {
return err
}
this.storePathIndex = int8(storePathIndex)
}
return nil
}