-
Notifications
You must be signed in to change notification settings - Fork 1
/
hacket.go
41 lines (39 loc) · 1.06 KB
/
hacket.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
package hacket
import (
"net"
)
// New initializes a packet server and a packet client
func New(network string, address string, options ...Options) (PacketServer, PacketClient, error) {
// Setup the connection based on the network protocol
switch network {
case "udp":
udpOptions := defaultPacketOption()
// set all options if supplied
for _, opt := range options {
opt.apply(udpOptions)
}
udpAddr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, nil, err
}
conn, err := net.ListenUDP(network, udpAddr)
if err != nil {
return nil, nil, err
}
if udpOptions.ReadBufferSize > 0 {
if err := conn.SetReadBuffer(udpOptions.ReadBufferSize); err != nil {
return nil, nil, err
}
}
if udpOptions.WriteBufferSize > 0 {
if err := conn.SetWriteBuffer(udpOptions.WriteBufferSize); err != nil {
return nil, nil, err
}
}
udpServer := newUDPPacketServer(conn, udpOptions)
udpClient := newUDPPacketClient(conn, udpOptions)
return udpServer, udpClient, nil
default:
return nil, nil, ErrInvalidProtocol
}
}