/// <param name="trusted">If true is the trusted message port of application, otherwise false</param>
public MessagePort(string portName, bool trusted)
{
+ if (String.IsNullOrEmpty(portName))
+ {
+ MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidParameter, "Invalid PortName", "PortName");
+ }
_portName = portName;
_trusted = trusted;
}
/// </summary>
public event EventHandler<MessageReceivedEventArgs> MessageReceived;
- private enum MessagePortError
- {
- None = Internals.Errors.ErrorCode.None,
- IOError = Internals.Errors.ErrorCode.IoError,
- OutOfMemory = Internals.Errors.ErrorCode.OutOfMemory,
- InvalidParameter = Internals.Errors.ErrorCode.InvalidParameter,
- PortNotFound = -0x01130000 | 0x01,
- CertificateNotMatch = -0x01130000 | 0x02,
- MaxExceeded = -0x01130000 | 0x03,
- ResourceUnavailable = -0x01130000 | 0x04
- }
-
/// <summary>
/// The name of the local message port
/// </summary>
Bundle bundle = new Bundle(message);
MessageReceivedEventArgs args;
- if (remotePortName != null)
+ if (!String.IsNullOrEmpty(remotePortName))
{
args = new MessageReceivedEventArgs(bundle, remoteAppId, remotePortName, trusted);
}
_portId = Interop.MessagePort.RegisterPort(_portName, _messageCallBack, IntPtr.Zero);
}
- if(_portId > 0)
+ if (_portId > 0)
{
s_portMap.Add(this, 1);
_listening = true;
}
else
{
- switch ((MessagePortError)_portId)
- {
- case MessagePortError.IOError: throw new IOException("I/O Error");
- case MessagePortError.OutOfMemory: throw new InvalidOperationException("Out of memory");
- case MessagePortError.InvalidParameter: throw new ArgumentException("Invalid parameter");
- }
+ MessagePortErrorFactory.ThrowException(_portId);
}
}
else
{
- throw new ArgumentException("Already listening");
+ MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Already listening");
}
}
ret = Interop.MessagePort.UnregisterPort(_portId);
}
- if (ret == 0)
+ if (ret == (int)MessagePortError.None)
{
s_portMap.Remove(this);
_portId = 0;
}
else
{
- switch ((MessagePortError)ret)
- {
- case MessagePortError.IOError: throw new IOException("I/O Error");
- case MessagePortError.OutOfMemory: throw new InvalidOperationException("Out of memory");
- case MessagePortError.InvalidParameter: throw new ArgumentException("Invalid parameter");
- case MessagePortError.PortNotFound: throw new ArgumentNullException("Port not found");
- }
+ MessagePortErrorFactory.ThrowException(ret);
}
}
else
{
- throw new InvalidOperationException("Already stopped");
+ MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Already stopped");
}
}
/// <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 the trusted message port of remote application otherwise false</param>
- public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted=false)
+ public void Send(Bundle message, string remoteAppId, string remotePortName, bool trusted = false)
{
if (_listening)
{
{
ret = Interop.MessagePort.SendMessageWithLocalPort(remoteAppId, remotePortName, message.Handle, _portId);
}
- if (ret != 0)
+
+ if (ret != (int)MessagePortError.None)
{
- switch ((MessagePortError)ret)
+ if (ret== (int)MessagePortError.MaxExceeded)
+ {
+ MessagePortErrorFactory.ThrowException(ret, "Message has exceeded the maximum limit(4KB)", "Message");
+ }
+ else
{
- case MessagePortError.IOError: throw new IOException("I/O Error");
- case MessagePortError.InvalidParameter: throw new ArgumentException("Invalid parameter");
- case MessagePortError.OutOfMemory: throw new InvalidOperationException("Out of memory");
- case MessagePortError.PortNotFound: throw new ArgumentNullException("Port not found");
- case MessagePortError.CertificateNotMatch: throw new ArgumentException("Certification not match");
- case MessagePortError.MaxExceeded: throw new ArgumentOutOfRangeException("Max(4KB) exceeded");
- case MessagePortError.ResourceUnavailable: throw new ArgumentNullException("Resource unavailable");
+ MessagePortErrorFactory.ThrowException(ret);
}
}
}
else
{
- throw new InvalidOperationException("Need listening");
+ MessagePortErrorFactory.ThrowException((int)MessagePortError.InvalidOperation, "Need listening");
}
}
public override int GetHashCode()
{
int hash = 0;
- if (_portName != null)
+ if (!String.IsNullOrEmpty(_portName))
{
hash ^= _portName.GetHashCode();
}
}
}
}
-}
+}
\ No newline at end of file
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tizen.Internals.Errors;
+
+namespace Tizen.Applications.Messages
+{
+ internal enum MessagePortError
+ {
+ None = ErrorCode.None,
+ IOError = ErrorCode.IoError,
+ OutOfMemory = ErrorCode.OutOfMemory,
+ InvalidParameter = ErrorCode.InvalidParameter,
+ InvalidOperation = ErrorCode.InvalidOperation,
+ PortNotFound = -0x01130000 | 0x01,
+ CertificateNotMatch = -0x01130000 | 0x02,
+ MaxExceeded = -0x01130000 | 0x03,
+ ResourceUnavailable = -0x01130000 | 0x04
+ }
+ internal static class MessagePortErrorFactory
+ {
+ internal static void ThrowException(int errorCode, string errorMessage=null, string paramName=null)
+ {
+ MessagePortError err = (MessagePortError)errorCode;
+ if (String.IsNullOrEmpty(errorMessage))
+ {
+ errorMessage = err.ToString();
+ }
+ switch ((MessagePortError)errorCode)
+ {
+ case MessagePortError.IOError:
+ case MessagePortError.OutOfMemory:
+ case MessagePortError.InvalidOperation:
+ case MessagePortError.PortNotFound:
+ case MessagePortError.ResourceUnavailable: throw new InvalidOperationException(errorMessage);
+ case MessagePortError.InvalidParameter:
+ case MessagePortError.CertificateNotMatch: throw new ArgumentException(errorMessage, paramName);
+ case MessagePortError.MaxExceeded: throw new ArgumentOutOfRangeException(paramName, errorMessage);
+ }
+ }
+ }
+}
\ No newline at end of file