-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtcp.go
53 lines (47 loc) · 1.03 KB
/
tcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gombus
import (
"context"
"fmt"
"net"
"time"
)
type conn struct {
conn net.Conn
}
func DialTCP(addr string) (Conn, error) {
var dialer net.Dialer
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel()
c, err := dialer.DialContext(ctx, "tcp", addr)
if err != nil {
return nil, fmt.Errorf("failed to dial: %w", err)
}
return &conn{conn: c}, nil
}
func (c *conn) Read(b []byte) (n int, err error) {
return c.conn.Read(b)
}
func (c *conn) Write(b []byte) (n int, err error) {
return c.conn.Write(b)
}
func (c *conn) Close() error {
return c.conn.Close()
}
func (c *conn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
func (c *conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
func (c *conn) SetDeadline(t time.Time) error {
return c.conn.SetDeadline(t)
}
func (c *conn) SetReadDeadline(t time.Time) error {
return c.conn.SetReadDeadline(t)
}
func (c *conn) SetWriteDeadline(t time.Time) error {
return c.conn.SetWriteDeadline(t)
}
func (c *conn) Conn() net.Conn {
return c.conn
}