This repository was archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathMessageContainer.cs
More file actions
191 lines (164 loc) · 4.55 KB
/
MessageContainer.cs
File metadata and controls
191 lines (164 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2006 Alp Toker <alp@atoker.com>
// This software is made available under the MIT License
// See COPYING for details
using System;
using System.Collections.Generic;
using System.IO;
namespace DBus.Protocol
{
public class MessageContainer
{
Message resultMessage;
public MessageContainer ()
{
Type = MessageType.MethodCall;
}
MessageContainer (Message originalMessage)
{
this.resultMessage = originalMessage;
}
public Message Message {
get {
if (resultMessage != null)
return resultMessage;
return resultMessage = ExportToMessage ();
}
}
public Message CreateError (string errorName, string errorMessage)
{
var message = Message;
var error = new MessageContainer {
Type = MessageType.Error,
ErrorName = errorName,
ReplySerial = message.Header.Serial,
Signature = Signature.StringSig,
Destination = Sender
};
var writer = new MessageWriter (message.Header.Endianness);
writer.Write (errorMessage);
message = error.Message;
message.AttachBodyTo (writer);
return message;
}
public static MessageContainer FromMessage (Message message)
{
MessageContainer container = new MessageContainer (message) {
Path = (ObjectPath)message.Header[FieldCode.Path],
Interface = (string)message.Header[FieldCode.Interface],
Member = (string)message.Header[FieldCode.Member],
Destination = (string)message.Header[FieldCode.Destination],
//TODO: filled by the bus so reliable, but not the case for p2p
//so we make it optional here, but this needs some more thought
//if (message.Header.Fields.ContainsKey (FieldCode.Sender))
Sender = (string)message.Header[FieldCode.Sender],
ErrorName = (string)message.Header[FieldCode.ErrorName],
ReplySerial = (uint?)message.Header[FieldCode.ReplySerial],
Signature = message.Signature,
Serial = message.Header.Serial,
Type = message.Header.MessageType,
};
#if PROTO_REPLY_SIGNATURE
//TODO: note that an empty ReplySignature should really be treated differently to the field not existing!
if (message.Header.Fields.ContainsKey (FieldCode.ReplySignature))
container.ReplySignature = (Signature)message.Header[FieldCode.ReplySignature];
else
container.ReplySignature = Signature.Empty;
#endif
return container;
}
Message ExportToMessage () {
var message = new Message ();
message.Header.MessageType = Type;
if (Type == MessageType.MethodCall)
message.ReplyExpected = true;
else
message.Header.Flags = HeaderFlag.NoReplyExpected | HeaderFlag.NoAutoStart;
message.Header[FieldCode.Path] = Path;
message.Header[FieldCode.Interface] = Interface;
message.Header[FieldCode.Member] = Member;
message.Header[FieldCode.Destination] = Destination;
message.Header[FieldCode.ErrorName] = ErrorName;
#if PROTO_REPLY_SIGNATURE
//TODO
#endif
message.Signature = Signature;
if (ReplySerial != null)
message.Header[FieldCode.ReplySerial] = (uint)ReplySerial;
if (Serial != null)
message.Header.Serial = (uint)Serial;
return message;
}
public MessageType Type {
get;
set;
}
public ObjectPath Path {
get;
set;
}
public string Interface {
get;
set;
}
public string Member {
get;
set;
}
public string Destination {
get;
set;
}
public string Sender {
get;
set;
}
#if PROTO_REPLY_SIGNATURE
public Signature ReplySignature {
get;
set;
}
#endif
public uint? ReplySerial {
get;
set;
}
public uint? Serial {
get;
set;
}
public Signature Signature {
get;
set;
}
public string ErrorName {
get;
set;
}
/*public override bool Equals (object other)
{
MessageContainer otherMethodCall = other as MessageContainer;
return otherMethodCall == null ? false : Equals (otherMethodCall);
}
public bool Equals (MessageContainer other)
{
return (Path == other.Path || Path.Equals (other.Path))
&& Interface == other.Interface
&& Member == other.Member
&& Destination == other.Destination
&& Sender == other.Destination
&& ErrorName == other.ErrorName
&& Signature.Equals (other.Signature)
&& ReplySerial.Equals (other.ReplySerial);
}
public override int GetHashCode ()
{
return (Path == null ? 0 : Path.GetHashCode ())
^ (Interface == null ? 0 : Interface.GetHashCode ())
^ (Member == null ? 0 : Member.GetHashCode ())
^ (Destination == null ? 0 : Destination.GetHashCode ())
^ (Sender == null ? 0 : Sender.GetHashCode ())
^ (ErrorName == null ? 0 : ErrorName.GetHashCode ())
^ Signature.GetHashCode ();
}*/
}
}