pub struct GenericMessage<H, B> {
pub service: ServiceId,
pub method: MethodId,
pub header: H,
pub body: B,
}Expand description
Message addressed to a service instance.
This is used to identify the contents of the payload so that it can be correctly serialized.
Has room for a header to include additional information about the body of the message.
§Notes
This is the generic implementation. You probably want to use Message<T> instead.
Fields§
§service: ServiceIdIdentifier of the service interface.
method: MethodIdIdentifier of the method or event.
header: HAdditional information about the message.
body: BContents of the message.
Implementations§
Source§impl<H, B> GenericMessage<H, B>
impl<H, B> GenericMessage<H, B>
Sourcepub const fn new(header: H, body: B) -> Self
pub const fn new(header: H, body: B) -> Self
Creates a new GenericMessage<H, B>.
§Examples
use rsomeip_proto::GenericMessage;
let message = GenericMessage::new("header", "body");
assert_eq!(message.header, "header");
assert_eq!(message.body, "body");Sourcepub const fn with_service(self, service: ServiceId) -> Self
pub const fn with_service(self, service: ServiceId) -> Self
Returns self with the given ServiceId.
§Examples
use rsomeip_proto::{GenericMessage, ServiceId};
let message = GenericMessage::new("header", "body").with_service(ServiceId::new(0x1234));
assert_eq!(message.service.as_u16(), 0x1234);Examples found in repository?
29fn send_requests(socket: &UdpSocket, endpoint: &Endpoint) {
30 // Create a request to send to the service provider.
31 let mut session_id = SessionId::ENABLED;
32 let request = Message::default()
33 .with_service(SAMPLE_SERVICE_ID)
34 .with_method(SAMPLE_METHOD_ID)
35 .with_client(ClientId::new(0x0001))
36 .with_body((0..10).collect::<Vec<u8>>());
37
38 // Continuously send requests to the service provider.
39 loop {
40 // Clone the request and increment the session id.
41 let request = request.clone().with_session(session_id.increment());
42 println!("> {request} {:02x?}", request.body);
43
44 // Process the request into bytes.
45 let mut buffer = BytesMut::with_capacity(request.size_hint());
46 endpoint
47 .process(request, &mut buffer)
48 .expect("should process the request");
49
50 // Send the data through the socket.
51 socket
52 .send_to(&buffer.freeze(), stub_address())
53 .expect("should send the data.");
54
55 // Wait for a response.
56 let mut buffer = BytesMut::zeroed(64);
57 let (size, _) = socket
58 .recv_from(&mut buffer[..])
59 .expect("should receive the data");
60
61 // Process the response.
62 let response: Message = endpoint
63 .poll(&mut buffer.split_to(size).freeze())
64 .expect("should process the response");
65 println!("< {response} {:02x?}", response.body);
66
67 // Wait before sending another request.
68 std::thread::sleep(std::time::Duration::from_secs(1));
69 }
70}Sourcepub const fn with_method(self, method: MethodId) -> Self
pub const fn with_method(self, method: MethodId) -> Self
Returns self with the given MethodId.
§Examples
use rsomeip_proto::{GenericMessage, MethodId};
let message = GenericMessage::new("header", "body").with_method(MethodId::new(0x1234));
assert_eq!(message.method.as_u16(), 0x1234);Examples found in repository?
29fn send_requests(socket: &UdpSocket, endpoint: &Endpoint) {
30 // Create a request to send to the service provider.
31 let mut session_id = SessionId::ENABLED;
32 let request = Message::default()
33 .with_service(SAMPLE_SERVICE_ID)
34 .with_method(SAMPLE_METHOD_ID)
35 .with_client(ClientId::new(0x0001))
36 .with_body((0..10).collect::<Vec<u8>>());
37
38 // Continuously send requests to the service provider.
39 loop {
40 // Clone the request and increment the session id.
41 let request = request.clone().with_session(session_id.increment());
42 println!("> {request} {:02x?}", request.body);
43
44 // Process the request into bytes.
45 let mut buffer = BytesMut::with_capacity(request.size_hint());
46 endpoint
47 .process(request, &mut buffer)
48 .expect("should process the request");
49
50 // Send the data through the socket.
51 socket
52 .send_to(&buffer.freeze(), stub_address())
53 .expect("should send the data.");
54
55 // Wait for a response.
56 let mut buffer = BytesMut::zeroed(64);
57 let (size, _) = socket
58 .recv_from(&mut buffer[..])
59 .expect("should receive the data");
60
61 // Process the response.
62 let response: Message = endpoint
63 .poll(&mut buffer.split_to(size).freeze())
64 .expect("should process the response");
65 println!("< {response} {:02x?}", response.body);
66
67 // Wait before sending another request.
68 std::thread::sleep(std::time::Duration::from_secs(1));
69 }
70}Sourcepub fn with_header<T>(self, header: T) -> GenericMessage<T, B>
pub fn with_header<T>(self, header: T) -> GenericMessage<T, B>
Maps a GenericMessage<H, B> to a GenericMessage<T, B> by replacing the header of the message
with the given value.
§Examples
use rsomeip_proto::GenericMessage;
let message = GenericMessage::new("header", "body").with_header(0x1234_u16);
assert_eq!(message.header, 0x1234_u16);Sourcepub fn with_body<T>(self, body: T) -> GenericMessage<H, T>
pub fn with_body<T>(self, body: T) -> GenericMessage<H, T>
Maps a GenericMessage<H, B> to a GenericMessage<H, T> by replacing the body of the message with the
given value.
§Examples
use rsomeip_proto::GenericMessage;
let message = GenericMessage::new("header", "body").with_body(0x1234_u16);
assert_eq!(message.body, 0x1234_u16);Examples found in repository?
29fn send_requests(socket: &UdpSocket, endpoint: &Endpoint) {
30 // Create a request to send to the service provider.
31 let mut session_id = SessionId::ENABLED;
32 let request = Message::default()
33 .with_service(SAMPLE_SERVICE_ID)
34 .with_method(SAMPLE_METHOD_ID)
35 .with_client(ClientId::new(0x0001))
36 .with_body((0..10).collect::<Vec<u8>>());
37
38 // Continuously send requests to the service provider.
39 loop {
40 // Clone the request and increment the session id.
41 let request = request.clone().with_session(session_id.increment());
42 println!("> {request} {:02x?}", request.body);
43
44 // Process the request into bytes.
45 let mut buffer = BytesMut::with_capacity(request.size_hint());
46 endpoint
47 .process(request, &mut buffer)
48 .expect("should process the request");
49
50 // Send the data through the socket.
51 socket
52 .send_to(&buffer.freeze(), stub_address())
53 .expect("should send the data.");
54
55 // Wait for a response.
56 let mut buffer = BytesMut::zeroed(64);
57 let (size, _) = socket
58 .recv_from(&mut buffer[..])
59 .expect("should receive the data");
60
61 // Process the response.
62 let response: Message = endpoint
63 .poll(&mut buffer.split_to(size).freeze())
64 .expect("should process the response");
65 println!("< {response} {:02x?}", response.body);
66
67 // Wait before sending another request.
68 std::thread::sleep(std::time::Duration::from_secs(1));
69 }
70}Sourcepub fn map<T, U, F>(self, f: F) -> GenericMessage<T, U>
pub fn map<T, U, F>(self, f: F) -> GenericMessage<T, U>
Maps a GenericMessage<H, B> to a GenericMessage<T, U> by applying a function to the header and body
of the message.
§Examples
use rsomeip_proto::GenericMessage;
let message = GenericMessage::new(1u8, 2u8)
.map(|header, body| (header + 1, body + 1));
assert_eq!(message.header, 2);
assert_eq!(message.body, 3);Sourcepub fn map_header<T, F>(self, f: F) -> GenericMessage<T, B>where
F: FnOnce(H) -> T,
pub fn map_header<T, F>(self, f: F) -> GenericMessage<T, B>where
F: FnOnce(H) -> T,
Maps a GenericMessage<H, B> to a GenericMessage<T, B> by applying a function to the header of the
message.
§Examples
use rsomeip_proto::GenericMessage;
let message = GenericMessage::new(1u8, "body").map_header(|header| header + 1);
assert_eq!(message.header, 2);Sourcepub fn map_body<T, F>(self, f: F) -> GenericMessage<H, T>where
F: FnOnce(B) -> T,
pub fn map_body<T, F>(self, f: F) -> GenericMessage<H, T>where
F: FnOnce(B) -> T,
Maps a GenericMessage<H, B> to a GenericMessage<H, T> by applying a function to the body of the
message.
§Examples
use rsomeip_proto::GenericMessage;
let message = GenericMessage::new("header", 2u8).map_body(|body| body + 1);
assert_eq!(message.body, 3);Sourcepub const fn replace_header(&mut self, value: H) -> H
pub const fn replace_header(&mut self, value: H) -> H
Replaces the header of the message with the given value.
Returns the old value.
§Examples
use rsomeip_proto::GenericMessage;
let mut message = GenericMessage::new("header", "body");
assert_eq!(message.replace_header("new_header"), "header");
assert_eq!(message.header, "new_header");Sourcepub const fn replace_body(&mut self, value: B) -> B
pub const fn replace_body(&mut self, value: B) -> B
Replaces the body of the message with the given value.
Returns the old value.
§Examples
use rsomeip_proto::GenericMessage;
let mut message = GenericMessage::new("header", "body");
assert_eq!(message.replace_body("new_body"), "body");
assert_eq!(message.body, "new_body");Source§impl<B> GenericMessage<Header, B>
Specialization for the Header type.
impl<B> GenericMessage<Header, B>
Specialization for the Header type.
Sourcepub fn into_response(self, return_code: ReturnCode) -> Self
pub fn into_response(self, return_code: ReturnCode) -> Self
Returns self as a response message with the given return_code.
§Examples
use rsomeip_proto::{GenericMessage, Header, MessageType, ReturnCode};
let message = GenericMessage::new(Header::new(), "body")
.into_response(ReturnCode::NotOk);
assert_eq!(message.header.message_type.as_type(), MessageType::Response);
assert_eq!(message.header.return_code, ReturnCode::NotOk);
assert_eq!(message.body, "body");Sourcepub fn into_error(self, return_code: ReturnCode) -> Self
pub fn into_error(self, return_code: ReturnCode) -> Self
Returns self as an error message with the given return_code.
§Examples
use rsomeip_proto::{GenericMessage, Header, MessageType, ReturnCode};
let message = GenericMessage::new(Header::new(), "body")
.into_error(ReturnCode::NotOk);
assert_eq!(message.header.message_type.as_type(), MessageType::Error);
assert_eq!(message.header.return_code, ReturnCode::NotOk);
assert_eq!(message.body, "body");Sourcepub const fn with_client(self, client: ClientId) -> Self
pub const fn with_client(self, client: ClientId) -> Self
Returns self with the given ClientId.
§Examples
use rsomeip_proto::{GenericMessage, Header, ClientId};
let message = GenericMessage::new(Header::new(), "body")
.with_client(ClientId::new(0x1234));
assert_eq!(message.client().as_u16(), 0x1234);Examples found in repository?
29fn send_requests(socket: &UdpSocket, endpoint: &Endpoint) {
30 // Create a request to send to the service provider.
31 let mut session_id = SessionId::ENABLED;
32 let request = Message::default()
33 .with_service(SAMPLE_SERVICE_ID)
34 .with_method(SAMPLE_METHOD_ID)
35 .with_client(ClientId::new(0x0001))
36 .with_body((0..10).collect::<Vec<u8>>());
37
38 // Continuously send requests to the service provider.
39 loop {
40 // Clone the request and increment the session id.
41 let request = request.clone().with_session(session_id.increment());
42 println!("> {request} {:02x?}", request.body);
43
44 // Process the request into bytes.
45 let mut buffer = BytesMut::with_capacity(request.size_hint());
46 endpoint
47 .process(request, &mut buffer)
48 .expect("should process the request");
49
50 // Send the data through the socket.
51 socket
52 .send_to(&buffer.freeze(), stub_address())
53 .expect("should send the data.");
54
55 // Wait for a response.
56 let mut buffer = BytesMut::zeroed(64);
57 let (size, _) = socket
58 .recv_from(&mut buffer[..])
59 .expect("should receive the data");
60
61 // Process the response.
62 let response: Message = endpoint
63 .poll(&mut buffer.split_to(size).freeze())
64 .expect("should process the response");
65 println!("< {response} {:02x?}", response.body);
66
67 // Wait before sending another request.
68 std::thread::sleep(std::time::Duration::from_secs(1));
69 }
70}Sourcepub const fn with_session(self, session: SessionId) -> Self
pub const fn with_session(self, session: SessionId) -> Self
Returns self with the given SessionId.
§Examples
use rsomeip_proto::{GenericMessage, Header, SessionId};
let message = GenericMessage::new(Header::new(), "body")
.with_session(SessionId::new(0x1234));
assert_eq!(message.session().as_u16(), 0x1234);Examples found in repository?
29fn send_requests(socket: &UdpSocket, endpoint: &Endpoint) {
30 // Create a request to send to the service provider.
31 let mut session_id = SessionId::ENABLED;
32 let request = Message::default()
33 .with_service(SAMPLE_SERVICE_ID)
34 .with_method(SAMPLE_METHOD_ID)
35 .with_client(ClientId::new(0x0001))
36 .with_body((0..10).collect::<Vec<u8>>());
37
38 // Continuously send requests to the service provider.
39 loop {
40 // Clone the request and increment the session id.
41 let request = request.clone().with_session(session_id.increment());
42 println!("> {request} {:02x?}", request.body);
43
44 // Process the request into bytes.
45 let mut buffer = BytesMut::with_capacity(request.size_hint());
46 endpoint
47 .process(request, &mut buffer)
48 .expect("should process the request");
49
50 // Send the data through the socket.
51 socket
52 .send_to(&buffer.freeze(), stub_address())
53 .expect("should send the data.");
54
55 // Wait for a response.
56 let mut buffer = BytesMut::zeroed(64);
57 let (size, _) = socket
58 .recv_from(&mut buffer[..])
59 .expect("should receive the data");
60
61 // Process the response.
62 let response: Message = endpoint
63 .poll(&mut buffer.split_to(size).freeze())
64 .expect("should process the response");
65 println!("< {response} {:02x?}", response.body);
66
67 // Wait before sending another request.
68 std::thread::sleep(std::time::Duration::from_secs(1));
69 }
70}Sourcepub const fn protocol(&self) -> ProtocolVersion
pub const fn protocol(&self) -> ProtocolVersion
Returns the ProtocolVersion portion of the Header.
§Examples
use rsomeip_proto::{GenericMessage, Header, ProtocolVersion};
let message = GenericMessage::new(Header::new(), "body")
.with_protocol(ProtocolVersion::new(0x12));
assert_eq!(message.protocol().as_u8(), 0x12);Sourcepub const fn with_protocol(self, protocol: ProtocolVersion) -> Self
pub const fn with_protocol(self, protocol: ProtocolVersion) -> Self
Returns self with the given ProtocolVersion.
§Examples
use rsomeip_proto::{GenericMessage, Header, ProtocolVersion};
let message = GenericMessage::new(Header::new(), "body")
.with_protocol(ProtocolVersion::new(0x12));
assert_eq!(message.protocol().as_u8(), 0x12);Sourcepub const fn interface(&self) -> InterfaceVersion
pub const fn interface(&self) -> InterfaceVersion
Returns the InterfaceVersion portion of the Header.
§Examples
use rsomeip_proto::{GenericMessage, Header, InterfaceVersion};
let message = GenericMessage::new(Header::new(), "body")
.with_interface(InterfaceVersion::new(0x12));
assert_eq!(message.interface().as_u8(), 0x12);Sourcepub const fn with_interface(self, interface: InterfaceVersion) -> Self
pub const fn with_interface(self, interface: InterfaceVersion) -> Self
Returns self with the given InterfaceVersion.
§Examples
use rsomeip_proto::{GenericMessage, Header, InterfaceVersion};
let message = GenericMessage::new(Header::new(), "body")
.with_interface(InterfaceVersion::new(0x12));
assert_eq!(message.interface().as_u8(), 0x12);Sourcepub const fn message_type(&self) -> MessageType
pub const fn message_type(&self) -> MessageType
Returns the MessageType of this GenericMessage<Header, B>.
§Examples
use rsomeip_proto::{GenericMessage, Header, MessageType};
let message = GenericMessage::new(Header::new(), "body")
.with_message_type(MessageType::Notification);
assert_eq!(message.message_type(), MessageType::Notification);Sourcepub const fn with_message_type(self, message_type: MessageType) -> Self
pub const fn with_message_type(self, message_type: MessageType) -> Self
Returns self with the given MessageType.
§Examples
use rsomeip_proto::{GenericMessage, Header, MessageType};
let message = GenericMessage::new(Header::new(), "body")
.with_message_type(MessageType::Notification);
assert_eq!(message.message_type(), MessageType::Notification);Examples found in repository?
29fn send_responses(socket: &UdpSocket, endpoint: &Endpoint) {
30 // Continuously process requests from service consumers.
31 loop {
32 // Wait for a request.
33 let mut buffer = BytesMut::zeroed(64);
34 let (size, remote_address) = socket
35 .recv_from(&mut buffer[..])
36 .expect("should receive the data");
37
38 // Process the request.
39 let request: Message = endpoint
40 .poll(&mut buffer.split_to(size).freeze())
41 .expect("should process the request");
42 println!("< {request} {:02x?}", request.body);
43
44 // Create a response.
45 let response = request.clone().with_message_type(MessageType::Response);
46 println!("> {response} {:02x?}", response.body);
47
48 // Process the response into bytes.
49 let mut buffer = BytesMut::with_capacity(request.size_hint());
50 endpoint
51 .process(response, &mut buffer)
52 .expect("should process the response");
53
54 // Send the data through the socket.
55 socket
56 .send_to(&buffer.freeze(), remote_address)
57 .expect("should send the data.");
58 }
59}Sourcepub const fn return_code(&self) -> ReturnCode
pub const fn return_code(&self) -> ReturnCode
Returns the ReturnCode portion of the Header.
§Examples
use rsomeip_proto::{GenericMessage, Header, ReturnCode};
let message = GenericMessage::new(Header::new(), "body")
.with_return_code(ReturnCode::E2e);
assert_eq!(message.return_code(), ReturnCode::E2e);Sourcepub const fn with_return_code(self, return_code: ReturnCode) -> Self
pub const fn with_return_code(self, return_code: ReturnCode) -> Self
Returns self with the given ReturnCode.
§Examples
use rsomeip_proto::{GenericMessage, Header, ReturnCode};
let message = GenericMessage::new(Header::new(), "body")
.with_return_code(ReturnCode::E2e);
assert_eq!(message.return_code(), ReturnCode::E2e);Source§impl<H, B> GenericMessage<H, B>
impl<H, B> GenericMessage<H, B>
Sourcepub fn to_bytes(&self) -> Result<Bytes, SerializeError>
pub fn to_bytes(&self) -> Result<Bytes, SerializeError>
Returns a buffer containing the serialized message.
§Errors
Returns a SerializeError if the message could not be serialized.
Sourcepub fn from_bytes(buffer: &mut impl Buf) -> Result<Self, DeserializeError>
pub fn from_bytes(buffer: &mut impl Buf) -> Result<Self, DeserializeError>
Returns a GenericMessage<H, B> deserialized from the buffer.
§Errors
Returns a DeserializeError if the message could not be deserialized.
Trait Implementations§
Source§impl<H: Clone, B: Clone> Clone for GenericMessage<H, B>
impl<H: Clone, B: Clone> Clone for GenericMessage<H, B>
Source§fn clone(&self) -> GenericMessage<H, B>
fn clone(&self) -> GenericMessage<H, B>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<H, B> Default for GenericMessage<H, B>
impl<H, B> Default for GenericMessage<H, B>
Source§impl<H, B> Deserialize for GenericMessage<H, B>where
H: Deserialize<Output = H>,
B: Deserialize<Output = B>,
impl<H, B> Deserialize for GenericMessage<H, B>where
H: Deserialize<Output = H>,
B: Deserialize<Output = B>,
Source§type Output = GenericMessage<H, B>
type Output = GenericMessage<H, B>
Source§fn deserialize(buffer: &mut impl Buf) -> Result<Self::Output, DeserializeError>
fn deserialize(buffer: &mut impl Buf) -> Result<Self::Output, DeserializeError>
Deserialize::Output from the buffer. Read moreSource§fn deserialize_len(
length: LengthField,
buffer: &mut impl Buf,
) -> Result<Self::Output, DeserializeError>
fn deserialize_len( length: LengthField, buffer: &mut impl Buf, ) -> Result<Self::Output, DeserializeError>
Deserialize::Output from the buffer. Read moreSource§impl<H, B> Display for GenericMessage<H, B>where
H: Display,
impl<H, B> Display for GenericMessage<H, B>where
H: Display,
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats self into a string.
§Examples
use rsomeip_proto::{GenericMessage, ServiceId, MethodId};
let message = GenericMessage::new("header", "body")
.with_service(ServiceId::new(0x1234))
.with_method(MethodId::new(0x5678));
assert_eq!(message.to_string(), "1234.5678.header")