stream-server/srt/tunnel.go

52 lines
946 B
Go

package srt
import (
"net"
"fmt"
)
type Tunnel struct {
socket net.PacketConn
peer net.Addr
queue chan []byte
broken bool
state *SRTManager
}
func (tunnel *Tunnel) Start() {
defer func(a *bool) {
if r := recover(); r != nil {
fmt.Println(r)
}
*a = true
}(&(tunnel.broken))
tunnel.state = NewSRTManager(tunnel.socket)
tunnel.state.make_syn_cookie(tunnel.peer)
for {
packet, err := tunnel.ReadPacket()
if err != nil {
tunnel.broken = true
}
response, err := tunnel.state.Process(packet)
if err != nil {
tunnel.broken = true
}
if response != nil {
tunnel.WritePacket(response)
}
}
}
func (tunnel *Tunnel) WritePacket(packet *Packet) {
buffer, err := MarshallPacket(packet, tunnel.state)
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)
}