-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_tx.go
56 lines (47 loc) · 913 Bytes
/
tcp_tx.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
package streamcast
import (
"fmt"
"time"
)
type TcpTx struct {
addr string
tcpServer *TcpServer
currentId uint32
timeout time.Duration
}
func NewTcpTx(network string, port int) (s *TcpTx, err error) {
addr := fmt.Sprintf("%s:%d", network, port)
s = new(TcpTx)
s.addr = addr
err, s.tcpServer = StartTcpServer(addr)
if err != nil {
return nil, err
}
s.currentId = 1
s.timeout = 1 * time.Second
return
}
func (s *TcpTx) WriteFrame(f *Frame) (err error) {
var b [MAX_FRAME_LENGTH]byte
n, err := f.Write(b[:])
if err != nil {
return err
}
s.tcpServer.Broadcast(b[:n])
return
}
func (s *TcpTx) Write(metadata []byte, data []byte) (err error) {
var f Frame
f.Data = data
f.Metadata = metadata
f.FrameId = s.currentId
s.currentId += 1
s.WriteFrame(&f)
return
}
func (s *TcpTx) SetTimeout(t time.Duration) {
s.timeout = t
}
func (s *TcpTx) Close() {
s.tcpServer.Close()
}