summaryrefslogtreecommitdiff
path: root/binstream.go
blob: 4240517e7ca27adde821dbcefeac6006436c7831 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package binproto

import (
	"encoding/binary"
	"errors"
	"github.com/kch42/kagus"
	"io"
	"sync"
)

// BinstreamReader reads a binary stream from a binproto stream.
type BinstreamReader struct {
	r      io.Reader
	err    error
	toread int
	surMu  *sync.Mutex // The mutex of the parent SimpleUnitReader
}

// Read implements io.Reader.
func (bsr *BinstreamReader) Read(p []byte) (int, error) {
	if bsr.err != nil {
		return 0, bsr.err
	}

	if bsr.toread == 0 {
		var _toread int32
		if err := binary.Read(bsr.r, binary.LittleEndian, &_toread); err != nil {
			bsr.err = err
			return 0, err
		}

		if _toread < 0 {
			bsr.toread = -1
			bsr.err = io.EOF
			bsr.surMu.Unlock() // TODO: Unlock on other conditions?
			return 0, io.EOF
		}

		bsr.toread = int(_toread)
	}

	want := len(p)
	if bsr.toread < want {
		want = bsr.toread
	}
	n, err := bsr.r.Read(p[:want])

	switch err {
	case nil:
		bsr.toread -= n
		return n, nil
	case io.EOF:
		// NOTE: Perhaps we should log this? IDK...
		bsr.err = errors.New("binstream terminated abnormally")
		return n, bsr.err
	}

	bsr.err = err
	return n, err
}

// FastForward skips to the end of the stream. Use this, if the data is useless.
func (bsr *BinstreamReader) FastForward() error {
	nirvana := kagus.NewNirvanaWriter()
	_, err := io.Copy(nirvana, bsr)
	return err
}

// BinstreamReader writes a binary stream to a binproto stream.
type BinstreamWriter struct {
	w   io.Writer
	err error
}

// Write implements io.Writer.
func (bsw *BinstreamWriter) Write(p []byte) (int, error) {
	if bsw.err != nil {
		return 0, bsw.err
	}

	l := len(p)
	if l == 0 {
		return 0, nil
	}

	if err := binary.Write(bsw.w, binary.LittleEndian, int32(l)); err != nil {
		bsw.err = err
		return 0, err
	}

	n, err := bsw.w.Write(p)
	if err != nil {
		bsw.err = err
	}

	return n, err
}

// Close implements io.Closer. You MUST close a stream, so it is terminated properly.
func (bsw *BinstreamWriter) Close() error {
	switch bsw.err {
	case nil:
	case io.EOF:
		return nil
	default:
		return bsw.err
	}
	if err := binary.Write(bsw.w, binary.LittleEndian, int32(-1)); err != nil {
		return err
	}

	bsw.err = io.EOF
	return nil
}