Skip to content

Commit

Permalink
smallnest#200 support utp and rudp
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Feb 21, 2018
1 parent 528790c commit 3a5c661
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ build:
go build ./...

build-all:
go build -tags "reuseport kcp quic zookeeper etcd consul ping" ./...
go build -tags "reuseport kcp quic zookeeper etcd consul ping utp rudp" ./...

test:
go test -race -tags "reuseport kcp quic zookeeper etcd consul ping" ./...
go test -tags "reuseport kcp quic zookeeper etcd consul ping utp rudp" ./...
2 changes: 2 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (t *PBArith) Mul(ctx context.Context, args *testutils.ProtoArgs, reply *tes
}

func TestClient_IT(t *testing.T) {
server.UsePool = false

s := server.NewServer()
s.RegisterName("Arith", new(Arith), "")
s.RegisterName("PBArith", new(PBArith), "")
Expand Down
11 changes: 10 additions & 1 deletion client/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"github.com/smallnest/rpcx/share"
)

type makeConnFn func(c *Client, network, address string) (net.Conn, error)

var makeConnMap = make(map[string]makeConnFn)

// Connect connects the server via specified network.
func (c *Client) Connect(network, address string) error {
var conn net.Conn
Expand All @@ -28,7 +32,12 @@ func (c *Client) Connect(network, address string) error {
case "unix":
conn, err = newDirectConn(c, network, address)
default:
conn, err = newDirectConn(c, network, address)
fn := makeConnMap[network]
if fn != nil {
conn, err = fn(c, network, address)
} else {
conn, err = newDirectConn(c, network, address)
}
}

if err == nil && conn != nil {
Expand Down
11 changes: 1 addition & 10 deletions client/connection_kcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,5 @@ import (
)

func newDirectKCPConn(c *Client, network, address string) (net.Conn, error) {
var conn net.Conn
var err error

conn, err = kcp.DialWithOptions(address, c.option.Block.(kcp.BlockCrypt), 10, 3)

if err != nil {
return nil, err
}

return conn, nil
return kcp.DialWithOptions(address, c.option.Block.(kcp.BlockCrypt), 10, 3)
}
11 changes: 1 addition & 10 deletions client/connection_quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,10 @@ import (
)

func newDirectQuicConn(c *Client, network, address string) (net.Conn, error) {
var conn net.Conn
var err error

tlsConf := c.option.TLSConfig
if tlsConf == nil {
tlsConf = &tls.Config{InsecureSkipVerify: true}
}

conn, err = quicconn.Dial(address, tlsConf)

if err != nil {
return nil, err
}

return conn, nil
return quicconn.Dial(address, tlsConf)
}
21 changes: 21 additions & 0 deletions client/connection_rudp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// +build rudp

package client

import (
"net"
)

func init() {
makeConnMap["rudp"] = newDirectRUDPConn
}

func newDirectRUDPConn(c *Client, network, address string) (net.Conn, error) {
addr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return nil, err
}

laddr := net.UDPAddr{IP: net.IPv4zero, Port: 0}
return net.DialUDP("udp", &laddr, addr)
}
17 changes: 17 additions & 0 deletions client/connection_utp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build utp

package client

import (
"net"

"github.com/anacrolix/utp"
)

func init() {
makeConnMap["utp"] = newDirectUTPConn
}

func newDirectUTPConn(c *Client, network, address string) (net.Conn, error) {
return utp.Dial(address)
}
27 changes: 27 additions & 0 deletions server/rudp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build rudp

package server

import (
"net"

"github.com/u35s/rudp"
)

func init() {
makeListeners["rudp"] = rudpMakeListener
}

func rudpMakeListener(s *Server, address string) (ln net.Listener, err error) {
addr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return nil, err
}

l, err := net.ListenUDP("udp", addr)
if err != nil {
return nil, err
}

return rudp.NewListener(l), nil
}
17 changes: 17 additions & 0 deletions server/utp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build utp

package server

import (
"net"

"github.com/anacrolix/utp"
)

func init() {
makeListeners["utp"] = utpMakeListener
}

func utpMakeListener(s *Server, address string) (ln net.Listener, err error) {
return utp.Listen(address)
}

0 comments on commit 3a5c661

Please sign in to comment.