-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrx_connection.go
74 lines (62 loc) · 1.46 KB
/
rx_connection.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
package streamcast
import (
"net"
"time"
)
/* Generic Receiver Connection interface */
type RxConn interface {
Close()
Reset() (err error)
Read(b []byte) (int, error)
SetDeadline(t time.Time) error
}
/* UDP Receiver Connection: mapping the generic methods above to UDP specific methods. */
type UdpRxConn struct {
conn *net.UDPConn
addr *net.UDPAddr
}
func (udpRxConn *UdpRxConn) Reset() (err error) {
udpRxConn.Close()
udpRxConn.conn, err = net.ListenUDP("udp4", udpRxConn.addr)
if err != nil {
return err
}
return
}
func (udpRxConn *UdpRxConn) SetDeadline(t time.Time) error {
return udpRxConn.conn.SetDeadline(t)
}
func (udpRxConn *UdpRxConn) Close() {
if udpRxConn.conn != nil {
udpRxConn.conn.Close()
}
}
func (udpRxConn *UdpRxConn) Read(b []byte) (int, error) {
n, _, err := udpRxConn.conn.ReadFromUDP(b)
return n, err
}
/* TCP Receiver Connection: mapping the generic methods above to TCP specific methods. */
type TcpRxConn struct {
conn *net.TCPConn
addr *net.TCPAddr
}
func (tcpRxConn *TcpRxConn) Reset() (err error) {
tcpRxConn.Close()
tcpRxConn.conn, err = net.DialTCP("tcp4", nil, tcpRxConn.addr)
if err != nil {
return err
}
return
}
func (tcpRxConn *TcpRxConn) SetDeadline(t time.Time) error {
return tcpRxConn.conn.SetDeadline(t)
}
func (tcpRxConn *TcpRxConn) Close() {
if tcpRxConn.conn != nil {
tcpRxConn.conn.Close()
}
}
func (tcpRxConn *TcpRxConn) Read(b []byte) (int, error) {
n, err := tcpRxConn.conn.Read(b)
return n, err
}