2 using System.Collections.Generic;
4 namespace Tizen.Applications.Messages
7 /// The Message Port API provides functions to send and receive messages between applications.
10 /// 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.
11 /// If a remote application sends a message, the registered callback function of the local port is called.
12 /// The trusted message-port API allows communications between applications that are signed by the same developer(author) certificate.
14 public class MessagePort : IDisposable
16 private static readonly object s_lock = new object();
17 private static readonly HashSet<string> s_portMap = new HashSet<string>();
19 // The name of the local message port
20 private readonly string _portName = null;
22 // If true the message port is a trusted port, otherwise false it is not
23 private readonly bool _trusted = false;
25 // The local message port ID
26 private int _portId = 0;
28 // If true the message port is listening, otherwise false it is not
29 private bool _listening = false;
31 private Interop.MessagePort.message_port_message_cb _messageCallBack;
34 /// Initializes the instance of the MessagePort class.
36 /// <param name="portName">The name of the local message port</param>
37 /// <param name="trusted">If true is the trusted message port of application, otherwise false</param>
38 public MessagePort(string portName, bool trusted)
40 if (String.IsNullOrEmpty(portName))
42 MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "Invalid PortName", "PortName");
49 /// Destructor of the MessagePort class.
57 /// Called when a message is received.
59 public event EventHandler<MessageReceivedEventArgs> MessageReceived;
62 /// The name of the local message port
64 public string PortName
72 /// If true the message port is a trusted port, otherwise false it is not
83 /// If true the message port is listening, otherwise false it is not
94 /// Register the local message port.
100 if (s_portMap.Contains(_portName))
102 throw new InvalidOperationException(_portName + " is already used");
104 _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
106 MessageReceivedEventArgs args = new MessageReceivedEventArgs()
108 Message = Bundle.MakeRetainedBundle(message)
111 if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
113 args.Remote = new RemoteValues()
116 PortName = remotePortName,
120 MessageReceived?.Invoke(this, args);
124 Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero) :
125 Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);
128 throw new InvalidOperationException("Can't Listening with " + _portName);
130 s_portMap.Add(_portName);
136 /// Unregisters the local message port.
138 public void StopListening()
142 throw new InvalidOperationException("Already stopped");
146 Interop.MessagePort.UnregisterTrustedPort(_portId) :
147 Interop.MessagePort.UnregisterPort(_portId);
149 if (ret != (int)MessagePortError.None)
151 MessagePortErrorFactory.ThrowException(ret);
156 s_portMap.Remove(_portName);
163 /// Sends a untrusted message to the message port of a remote application.
165 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB</param>
166 /// <param name="remoteAppId">The ID of the remote application</param>
167 /// <param name="remotePortName">The name of the remote message port</param>
168 public void Send(Bundle message, string remoteAppId, string remotePortName)
170 Send(message, remoteAppId, remotePortName, false);
174 /// Sends a message to the message port of a remote application.
176 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB</param>
177 /// <param name="remoteAppId">The ID of the remote application</param>
178 /// <param name="remotePortName">The name of the remote message port</param>
179 /// <param name="trusted">If true the trusted message port of remote application otherwise false</param>
180 public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted)
184 throw new InvalidOperationException("Should start listen before send");
188 throw new ArgumentNullException("message");
191 Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId) :
192 Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
194 if (ret != (int)MessagePortError.None)
196 if (ret == (int)MessagePortError.MaxExceeded)
198 MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
200 MessagePortErrorFactory.ThrowException(ret, "Can't send message");
205 /// Releases the unmanaged resourced used by the MessagePort class specifying whether to perform a normal dispose operation.
207 /// <param name="disposing">true for a normal dispose operation; false to finalize the handle.</param>
208 protected virtual void Dispose(bool disposing)
218 Log.Warn(GetType().Namespace, "Exception in Dispose :" + e.Message);
224 /// Releases all resources used by the MessagePort class.
226 public void Dispose()
229 GC.SuppressFinalize(this);