forked from openp2p-cn/openp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.go
43 lines (39 loc) · 1.04 KB
/
udp.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
42
43
package openp2p
import (
"bytes"
"encoding/binary"
"net"
"time"
)
func UDPWrite(conn *net.UDPConn, dst net.Addr, mainType uint16, subType uint16, packet interface{}) (len int, err error) {
msg, err := newMessage(mainType, subType, packet)
if err != nil {
return 0, err
}
if dst == nil {
return conn.Write(msg)
}
return conn.WriteTo(msg, dst)
}
func UDPRead(conn *net.UDPConn, timeout time.Duration) (ra net.Addr, head *openP2PHeader, result []byte, len int, err error) {
if timeout > 0 {
err = conn.SetReadDeadline(time.Now().Add(timeout))
if err != nil {
gLog.Println(LvERROR, "SetReadDeadline error")
return nil, nil, nil, 0, err
}
}
result = make([]byte, 1024)
len, ra, err = conn.ReadFrom(result)
if err != nil {
// gLog.Println(LevelDEBUG, "ReadFrom error")
return nil, nil, nil, 0, err
}
head = &openP2PHeader{}
err = binary.Read(bytes.NewReader(result[:openP2PHeaderSize]), binary.LittleEndian, head)
if err != nil {
gLog.Println(LvERROR, "parse p2pheader error:", err)
return nil, nil, nil, 0, err
}
return
}