コード例 #1
0
ファイル: RemotePort.cs プロジェクト: yourina/TizenFX
        /// <summary>
        /// Constructor of the RemotePort class.
        /// </summary>
        /// <since_tizen> 4 </since_tizen>
        /// <param name="appId">The Id of the remote application</param>
        /// <param name="portName">The name of the remote message port</param>
        /// <param name="trusted">If true is the trusted message port of application, otherwise false</param>
        /// <exception cref="System.ArgumentException">Thrown when appId is null or empty, when portName is null or empty</exception>
        /// <example>
        /// <code>
        /// RemotePort remotePort = new RemotePort("org.tizen.example.messageport", "SenderPort", false);
        /// </code>
        /// </example>
        public RemotePort(String appId, string portName, bool trusted)
        {
            if (String.IsNullOrEmpty(appId) || String.IsNullOrEmpty(portName))
            {
                MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "Remote Port", "AppId or PortName");
            }

            _appId    = appId;
            _portName = portName;
            _trusted  = trusted;

            _registeredCallBack = (string remoteAppId, string remotePortName, bool remoteTrusted, IntPtr userData) =>
            {
                RemotePortStateChangedEventArgs args = new RemotePortStateChangedEventArgs()
                {
                    Status = State.Registered
                };

                _remotePortRegistered?.Invoke(this, args);
            };

            _unregisteredCallBack = (string remoteAppId, string remotePortName, bool remoteTrusted, IntPtr userData) =>
            {
                RemotePortStateChangedEventArgs args = new RemotePortStateChangedEventArgs()
                {
                    Status = State.Unregistered
                };

                _remotePortUnregistered?.Invoke(this, args);
            };
        }
コード例 #2
0
 /// <summary>
 /// Initializes the instance of the MessagePort class.
 /// </summary>
 /// <param name="portName">The name of the local message port.</param>
 /// <param name="trusted">If true, it is the trusted message port of application, otherwise false.</param>
 /// <exception cref="System.ArgumentException">Thrown when portName is null or empty.</exception>
 /// <example>
 /// <code>
 /// MessagePort messagePort = new MessagePort("SenderPort", true);
 /// </code>
 /// </example>
 /// <since_tizen> 3 </since_tizen>
 public MessagePort(string portName, bool trusted)
 {
     if (String.IsNullOrEmpty(portName))
     {
         MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "Invalid PortName", "PortName");
     }
     _portName = portName;
     _trusted  = trusted;
 }
コード例 #3
0
        /// <summary>
        /// Register the local message port.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown when portName is already used, when there is an I/O error.</exception>
        /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
        /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
        /// <example>
        /// <code>
        /// MessagePort messagePort = new MessagePort("SenderPort", true);
        /// messagePort.MessageReceived += MessageReceivedCallback;
        /// messagePort.Listen();
        /// </code>
        /// </example>
        /// <since_tizen> 3 </since_tizen>
        public void Listen()
        {
            lock (s_lock)
            {
                if (s_portMap.Contains(_portName))
                {
                    MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, _portName + "is already used");
                }
                _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
                {
                    MessageReceivedEventArgs args = new MessageReceivedEventArgs();
                    try
                    {
                        args.Message = new Bundle(new SafeBundleHandle(message, false));
                    }
                    catch (Exception ex)
                    {
                        Log.Error(LogTag, "Exception(" + ex.ToString() + ")");
                        args.Message = null;
                    }

                    if (args.Message == null)
                    {
                        Log.Error(LogTag, "Failed to create Bundle. message({0})", (message == IntPtr.Zero) ? "null" : message.ToString());
                        return;
                    }

                    if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
                    {
                        args.Remote = new RemoteValues()
                        {
                            AppId    = remoteAppId,
                            PortName = remotePortName,
                            Trusted  = trusted
                        };
                    }
                    MessageReceived?.Invoke(this, args);
                };

                _portId = _trusted ?
                          Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero) :
                          Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);

                if (_portId <= 0)
                {
                    MessagePortErrorFactory.ThrowException(_portId, "RegisterPort", _portName);
                }

                s_portMap.Add(_portName);
                _listening = true;
            }
        }
コード例 #4
0
ファイル: RemotePort.cs プロジェクト: yourina/TizenFX
        /// <summary>
        /// Check if the remote message port is running.
        /// </summary>
        /// <since_tizen> 4 </since_tizen>
        /// <exception cref="System.InvalidOperationException">Thrown when there is an I/O error</exception>
        /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
        /// <example>
        /// <code>
        /// Remote remotePort = new RemotePort("org.tizen.example", "SenderPort", true);
        /// bool isRunning = remotePort.isRunning();
        /// </code>
        /// </example>
        /// <returns> Return true if Remote Port is running </returns>
        public bool IsRunning()
        {
            int ret;

            ret = _trusted ?
                  Interop.MessagePort.CheckTrustedRemotePort(_appId, _portName, out _isRunning) :
                  Interop.MessagePort.CheckRemotePort(_appId, _portName, out _isRunning);

            if (ret == (int)MessagePortError.CertificateNotMatch)
            {
                /* Although Remote port is NotMatch, it is running */
                _isRunning = true;
            }
            else if (ret != (int)MessagePortError.None)
            {
                MessagePortErrorFactory.ThrowException(ret);
            }

            return(_isRunning);
        }
コード例 #5
0
ファイル: MessagePort.cs プロジェクト: yourina/TizenFX
        /// <summary>
        /// Register the local message port.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown when portName is already used, when there is an I/O error.</exception>
        /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
        /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
        /// <example>
        /// <code>
        /// MessagePort messagePort = new MessagePort("SenderPort", true);
        /// messagePort.MessageReceived += MessageReceivedCallback;
        /// messagePort.Listen();
        /// </code>
        /// </example>
        /// <since_tizen> 3 </since_tizen>
        public void Listen()
        {
            lock (s_lock)
            {
                if (s_portMap.Contains(_portName))
                {
                    MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, _portName + "is already used");
                }
                _messageCallBack = (int localPortId, string remoteAppId, string remotePortName, bool trusted, IntPtr message, IntPtr userData) =>
                {
                    MessageReceivedEventArgs args = new MessageReceivedEventArgs()
                    {
                        Message = new Bundle(new SafeBundleHandle(message, false))
                    };

                    if (!String.IsNullOrEmpty(remotePortName) && !String.IsNullOrEmpty(remoteAppId))
                    {
                        args.Remote = new RemoteValues()
                        {
                            AppId    = remoteAppId,
                            PortName = remotePortName,
                            Trusted  = trusted
                        };
                    }
                    MessageReceived?.Invoke(this, args);
                };

                _portId = _trusted ?
                          Interop.MessagePort.RegisterTrustedPort(_portName, _messageCallBack, IntPtr.Zero) :
                          Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);

                if (_portId <= 0)
                {
                    MessagePortErrorFactory.ThrowException(_portId, "RegisterPort", _portName);
                }

                s_portMap.Add(_portName);
                _listening = true;
            }
        }
コード例 #6
0
ファイル: RemotePort.cs プロジェクト: yourina/TizenFX
        private void RemoveRegistrationCallback()
        {
            if (_watcherIdForRegistered != -1 && _watcherIdForUnRegistered != -1)
            {
                int ret = Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForRegistered);

                if (ret != (int)MessagePortError.None)
                {
                    MessagePortErrorFactory.ThrowException(ret);
                }

                _watcherIdForRegistered = -1;

                ret = Interop.MessagePort.RemoveRegistrationCallback(_watcherIdForUnRegistered);

                if (ret != (int)MessagePortError.None)
                {
                    MessagePortErrorFactory.ThrowException(ret);
                }

                _watcherIdForUnRegistered = -1;
            }
        }
コード例 #7
0
        /// <summary>
        /// Sends a message to the message port of a remote application.
        /// </summary>
        /// <param name="message">The message to be passed to the remote application, the recommended message size is under 4KB.</param>
        /// <param name="remoteAppId">The ID of the remote application.</param>
        /// <param name="remotePortName">The name of the remote message port.</param>
        /// <param name="trusted">If true, it is the trusted message port of remote application, otherwise false.</param>
        /// <exception cref="System.InvalidOperationException">Thrown when there is an I/O error, when the port is not found.</exception>
        /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
        /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown when message has exceeded the maximum limit(4KB).</exception>
        /// <exception cref="System.UnauthorizedAccessException">Thrown when the remote application is not signed with the same certificate.</exception>
        /// <example>
        /// <code>
        /// MessagePort messagePort = new MessagePort("SenderPort", true);
        /// messagePort.MessageReceived += MessageReceivedCallback;
        /// messagePort.Listen();
        /// using (var message = new Tizen.Application.Bundle())
        /// {
        ///     message.AddItem("message", "a_string");
        ///     messagePort.Send(message, "ReceiverAppID", "ReceiverPort", true);
        /// }
        /// </code>
        /// </example>
        /// <since_tizen> 3 </since_tizen>
        public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted)
        {
            if (!_listening)
            {
                MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Should start listen before send");
            }
            if (message == null || message.SafeBundleHandle == null || message.SafeBundleHandle.IsInvalid)
            {
                MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "message is null", "Message");
            }
            int ret = trusted ?
                      Interop.MessagePort.SendTrustedMessageWithLocalPort(remoteAppId, remotePortName, message.SafeBundleHandle, _portId) :
                      Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.SafeBundleHandle, _portId);

            if (ret != (int)MessagePortError.None)
            {
                if (ret == (int)MessagePortError.MaxExceeded)
                {
                    MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
                }
                MessagePortErrorFactory.ThrowException(ret, "Can't send message");
            }
        }
コード例 #8
0
        /// <summary>
        /// Unregisters the local message port.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown when messageport is already stopped, when there is an I/O error, when the port is not found.</exception>
        /// <exception cref="System.ArgumentException">Thrown when there is an invalid parameter.</exception>
        /// <exception cref="System.OutOfMemoryException">Thrown when out of memory.</exception>
        /// <example>
        /// <code>
        /// MessagePort messagePort = new MessagePort("SenderPort", true);
        /// messagePort.MessageReceived += MessageReceivedCallback;
        /// messagePort.Listen();
        /// using (var message = new Tizen.Application.Bundle())
        /// {
        ///     message.AddItem("message", "a_string");
        ///     messagePort.Send(message, "ReceiverAppID", "ReceiverPort");
        /// }
        /// messagePort.StopListening();
        /// </code>
        /// </example>
        /// <since_tizen> 3 </since_tizen>
        public void StopListening()
        {
            if (!_listening)
            {
                MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Already stopped");
            }

            int ret = _trusted ?
                      Interop.MessagePort.UnregisterTrustedPort(_portId) :
                      Interop.MessagePort.UnregisterPort(_portId);

            if (ret != (int)MessagePortError.None)
            {
                MessagePortErrorFactory.ThrowException(ret, "Error Unregister port");
            }

            lock (s_lock)
            {
                s_portMap.Remove(_portName);
            }
            _portId    = 0;
            _listening = false;
        }