You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package wdclient
  2. import (
  3. "bufio"
  4. "github.com/chrislusf/seaweedfs/weed/pb"
  5. "github.com/chrislusf/seaweedfs/weed/udptransfer"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "io"
  8. "net"
  9. )
  10. // VolumeUdpClient put/get/delete file chunks directly on volume servers without replication
  11. type VolumeUdpClient struct {
  12. }
  13. type VolumeUdpConn struct {
  14. net.Conn
  15. bufWriter *bufio.Writer
  16. bufReader *bufio.Reader
  17. }
  18. func NewVolumeUdpClient() *VolumeUdpClient {
  19. return &VolumeUdpClient{
  20. }
  21. }
  22. func (c *VolumeUdpClient) PutFileChunk(volumeServerAddress string, fileId string, fileSize uint32, fileReader io.Reader) (err error) {
  23. udpAddress, parseErr := pb.ParseServerAddress(volumeServerAddress, 20001)
  24. if parseErr != nil {
  25. return parseErr
  26. }
  27. listener, err := udptransfer.NewEndpoint(&udptransfer.Params{
  28. LocalAddr: "",
  29. Bandwidth: 100,
  30. FastRetransmit: true,
  31. FlatTraffic: true,
  32. IsServ: false,
  33. })
  34. if err != nil {
  35. return err
  36. }
  37. defer listener.Close()
  38. conn, err := listener.Dial(udpAddress)
  39. if err != nil {
  40. return err
  41. }
  42. defer conn.Close()
  43. bufWriter := bufio.NewWriter(conn)
  44. buf := []byte("+" + fileId + "\n")
  45. _, err = bufWriter.Write([]byte(buf))
  46. if err != nil {
  47. return
  48. }
  49. util.Uint32toBytes(buf[0:4], fileSize)
  50. _, err = bufWriter.Write(buf[0:4])
  51. if err != nil {
  52. return
  53. }
  54. _, err = io.Copy(bufWriter, fileReader)
  55. if err != nil {
  56. return
  57. }
  58. bufWriter.Flush()
  59. return nil
  60. }