-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpconn.go
38 lines (35 loc) · 827 Bytes
/
pconn.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
package plistener
import (
"net"
)
type pConn struct {
net.Conn
listener *PListener
}
func (pConn *pConn) Close() error {
myListener := pConn.listener
if myListener != nil {
myListener.connCond.L.Lock()
myListener.currentConn--
myListener.connCond.L.Unlock()
tcpAddr := pConn.Conn.RemoteAddr().(*net.TCPAddr)
var ip [16]byte
copy(ip[:], tcpAddr.IP.To16())
record := myListener.getRecord(ip)
record.mut.Lock()
defer record.mut.Unlock()
if len(record.activeConns) > 0 {
var index int
for i, c := range record.activeConns {
if c == pConn {
index = i
break
}
}
record.activeConns[index] = record.activeConns[len(record.activeConns)-1]
record.activeConns = record.activeConns[:len(record.activeConns)-1]
}
myListener.connCond.Signal()
}
return pConn.Conn.Close()
}