mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
improve details for package gjson/gtcp/gudp
This commit is contained in:
parent
3b811c3434
commit
e517bf7b0e
@ -9,6 +9,7 @@ package gjson
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
@ -75,7 +76,7 @@ func Decode(data interface{}, options ...Options) (interface{}, error) {
|
||||
// DecodeTo decodes json format `data` to specified golang variable `v`.
|
||||
// The parameter `data` can be either bytes or string type.
|
||||
// The parameter `v` should be a pointer type.
|
||||
func DecodeTo(data interface{}, v interface{}, options ...Options) error {
|
||||
func DecodeTo(data interface{}, v interface{}, options ...Options) (err error) {
|
||||
decoder := json.NewDecoder(bytes.NewReader(gconv.Bytes(data)))
|
||||
if len(options) > 0 {
|
||||
// The StrNumber option is for certain situations, not for all.
|
||||
@ -84,7 +85,10 @@ func DecodeTo(data interface{}, v interface{}, options ...Options) error {
|
||||
decoder.UseNumber()
|
||||
}
|
||||
}
|
||||
return decoder.Decode(v)
|
||||
if err = decoder.Decode(v); err != nil {
|
||||
err = gerror.Wrap(err, `json Decode failed`)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DecodeToJson codes json format `data` to a Json object.
|
||||
|
@ -30,13 +30,20 @@ type Retry struct {
|
||||
// NewNetConn creates and returns a net.Conn with given address like "127.0.0.1:80".
|
||||
// The optional parameter `timeout` specifies the timeout for dialing connection.
|
||||
func NewNetConn(address string, timeout ...time.Duration) (net.Conn, error) {
|
||||
duration := defaultConnTimeout
|
||||
var (
|
||||
network = `tcp`
|
||||
duration = defaultConnTimeout
|
||||
)
|
||||
if len(timeout) > 0 {
|
||||
duration = timeout[0]
|
||||
}
|
||||
conn, err := net.DialTimeout("tcp", address, duration)
|
||||
conn, err := net.DialTimeout(network, address, duration)
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `net.DialTimeout failed with address "%s", timeout "%s"`, address, duration)
|
||||
err = gerror.Wrapf(
|
||||
err,
|
||||
`net.DialTimeout failed with network "%s", address "%s", timeout "%s"`,
|
||||
network, address, duration,
|
||||
)
|
||||
}
|
||||
return conn, err
|
||||
}
|
||||
@ -44,15 +51,22 @@ func NewNetConn(address string, timeout ...time.Duration) (net.Conn, error) {
|
||||
// NewNetConnTLS creates and returns a TLS net.Conn with given address like "127.0.0.1:80".
|
||||
// The optional parameter `timeout` specifies the timeout for dialing connection.
|
||||
func NewNetConnTLS(address string, tlsConfig *tls.Config, timeout ...time.Duration) (net.Conn, error) {
|
||||
dialer := &net.Dialer{
|
||||
Timeout: defaultConnTimeout,
|
||||
}
|
||||
var (
|
||||
network = `tcp`
|
||||
dialer = &net.Dialer{
|
||||
Timeout: defaultConnTimeout,
|
||||
}
|
||||
)
|
||||
if len(timeout) > 0 {
|
||||
dialer.Timeout = timeout[0]
|
||||
}
|
||||
conn, err := tls.DialWithDialer(dialer, "tcp", address, tlsConfig)
|
||||
conn, err := tls.DialWithDialer(dialer, network, address, tlsConfig)
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `tls.DialWithDialer failed with address "%s"`, address)
|
||||
err = gerror.Wrapf(
|
||||
err,
|
||||
`tls.DialWithDialer failed with network "%s", address "%s", timeout "%s", tlsConfig "%v"`,
|
||||
network, address, dialer.Timeout, tlsConfig,
|
||||
)
|
||||
}
|
||||
return conn, err
|
||||
}
|
||||
@ -138,8 +152,10 @@ func LoadKeyCrt(crtFile, keyFile string) (*tls.Config, error) {
|
||||
}
|
||||
crt, err := tls.LoadX509KeyPair(crtPath, keyPath)
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `tls.LoadX509KeyPair failed for certFile "%s" and keyFile "%s"`, crtPath, keyPath)
|
||||
return nil, err
|
||||
return nil, gerror.Wrapf(err,
|
||||
`tls.LoadX509KeyPair failed for certFile "%s" and keyFile "%s"`,
|
||||
crtPath, keyPath,
|
||||
)
|
||||
}
|
||||
tlsConfig := &tls.Config{}
|
||||
tlsConfig.Certificates = []tls.Certificate{crt}
|
||||
|
@ -18,23 +18,33 @@ func NewNetConn(remoteAddress string, localAddress ...string) (*net.UDPConn, err
|
||||
err error
|
||||
remoteAddr *net.UDPAddr
|
||||
localAddr *net.UDPAddr
|
||||
network = `udp`
|
||||
)
|
||||
remoteAddr, err = net.ResolveUDPAddr("udp", remoteAddress)
|
||||
remoteAddr, err = net.ResolveUDPAddr(network, remoteAddress)
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `net.ResolveUDPAddr failed for address "%s"`, remoteAddress)
|
||||
return nil, err
|
||||
return nil, gerror.Wrapf(
|
||||
err,
|
||||
`net.ResolveUDPAddr failed for network "%s", address "%s"`,
|
||||
network, remoteAddress,
|
||||
)
|
||||
}
|
||||
if len(localAddress) > 0 {
|
||||
localAddr, err = net.ResolveUDPAddr("udp", localAddress[0])
|
||||
localAddr, err = net.ResolveUDPAddr(network, localAddress[0])
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `net.ResolveUDPAddr failed for address "%s"`, localAddress[0])
|
||||
return nil, err
|
||||
return nil, gerror.Wrapf(
|
||||
err,
|
||||
`net.ResolveUDPAddr failed for network "%s", address "%s"`,
|
||||
network, localAddress[0],
|
||||
)
|
||||
}
|
||||
}
|
||||
conn, err := net.DialUDP("udp", localAddr, remoteAddr)
|
||||
conn, err := net.DialUDP(network, localAddr, remoteAddr)
|
||||
if err != nil {
|
||||
err = gerror.Wrapf(err, `net.DialUDP failed for local "%s", remote "%s"`, localAddr.String(), remoteAddr.String())
|
||||
return nil, err
|
||||
return nil, gerror.Wrapf(
|
||||
err,
|
||||
`net.DialUDP failed for network "%s", local "%s", remote "%s"`,
|
||||
network, localAddr.String(), remoteAddr.String(),
|
||||
)
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user