-
Notifications
You must be signed in to change notification settings - Fork 1
/
packetclient.go
39 lines (33 loc) · 984 Bytes
/
packetclient.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
package hacket
import (
"net"
"time"
)
// PacketClient defines a packet client interface
type PacketClient interface {
WriteTo(msg PacketMessage, addr net.Addr) (int, error)
}
var _ PacketClient = &udpPacketClientImpl{}
// udpPacketClientImpl implements the packet client interface
type udpPacketClientImpl struct {
conn net.PacketConn
options *packetOptions
}
// NewUDPPacketClient creates a new UDP packet client
func newUDPPacketClient(conn *net.UDPConn, options *packetOptions) *udpPacketClientImpl {
return &udpPacketClientImpl{
conn: conn,
options: options,
}
}
// WriteTo writes a packet to the target destination
func (pc *udpPacketClientImpl) WriteTo(msg PacketMessage, address net.Addr) (int, error) {
if pc.options.WriteDeadline > 0 {
deadline := time.Now().Add(pc.options.WriteDeadline)
if err := pc.conn.SetWriteDeadline(deadline); err != nil {
return 0, err
}
}
// Interal packet write function
return pc.conn.WriteTo(msg, address)
}