2 using System.Collections.Generic;
5 using System.Runtime.InteropServices;
7 using System.Threading.Tasks;
10 namespace Tizen.Applications.MessagePort
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
22 private static Dictionary<MessagePort, int> s_portMap = new Dictionary<MessagePort, int>();
27 /// <param name="portName">The name of the local message port</param>
28 /// <param name="trusted">If true is the trusted message port of application, otherwise false</param>
29 public MessagePort(string portName, bool trusted)
44 /// Called when a message is received.
46 public event EventHandler<MessageReceivedEventArgs> MessageReceived;
48 private enum MessagePortError
50 None = Internals.Errors.ErrorCode.None,
51 IOError = Internals.Errors.ErrorCode.IoError,
52 OutOfMemory = Internals.Errors.ErrorCode.OutOfMemory,
53 InvalidParameter = Internals.Errors.ErrorCode.InvalidParameter,
54 PortNotFound = -0x01130000 | 0x01,
55 CertificateNotMatch = -0x01130000 | 0x02,
56 MaxExceeded = -0x01130000 | 0x03,
57 ResourceUnavailable = -0x01130000 | 0x04
61 /// The name of the local message port
63 public string PortName
71 /// If true the message port is a trusted port, otherwise false it is not
82 /// If true the message port is listening, otherwise false it is not
93 /// The local message port ID
95 private int _portId = 0;
98 /// The name of the local message port
100 private string _portName = null;
103 /// If true the message port is a trusted port, otherwise false it is not
105 private bool _trusted = false;
108 /// If true the message port is listening, otherwise false it is not
110 private bool _listening = false;
112 private Interop.MessagePort.message_port_message_cb _messageCallBack;
115 /// Register the local message port.
119 if (!s_portMap.ContainsKey(this))
121 _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
123 Bundle bundle = new Bundle(message);
124 MessageReceivedEventArgs args;
126 if (remotePortName != null)
128 args = new MessageReceivedEventArgs(bundle, remoteAppId, remotePortName, trusted);
132 args = new MessageReceivedEventArgs(bundle);
135 RaiseMessageReceivedEvent(MessageReceived, args);
140 _portId = Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero);
144 _portId = Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);
149 s_portMap.Add(this, 1);
154 switch ((MessagePortError)_portId)
156 case MessagePortError.IOError: throw new IOException("I/O Error");
157 case MessagePortError.OutOfMemory: throw new InvalidOperationException("Out of memory");
158 case MessagePortError.InvalidParameter: throw new ArgumentException("Invalid parameter");
164 throw new ArgumentException("Already listening");
169 /// Unregisters the local message port.
171 public void StopListening()
178 ret = Interop.MessagePort.UnregisterTrustedPort(_portId);
182 ret = Interop.MessagePort.UnregisterPort(_portId);
187 s_portMap.Remove(this);
193 switch ((MessagePortError)ret)
195 case MessagePortError.IOError: throw new IOException("I/O Error");
196 case MessagePortError.OutOfMemory: throw new InvalidOperationException("Out of memory");
197 case MessagePortError.InvalidParameter: throw new ArgumentException("Invalid parameter");
198 case MessagePortError.PortNotFound: throw new ArgumentNullException("Port not found");
204 throw new InvalidOperationException("Already stopped");
209 /// Sends a message to the message port of a remote application.
211 /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB</param>
212 /// <param name="remoteAppId">The ID of the remote application</param>
213 /// <param name="remotePortName">The name of the remote message port</param>
214 /// <param name="trusted">If true the trusted message port of remote application otherwise false</param>
215 public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted=false)
222 ret = Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
226 ret = Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
230 switch ((MessagePortError)ret)
232 case MessagePortError.IOError: throw new IOException("I/O Error");
233 case MessagePortError.InvalidParameter: throw new ArgumentException("Invalid parameter");
234 case MessagePortError.OutOfMemory: throw new InvalidOperationException("Out of memory");
235 case MessagePortError.PortNotFound: throw new ArgumentNullException("Port not found");
236 case MessagePortError.CertificateNotMatch: throw new ArgumentException("Certification not match");
237 case MessagePortError.MaxExceeded: throw new ArgumentOutOfRangeException("Max(4KB) exceeded");
238 case MessagePortError.ResourceUnavailable: throw new ArgumentNullException("Resource unavailable");
244 throw new InvalidOperationException("Need listening");
249 /// Override GetHashCode
251 /// <returns></returns>
252 public override int GetHashCode()
255 if (_portName != null)
257 hash ^= _portName.GetHashCode();
259 hash ^= _trusted.GetHashCode();
266 /// <param name="obj"></param>
267 /// <returns></returns>
268 public override bool Equals(object obj)
270 MessagePort p = obj as MessagePort;
275 return (_portName == p._portName) & (_trusted == p._trusted);
278 private void RaiseMessageReceivedEvent(EventHandler<MessageReceivedEventArgs> evt, MessageReceivedEventArgs args)