2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.Applications.RPCPort
22 /// Abstract class for creating a proxy class for RPC.
24 /// <since_tizen> 5 </since_tizen>
25 public abstract class ProxyBase : IDisposable
27 private Interop.LibRPCPort.Proxy.ConnectedEventCallback _connectedEventCallback;
28 private Interop.LibRPCPort.Proxy.DisconnectedEventCallback _disconnectedEventCallback;
29 private Interop.LibRPCPort.Proxy.RejectedEventCallback _rejectedEventCallback;
30 private Interop.LibRPCPort.Proxy.ReceivedEventCallback _receivedEventCallback;
31 private IntPtr _proxy;
36 /// <since_tizen> 5 </since_tizen>
37 protected Port Port { get; private set; }
40 /// Gets Port object for asynchronous events.
42 /// <since_tizen> 5 </since_tizen>
43 protected Port CallbackPort { get; private set; }
46 /// Constructor for this class.
48 /// <exception cref="InvalidIOException">Thrown when internal IO error occurs.</exception>
49 /// <since_tizen> 5 </since_tizen>
52 if (Interop.LibRPCPort.Proxy.Create(out _proxy) != Interop.LibRPCPort.ErrorCode.None)
53 throw new InvalidIOException();
54 _connectedEventCallback = new Interop.LibRPCPort.Proxy.ConnectedEventCallback(OnConnectedEvent);
55 _disconnectedEventCallback = new Interop.LibRPCPort.Proxy.DisconnectedEventCallback(OnDisconnectedEvent);
56 _rejectedEventCallback = new Interop.LibRPCPort.Proxy.RejectedEventCallback(OnRejectedEvent);
57 _receivedEventCallback = new Interop.LibRPCPort.Proxy.ReceivedEventCallback(OnReceivedEvent);
58 Interop.LibRPCPort.Proxy.AddConnectedEventCb(_proxy, _connectedEventCallback, IntPtr.Zero);
59 Interop.LibRPCPort.Proxy.AddDisconnectedEventCb(_proxy, _disconnectedEventCallback, IntPtr.Zero);
60 Interop.LibRPCPort.Proxy.AddRejectedEventCb(_proxy, _rejectedEventCallback, IntPtr.Zero);
61 Interop.LibRPCPort.Proxy.AddReceivedEventCb(_proxy, _receivedEventCallback, IntPtr.Zero);
67 /// <param name="appid">The target stub app ID.</param>
68 /// <param name="port">The name of the RPC port.</param>
69 /// <exception cref="InvalidIDException">Thrown when not available app ID is used.</exception>
70 /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
71 /// <exception cref="PermissionDeniedException">Thrown when the permission is denied.</exception>
72 /// <privilege>http://tizen.org/privilege/datasharing</privilege>
73 /// <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
74 /// <since_tizen> 5 </since_tizen>
75 protected void Connect(string appid, string port)
77 var err = Interop.LibRPCPort.Proxy.Connect(_proxy, appid, port);
80 case Interop.LibRPCPort.ErrorCode.InvalidParameter:
81 throw new InvalidIDException();
82 case Interop.LibRPCPort.ErrorCode.PermissionDenied:
83 throw new PermissionDeniedException();
84 case Interop.LibRPCPort.ErrorCode.IoError:
85 throw new InvalidIOException();
92 /// <param name="t">The type of port.</param>
93 /// <returns>Port object.</returns>
94 /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
95 /// <since_tizen> 5 </since_tizen>
96 protected Port GetPort(Port.Type t)
98 var err = Interop.LibRPCPort.Proxy.GetPort(_proxy,
99 (Interop.LibRPCPort.PortType)t, out IntPtr port);
102 case Interop.LibRPCPort.ErrorCode.InvalidParameter:
103 case Interop.LibRPCPort.ErrorCode.IoError:
104 throw new InvalidIOException();
107 return new Port() { Handle = port };
111 /// Abstract method for receiving connected event.
113 /// <param name="endPoint">The target stub app ID.</param>
114 /// <param name="portName">The name of the RPC port.</param>
115 /// <param name="port">Port object for reading and writing.</param>
116 /// <since_tizen> 5 </since_tizen>
117 protected abstract void OnConnectedEvent(string endPoint, string portName, Port port);
120 /// Abstract method for receiving disconnected event.
122 /// <param name="endPoint">The target stub app ID.</param>
123 /// <param name="portName">The name of the port.</param>
124 /// <since_tizen> 5 </since_tizen>
125 protected abstract void OnDisconnectedEvent(string endPoint, string portName);
128 /// Abstract method called when the proxy receives data from stub.
130 /// <param name="endPoint">The target stub app ID.</param>
131 /// <param name="portName">The name of the RPC port.</param>
132 /// <since_tizen> 5 </since_tizen>
133 protected abstract void OnReceivedEvent(string endPoint, string portName);
136 /// Abstract method for receiving rejected event.
138 /// <param name="endPoint">The target stub app ID.</param>
139 /// <param name="portName">The name of the RPC port.</param>
140 /// <since_tizen> 5 </since_tizen>
141 protected abstract void OnRejectedEvent(string endPoint, string portName);
143 private void OnConnectedEvent(string endPoint, string portName, IntPtr port, IntPtr data)
145 Port = new Port() { Handle = port };
146 CallbackPort = GetPort(Port.Type.Callback);
147 OnConnectedEvent(endPoint, portName, Port);
150 private void OnDisconnectedEvent(string endPoint, string portName, IntPtr data)
154 OnDisconnectedEvent(endPoint, portName);
157 private void OnReceivedEvent(string endPoint, string portName, IntPtr data)
159 OnReceivedEvent(endPoint, portName);
162 private void OnRejectedEvent(string endPoint, string portName, IntPtr data)
164 OnRejectedEvent(endPoint, portName);
167 #region IDisposable Support
168 private bool disposedValue = false;
171 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
173 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
174 /// <since_tizen> 6 </since_tizen>
175 protected virtual void Dispose(bool disposing)
182 if (_proxy != IntPtr.Zero)
183 Interop.LibRPCPort.Proxy.Destroy(_proxy);
184 _proxy = IntPtr.Zero;
186 disposedValue = true;
191 /// Finalizer of the class ProxyBase.
199 /// Release all resources used by the class ProxyBase.
201 /// <since_tizen> 5 </since_tizen>
202 public void Dispose()
205 GC.SuppressFinalize(this);