Пример #1
0
func (related *Related) Decode(buffer []byte) (err error) {
	if len(buffer) < 44 {
		err = errors.New("Related Decode Error: the []byte length is shorter than 44")
		return err
	}
	index := 0
	err = related.TsHead.Decode(buffer[index : index+12])
	if err != nil {
		err = fmt.Errorf("related Decode Error: %v", err)
		return err
	}
	index += 12

	related.FromUuid, err = core.UuidToString(buffer[index : index+16])
	if err != nil {
		err = fmt.Errorf("related Decode Error: %v", err)
		return err
	}
	index += 16

	related.DestUuid, err = core.UuidToString(buffer[index : index+16])
	if err != nil {
		err = fmt.Errorf("related Decode Error: %v", err)
		return err
	}
	index += 16

	return nil
}
Пример #2
0
func (request *DispatchRequest) Decode(buffer []byte) (err error) {
	if len(buffer) < 48 {
		err = errors.New("DispatchRequest Decode Error : the []byte is shorter than 48")
		return err
	}
	index := 0
	request.FromUuid, err = core.UuidToString(buffer[index : index+16])
	if err != nil {
		err = fmt.Errorf("DispacthREquest Decode Error : %v", err)
		return err
	}
	index += 16

	request.DestUuid, err = core.UuidToString(buffer[index : index+16])
	if err != nil {
		err = fmt.Errorf("DispacthREquest Decode Error : %v", err)
		return err
	}
	index += 16

	request.Token, err = core.UuidToString(buffer[index : index+16])
	if err != nil {
		err = fmt.Errorf("DispacthREquest Decode Error : %v", err)
		return err
	}
	index += 16

	return err
}
Пример #3
0
func (this *Operation) Decode(buff []byte) error {
	buff_length := len(buff)
	user_count := (buff_length - BASE_LENGTH) / (UUID_LENGTH + DEVICEID_LENGTH)

	this.List = make([]*List, user_count)

	index := 0
	this.Len = binary.BigEndian.Uint16(buff[index : index+LEN_LENGTH])
	index += LEN_LENGTH

	this.Type = buff[index]
	index += TYPE_LENGTH

	for i, _ := range this.List {
		Uuid, err := protocol.UuidToString(buff[index : index+UUID_LENGTH])
		if err != nil {
			return fmt.Errorf("Operation Decode Error:%v", err)
		}
		index += UUID_LENGTH

		DeviceId := binary.BigEndian.Uint32(buff[index : index+DEVICEID_LENGTH])
		index += DEVICEID_LENGTH

		this.List[i] = NewList(Uuid, DeviceId)
	}

	if index != buff_length {
		return errors.New("Operation Decode Error: index != length")
	}

	return nil
}
Пример #4
0
func (this *RelatedV) Decode(buff []byte) (err error) {
	index := 0
	this.FromUuid, err = core.UuidToString(buff[index : index+16])
	if err != nil {
		return err
	}
	index += 16

	this.DestUuid, err = core.UuidToString(buff[index : index+16])
	if err != nil {
		return err
	}
	index += 16

	return nil
}
Пример #5
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
}
Пример #6
0
func (token *Token) Decode(buffer []byte) (err error) {
	if len(buffer) < ALL_LENGTH {
		err = errors.New("Token Decode Error: the []byte lenght is shorter than 84")
		return err
	}

	index := 0
	err = token.TsHead.Decode(buffer[:protocol.HEADLENGTH])
	if err != nil {
		err = fmt.Errorf("Token Decode Error : %v", err)
		return err
	}
	index += protocol.HEADLENGTH

	token.FromUuid, err = core.UuidToString(buffer[index : index+UUID_LENGTH])
	if err != nil {
		err = fmt.Errorf("Token Decode Error : %v", err)
		return err
	}
	index += UUID_LENGTH

	token.FromToken, err = core.UuidToString(buffer[index : index+TOKEN_LENGTH])
	if err != nil {
		err = fmt.Errorf("Token Decode Error : %v", err)
		return err
	}
	index += TOKEN_LENGTH

	token.DestUuid, err = core.UuidToString(buffer[index : index+UUID_LENGTH])
	if err != nil {
		err = fmt.Errorf("Token Decode Error : %v", err)
		return err
	}
	index += UUID_LENGTH

	token.DestToken, err = core.UuidToString(buffer[index : index+TOKEN_LENGTH])
	if err != nil {
		err = fmt.Errorf("Token Decode Error : %v", err)
		return err
	}
	index += TOKEN_LENGTH

	token.TimeOut = int64(binary.BigEndian.Uint64(buffer[index : index+TIMEOUT_LENGTH]))
	index += TIMEOUT_LENGTH

	return nil
}
Пример #7
0
func (this *Request) Decode(buff []byte) error {
	if len(buff) < REQUEST_LENGTH {
		return errors.New("Request Decode Error : the []byte length is shorter than 42")
	}
	index := 0
	dataLength := len(buff)

	this.Version = buff[index]
	index += VERSION_LENGTH

	this.Type = buff[index]
	index += TYPE_LENGTH

	var err error

	if this.FromUuid, err = protocol.UuidToString(buff[index : index+UUID_LENGTH]); err != nil {
		return fmt.Errorf("Request Decode Error: %v", err)
	}
	index += UUID_LENGTH

	if this.DestUuid, err = protocol.UuidToString(buff[index : index+UUID_LENGTH]); err != nil {
		return fmt.Errorf("Request Decode Error: %v", err)
	}
	index += UUID_LENGTH

	this.FromDeviceId = binary.BigEndian.Uint32(buff[index : index+DEVICEID_LENGTH])
	index += DEVICEID_LENGTH

	this.DestDeviceId = binary.BigEndian.Uint32(buff[index : index+DEVICEID_LENGTH])
	index += DEVICEID_LENGTH

	this.Data = buff[index:dataLength]
	index += dataLength - index
	if index != dataLength {
		return errors.New("Request Decode Error: dataLength is wrong")
	}

	return nil
}
Пример #8
0
func (tokener *Tokener) Decode(buffer []byte) (err error) {
	if len(buffer) < 40 {
		err = errors.New("Tokener Decode Errror: the []byte length is shorter than 40")
		return err
	}

	index := 0
	tokener.FromToken, err = core.UuidToString(buffer[index : index+TOKEN_LENGTH])
	if err != nil {
		err = fmt.Errorf("Tokener Decode Errror: %v", err)
		return err
	}
	index += TOKEN_LENGTH

	tokener.DestToken, err = core.UuidToString(buffer[index : index+TOKEN_LENGTH])
	if err != nil {
		err = fmt.Errorf("Tokener Decode Errror: %v", err)
		return err
	}
	index += TOKEN_LENGTH

	tokener.TimeOut = int64(binary.BigEndian.Uint64(buffer[index : index+8]))
	return nil
}
Пример #9
0
func (this *Tokener) Handle(Conn *net.Conn) {
	defer (*Conn).Close()

	buffer := make([]byte, 44)
	for {
		n, err := (*Conn).Read(buffer)
		if n == 0 || err != nil {
			fmt.Println("token-server read error")
			return
		}

		related := model.Related{}
		err = related.Decode(buffer)
		if err != nil {
			err = fmt.Errorf("token-server Handle Error:%v", err)
			fmt.Println(err.Error())
			return
		}

		var tokener *model.Tokener
		if t, err := this.Tokens.Query(related.FromUuid, related.DestUuid); t != nil && err == nil && t.TimeOut-time.Now().Unix() > 10 {
			tokener = t
		} else {
			fmt.Println("token-server Handle: Get Token")
			if t != nil {
				this.Tokens.Delete(related.FromUuid, related.DestUuid)
			}

			tokener = &model.Tokener{}
			fromToken := uuid.NewV4()
			tokener.FromToken, err = core.UuidToString(fromToken[:16])
			if err != nil {
				err = fmt.Errorf("token-server Handle Error:%v", err)
				fmt.Println(err.Error())
				return
			}

			destToken := uuid.NewV4()
			tokener.DestToken, err = core.UuidToString(destToken[:16])
			if err != nil {
				err = fmt.Errorf("token-server Handle Error:%v", err)
				fmt.Println(err.Error())
				return
			}

			tokener.TimeOut = time.Now().Unix() + 10
			err = this.Tokens.Add(related.FromUuid, related.DestUuid, tokener)
			if err != nil {
				err = fmt.Errorf("token-server Handle Error:%v", err)
				fmt.Println(err.Error())
				return
			}
		}
		token := model.Token{
			protocol.TsHead{Len: 50, Version: 1, Type: core.CTRL, Number: protocol.USER_RES_SEND_TOKEN},
			related.FromUuid,
			tokener.FromToken,
			related.DestUuid,
			tokener.DestToken,
			tokener.TimeOut,
		}
		tokenBuff, err := token.Encode()
		if err != nil {
			err = fmt.Errorf("token-server Handle Error:%v", err)
			fmt.Println(err.Error())
			return
		}

		n, err = (*Conn).Write(tokenBuff)
		if n == 0 || err != nil {
			fmt.Println("token-server Handle Write data error")
			return
		}
	}
}