2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using Tizen.Applications;
21 namespace Tizen.Applications.Messages
24 /// The message port API provides functions to send and receive messages between applications.
27 /// The message port API provides functions for passing messages between applications. An application should register its own local port to receive messages from remote applications.
28 /// If a remote application sends a message, the registered callback function of the local port is called.
29 /// The trusted message-port API allows communications between applications that are signed by the same developer(author) certificate.
31 /// <since_tizen> 3 </since_tizen>
32 public class MessagePort : IDisposable
34 private static readonly object s_lock = new object();
35 private static readonly HashSet<string> s_portMap = new HashSet<string>();
37 // The name of the local message port
38 private readonly string _portName = null;
40 // If true the message port is a trusted port, otherwise false it is not
41 private readonly bool _trusted = false;
43 // The local message port ID
44 private int _portId = 0;
46 // If true the message port is listening, otherwise false it is not
47 private bool _listening = false;
49 private Interop.MessagePort.message_port_message_cb _messageCallBack;
52 /// Initializes the instance of the MessagePort class.
54 /// <param name="portName">The name of the local message port.</param>
55 /// <param name="trusted">If true, it is the trusted message port of application, otherwise false.</param>
56 /// <exception cref="System.ArgumentException">Thrown when portName is null or empty.</exception>
59 /// MessagePort messagePort = new MessagePort("SenderPort", true);
62 /// <since_tizen> 3 </since_tizen>
63 public MessagePort(string portName, bool trusted)
65 if (String.IsNullOrEmpty(portName))
67 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "Invalid PortName", "PortName");
74 /// Destructor of the MessagePort class.
82 /// Called when a message is received.
86 /// MessagePort messagePort = new MessagePort("SenderPort", true);
87 /// messagePort.MessageReceived += MessageReceivedCallback;
88 /// static void MessageReceivedCallback(object sender, MessageReceivedEventArgs e)
90 /// Console.WriteLine("Message Received ");
91 /// if (e.Remote.AppId != null) {
92 /// Console.WriteLine("from :"+e.Remote.AppId);
97 /// <since_tizen> 3 </since_tizen>
98 public event EventHandler<MessageReceivedEventArgs> MessageReceived;
101 /// The name of the local message port.
103 /// <since_tizen> 3 </since_tizen>
104 public string PortName
112 /// If true, the message port is a trusted port, otherwise false.
114 /// <since_tizen> 3 </since_tizen>
124 /// If true, the message port is listening, otherwise false.
126 /// <since_tizen> 3 </since_tizen>
127 public bool Listening
136 /// Register the local message port.
138 /// <exception cref="System.InvalidOperationException">Thrown when portName is already used, when there is an I/O error.</exception>
139 /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
140 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
143 /// MessagePort messagePort = new MessagePort("SenderPort", true);
144 /// messagePort.MessageReceived += MessageReceivedCallback;
145 /// messagePort.Listen();
148 /// <since_tizen> 3 </since_tizen>
153 if (s_portMap.Contains(_portName))
155 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, _portName + "is already used");
157 _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
159 MessageReceivedEventArgs args = new MessageReceivedEventArgs()
161 Message = new Bundle(new SafeBundleHandle(message, false))
164 if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
166 args.Remote = new RemoteValues()
169 PortName = remotePortName,
173 MessageReceived?.Invoke(this, args);
177 Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero) :
178 Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);
181 MessagePortErrorFactory.ThrowException(_portId, "RegisterPort", _portName);
183 s_portMap.Add(_portName);
189 /// Unregisters the local message port.
191 /// <exception cref="System.InvalidOperationException">Thrown when messageport is already stopped, when there is an I/O error, when the port is not found.</exception>
192 /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
193 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
196 /// MessagePort messagePort = new MessagePort("SenderPort", true);
197 /// messagePort.MessageReceived += MessageReceivedCallback;
198 /// messagePort.Listen();
199 /// using (var message = new Tizen.Application.Bundle())
201 /// message.AddItem("message", "a_string");
202 /// messagePort.Send(message, "ReceiverAppID", "ReceiverPort");
204 /// messagePort.StopListening();
207 /// <since_tizen> 3 </since_tizen>
208 public void StopListening()
212 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Already stopped");
216 Interop.MessagePort.UnregisterTrustedPort(_portId) :
217 Interop.MessagePort.UnregisterPort(_portId);
219 if (ret != (int)MessagePortError.None)
221 MessagePortErrorFactory.ThrowException(ret, "Error Unregister port");
226 s_portMap.Remove(_portName);
233 /// Sends an untrusted message to the message port of a remote application.
235 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB.</param>
236 /// <param name="remoteAppId">The ID of the remote application.</param>
237 /// <param name="remotePortName">The name of the remote message port.</param>
238 /// <exception cref="System.InvalidOperationException">Thrown when there is an I/O error, when the port is not found.</exception>
239 /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
240 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
241 /// <exception cref="System.ArgumentOutOfRangeException">Thrown when message has exceeded the maximum limit(4KB).</exception>
244 /// MessagePort messagePort = new MessagePort("SenderPort", true);
245 /// messagePort.MessageReceived += MessageReceivedCallback;
246 /// messagePort.Listen();
247 /// using (var message = new Tizen.Application.Bundle())
249 /// message.AddItem("message", "a_string");
250 /// messagePort.Send(message, "ReceiverAppID", "ReceiverPort");
254 /// <since_tizen> 3 </since_tizen>
255 public void Send(Bundle message, string remoteAppId, string remotePortName)
257 Send(message, remoteAppId, remotePortName, false);
261 /// Sends a message to the message port of a remote application.
263 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB.</param>
264 /// <param name="remoteAppId">The ID of the remote application.</param>
265 /// <param name="remotePortName">The name of the remote message port.</param>
266 /// <param name="trusted">If true, it is the trusted message port of remote application, otherwise false.</param>
267 /// <exception cref="System.InvalidOperationException">Thrown when there is an I/O error, when the port is not found.</exception>
268 /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
269 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
270 /// <exception cref="System.ArgumentOutOfRangeException">Thrown when message has exceeded the maximum limit(4KB).</exception>
271 /// <exception cref="System.UnauthorizedAccessException">Thrown when the remote application is not signed with the same certificate.</exception>
274 /// MessagePort messagePort = new MessagePort("SenderPort", true);
275 /// messagePort.MessageReceived += MessageReceivedCallback;
276 /// messagePort.Listen();
277 /// using (var message = new Tizen.Application.Bundle())
279 /// message.AddItem("message", "a_string");
280 /// messagePort.Send(message, "ReceiverAppID", "ReceiverPort", true);
284 /// <since_tizen> 3 </since_tizen>
285 public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted)
289 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Should start listen before send");
291 if (message == null || message.SafeBundleHandle == null || message.SafeBundleHandle.IsInvalid)
293 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "message is null", "Message");
296 Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.SafeBundleHandle, _portId) :
297 Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.SafeBundleHandle, _portId);
299 if (ret != (int)MessagePortError.None)
301 if (ret == (int)MessagePortError.MaxExceeded)
303 MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
305 MessagePortErrorFactory.ThrowException(ret, "Can't send message");
310 /// Releases the unmanaged resources used by the MessagePort class specifying whether to perform a normal dispose operation.
312 /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
313 /// <since_tizen> 3 </since_tizen>
314 protected virtual void Dispose(bool disposing)
324 Log.Warn(GetType().Namespace, "Exception in Dispose :" + e.Message);
330 /// Releases all resources used by the MessagePort class.
332 /// <since_tizen> 3 </since_tizen>
333 public void Dispose()
336 GC.SuppressFinalize(this);