aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2018-10-11 17:23:08 +0200
committerPřemysl Janouch <p@janouch.name>2018-10-11 17:28:37 +0200
commit61e3c68cc80a00827fc840af4b0989296ce344fa (patch)
treee6e91f30136de6725f17b76498da3b12d4e726a3 /main.go
parentdddbc5556e278b4f6770d9b036158eacd90429f1 (diff)
downloadbbc-on-ice-61e3c68cc80a00827fc840af4b0989296ce344fa.tar.gz
bbc-on-ice-61e3c68cc80a00827fc840af4b0989296ce344fa.tar.xz
bbc-on-ice-61e3c68cc80a00827fc840af4b0989296ce344fa.zip
Some cleanup
The proxy function still keeps bothering me.
Diffstat (limited to 'main.go')
-rw-r--r--main.go44
1 files changed, 21 insertions, 23 deletions
diff --git a/main.go b/main.go
index a1a7357..0c257cb 100644
--- a/main.go
+++ b/main.go
@@ -289,7 +289,19 @@ func proxy(w http.ResponseWriter, req *http.Request) {
// dataProc may return less data near the end of a subfile, so we give it
// a maximum count of bytes to return at once and do our own buffering.
- var queuedMetaUpdate, queuedChunk []byte
+ var queuedMetaUpdate, queuedData []byte
+ writeMeta := func() error {
+ if !wantMeta {
+ return nil
+ }
+
+ var meta [1 + 16*255]byte
+ meta[0] = byte((copy(meta[1:], queuedMetaUpdate) + 15) / 16)
+ queuedMetaUpdate = nil
+
+ _, err := bufrw.Write(meta[:1+int(meta[0])*16])
+ return err
+ }
for {
select {
case title := <-metaChan:
@@ -298,31 +310,17 @@ func proxy(w http.ResponseWriter, req *http.Request) {
if !ok {
return
}
-
- space := metaint - len(queuedChunk)
- if space > len(chunk) {
- space = len(chunk)
- }
-
- queuedChunk = append(queuedChunk, chunk[:space]...)
- if len(queuedChunk) < metaint {
- break
+ missing := metaint - len(queuedData)
+ if len(chunk) < missing {
+ queuedData = append(queuedData, chunk...)
+ continue
}
- if _, err := bufrw.Write(queuedChunk); err != nil {
+ queuedData = append(queuedData, chunk[:missing]...)
+ if _, err := bufrw.Write(queuedData); err != nil {
return
}
-
- queuedChunk = chunk[space:]
- if wantMeta {
- var meta [1 + 16*255]byte
- meta[0] = byte((copy(meta[1:], queuedMetaUpdate) + 15) / 16)
- queuedMetaUpdate = nil
-
- if _, err := bufrw.Write(meta[:1+int(meta[0])*16]); err != nil {
- return
- }
- }
- if err := bufrw.Flush(); err != nil {
+ queuedData = chunk[missing:]
+ if writeMeta() != nil || bufrw.Flush() != nil {
return
}
}