stream-server/srt/tunnel.go

45 lines
758 B
Go
Raw Normal View History

2023-09-18 11:51:55 +05:00
package srt
import (
"net"
)
type Tunnel struct {
socket net.PacketConn
peer net.Addr
queue chan []byte
2023-09-18 11:51:55 +05:00
broken bool
state *SRTManager
}
func (tunnel *Tunnel) Start() {
tunnel.state = new(SRTManager)
for {
packet, err := tunnel.ReadPacket()
if err != nil {
tunnel.broken = true
}
response, err := tunnel.state.Decide(packet)
if err != nil {
tunnel.broken = true
}
if response != nil {
tunnel.WritePacket(response)
}
}
}
func (tunnel *Tunnel) WritePacket(packet *Packet) {
buffer, err := MarshallPacket(packet)
if err != nil {
tunnel.broken = true
return
}
tunnel.socket.WriteTo(buffer, tunnel.peer)
}
func (tunnel *Tunnel) ReadPacket() (*Packet, error) {
packet := <- tunnel.queue
return ParsePacket(packet)
2023-09-18 11:51:55 +05:00
}