bep_request_response.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. BlockNo int
  24. }
  25. func (r *Request) toWire() *bep.Request {
  26. return &bep.Request{
  27. Id: int32(r.ID),
  28. Folder: r.Folder,
  29. Name: r.Name,
  30. Offset: r.Offset,
  31. Size: int32(r.Size),
  32. Hash: r.Hash,
  33. FromTemporary: r.FromTemporary,
  34. BlockNo: int32(r.BlockNo),
  35. }
  36. }
  37. func requestFromWire(w *bep.Request) *Request {
  38. return &Request{
  39. ID: int(w.Id),
  40. Folder: w.Folder,
  41. Name: w.Name,
  42. Offset: w.Offset,
  43. Size: int(w.Size),
  44. Hash: w.Hash,
  45. FromTemporary: w.FromTemporary,
  46. BlockNo: int(w.BlockNo),
  47. }
  48. }
  49. type Response struct {
  50. ID int
  51. Data []byte
  52. Code ErrorCode
  53. }
  54. func (r *Response) toWire() *bep.Response {
  55. return &bep.Response{
  56. Id: int32(r.ID),
  57. Data: r.Data,
  58. Code: r.Code,
  59. }
  60. }
  61. func responseFromWire(w *bep.Response) *Response {
  62. return &Response{
  63. ID: int(w.Id),
  64. Data: w.Data,
  65. Code: w.Code,
  66. }
  67. }