stream-server/srt/tunnel.go

65 lines
1.2 KiB
Go
Raw Normal View History

2023-09-18 11:51:55 +05:00
package srt
import (
"net"
"fmt"
2023-09-18 11:51:55 +05:00
)
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() {
defer func(a *bool) {
if r := recover(); r != nil {
fmt.Println(r)
}
*a = true
}(&(tunnel.broken))
tunnel.state = NewSRTManager(tunnel.socket)
for {
packet, err := tunnel.ReadPacket()
if err != nil {
tunnel.broken = true
}
response, err := tunnel.state.Process(packet)
if err != nil {
2023-09-20 15:06:32 +05:00
fmt.Println(err)
tunnel.broken = true
}
if response != nil {
tunnel.WritePacket(response)
}
}
}
2023-09-20 15:06:32 +05:00
func (tunnel *Tunnel) Shutdown() {
if tunnel.state != nil && tunnel.state.state > 1 {
packet := tunnel.state.create_basic_header()
packet.packet_type = SHUTDOWN
info := new(ControlHeader)
info.ctrl_type = 5
packet.header_info = info
tunnel.WritePacket(packet)
}
}
func (tunnel *Tunnel) WritePacket(packet *Packet) {
buffer, err := MarshallPacket(packet, tunnel.state)
if err != nil {
tunnel.broken = true
2023-09-20 15:06:32 +05:00
fmt.Println(err)
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
}