//NewUDPServer 根据 tick 心跳包时间间隔, tickport 监听广播包端口, sendport 发送广播包端口, //schan 发送数据来源channel, rchan 数据接收channel,这5个参数生成UDPServer实例 func NewUDPServer(tick int, tickport int, sendport int, schan chan []byte, rchan chan []byte) (server *UDPServer) { fmt.Println("send port:", sendport, " tickport:", tickport) send, err := net.ResolveUDPAddr("udp", misc.GetLocalIP()+":"+strconv.Itoa(sendport)) errorCheck(err) rece, err := net.ResolveUDPAddr("udp", misc.GetLocalIP()+":"+strconv.Itoa(tickport)) errorCheck(err) bcast, err := net.ResolveUDPAddr("udp", net.IPv4bcast.String()+":"+strconv.Itoa(tickport)) ctrl := make(chan bool, 1) server = &UDPServer{tick: tick, sendAddr: send, receiveAddr: rece, bcastAddr: bcast, sendchan: schan, receivechan: rchan, sexitchan: ctrl, rexitchan: ctrl} fmt.Println("send:" + send.String() + " ticket:" + bcast.String()) return }
func UDPTest(t *testing.T) { receChan := make(chan []byte, 1024) sendChan := make(chan []byte, 1024) broadcast := NewUDPServer(1000, 998, 999) broadcast.Start(sendChan, receChan) go func() { for { rbuf := <-receChan var notice packet.NoticePacket err := json.Unmarshal(rbuf, ¬ice) if err != nil { t.Error(err.Error()) return } check(t, ¬ice) } }() ip := []byte(misc.GetLocalIP()) p := packet.NewNoticePacket(packet.STATE_ONLINE, ip, []byte("Jack Lee")) sbuf, err := json.Marshal(p) if err != nil { t.Error(err.Error()) return } sendChan <- sbuf sendChan <- []byte("{\"State\":1,\"IP\":\"MTAuMC4yMC4xNjg=\"}") sendChan <- []byte("{\"State\":2,\"IP\":\"MTAuMC4yMC4xNjg=\"}") sendChan <- []byte("{\"State\":3,\"IP\":\"MTAuMC4yMC4xNjg=\"}") sendChan <- []byte("{\"State\":4,\"IP\":\"MTAuMC4yMC4xNjg=\"}") sendChan <- []byte("{\"State\":5,\"IP\":\"MTAuMC4yMC4xNjg=\"}") //time.Sleep(5000 * time.Millisecond) }
func CreateTCPInst(t *testing.T) { receChan := make(chan []byte, 1024) sendChan := make(chan []byte, 1024) server := NewTCPServer(misc.GetLocalIP(), sport, receChan, sendChan, cport) sport += 10 cport += 500 server.Start() server.CreateSingleChat(misc.GetLocalIP()) server.Send([]string{misc.GetLocalIP()}, &packet.MessagePacket{IP: []byte(misc.GetLocalIP()), Class: packet.FILE_UNKNOWN, Content: "hello", Path: "0000"}) go func() { for { rbuf := <-receChan var p packet.MessagePacket err := json.Unmarshal(rbuf, &p) if err != nil { t.Error(err.Error()) return } t.Log("IP:", string(p.IP), ":", p.Content) } }() }
//NewManager 生成 Manager 类实例 func NewManager() *Manager { user := new(UserInfo) user.IP = misc.GetLocalIP() user.State = packet.STATE_ONLINE user.NickName = nickname usc := make(chan []byte /*, 1024*/) urc := make(chan []byte /*, 1024*/) tsc := make(chan []byte /*, 1024*/) trc := make(chan []byte /*, 1024*/) map1 := make(map[string]*Client) map2 := make(map[string]*time.Timer) udpsrv := server.NewUDPServer(ticktime, sendUDPPort, listenUDPPort, usc, urc) tcpsrv := server.NewTCPServer(user.IP, listenTCPPort, trc, tsc, sendTCPPort) return &Manager{usc, urc, tsc, trc, map1, map2, udpsrv, tcpsrv, user, false} }
//CreateSingleChat 与 ip 的客户端建立TCP会话 func (server *TCPServer) CreateSingleChat(ip string) (key string) { log.Println("CreateSingleChat") key = base64.StdEncoding.EncodeToString([]byte(ip)) conn, ok := server.clientList[key] //查看此IP是否已经在非离线客户端列表中,如果不存在则创建会话并添加进去,存在则直接返回此IP在客户列表中的索引 if !ok { laddr, err := net.ResolveTCPAddr("tcp", misc.GetLocalIP()+":"+strconv.Itoa(server.clientport)) errorCheck(err) raddr, err := net.ResolveTCPAddr("tcp", ip+":"+strconv.Itoa(server.sport)) server.clientport++ conn, err = net.DialTCP("tcp", laddr, raddr) errorCheck(err) server.clientList[key] = conn go ReadMsg(server.rmsgChan, conn) log.Println("CreateSingleChat ", ip, " success!") } return }