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");
50 /// Called when a message is received.
52 public event EventHandler<MessageReceivedEventArgs> MessageReceived;
55 /// The name of the local message port
57 public string PortName
65 /// If true the message port is a trusted port, otherwise false it is not
76 /// If true the message port is listening, otherwise false it is not
87 /// The local message port ID
89 private int _portId = 0;
92 /// The name of the local message port
94 private string _portName = null;
97 /// If true the message port is a trusted port, otherwise false it is not
99 private bool _trusted = false;
102 /// If true the message port is listening, otherwise false it is not
104 private bool _listening = false;
106 private Interop.MessagePort.message_port_message_cb _messageCallBack;
109 /// Register the local message port.
115 if (s_portMap.Contains(_portName))
117 throw new InvalidOperationException(_portName + " is already used");
119 _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
121 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 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 /// <param name="trusted">If true the trusted message port of remote application otherwise false</param>
183 public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted = false)
187 throw new InvalidOperationException("Sould start listen before send");
190 Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId) :
191 Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
193 if (ret != (int)MessagePortError.None)
195 if (ret == (int)MessagePortError.MaxExceeded)
197 MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
199 MessagePortErrorFactory.ThrowException(ret, "Can't send message");
203 private void RaiseMessageReceivedEvent(EventHandler<MessageReceivedEventArgs> evt, MessageReceivedEventArgs args)
211 protected virtual void Dispose(bool disposing)
219 public void Dispose()
222 GC.SuppressFinalize(this);