aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/protocol/packp/sideband/muxer.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-11-30 17:24:11 +0100
committerGitHub <noreply@github.com>2016-11-30 17:24:11 +0100
commitbd3dd4d421299699854bfe0353aae312bcf8c97c (patch)
tree60f22b556b929143955bd54e6ac7ae69563308ed /plumbing/protocol/packp/sideband/muxer.go
parentb0d756c93d8deb5d4c6a129c5bd3163dddd10132 (diff)
downloadgo-git-bd3dd4d421299699854bfe0353aae312bcf8c97c.tar.gz
protocol/packp: sideband muxer and demuxer (#143)
* format/pakp: sideband demuxer * format/pakp: sideband muxer * format/pakp: sideband demuxer and muxer * protocol/pakp: sideband demuxer and muxer * documentation and improvements * improvements * handle scan errors properly
Diffstat (limited to 'plumbing/protocol/packp/sideband/muxer.go')
-rw-r--r--plumbing/protocol/packp/sideband/muxer.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/plumbing/protocol/packp/sideband/muxer.go b/plumbing/protocol/packp/sideband/muxer.go
new file mode 100644
index 0000000..2ab7da8
--- /dev/null
+++ b/plumbing/protocol/packp/sideband/muxer.go
@@ -0,0 +1,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 lenght 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]))
+}