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 public class MessagePort : IDisposable
33 private static readonly object s_lock = new object();
34 private static readonly HashSet<string> s_portMap = new HashSet<string>();
36 // The name of the local message port
37 private readonly string _portName = null;
39 // If true the message port is a trusted port, otherwise false it is not
40 private readonly bool _trusted = false;
42 // The local message port ID
43 private int _portId = 0;
45 // If true the message port is listening, otherwise false it is not
46 private bool _listening = false;
48 private Interop.MessagePort.message_port_message_cb _messageCallBack;
51 /// Initializes the instance of the MessagePort class.
53 /// <param name="portName">The name of the local message port.</param>
54 /// <param name="trusted">If true, it is the trusted message port of application, otherwise false.</param>
55 /// <exception cref="System.ArgumentException">Thrown when portName is null or empty.</exception>
58 /// MessagePort messagePort = new MessagePort("SenderPort", true);
61 public MessagePort(string portName, bool trusted)
63 if (String.IsNullOrEmpty(portName))
65 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "Invalid PortName", "PortName");
72 /// Destructor of the MessagePort class.
80 /// Called when a message is received.
84 /// MessagePort messagePort = new MessagePort("SenderPort", true);
85 /// messagePort.MessageReceived += MessageReceivedCallback;
86 /// static void MessageReceivedCallback(object sender, MessageReceivedEventArgs e)
88 /// Console.WriteLine("Message Received ");
89 /// if (e.Remote.AppId != null) {
90 /// Console.WriteLine("from :"+e.Remote.AppId);
95 public event EventHandler<MessageReceivedEventArgs> MessageReceived;
98 /// The name of the local message port.
100 public string PortName
108 /// If true, the message port is a trusted port, otherwise false.
119 /// If true, the message port is listening, otherwise false.
121 public bool Listening
130 /// Register the local message port.
132 /// <exception cref="System.InvalidOperationException">Thrown when portName is already used, when there is an I/O error.</exception>
133 /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
134 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
137 /// MessagePort messagePort = new MessagePort("SenderPort", true);
138 /// messagePort.MessageReceived += MessageReceivedCallback;
139 /// messagePort.Listen();
146 if (s_portMap.Contains(_portName))
148 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, _portName + "is already used");
150 _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
152 MessageReceivedEventArgs args = new MessageReceivedEventArgs()
154 Message = new Bundle(new SafeBundleHandle(message, false))
157 if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
159 args.Remote = new RemoteValues()
162 PortName = remotePortName,
166 MessageReceived?.Invoke(this, args);
170 Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero) :
171 Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);
174 MessagePortErrorFactory.ThrowException(_portId, "RegisterPort", _portName);
176 s_portMap.Add(_portName);
182 /// Unregisters the local message port.
184 /// <exception cref="System.InvalidOperationException">Thrown when messageport is already stopped, when there is an I/O error, when the port is not found.</exception>
185 /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
186 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
189 /// MessagePort messagePort = new MessagePort("SenderPort", true);
190 /// messagePort.MessageReceived += MessageReceivedCallback;
191 /// messagePort.Listen();
192 /// using (var message = new Tizen.Application.Bundle())
194 /// message.AddItem("message", "a_string");
195 /// messagePort.Send(message, "ReceiverAppID", "ReceiverPort");
197 /// messagePort.StopListening();
200 public void StopListening()
204 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Already stopped");
208 Interop.MessagePort.UnregisterTrustedPort(_portId) :
209 Interop.MessagePort.UnregisterPort(_portId);
211 if (ret != (int)MessagePortError.None)
213 MessagePortErrorFactory.ThrowException(ret, "Error Unregister port");
218 s_portMap.Remove(_portName);
225 /// Sends an untrusted message to the message port of a remote application.
227 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB.</param>
228 /// <param name="remoteAppId">The ID of the remote application.</param>
229 /// <param name="remotePortName">The name of the remote message port.</param>
230 /// <exception cref="System.InvalidOperationException">Thrown when there is an I/O error, when the port is not found.</exception>
231 /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
232 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
233 /// <exception cref="System.ArgumentOutOfRangeException">Thrown when message has exceeded the maximum limit(4KB).</exception>
236 /// MessagePort messagePort = new MessagePort("SenderPort", true);
237 /// messagePort.MessageReceived += MessageReceivedCallback;
238 /// messagePort.Listen();
239 /// using (var message = new Tizen.Application.Bundle())
241 /// message.AddItem("message", "a_string");
242 /// messagePort.Send(message, "ReceiverAppID", "ReceiverPort");
246 public void Send(Bundle message, string remoteAppId, string remotePortName)
248 Send(message, remoteAppId, remotePortName, false);
252 /// Sends a message to the message port of a remote application.
254 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB.</param>
255 /// <param name="remoteAppId">The ID of the remote application.</param>
256 /// <param name="remotePortName">The name of the remote message port.</param>
257 /// <param name="trusted">If true, it is the trusted message port of remote application, otherwise false.</param>
258 /// <exception cref="System.InvalidOperationException">Thrown when there is an I/O error, when the port is not found.</exception>
259 /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
260 /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
261 /// <exception cref="System.ArgumentOutOfRangeException">Thrown when message has exceeded the maximum limit(4KB).</exception>
262 /// <exception cref="System.UnauthorizedAccessException">Thrown when the remote application is not signed with the same certificate.</exception>
265 /// MessagePort messagePort = new MessagePort("SenderPort", true);
266 /// messagePort.MessageReceived += MessageReceivedCallback;
267 /// messagePort.Listen();
268 /// using (var message = new Tizen.Application.Bundle())
270 /// message.AddItem("message", "a_string");
271 /// messagePort.Send(message, "ReceiverAppID", "ReceiverPort", true);
275 public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted)
279 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Should start listen before send");
281 if (message == null || message.SafeBundleHandle == null || message.SafeBundleHandle.IsInvalid)
283 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "message is null", "Message");
286 Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.SafeBundleHandle, _portId) :
287 Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.SafeBundleHandle, _portId);
289 if (ret != (int)MessagePortError.None)
291 if (ret == (int)MessagePortError.MaxExceeded)
293 MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
295 MessagePortErrorFactory.ThrowException(ret, "Can't send message");
300 /// Releases the unmanaged resources used by the MessagePort class specifying whether to perform a normal dispose operation.
302 /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
303 protected virtual void Dispose(bool disposing)
313 Log.Warn(GetType().Namespace, "Exception in Dispose :" + e.Message);
319 /// Releases all resources used by the MessagePort class.
321 public void Dispose()
324 GC.SuppressFinalize(this);