2 Star 0 Fork 0

韧启科技/can

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
dial.go 2.02 KB
Copy Edit Raw Blame History
bz5011 authored 2025-11-05 14:13 +08:00 . init
package socketcan
import (
"context"
"net"
)
const udp = "udp"
type DialOption func(*dialOpts)
// Dial connects to the address on the named net.
//
// Linux only: If net is "can" it creates a SocketCAN connection to the device
// (address is interpreted as a device name).
//
// If net is "udp" it assumes UDP multicast and sets up 2 connections, one for
// receiving and one for transmitting.
// See: https://golang.org/pkg/net/#Dial
func Dial(network, address string, opt ...DialOption) (net.Conn, error) {
switch network {
case udp:
return udpTransceiver(network, address)
case canRawNetwork:
return dialRaw(address, opt...) // platform-specific
default:
return net.Dial(network, address)
}
}
// DialContext connects to the address on the named net using
// the provided context.
//
// Linux only: If net is "can" it creates a SocketCAN connection to the device
// (address is interpreted as a device name).
//
// See: https://golang.org/pkg/net/#Dialer.DialContext
func DialContext(ctx context.Context, network, address string, opt ...DialOption) (net.Conn, error) {
switch network {
case canRawNetwork:
return dialCtx(ctx, func() (net.Conn, error) {
return dialRaw(address, opt...)
})
case udp:
return dialCtx(ctx, func() (net.Conn, error) {
return udpTransceiver(network, address)
})
default:
var d net.Dialer
return d.DialContext(ctx, network, address)
}
}
func dialCtx(ctx context.Context, connProvider func() (net.Conn, error)) (net.Conn, error) {
resultChan := make(chan struct {
conn net.Conn
err error
})
go func() {
conn, err := connProvider()
resultChan <- struct {
conn net.Conn
err error
}{conn: conn, err: err}
}()
// wait for connection or timeout
select {
case result := <-resultChan:
return result.conn, result.err
case <-ctx.Done():
// timeout - make sure we clean up the connection
// error handling not possible since we've already returned
go func() {
result := <-resultChan
if result.conn != nil {
_ = result.conn.Close()
}
}()
return nil, ctx.Err()
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/a-fuzz/can.git
git@gitee.com:a-fuzz/can.git
a-fuzz
can
can
v0.12.2

Search