aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/protocol/packp/sideband/muxer.go
blob: 45fecc2cbd2d0e3dd77fb46fcc8f0c6431ea01de (plain) (blame)
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
package sideband

import (
	"io"

	"gopkg.in/src-d/go-git.v4/plumbing/format/pktline"
)

// Muxer multiplex the packfile along with the progress messages and the error
// information. The multiplex is perform using pktline format.
type Muxer struct {
	max int
	e   *pktline.Encoder
}

const chLen = 1

// NewMuxer returns a new Muxer for the given t that writes on w.
//
// If t is equal to `Sideband` the max pack size is set to MaxPackedSize, in any
// other value is given, max pack is set to MaxPackedSize64k, that is the
// maximum length of a line in pktline format.
func NewMuxer(t Type, w io.Writer) *Muxer {
	max := MaxPackedSize64k
	if t == Sideband {
		max = MaxPackedSize
	}

	return &Muxer{
		max: max - chLen,
		e:   pktline.NewEncoder(w),
	}
}

// Write writes p in the PackData channel
func (m *Muxer) Write(p []byte) (int, error) {
	return m.WriteChannel(PackData, p)
}

// WriteChannel writes p in the given channel. This method can be used with any
// channel, but is recommend use it only for the ProgressMessage and
// ErrorMessage channels and use Write for the PackData channel
func (m *Muxer) WriteChannel(t Channel, p []byte) (int, error) {
	wrote := 0
	size := len(p)
	for wrote < size {
		n, err := m.doWrite(t, p[wrote:])
		wrote += n

		if err != nil {
			return wrote, err
		}
	}

	return wrote, nil
}

func (m *Muxer) doWrite(ch Channel, p []byte) (int, error) {
	sz := len(p)
	if sz > m.max {
		sz = m.max
	}

	return sz, m.e.Encode(ch.WithPayload(p[:sz]))
}