Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
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
package internet
 
import (
    "net"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/features/stats"
)
 
type Connection interface {
    net.Conn
}
 
type AbstractPacketConnReader interface {
    ReadFrom(p []byte) (n int, addr net.Addr, err error)
}
 
type AbstractPacketConnWriter interface {
    WriteTo(p []byte, addr net.Addr) (n int, err error)
}
 
type AbstractPacketConn interface {
    AbstractPacketConnReader
    AbstractPacketConnWriter
    common.Closable
}
 
type PacketConn interface {
    AbstractPacketConn
    net.PacketConn
}
 
type StatCouterConnection struct {
    Connection
    ReadCounter  stats.Counter
    WriteCounter stats.Counter
}
 
func (c *StatCouterConnection) Read(b []byte) (int, error) {
    nBytes, err := c.Connection.Read(b)
    if c.ReadCounter != nil {
        c.ReadCounter.Add(int64(nBytes))
    }
 
    return nBytes, err
}
 
func (c *StatCouterConnection) Write(b []byte) (int, error) {
    nBytes, err := c.Connection.Write(b)
    if c.WriteCounter != nil {
        c.WriteCounter.Add(int64(nBytes))
    }
    return nBytes, err
}