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

feat:bind local ip

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