-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
56 lines (48 loc) · 929 Bytes
/
client.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
44
45
46
47
48
49
50
51
52
53
54
55
56
package ri
import (
"fmt"
"log"
"net"
"time"
)
type Client struct {
Id string
conn *net.UDPConn
ni *NetworkInfo
//UDP resend time to ensure UDP socket lost
UDPResend int
}
func NewClient() *Client {
c := new(Client)
c.UDPResend = 8
return c
}
func (c *Client) ConnectTo(srvAddr string) error {
ServerAddr, err := net.ResolveUDPAddr("udp", srvAddr)
if err != nil {
return err
}
Conn, err := net.DialUDP("udp", nil, ServerAddr)
if err != nil {
return err
}
c.conn = Conn
c.ni = NewNI()
c.ni.InitNetworkInfo(c.conn.LocalAddr().String())
return nil
}
func (c *Client) SendRoutingInfo() error {
for i := 0; i < c.UDPResend; i++ {
msg := EncodeRoutingInfo(c.Id, c.ni)
buf := []byte(msg)
log.Println("write->", msg)
_, err := c.conn.Write(buf)
if err != nil {
fmt.Println(msg, err)
return err
}
time.Sleep(time.Second * 1)
}
log.Println("SendRoutingInfo done")
return nil
}