2 using System.Collections.Generic;
5 using System.Runtime.InteropServices;
7 using System.Threading.Tasks;
10 namespace Tizen.Applications.Messages
13 /// The Message Port API provides functions to send and receive messages between applications.
16 /// 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.
17 /// If a remote application sends a message, the registered callback function of the local port is called.
18 /// The trusted message-port API allows communications between applications that are signed by the same developer(author) certificate.
20 public class MessagePort : IDisposable
22 private static readonly object s_lock = new object();
23 private static readonly HashSet<string> s_portMap = new HashSet<string>();
26 /// Initializes the instance of the MessagePort class.
28 /// <param name="portName">The name of the local message port</param>
29 /// <param name="trusted">If true is the trusted message port of application, otherwise false</param>
30 public MessagePort(string portName, bool trusted)
32 if (String.IsNullOrEmpty(portName))
34 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "Invalid PortName", "PortName");
41 /// Destructor of the MessagePort class.
49 /// Called when a message is received.
51 public event EventHandler<MessageReceivedEventArgs> MessageReceived;
54 /// The name of the local message port
56 public string PortName
64 /// If true the message port is a trusted port, otherwise false it is not
75 /// If true the message port is listening, otherwise false it is not
86 /// The local message port ID
88 private int _portId = 0;
91 /// The name of the local message port
93 private readonly string _portName = null;
96 /// If true the message port is a trusted port, otherwise false it is not
98 private readonly bool _trusted = false;
101 /// If true the message port is listening, otherwise false it is not
103 private bool _listening = false;
105 private Interop.MessagePort.message_port_message_cb _messageCallBack;
108 /// Register the local message port.
114 if (s_portMap.Contains(_portName))
116 throw new InvalidOperationException(_portName + " is already used");
118 _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
120 MessageReceivedEventArgs args = new MessageReceivedEventArgs()
122 Message = Bundle.MakeRetainedBundle(message)
125 if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
127 args.Remote = new RemoteValues()
130 PortName = remotePortName,
134 RaiseMessageReceivedEvent(MessageReceived, args);
138 Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero) :
139 Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);
142 throw new InvalidOperationException("Can't Listening with " + _portName);
144 s_portMap.Add(_portName);
150 /// Unregisters the local message port.
152 public void StopListening()
156 throw new InvalidOperationException("Already stopped");
160 Interop.MessagePort.UnregisterTrustedPort(_portId) :
161 Interop.MessagePort.UnregisterPort(_portId);
163 if (ret != (int)MessagePortError.None)
165 MessagePortErrorFactory.ThrowException(ret);
170 s_portMap.Remove(_portName);
177 /// Sends a untrusted message to the message port of a remote application.
179 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB</param>
180 /// <param name="remoteAppId">The ID of the remote application</param>
181 /// <param name="remotePortName">The name of the remote message port</param>
182 public void Send(Bundle message, string remoteAppId, string remotePortName)
184 Send(message, remoteAppId, remotePortName, false);
188 /// Sends a message to the message port of a remote application.
190 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB</param>
191 /// <param name="remoteAppId">The ID of the remote application</param>
192 /// <param name="remotePortName">The name of the remote message port</param>
193 /// <param name="trusted">If true the trusted message port of remote application otherwise false</param>
194 public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted)
198 throw new InvalidOperationException("Should start listen before send");
202 throw new ArgumentNullException("message");
205 Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId) :
206 Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
208 if (ret != (int)MessagePortError.None)
210 if (ret == (int)MessagePortError.MaxExceeded)
212 MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
214 MessagePortErrorFactory.ThrowException(ret, "Can't send message");
218 private void RaiseMessageReceivedEvent(EventHandler<MessageReceivedEventArgs> evt, MessageReceivedEventArgs args)
227 /// Releases the unmanaged resourced used by the MessagePort class specifying whether to perform a normal dispose operation.
229 /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
230 protected virtual void Dispose(bool disposing)
240 Tizen.Log.Warn(GetType().Namespace, "Exception in Dispose :" + e.Message);
246 /// Releases all resources used by the MessagePort class.
248 public void Dispose()
251 GC.SuppressFinalize(this);