func encodeMessageFromVom(header bindings.MessageHeader, argptrs []interface{}, t *vdl.Type) (*bindings.Message, error) { // Convert argptrs into their true form: []*vom.RawBytes inargs := make([]*vom.RawBytes, len(argptrs)) for i := range argptrs { inargs[i] = *argptrs[i].(**vom.RawBytes) } encoder := bindings.NewEncoder() if err := header.Encode(encoder); err != nil { return nil, err } if bytes, handles, err := encoder.Data(); err != nil { return nil, err } else { target := transcoder.ToMojomTarget() if err := util.JoinRawBytesAsStruct(target, t, inargs); err != nil { return nil, err } moreBytes := target.Bytes() // Append the encoded "payload" to the end of the slice. bytes = append(bytes, moreBytes...) return &bindings.Message{ Header: header, Bytes: bytes, Handles: handles, Payload: moreBytes, }, nil } }
func (s *messageReceiver) Accept(message *bindings.Message) (err error) { if _, ok := s.header.ifaceSig.Methods[message.Header.Type]; !ok { return fmt.Errorf("Method had index %d, but interface only has %d methods", message.Header.Type, len(s.header.ifaceSig.Methods)) } methodSig := s.header.ifaceSig.Methods[message.Header.Type] methodName := *methodSig.DeclData.ShortName // Should we perform validation of flags like generated methods? // Does this handle 0-arg methods? messageBytes := message.Payload if methodSig.ResponseParams == nil { // This may be complicated to support because of a race on the server side. // When we send the no-response message through mojo we don't know when it has been received // and we can close the pipe (one is opened per incoming message). // Note that because the client expects no response, it will not receive the following // error. return fmt.Errorf("this error not received") } response, err := s.call(s.header.v23Name, methodName, messageBytes, methodSig.Parameters, methodSig.ResponseParams) if err != nil { return err } // TODO(alexfandrianto): This assumes that bindings.Encoder has the method // WriteRawBytes. We will need to add this to Mojo ourselves. // func (e *Encoder) WriteRawBytes(data []byte) { // first := e.end // e.claimData(align(len(data), defaultAlignment)) // copy(e.buf[first:], data) // } // // See: https://codereview.chromium.org/1416433002/ responseHeader := bindings.MessageHeader{ Type: message.Header.Type, Flags: bindings.MessageIsResponseFlag, RequestId: message.Header.RequestId, } // responseMessage, err := bindings.EncodeMessage(responseHeader, byteCopyingPayload(response)) // if err != nil { // return err // } // return s.connector.WriteMessage(responseMessage) // TODO(alexfandrianto): Replace this block with the above. encoder := bindings.NewEncoder() if err := responseHeader.Encode(encoder); err != nil { return err } if bytes, handles, err := encoder.Data(); err != nil { return err } else { // response is our payload; append to the end of our slice. bytes = append(bytes, response...) // This is analogous to bindings.newMessage responseMessage := &bindings.Message{ Header: responseHeader, Bytes: bytes, Handles: handles, Payload: response, } return s.connector.WriteMessage(responseMessage) } }