Skip to main content

rustorb_giop/
service_context.rs

1use rustorb_cdr::{ByteOrder, CdrDecode, CdrEncode, Decoder, Encoder, Encoding};
2
3/// `IOP::ServiceContext`, with the context data left as an encapsulation.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct ServiceContext {
6    /// Context id such as [`service_id::CODE_SETS`].
7    pub context_id: u32,
8    /// Encapsulated context data.
9    pub context_data: Vec<u8>,
10}
11
12impl CdrEncode for ServiceContext {
13    fn encode(&self, enc: &mut Encoder) -> rustorb_cdr::Result<()> {
14        enc.write_u32(self.context_id);
15        enc.write_octet_seq(&self.context_data)
16    }
17}
18
19impl CdrDecode for ServiceContext {
20    fn decode(dec: &mut Decoder<'_>) -> rustorb_cdr::Result<Self> {
21        Ok(ServiceContext {
22            context_id: dec.read_u32()?,
23            context_data: dec.read_octet_seq()?.to_vec(),
24        })
25    }
26}
27
28/// Standard `IOP::ServiceId` values.
29pub mod service_id {
30    /// `TransactionService`.
31    pub const TRANSACTION_SERVICE: u32 = 0;
32    /// `CodeSets`, carrying a [`CodeSetContext`](crate::CodeSetContext).
33    pub const CODE_SETS: u32 = 1;
34    /// `ChainBypassCheck`.
35    pub const CHAIN_BYPASS_CHECK: u32 = 2;
36    /// `ChainBypassInfo`.
37    pub const CHAIN_BYPASS_INFO: u32 = 3;
38    /// `LogicalThreadId`.
39    pub const LOGICAL_THREAD_ID: u32 = 4;
40    /// `BI_DIR_IIOP`.
41    pub const BI_DIR_IIOP: u32 = 5;
42    /// `SendingContextRunTime`.
43    pub const SENDING_CONTEXT_RUN_TIME: u32 = 6;
44    /// `INVOCATION_POLICIES`.
45    pub const INVOCATION_POLICIES: u32 = 7;
46    /// `FORWARDED_IDENTITY`.
47    pub const FORWARDED_IDENTITY: u32 = 8;
48    /// `UnknownExceptionInfo`.
49    pub const UNKNOWN_EXCEPTION_INFO: u32 = 9;
50    /// `RTCorbaPriority`.
51    pub const RT_CORBA_PRIORITY: u32 = 10;
52    /// `RTCorbaPriorityRange`.
53    pub const RT_CORBA_PRIORITY_RANGE: u32 = 11;
54    /// `FT_GROUP_VERSION`.
55    pub const FT_GROUP_VERSION: u32 = 12;
56    /// `FT_REQUEST`.
57    pub const FT_REQUEST: u32 = 13;
58    /// `ExceptionDetailMessage`.
59    pub const EXCEPTION_DETAIL_MESSAGE: u32 = 14;
60    /// `SecurityAttributeService`.
61    pub const SECURITY_ATTRIBUTE_SERVICE: u32 = 15;
62    /// `ActivityService`.
63    pub const ACTIVITY_SERVICE: u32 = 16;
64}
65
66/// OSF registry ids of common code sets (`CONV_FRAME::CodeSetId`).
67pub mod code_set {
68    /// ISO 8859-1 (Latin-1), the default `char` code set.
69    pub const ISO_8859_1: u32 = 0x0001_0001;
70    /// UCS-2 level 1.
71    pub const UCS_2_LEVEL_1: u32 = 0x0001_0100;
72    /// UTF-16.
73    pub const UTF_16: u32 = 0x0001_0109;
74    /// UTF-8.
75    pub const UTF_8: u32 = 0x0501_0001;
76}
77
78/// `CONV_FRAME::CodeSetContext`, the transmission code sets chosen by a
79/// client.
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub struct CodeSetContext {
82    /// Transmission code set for `char` and `string`.
83    pub char_data: u32,
84    /// Transmission code set for `wchar` and `wstring`.
85    pub wchar_data: u32,
86}
87
88impl CodeSetContext {
89    /// Wraps the context in a [`service_id::CODE_SETS`] service context.
90    pub fn to_service_context(&self, order: ByteOrder) -> ServiceContext {
91        let mut enc = Encoder::encapsulation(order, Encoding::Giop1_2);
92        enc.write_u32(self.char_data);
93        enc.write_u32(self.wchar_data);
94        ServiceContext {
95            context_id: service_id::CODE_SETS,
96            context_data: enc.into_bytes(),
97        }
98    }
99
100    /// Reads the context from a service context's data.
101    pub fn from_service_context(context: &ServiceContext) -> rustorb_cdr::Result<CodeSetContext> {
102        let mut dec = Decoder::encapsulation(&context.context_data, Encoding::Giop1_2)?;
103        Ok(CodeSetContext {
104            char_data: dec.read_u32()?,
105            wchar_data: dec.read_u32()?,
106        })
107    }
108
109    /// Finds and reads the code set context in a service context list.
110    pub fn find(contexts: &[ServiceContext]) -> Option<rustorb_cdr::Result<CodeSetContext>> {
111        contexts
112            .iter()
113            .find(|c| c.context_id == service_id::CODE_SETS)
114            .map(CodeSetContext::from_service_context)
115    }
116}