Packet-byte handling, protocol struct to handle protocol states

This commit is contained in:
Muaz Ahmad 2023-09-18 12:28:04 +05:00
parent 184dac910a
commit d517877720
4 changed files with 65 additions and 3 deletions

View file

@ -27,7 +27,7 @@ func (intake *Intake) NewTunnel(l net.PacketConn, peer net.Addr) (*Tunnel) {
tunnel := new(Tunnel) tunnel := new(Tunnel)
tunnel.socket = l tunnel.socket = l
tunnel.peer = peer tunnel.peer = peer
tunnel.queue = make(chan *Packet, 10) tunnel.queue = make(chan []byte, 10)
return tunnel return tunnel
} }
return nil return nil
@ -61,5 +61,7 @@ func (intake *Intake) Read() {
return return
} }
fmt.Println(string(intake.buffer[:n])) fmt.Println(string(intake.buffer[:n]))
tunnel.queue <- &(Packet{intake.buffer[:n]}) pkt := make([]byte, n)
copy(pkt, intake.buffer[:n])
tunnel.queue <- pkt
} }

View file

@ -6,3 +6,11 @@ import (
type Packet struct { type Packet struct {
raw []byte raw []byte
} }
func MarshallPacket(packet *Packet) ([]byte, error) {
return packet.raw, nil
}
func ParsePacket(buffer []byte) (*Packet, error) {
return &Packet{buffer}, nil
}

20
srt/protocol.go Normal file
View file

@ -0,0 +1,20 @@
package srt
import (
"fmt"
)
const (
INDUCTION uint8 = iota
CONCLUSION
DATA_LOOP
)
type SRTManager struct {
state uint8
}
func (proto *SRTManager) Decide(packet *Packet) (*Packet, error) {
fmt.Println(*packet)
return nil, nil
}

View file

@ -7,6 +7,38 @@ import (
type Tunnel struct { type Tunnel struct {
socket net.PacketConn socket net.PacketConn
peer net.Addr peer net.Addr
queue chan *Packet queue chan []byte
broken bool 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)
} }