bep_request_response.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import "github.com/syncthing/syncthing/internal/gen/bep"
  8. type ErrorCode = bep.ErrorCode
  9. const (
  10. ErrorCodeNoError = bep.ErrorCode_ERROR_CODE_NO_ERROR
  11. ErrorCodeGeneric = bep.ErrorCode_ERROR_CODE_GENERIC
  12. ErrorCodeNoSuchFile = bep.ErrorCode_ERROR_CODE_NO_SUCH_FILE
  13. ErrorCodeInvalidFile = bep.ErrorCode_ERROR_CODE_INVALID_FILE
  14. )
  15. type Request struct {
  16. ID int
  17. Folder string
  18. Name string
  19. Offset int64
  20. Size int
  21. Hash []byte
  22. FromTemporary bool
  23. WeakHash uint32
  24. BlockNo int
  25. }
  26. func (r *Request) toWire() *bep.Request {
  27. return &bep.Request{
  28. Id: int32(r.ID),
  29. Folder: r.Folder,
  30. Name: r.Name,
  31. Offset: r.Offset,
  32. Size: int32(r.Size),
  33. Hash: r.Hash,
  34. FromTemporary: r.FromTemporary,
  35. WeakHash: r.WeakHash,
  36. BlockNo: int32(r.BlockNo),
  37. }
  38. }
  39. func requestFromWire(w *bep.Request) *Request {
  40. return &Request{
  41. ID: int(w.Id),
  42. Folder: w.Folder,
  43. Name: w.Name,
  44. Offset: w.Offset,
  45. Size: int(w.Size),
  46. Hash: w.Hash,
  47. FromTemporary: w.FromTemporary,
  48. WeakHash: w.WeakHash,
  49. BlockNo: int(w.BlockNo),
  50. }
  51. }
  52. type Response struct {
  53. ID int
  54. Data []byte
  55. Code ErrorCode
  56. }
  57. func (r *Response) toWire() *bep.Response {
  58. return &bep.Response{
  59. Id: int32(r.ID),
  60. Data: r.Data,
  61. Code: r.Code,
  62. }
  63. }
  64. func responseFromWire(w *bep.Response) *Response {
  65. return &Response{
  66. ID: int(w.Id),
  67. Data: w.Data,
  68. Code: w.Code,
  69. }
  70. }