implement channel recv & send
This commit is contained in:
parent
47384fb3bb
commit
ac8b60e030
3 changed files with 77 additions and 9 deletions
|
|
@ -10,9 +10,10 @@ const (
|
|||
)
|
||||
|
||||
type LuxChannel struct {
|
||||
Type LuxChannelType
|
||||
Address *net.UDPAddr
|
||||
conn *net.UDPConn
|
||||
Type LuxChannelType
|
||||
Address *net.UDPAddr
|
||||
conn *net.UDPConn
|
||||
preconnected bool
|
||||
}
|
||||
|
||||
// bind udp socket to receive packets
|
||||
|
|
@ -28,12 +29,14 @@ func NewLuxInboundChannel(udpStr string, chType LuxChannelType) (LuxChannel, err
|
|||
}
|
||||
|
||||
return LuxChannel{
|
||||
Type: chType,
|
||||
Address: addr,
|
||||
conn: conn,
|
||||
Type: chType,
|
||||
Address: addr,
|
||||
conn: conn,
|
||||
preconnected: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// udp socket to send datagrams
|
||||
func NewLuxOutboundChannel(udpStr string, chType LuxChannelType) (LuxChannel, error) {
|
||||
addr, err := net.ResolveUDPAddr("udp", udpStr)
|
||||
if err != nil {
|
||||
|
|
@ -46,12 +49,37 @@ func NewLuxOutboundChannel(udpStr string, chType LuxChannelType) (LuxChannel, er
|
|||
}
|
||||
|
||||
return LuxChannel{
|
||||
Type: chType,
|
||||
Address: addr,
|
||||
conn: conn,
|
||||
Type: chType,
|
||||
Address: addr,
|
||||
conn: conn,
|
||||
preconnected: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const MAX_UDP_SIZE = 65535
|
||||
|
||||
func (ch *LuxChannel) Recv() (LuxDatagram, *net.UDPAddr, error) {
|
||||
buf := make([]byte, MAX_UDP_SIZE)
|
||||
len, addr, err := ch.conn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
return LuxDatagram{}, nil, err
|
||||
}
|
||||
|
||||
return LuxDatagram{
|
||||
Payload: buf[:len],
|
||||
}, addr, nil
|
||||
}
|
||||
|
||||
func (ch *LuxChannel) Send(dgram LuxDatagram, addr *net.UDPAddr) error {
|
||||
if ch.preconnected {
|
||||
_, err := ch.conn.Write(dgram.Payload)
|
||||
return err
|
||||
} else {
|
||||
_, err := ch.conn.WriteToUDP(dgram.Payload, addr)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (ch *LuxChannel) Close() {
|
||||
ch.conn.Close()
|
||||
}
|
||||
|
|
|
|||
5
net/lux_datagram.go
Normal file
5
net/lux_datagram.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package net
|
||||
|
||||
type LuxDatagram struct {
|
||||
Payload []byte
|
||||
}
|
||||
35
tests/lux_channel_test.go
Normal file
35
tests/lux_channel_test.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"lux/net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRecvSend(t *testing.T) {
|
||||
sv, err := net.NewLuxInboundChannel("127.0.0.2:65535", net.LuxChannelInterior)
|
||||
if err != nil {
|
||||
t.Fatal(t)
|
||||
}
|
||||
|
||||
cl, err := net.NewLuxOutboundChannel("127.0.0.2:65535", net.LuxChannelInterior)
|
||||
if err != nil {
|
||||
t.Fatal(t)
|
||||
}
|
||||
|
||||
dgram := net.LuxDatagram{Payload: []byte("TEEEESTTTT 3243492i0423")}
|
||||
if err := cl.Send(dgram, sv.Address); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
recv, _, err := sv.Recv()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(dgram.Payload, recv.Payload) {
|
||||
t.Log(dgram)
|
||||
t.Log(recv)
|
||||
t.Fatal("sent and recv payload not equal!")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue