seaweedfs/weed/wdclient/volume_udp_client.go

63 lines
1.3 KiB
Go
Raw Normal View History

2021-03-13 18:16:32 +00:00
package wdclient
import (
"bufio"
2021-03-13 18:16:32 +00:00
"github.com/chrislusf/seaweedfs/weed/pb"
"github.com/chrislusf/seaweedfs/weed/util"
"github.com/chrislusf/seaweedfs/weed/wdclient/penet"
2021-03-13 18:16:32 +00:00
"io"
"net"
"time"
2021-03-13 18:16:32 +00:00
)
// VolumeUdpClient put/get/delete file chunks directly on volume servers without replication
2021-03-13 18:16:32 +00:00
type VolumeUdpClient struct {
Conn net.Conn
bufWriter *bufio.Writer
bufReader *bufio.Reader
}
type VolumeUdpConn struct {
Conn net.Conn
bufWriter *bufio.Writer
bufReader *bufio.Reader
2021-03-13 18:16:32 +00:00
}
func NewVolumeUdpClient() *VolumeUdpClient {
return &VolumeUdpClient{
}
}
func (c *VolumeUdpClient) PutFileChunk(volumeServerAddress string, fileId string, fileSize uint32, fileReader io.Reader) (err error) {
udpAddress, parseErr := pb.ParseServerAddress(volumeServerAddress, 20001)
if parseErr != nil {
return parseErr
}
if c.Conn == nil {
c.Conn, err = penet.DialTimeout("", udpAddress, 500*time.Millisecond)
if err != nil {
return err
}
c.bufWriter = bufio.NewWriter(c.Conn)
}
2021-03-13 22:48:28 +00:00
buf := []byte("+" + fileId + "\n")
_, err = c.bufWriter.Write([]byte(buf))
if err != nil {
return
}
2021-03-13 22:48:28 +00:00
util.Uint32toBytes(buf[0:4], fileSize)
_, err = c.bufWriter.Write(buf[0:4])
if err != nil {
return
}
_, err = io.Copy(c.bufWriter, fileReader)
2021-03-13 18:16:32 +00:00
if err != nil {
return
}
c.bufWriter.Flush()
2021-03-13 18:16:32 +00:00
return nil
2021-03-13 18:16:32 +00:00
}