-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_imux.go
88 lines (83 loc) · 2.15 KB
/
data_imux.go
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
package imux
import (
log "github.com/Sirupsen/logrus"
"io"
)
var MaxChunkDataSize = 16384
// A DataIMUX will read data from multiple io.Readers and chunk the data
// into a chunk chan. The Stale attribute provides a way to insert chunks
// back into the chan from external sources.
type DataIMUX struct {
Chunks chan Chunk
Stale chan Chunk
SessionID string
}
// Create a new DataIMUX for a given session
func NewDataIMUX(session_id string) DataIMUX {
log.WithFields(log.Fields{
"at": "NewDataIMUX",
"session_id": session_id,
}).Debug("creating data imux")
return DataIMUX{
Chunks: make(chan Chunk, 10),
Stale: make(chan Chunk, 50),
SessionID: session_id,
}
}
// Read from a new data source in this DataIMUX, create chunks from it tagged with the
// provided socket ID.
func (data_imux *DataIMUX) ReadFrom(id string, conn io.Reader) {
log.WithFields(log.Fields{
"at": "DataIMUX.ReadFrom",
"socket_id": id,
}).Debug("reading from new data source")
data_imux.Chunks <- Chunk{
SocketID: id,
SessionID: data_imux.SessionID,
Setup: true,
}
sequence := uint64(1)
for {
chunk_data := make([]byte, MaxChunkDataSize)
read, err := conn.Read(chunk_data)
log.WithFields(log.Fields{
"at": "DataIMUX.ReadFrom",
"socket_id": id,
"size": read,
}).Debug("read data from data source")
chunk_data = chunk_data[:read]
close := false
if err != nil {
if err == io.EOF {
log.WithFields(log.Fields{
"at": "DataIMUX.ReadFrom",
"error": err.Error(),
"socket_id": id,
}).Debug("EOF from data imux source")
} else {
log.WithFields(log.Fields{
"at": "DataIMUX.ReadFrom",
"error": err.Error(),
"socket_id": id,
}).Debug("error reading data from imux data source")
}
close = true
}
data_imux.Chunks <- Chunk{
SequenceID: sequence,
SocketID: id,
SessionID: data_imux.SessionID,
Data: chunk_data,
Close: close,
}
log.WithFields(log.Fields{
"at": "DataIMUX.ReadFrom",
"socket_id": id,
"size": read,
}).Debug("write chunk from data source")
sequence += 1
if close {
return
}
}
}