Exemplo n.º 1
0
func (server *UDPServer) Audio(audio *protocol.AudioMsg, addr *net.UDPAddr) {
	server.Add()

	if fromUser := server.UserCache.Query(audio.FromUuid); fromUser == nil {
		fmt.Println("Not have fromUser ", audio.FromUuid)
		return
	}

	var destUser *model.User
	if destUser = server.UserCache.Query(audio.DestUuid); destUser == nil {
		fmt.Println("Not have destUser", audio.DestUuid)
		return
	}

	if destUser.Port == 0 {
		fmt.Println("The destUser's Port is 0")
		return
	}

	buffer, err := audio.Encode()
	if err != nil {
		fmt.Printf("UDPServer Audio Error: %v", err.Error())
		return
	}
	n, err := server.Conn.WriteToUDP(buffer, &net.UDPAddr{IP: destUser.IP, Port: int(destUser.Port)})
	if n == 0 || err != nil {
		fmt.Printf("UDPServer send data error: %v", err.Error())
		return
	}
}
Exemplo n.º 2
0
func (server *UDPServer) Unpack(data []byte, addr *net.UDPAddr) {
	if len(data) != 0 {
		Type := data[9]
		switch int8(Type) {
		case protocol.AUDIO:
			audio := protocol.AudioMsg{}
			err := audio.Decode(data[:])
			if err != nil {
				fmt.Printf("UDPServer Unpack Error:%v", err.Error())
				return
			}
			server.Audio(&audio, addr)
		default:
		}
	}
}