File: bep_request_response.go

package info (click to toggle)
syncthing 1.29.5~ds1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 22,848 kB
  • sloc: javascript: 37,288; sh: 1,838; xml: 1,115; makefile: 66
file content (80 lines) | stat: -rw-r--r-- 1,802 bytes parent folder | download | duplicates (2)
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
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package protocol

import "github.com/syncthing/syncthing/internal/gen/bep"

type ErrorCode = bep.ErrorCode

const (
	ErrorCodeNoError     = bep.ErrorCode_ERROR_CODE_NO_ERROR
	ErrorCodeGeneric     = bep.ErrorCode_ERROR_CODE_GENERIC
	ErrorCodeNoSuchFile  = bep.ErrorCode_ERROR_CODE_NO_SUCH_FILE
	ErrorCodeInvalidFile = bep.ErrorCode_ERROR_CODE_INVALID_FILE
)

type Request struct {
	ID            int
	Folder        string
	Name          string
	Offset        int64
	Size          int
	Hash          []byte
	FromTemporary bool
	WeakHash      uint32
	BlockNo       int
}

func (r *Request) toWire() *bep.Request {
	return &bep.Request{
		Id:            int32(r.ID),
		Folder:        r.Folder,
		Name:          r.Name,
		Offset:        r.Offset,
		Size:          int32(r.Size),
		Hash:          r.Hash,
		FromTemporary: r.FromTemporary,
		WeakHash:      r.WeakHash,
		BlockNo:       int32(r.BlockNo),
	}
}

func requestFromWire(w *bep.Request) *Request {
	return &Request{
		ID:            int(w.Id),
		Folder:        w.Folder,
		Name:          w.Name,
		Offset:        w.Offset,
		Size:          int(w.Size),
		Hash:          w.Hash,
		FromTemporary: w.FromTemporary,
		WeakHash:      w.WeakHash,
		BlockNo:       int(w.BlockNo),
	}
}

type Response struct {
	ID   int
	Data []byte
	Code ErrorCode
}

func (r *Response) toWire() *bep.Response {
	return &bep.Response{
		Id:   int32(r.ID),
		Data: r.Data,
		Code: r.Code,
	}
}

func responseFromWire(w *bep.Response) *Response {
	return &Response{
		ID:   int(w.Id),
		Data: w.Data,
		Code: w.Code,
	}
}