Skip to content

Commit

Permalink
adaptation to new version of tuna
Browse files Browse the repository at this point in the history
  • Loading branch information
bufrr committed Mar 7, 2023
1 parent 27f0e79 commit 8601ea9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
15 changes: 7 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/nknorg/nkngomobile"
"github.com/nknorg/tuna"
tpb "github.com/nknorg/tuna/pb"
"github.com/nknorg/tuna/udp"
gocache "github.com/patrickmn/go-cache"
"google.golang.org/protobuf/proto"
)
Expand All @@ -48,7 +47,7 @@ type TunaSessionClient struct {
onConnect chan struct{}
onClose chan struct{}
connectedOnce sync.Once
udpConn *udp.EncryptUDPConn
udpConn *tuna.EncryptUDPConn

sync.RWMutex
listeners []net.Listener
Expand Down Expand Up @@ -849,7 +848,7 @@ func (c *TunaSessionClient) startExits() error {
return nil
}

func (c *TunaSessionClient) ListenUDP(addrsRe *nkngomobile.StringArray) (*udp.EncryptUDPConn, error) {
func (c *TunaSessionClient) ListenUDP(addrsRe *nkngomobile.StringArray) (*tuna.EncryptUDPConn, error) {
acceptAddrs, err := getAcceptAddrs(addrsRe)
if err != nil {
return nil, err
Expand Down Expand Up @@ -887,15 +886,15 @@ func (c *TunaSessionClient) ListenUDP(addrsRe *nkngomobile.StringArray) (*udp.En
return nil, err
}

c.udpConn = udp.NewEncryptUDPConn(conn)
c.udpConn = tuna.NewEncryptUDPConn(conn)
return c.udpConn, nil
}

func (c *TunaSessionClient) DialUDP(remoteAddr string) (*udp.EncryptUDPConn, error) {
func (c *TunaSessionClient) DialUDP(remoteAddr string) (*tuna.EncryptUDPConn, error) {
return c.DialUDPWithConfig(remoteAddr, nil)
}

func (c *TunaSessionClient) DialUDPWithConfig(remoteAddr string, config *nkn.DialConfig) (*udp.EncryptUDPConn, error) {
func (c *TunaSessionClient) DialUDPWithConfig(remoteAddr string, config *nkn.DialConfig) (*tuna.EncryptUDPConn, error) {
config, err := nkn.MergeDialConfig(c.config.SessionConfig, config)
if err != nil {
return nil, err
Expand All @@ -919,7 +918,7 @@ func (c *TunaSessionClient) DialUDPWithConfig(remoteAddr string, config *nkn.Dia
return nil, err
}

udpConn := new(udp.EncryptUDPConn)
udpConn := new(tuna.EncryptUDPConn)
for i, addr := range pubAddrs.Addrs {
if len(addr.IP) > 0 && addr.Port > 0 {
udpAddr := net.UDPAddr{IP: net.ParseIP(addr.IP), Port: int(addr.Port)}
Expand All @@ -931,7 +930,7 @@ func (c *TunaSessionClient) DialUDPWithConfig(remoteAddr string, config *nkn.Dia
log.Printf("dial udp err: %v", err)
continue
}
udpConn = udp.NewEncryptUDPConn(conn)
udpConn = tuna.NewEncryptUDPConn(conn)
break
}
}
Expand Down
10 changes: 7 additions & 3 deletions examples/throughput/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/hex"
"flag"
"fmt"
"github.com/nknorg/tuna/udp"
"github.com/nknorg/tuna"
"log"
"math"
"net"
Expand Down Expand Up @@ -100,11 +100,15 @@ func write(sess net.Conn, numBytes int) error {
return nil
}

func readUDP(conn *udp.EncryptUDPConn, numBytes int) error {
func readUDP(conn *tuna.EncryptUDPConn, numBytes int) error {
defer conn.Close()
buffer := make([]byte, 1024)
var timeStart time.Time
for {
err := conn.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil {
return err
}
n, _, err := conn.ReadFromUDP(buffer)
if udpBytesReceived == 0 {
timeStart = time.Now()
Expand All @@ -125,7 +129,7 @@ func readUDP(conn *udp.EncryptUDPConn, numBytes int) error {
}
}

func writeUDP(conn *udp.EncryptUDPConn, numBytes int) error {
func writeUDP(conn *tuna.EncryptUDPConn, numBytes int) error {
defer conn.Close()
buffer := make([]byte, 1024)
timeStart := time.Now()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/nknorg/nkn-sdk-go v1.4.3
github.com/nknorg/nkn/v2 v2.1.8
github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9
github.com/nknorg/tuna v0.0.0-20230224012633-b53aa9c90cd5
github.com/nknorg/tuna v0.0.0-20230307074911-ced36707f273
github.com/patrickmn/go-cache v2.1.0+incompatible
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
google.golang.org/protobuf v1.27.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9 h1:Gr37j7Ttvcn8
github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9/go.mod h1:zNY9NCyBcJCCDrXhwOjKarkW5cngPs/Z82xVNy/wvEA=
github.com/nknorg/nnet v0.0.0-20220217113219-4d27780990b1/go.mod h1:4DHEQEMhlRGIKGSyhATdjeusdqaHafDatadtpeHBpvI=
github.com/nknorg/portmapper v0.0.0-20200114081049-1c03cdccc283/go.mod h1:dL4PQJ4670oTO6LqvkjrBQEkD+iMiOYjlKRBBw55Csg=
github.com/nknorg/tuna v0.0.0-20230224012633-b53aa9c90cd5 h1:b6uQxUCJ3Xtq+YO8asJYzGKdP1ZvtCB7qKqt178blC0=
github.com/nknorg/tuna v0.0.0-20230224012633-b53aa9c90cd5/go.mod h1:HtzD/h1ARuyCKoM9d28HWHsel0M4e5UNuk4j9928i9s=
github.com/nknorg/tuna v0.0.0-20230307074911-ced36707f273 h1:pE6Tq5iq/Il5ZEYu/HZ9mNtHYqKLYS2FmyZfP0LkMxs=
github.com/nknorg/tuna v0.0.0-20230307074911-ced36707f273/go.mod h1:HtzD/h1ARuyCKoM9d28HWHsel0M4e5UNuk4j9928i9s=
github.com/nrdcg/auroradns v1.0.1/go.mod h1:y4pc0i9QXYlFCWrhWrUSIETnZgrf4KuwjDIWmmXo3JI=
github.com/nrdcg/dnspod-go v0.4.0/go.mod h1:vZSoFSFeQVm2gWLMkyX61LZ8HI3BaqtHZWgPTGKr6KQ=
github.com/nrdcg/goinwx v0.7.0/go.mod h1:4tKJOCi/1lTxuw9/yB2Ez0aojwtUCSkckjc22eALpqE=
Expand Down
4 changes: 2 additions & 2 deletions tests/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/nknorg/nkn-sdk-go"
ts "github.com/nknorg/nkn-tuna-session"
"github.com/nknorg/nkn/v2/crypto"
"github.com/nknorg/tuna"
_ "github.com/nknorg/tuna/tests"
"github.com/nknorg/tuna/udp"
"io"
"net"
"os"
Expand Down Expand Up @@ -157,7 +157,7 @@ func testTCP(conn net.Conn) error {
return nil
}

func testUDP(from, to *udp.EncryptUDPConn) error {
func testUDP(from, to *tuna.EncryptUDPConn) error {
count := 1000
sendList := make([]string, count)
recvList := make([]string, count)
Expand Down

0 comments on commit 8601ea9

Please sign in to comment.