예제 #1
0
// Handle handles the given binary payload.
func (s Server) Handle(data []byte) ([]byte, error) {
	request, err := s.p.DecodeEnveloped(bytes.NewReader(data))
	if err != nil {
		return nil, err
	}

	response := wire.Envelope{
		Name:  request.Name,
		SeqID: request.SeqID,
		Type:  wire.Reply,
	}

	response.Value, err = s.h.Handle(request.Name, request.Value)
	if err != nil {
		response.Type = wire.Exception
		switch err.(type) {
		case ErrUnknownMethod:
			response.Value, err = tappExc(err, exception.ExceptionTypeUnknownMethod)
		default:
			response.Value, err = tappExc(err, exception.ExceptionTypeInternalError)
		}

		if err != nil {
			return nil, err
		}
	}

	var buff bytes.Buffer
	if err := s.p.EncodeEnveloped(response, &buff); err != nil {
		return nil, err
	}

	return buff.Bytes(), nil
}
예제 #2
0
func (bw *Reader) readStrictNameType(initial int32, off int64) (wire.Envelope, int64, error) {
	var e wire.Envelope

	if v := uint32(initial) & versionMask; v != version1 {
		return e, off, fmt.Errorf("cannot decode envelope of version: %v", v)
	}

	// This will truncate the bits that are not required.
	e.Type = wire.EnvelopeType(initial)

	var err error
	e.Name, off, err = bw.readString(off)
	return e, off, err
}
예제 #3
0
func (bw *Reader) readNonStrictNameType() (wire.Envelope, int64, error) {
	var e wire.Envelope

	name, off, err := bw.readString(0)
	if err != nil {
		return e, off, err
	}
	e.Name = name

	typeID, off, err := bw.readByte(off)
	if err != nil {
		return e, off, err
	}
	e.Type = wire.EnvelopeType(typeID)

	return e, off, nil
}