Commit b889df9d authored by wei.xuan's avatar wei.xuan

feat:bind local ip

parent 3db78c9b
......@@ -27,9 +27,7 @@ import (
"sync"
"sync/atomic"
"time"
)
import (
"github.com/adamweixuan/gostnops/bytes"
"github.com/adamweixuan/gostnops/net"
gxsync "github.com/adamweixuan/gostnops/sync"
......@@ -102,12 +100,12 @@ func newClient(t EndPointType, opts ...ClientOption) *client {
// NewTCPClient builds a tcp client.
func NewTCPClient(opts ...ClientOption) Client {
return newClient(TCP_CLIENT, opts...)
return newClient(TcpClient, opts...)
}
// NewUDPClient builds a connected udp client
func NewUDPClient(opts ...ClientOption) Client {
return newClient(UDP_CLIENT, opts...)
return newClient(UdpClient, opts...)
}
func (c *client) ID() EndPointID {
......@@ -134,18 +132,27 @@ func (c *client) dialTCP() Session {
conn, err = tls.DialWithDialer(d, "tcp", c.addr, sslConfig)
}
} else {
conn, err = net.DialTimeout("tcp", c.addr, connectTimeout)
if c.laddr != nil {
dialer := net.Dialer{
Timeout: connectTimeout,
LocalAddr: c.laddr,
}
conn, err = dialer.Dial("tcp", c.addr)
} else {
conn, err = net.DialTimeout("tcp", c.addr, connectTimeout)
}
}
if err == nil && gxnet.IsSameAddr(conn.RemoteAddr(), conn.LocalAddr()) {
conn.Close()
err = errSelfConnect
}
if err == nil {
log.Infof("net.DialSuccess(laddr:%s->addr:%s)", c.laddr, c.addr)
return newTCPSession(conn, c)
}
log.Infof("net.DialTimeout(addr:%s, timeout:%v) = error:%+v", c.addr, connectTimeout, perrors.WithStack(err))
<-gxtime.After(connectInterval)
log.Warnf("net.DialTimeout(addr:%s, timeout:%v) = error:%+v", c.addr, connectTimeout, err)
<-gxtime.After(time.Second)
}
}
......@@ -316,11 +323,11 @@ func (c *client) dialWSS() Session {
func (c *client) dial() Session {
switch c.endPointType {
case TCP_CLIENT:
case TcpClient:
return c.dialTCP()
case UDP_CLIENT:
case UdpClient:
return c.dialUDP()
case WS_CLIENT:
case WsClient:
return c.dialWS()
case WSS_CLIENT:
return c.dialWSS()
......
......@@ -25,9 +25,7 @@ import (
"net"
"sync"
"time"
)
import (
"github.com/golang/snappy"
"github.com/gorilla/websocket"
perrors "github.com/pkg/errors"
......
......@@ -18,6 +18,8 @@
package getty
import (
"net"
gxsync "github.com/adamweixuan/gostnops/sync"
)
......@@ -101,7 +103,7 @@ type ClientOption func(*ClientOptions)
type ClientOptions struct {
addr string
laddr string // local addr
laddr net.Addr // local addr
number int
reconnectInterval int // reConnect Interval
......@@ -125,7 +127,7 @@ func WithServerAddress(addr string) ClientOption {
}
// WithLocalAddressClient @addr is server address.
func WithLocalAddressClient(addr string) ClientOption {
func WithLocalAddressClient(addr net.Addr) ClientOption {
return func(o *ClientOptions) {
o.laddr = addr
}
......
......@@ -28,17 +28,12 @@ import (
"strings"
"sync"
"time"
)
import (
gxnet "github.com/adamweixuan/gostnops/net"
gxsync "github.com/adamweixuan/gostnops/sync"
gxtime "github.com/adamweixuan/gostnops/time"
"github.com/gorilla/websocket"
perrors "github.com/pkg/errors"
uatomic "go.uber.org/atomic"
)
......
......@@ -26,23 +26,18 @@ import (
"runtime"
"sync"
"time"
)
import (
gxbytes "github.com/adamweixuan/gostnops/bytes"
gxcontext "github.com/adamweixuan/gostnops/context"
gxtime "github.com/adamweixuan/gostnops/time"
"github.com/gorilla/websocket"
perrors "github.com/pkg/errors"
uatomic "go.uber.org/atomic"
)
const (
maxReadBufLen = 4 * 1024
netIOTimeout = 1e9 // 1s
netIOTimeout = 1e9 * 10 // 10s
period = 60 * 1e9 // 1 minute
pendingDuration = 3e9
// MaxWheelTimeSpan 900s, 15 minute
......
......@@ -22,9 +22,7 @@ import (
"crypto/x509"
"fmt"
"io/ioutil"
)
import (
perrors "github.com/pkg/errors"
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment