Пример #1
0
func TextHandle(buffer []byte, User *model.User, userCache *model.UserCache, Conn *net.Conn) error {

	text := core.TextMsg{}
	err := text.Decode(buffer)
	if err != nil {
		err = fmt.Errorf("TextHandle Error : %v", err)
		fmt.Println(err.Error())
		return err
	}

	var destUser *model.User
	if destUser = userCache.Query(text.DestUuid); destUser == nil {
		err = fmt.Errorf("Don't have the destUser : %v", text.DestUuid)
		fmt.Println(err.Error())
		return err
	}

	text.DestUuid = User.Uuid
	requestBuff, err := text.Encode()
	if err != nil {
		err = fmt.Errorf("TextHandle Error : %v", err)
		fmt.Println(err.Error())
		return err
	}

	n, err := destUser.Write(requestBuff)
	if err != nil || n == 0 {
		err = errors.New("Write to " + text.DestUuid + " error")
		return err
	}
	fmt.Println("send text ok")
	return nil
}
Пример #2
0
func AudioFragmentHandle(buffer []byte, User *model.User, userCache *model.UserCache, Conn *net.Conn) error {
	DestUuid, err := core.UuidToString(buffer[12:28])
	if err != nil {
		err = fmt.Errorf("AudioFragmentHandle Error : %v", err)
		fmt.Println(err.Error())
		return err
	}

	var destUser *model.User
	if destUser = userCache.Query(DestUuid); destUser == nil {
		err = fmt.Errorf("Don't have the destUser : %v", DestUuid)
		fmt.Println(err.Error())
		return err
	}

	DestUuidBuff, err := core.StringToUuid(User.Uuid)
	if err != nil {
		err = fmt.Errorf("AudioFragmentHandle Error : %v", err)
		fmt.Println(err.Error())
		return err
	}

	copy(buffer[12:28], DestUuidBuff[0:16])
	n, err := destUser.Write(buffer)
	if n == 0 || err != nil {
		err = errors.New("Write to " + DestUuid + " error")
		return err
	}
	fmt.Println("send audio ok")
	return nil
}
Пример #3
0
func Login(buffer []byte, Conn *net.Conn, User *model.User, userCache *model.UserCache) *model.User {
	confirm := core.Confirm{}
	err := confirm.Decode(buffer)
	if err != nil {
		fmt.Printf("TCPHandle Login Error:%v", err.Error)
		return nil
	}
	if confirm.UserUuid != "" {
		remote := (*Conn).RemoteAddr().String()
		ips := strings.Split(remote, ":")
		ip := net.ParseIP(ips[0])
		User = &model.User{IP: ip, Port: int32(confirm.Port), Uuid: confirm.UserUuid, Conn: Conn}
	}
	if User == nil {
		return nil
	}
	userCache.Add(User)
	return User
}
Пример #4
0
func FileRequestHandle(buffer []byte, Conn *net.Conn, userCache *model.UserCache, User *model.User) error {
	fileRequest := core.FileMsgRequest{}
	err := fileRequest.Decode(buffer)
	if err != nil {
		err = fmt.Errorf("FileRequestHandle Error: %v", err)
		fmt.Println(err.Error())
		return err
	}

	if fileRequest.Number != core.FILE_REQ_SEND_FILE {
		err = errors.New("Not the file request number")
		fmt.Println(err.Error())
		return err
	}

	var destUser *model.User
	//这里的DestID应该是请求接收方
	if destUser = userCache.Query(fileRequest.DestUuid); destUser == nil {
		err = fmt.Errorf("Don't have the DestUser : %v", fileRequest.DestUuid)
		fmt.Println(err.Error())
		return err
	}
	//下面这一部分是向请求接收方设置参数,已请求接收方的角度来看,DestID为当前用户的Uuid
	fileRequest.DestUuid = User.Uuid
	//服务器发送发起方的请求给请求接收方
	fileRequest.QCHeadBase.Number = core.FILE_RES_SEND_TRAN
	response, err := fileRequest.Encode()
	if err != nil {
		err = fmt.Errorf("FileRequestHandle Error: %v", err)
		fmt.Println(err.Error())
		return err
	}
	//向目标用户的流中写入数据
	n, err := destUser.Write(response)
	if err != nil || n == 0 {
		err = errors.New("Write file response fail")
		fmt.Println(err.Error())
		return err
	}

	return nil
}
Пример #5
0
func FileResponseHandle(buffer []byte, Conn *net.Conn, userCache *model.UserCache, User *model.User, TokenIP string, TokenPort int) error {
	fileResponse := core.FileMsgResponse2{}
	err := fileResponse.Decode(buffer)
	if err != nil {
		err = fmt.Errorf("FileResponseHandle Error : %v", err)
		fmt.Println(err.Error())
		return err
	}
	if fileResponse.Number != core.FILE_REQ_SEND_STATUS {
		err = errors.New("Not has file response nubmer ")
		fmt.Println(err.Error())
		return err
	}

	var destUser *model.User
	//这里的DestID应该是之前的文件请求发起方
	if destUser = userCache.Query(fileResponse.DestUuid); destUser == nil {
		err = fmt.Errorf("Don't have the destUser: %v", fileResponse.DestUuid)
		fmt.Println(err.Error())
		return err
	}

	fileResponse.DestUuid = destUser.Uuid
	//服务器发送请求接收方的回应给请求发起方:
	fileResponse.Number = core.FILE_RES_SEND_STATUS
	responseBuff, err := fileResponse.Encode()
	if err != nil {
		err = fmt.Errorf("FileResponseHandle Error: %v", err)
		fmt.Println(err.Error())
		return err
	}
	n, err := (*destUser.Conn).Write(responseBuff)
	if n == 0 || err != nil {
		err = errors.New("Write response to from error")
		fmt.Println(err)
		return err
	}

	if fileResponse.State == 0 {
		return nil
	}

	conn, err := net.Dial("tcp", TokenIP+":"+strconv.Itoa(TokenPort))
	if err != nil {
		err = errors.New("token server is closed")
		fmt.Println(err.Error())
		return err
	}
	//destUuid应该是之前的文件请求发起方,User应该是之前的文件接收方
	related := &model.Related{
		protocol.TsHead{Len: 44, Version: 1, Type: core.CTRL, Number: protocol.USER_REQ_SEND_TOKEN},
		destUser.Uuid,
		User.Uuid,
	}
	tokenRequestBuff, err := related.Encode()
	if err != nil {
		err = fmt.Errorf("FileResponseHandle Error : %v", err)
		fmt.Println(err.Error())
		return err
	}
	n, err = conn.Write(tokenRequestBuff)
	if n == 0 && err != nil {
		err = errors.New("Write the tokenRequestBuff error")
		fmt.Println(err.Error())
		return err
	}
	tokenResponseBuff := make([]byte, 52)
	n, err = conn.Read(tokenResponseBuff)
	if n == 0 && err != nil && n != len(tokenResponseBuff) {
		err = errors.New("Read the tokenResponseBuff error")
		fmt.Println(err.Error())
		return err
	}
	conn.Close()

	token := &model.Token{}
	err = token.Decode(tokenResponseBuff)
	if err != nil {
		err = fmt.Errorf("FileResponseHandle Error : %v", err)
		fmt.Println(err.Error())
		return err
	}

	if token.Number != protocol.USER_RES_SEND_TOKEN {
		err = errors.New("Don't have the USER_RES_SEND_TOKEN type")
		fmt.Println(err.Error())
		return err
	}

	fromResponse := core.FileMsgResponse{}
	destResponse := core.FileMsgResponse{}

	fromResponse.QCHeadBase.Len = 51
	fromResponse.QCHeadBase.Version = 1
	fromResponse.QCHeadBase.Type = core.FILE
	//服务器发送token和TDS的IP地址、对client提供服务的端口以及连接方式给请求发起方
	fromResponse.QCHeadBase.Number = core.FILE_RES_SEND_ACTIVE_STATUS
	//User应该是之前的文件请求接收方
	fromResponse.DestUuid = User.Uuid

	fromResponse.IP = TokenIP
	fromResponse.Port = 9004
	fromResponse.Token = token.FromToken
	fromResponse.ConnMethod = protocol.TCP

	destResponse.QCHeadBase.Len = 51
	destResponse.QCHeadBase.Version = 1
	destResponse.QCHeadBase.Type = core.FILE
	//服务器发送token和TDS的IP地址,对client提供服务的端口以及连接方式给请求接收方:
	destResponse.QCHeadBase.Number = core.FILE_RES_SEND_PASSIVE_STATUS
	destResponse.DestUuid = destUser.Uuid

	destResponse.IP = TokenIP
	destResponse.Port = 9004
	destResponse.Token = token.DestToken
	destResponse.ConnMethod = protocol.TCP

	fromBuff, err := fromResponse.Encode()
	if err != nil {
		err = fmt.Errorf("FileResponseHandle Error : %v", err)
		fmt.Println(err.Error())
		return err
	}

	destBuff, err := destResponse.Encode()
	if err != nil {
		err = fmt.Errorf("FileResponseHandle Error : %v", err)
		fmt.Println(err.Error())
		return err
	}
	//User是文件请求的接收方
	n, err = (*User.Conn).Write(destBuff)
	if n == 0 || err != nil {
		err = errors.New("Send dest buff error")
		fmt.Println(err.Error())
		return err
	}
	//destUser是文件请求的发起方
	n, err = (*destUser.Conn).Write(fromBuff)
	if n == 0 || err != nil {
		err = errors.New("Send from buff error")
		return err
	}
	fmt.Println("send ok")
	return nil
}