func (related *Related) Encode() (buffer []byte, err error) { buffer, err = related.TsHead.Encode() if err != nil { err = fmt.Errorf("Related Encode Error: %v", err) return buffer, err } uuidBuff := make([]byte, 16) uuidBuff, err = core.StringToUuid(related.FromUuid) if err != nil { err = fmt.Errorf("Related Encode Error: %v", err) return buffer, err } buffer = append(buffer, uuidBuff...) uuidBuff, err = core.StringToUuid(related.DestUuid) if err != nil { err = fmt.Errorf("Related Encode Error: %v", err) return buffer, err } buffer = append(buffer, uuidBuff...) return buffer, nil }
func (this *Operation) Encode() ([]byte, error) { user_count := len(this.List) length := BASE_LENGTH + user_count*UUID_LENGTH + user_count*DEVICEID_LENGTH buff := make([]byte, length) index := 0 binary.BigEndian.PutUint16(buff[index:index+LEN_LENGTH], this.Len) index += LEN_LENGTH buff[index] = this.Type index += TYPE_LENGTH for _, v := range this.List { user_uuid_buff, err := protocol.StringToUuid(v.Uuid) if err != nil { err = fmt.Errorf("Operation Encode Error: %v", err) return nil, err } copy(buff[index:index+UUID_LENGTH], user_uuid_buff[0:]) index += UUID_LENGTH binary.BigEndian.PutUint32(buff[index:index+DEVICEID_LENGTH], v.DeviceId) index += DEVICEID_LENGTH } if index != length { return nil, errors.New("Operation Encode Error: index != length") } return buff, nil }
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 }
func (this *RelatedV) Encode() (buff []byte, err error) { tmpbuff := make([]byte, 16) tmpbuff, err = core.StringToUuid(this.FromUuid) if err != nil { return nil, err } buff = append(buff, tmpbuff...) tmpbuff, err = core.StringToUuid(this.DestUuid) if err != nil { return nil, err } buff = append(buff, tmpbuff...) return buff, nil }
func (tokener *Tokener) Encode() (buffer []byte, err error) { buffer, err = core.StringToUuid(tokener.FromToken) if err != nil { err = fmt.Errorf("Tokener Encode Error: %v", err) return buffer, err } tempBuff := make([]byte, 16) tempBuff, err = core.StringToUuid(tokener.DestToken) if err != nil { err = fmt.Errorf("Tokener Encode Error: %v", err) return buffer, err } buffer = append(buffer, tempBuff...) binary.BigEndian.PutUint64(tempBuff, uint64(tokener.TimeOut)) buffer = append(buffer, tempBuff[:8]...) return buffer, err }
func (token *Token) Encode() (buffer []byte, err error) { buffer, err = token.TsHead.Encode() if err != nil { err = fmt.Errorf("Token Encode Error: %v", err) return buffer, err } uuidBuff := make([]byte, UUID_LENGTH) uuidBuff, err = core.StringToUuid(token.FromUuid) if err != nil { err = fmt.Errorf("Token Encode Error: %v", err) return buffer, err } buffer = append(buffer, uuidBuff...) tokenBuff := make([]byte, TOKEN_LENGTH) tokenBuff, err = core.StringToUuid(token.FromToken) if err != nil { err = fmt.Errorf("Token Encode Error: %v", err) return buffer, err } buffer = append(buffer, tokenBuff...) uuidBuff, err = core.StringToUuid(token.DestUuid) if err != nil { err = fmt.Errorf("Token Encode Error: %v", err) return buffer, err } tokenBuff, err = core.StringToUuid(token.DestToken) if err != nil { err = fmt.Errorf("Token Encode Error: %v", err) return buffer, err } buffer = append(buffer, tokenBuff...) binary.BigEndian.PutUint64(uuidBuff, uint64(token.TimeOut)) buffer = append(buffer, uuidBuff[:TOKEN_LENGTH]...) return buffer, nil }
func (this *Request) Encode() (buff []byte, err error) { dataLength := len(this.Data) buff = make([]byte, REQUEST_LENGTH+dataLength) index := 0 buff[index] = this.Version index += VERSION_LENGTH buff[index] = this.Type index += TYPE_LENGTH fromUuidBuff, err := protocol.StringToUuid(this.FromUuid) if err != nil { return nil, fmt.Errorf("Request Encode Error: %v", err) } copy(buff[index:index+UUID_LENGTH], fromUuidBuff) index += UUID_LENGTH destUuidBuff, err := protocol.StringToUuid(this.DestUuid) if err != nil { return nil, fmt.Errorf("Request Encode Error: %v", err) } copy(buff[index:index+UUID_LENGTH], destUuidBuff) index += UUID_LENGTH binary.BigEndian.PutUint32(buff[index:index+DEVICEID_LENGTH], this.FromDeviceId) index += DEVICEID_LENGTH binary.BigEndian.PutUint32(buff[index:index+DEVICEID_LENGTH], this.DestDeviceId) index += DEVICEID_LENGTH copy(buff[index:index+dataLength], this.Data[0:dataLength]) index += dataLength if index != REQUEST_LENGTH+dataLength { return nil, errors.New("Request Encode Error:encode length is wrong ") } return buff, nil }
func (request *DispatchRequest) Encode() (buffer []byte, err error) { buffer = make([]byte, 48) fromBuff, err := core.StringToUuid(request.FromUuid) if err != nil { err = fmt.Errorf("DispatchRequest Encode Error : %v", err) return buffer, err } buffer = append(buffer, fromBuff...) destBuff, err := core.StringToUuid(request.FromUuid) if err != nil { err = fmt.Errorf("DispatchRequest Encode Error : %v", err) return buffer, err } buffer = append(buffer, destBuff...) tokenBuff, err := core.StringToUuid(request.Token) if err != nil { err = fmt.Errorf("DispatchRequest Encode Error : %v", err) return buffer, err } buffer = append(buffer, tokenBuff...) return buffer, err }
func (this *DispatcherComponent) handleQueryUser(msg *protocol.Message) { if msg == nil { return } data := make([]byte, stree_define.QUERY_USER_REQUEST_LEN) copy(data, msg.Data[:4]) serverID, err := connect_protocol.StringToUuid(msg.UserUuid) if err != nil { err = errors.New("DispatcherComponent.handleQueryUser() Error: " + err.Error()) fmt.Println(err.Error()) return } copy(data[4:20], serverID) binary.BigEndian.PutUint16(data[20:], msg.Number) snode_component.GetNodeComponent().Forward(stree_define.QUERY_USER, msg.DestUuid, msg.DeviceId, data) return }