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 object s_lock = new object();
23 private static HashSet<string> s_portMap = new HashSet<string>();
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");
46 /// Called when a message is received.
48 public event EventHandler<MessageReceivedEventArgs> MessageReceived;
51 /// The name of the local message port
53 public string PortName
61 /// If true the message port is a trusted port, otherwise false it is not
72 /// If true the message port is listening, otherwise false it is not
83 /// The local message port ID
85 private int _portId = 0;
88 /// The name of the local message port
90 private string _portName = null;
93 /// If true the message port is a trusted port, otherwise false it is not
95 private bool _trusted = false;
98 /// If true the message port is listening, otherwise false it is not
100 private bool _listening = false;
102 private Interop.MessagePort.message_port_message_cb _messageCallBack;
105 /// Register the local message port.
111 if (s_portMap.Contains(_portName))
113 throw new InvalidOperationException(_portName + " is already used");
115 _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
117 MessageReceivedEventArgs args = new MessageReceivedEventArgs() {
118 Message = Bundle.MakeRetainedBundle(message)
121 if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
123 args.Remote = new RemoteValues()
126 PortName = remotePortName,
130 RaiseMessageReceivedEvent(MessageReceived, args);
134 Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero) :
135 Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);
138 throw new InvalidOperationException("Can't Listening with " + _portName);
140 s_portMap.Add(_portName);
146 /// Unregisters the local message port.
148 public void StopListening()
152 throw new InvalidOperationException("Already stopped");
156 Interop.MessagePort.UnregisterTrustedPort(_portId) :
157 Interop.MessagePort.UnregisterPort(_portId);
159 if (ret != (int)MessagePortError.None)
161 MessagePortErrorFactory.ThrowException(ret);
166 s_portMap.Remove(_portName);
173 /// Sends a message to the message port of a remote application.
175 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB</param>
176 /// <param name="remoteAppId">The ID of the remote application</param>
177 /// <param name="remotePortName">The name of the remote message port</param>
178 /// <param name="trusted">If true the trusted message port of remote application otherwise false</param>
179 public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted = false)
183 throw new InvalidOperationException("Sould start listen before send");
186 Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId) :
187 Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
189 if (ret != (int)MessagePortError.None)
191 if (ret == (int)MessagePortError.MaxExceeded)
193 MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
195 MessagePortErrorFactory.ThrowException(ret, "Can't send message");
199 private void RaiseMessageReceivedEvent(EventHandler<MessageReceivedEventArgs> evt, MessageReceivedEventArgs args)
207 protected virtual void Dispose(bool disposing)
214 } catch (Exception e)
216 Tizen.Log.Warn(GetType().Namespace, "Exception in Dispose :" + e.Message);
221 public void Dispose()
224 GC.SuppressFinalize(this);