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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package commonhandle
import (
"io"
"log"
"net"
"net/url"
)
//使用 协程处理 tcp 连接
type CommonHandler struct {
}
//开启协程,简历连接
func transferData(con net.Conn, to *url.URL) {
//defer con.Close()
target, err := net.Dial("tcp", to.Host)
if err != nil {
log.Printf("error occur [%+v]\n", err)
return
}
//defer target.Close()
go func() {
io.Copy(con, target)
con.Close()
target.Close()
}()
go func() {
io.Copy(target, con)
target.Close()
con.Close()
}()
//waitExit := make(chan bool, 1)
//var buf = make([]byte, consts.TcpBufSize)
//for {
// nbuf, err := con.Read(buf)
// if err != nil {
// log.Printf("unable read conn data %+v\n", err)
// break
// }
// _, err = target.Write(buf[:nbuf])
// if err != nil {
// log.Printf("unable write conn data %+v", err)
// }
//}
//<-waitExit
}
func handle0(from, to *url.URL) error {
log.Printf("listen host [%v]", from.Host)
//监听本地端口
listener, err := net.Listen("tcp", from.Host)
if err != nil {
return err
}
defer listener.Close()
for {
//处理连接事件
con, err := listener.Accept()
if err != nil {
log.Printf("connct error %+v\n", err)
}
go transferData(con, to)
}
}
func (*CommonHandler) Serve(from, to *url.URL) {
if from == nil || to == nil {
panic("empty url config")
}
//非阻塞的方式处理多个连接
go func() {
err := handle0(from, to)
if err != nil {
log.Printf("%+v\n", err)
}
}()
}
|