/
ipmsg.go
141 lines (126 loc) · 2.82 KB
/
ipmsg.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package ipmsg
import (
"bytes"
"errors"
"fmt"
"net"
"time"
)
type IPMSG struct {
ClientData ClientData
Conn *net.UDPConn
Conf *IPMSGConfig
Handlers []*EventHandler
PacketNum int
}
type IPMSGConfig struct {
NickName string
GroupName string
UserName string
HostName string
Port int
Local string
}
const (
DefaultPort int = 2425
Buflen int = 65535
)
func NewIPMSGConf() *IPMSGConfig {
conf := &IPMSGConfig{
Port: DefaultPort,
}
return conf
}
func NewIPMSG(conf *IPMSGConfig) (*IPMSG, error) {
ipmsg := &IPMSG{
PacketNum: 0,
}
ipmsg.Conf = conf
// UDP server
service := fmt.Sprintf("%v:%d", conf.Local, conf.Port)
//fmt.Println("service =", service)
udpAddr, err := net.ResolveUDPAddr("udp", service)
if err != nil {
return ipmsg, err
}
conn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
return ipmsg, err
}
ipmsg.Conn = conn
return ipmsg, err
}
func (ipmsg *IPMSG) Close() error {
conn := ipmsg.Conn
if conn == nil {
err := errors.New("Conn is not defined")
return err
}
err := conn.Close()
return err
}
func (ipmsg *IPMSG) BuildData(addr *net.UDPAddr, msg string, cmd Command) *ClientData {
conf := ipmsg.Conf
clientdata := NewClientData("", addr)
clientdata.Version = 1
clientdata.PacketNum = ipmsg.GetNewPacketNum()
clientdata.User = conf.UserName
clientdata.Host = conf.HostName
clientdata.Command = cmd
clientdata.Option = msg
return clientdata
}
func (ipmsg *IPMSG) SendMSG(addr *net.UDPAddr, msg string, cmd Command) error {
clientdata := ipmsg.BuildData(addr, msg, cmd)
conn := ipmsg.Conn
_, err := conn.WriteToUDP([]byte(clientdata.String()), addr)
if err != nil {
return err
}
return nil
}
func (ipmsg *IPMSG) RecvMSG() (*ClientData, error) {
var buf [Buflen]byte
conn := ipmsg.Conn
_, addr, err := conn.ReadFromUDP(buf[0:])
if err != nil {
return nil, err
}
trimmed := bytes.Trim(buf[:], "\x00")
clientdata := NewClientData(string(trimmed[:]), addr)
handlers := ipmsg.Handlers
for _, v := range handlers {
err := v.Run(clientdata, ipmsg)
if err != nil {
return clientdata, err
}
}
return clientdata, nil
}
// convert net.Addr to net.UDPAddr
func (ipmsg *IPMSG) UDPAddr() (*net.UDPAddr, error) {
conn := ipmsg.Conn
if conn == nil {
err := errors.New("Conn is not defined")
return nil, err
}
addr := conn.LocalAddr()
network := addr.Network()
str := addr.String()
//fmt.Println("str =", str)
udpAddr, err := net.ResolveUDPAddr(network, str)
return udpAddr, err
}
func (ipmsg *IPMSG) AddEventHandler(ev *EventHandler) {
sl := ipmsg.Handlers
sl = append(sl, ev)
ipmsg.Handlers = sl
}
func (ipmsg *IPMSG) GetNewPacketNum() int {
ipmsg.PacketNum++
return int(time.Now().Unix()) + ipmsg.PacketNum
}
func (ipmsg *IPMSG) Myinfo() string {
conf := ipmsg.Conf
return fmt.Sprintf("%v\x00%v\x00", conf.NickName, conf.GroupName)
}