[Applications.Common] Remove warning messages (#5532)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications.RPCPort / StubBase.cs
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 namespace Tizen.Applications.RPCPort
20 {
21     /// <summary>
22     /// Abstract class for creating a stub class for RPC.
23     /// </summary>
24     /// <since_tizen> 5 </since_tizen>
25     public abstract class StubBase : IDisposable
26     {
27         private Interop.LibRPCPort.Stub.ConnectedEventCallback _connectedEventCallback;
28         private Interop.LibRPCPort.Stub.DisconnectedEventCallback _disconnectedEventCallback;
29         private Interop.LibRPCPort.Stub.ReceivedEventCallback _receivedEventCallback;
30         private IntPtr _stub;
31
32         /// <summary>
33         /// Gets port name.
34         /// </summary>
35         /// <since_tizen> 5 </since_tizen>
36         public string PortName { get; }
37
38         /// <summary>
39         /// Constructor for this class.
40         /// </summary>
41         /// <param name="portName">The name of the port that wants to listen.</param>
42         /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
43         /// <since_tizen> 5 </since_tizen>
44         public StubBase(string portName)
45         {
46             if (Interop.LibRPCPort.Stub.Create(out _stub, portName) != Interop.LibRPCPort.ErrorCode.None)
47                 throw new InvalidIOException();
48             PortName = portName;
49             _connectedEventCallback = new Interop.LibRPCPort.Stub.ConnectedEventCallback(OnConnectedEvent);
50             _disconnectedEventCallback = new Interop.LibRPCPort.Stub.DisconnectedEventCallback(OnDisconnectedEvent);
51             _receivedEventCallback = new Interop.LibRPCPort.Stub.ReceivedEventCallback(OnReceivedEvent);
52             Interop.LibRPCPort.Stub.AddReceivedEventCb(_stub, _receivedEventCallback, IntPtr.Zero);
53             Interop.LibRPCPort.Stub.AddConnectedEventCb(_stub, _connectedEventCallback, IntPtr.Zero);
54             Interop.LibRPCPort.Stub.AddDisconnectedEventCb(_stub, _disconnectedEventCallback, IntPtr.Zero);
55         }
56
57         /// <summary>
58         /// Listens to the requests for connections.
59         /// </summary>
60         /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
61         /// <since_tizen> 5 </since_tizen>
62         protected void Listen()
63         {
64             var err = Interop.LibRPCPort.Stub.Listen(_stub);
65             switch (err)
66             {
67                 case Interop.LibRPCPort.ErrorCode.InvalidParameter:
68                 case Interop.LibRPCPort.ErrorCode.IoError:
69                     throw new InvalidIOException();
70             }
71         }
72
73         /// <summary>
74         /// Adds a privilege to the stub.
75         /// </summary>
76         /// <param name="privilege">The privilege to access this stub.</param>
77         /// <exception cref="ArgumentNullException">Thrown when the privilege is null.</exception>
78         /// <since_tizen> 5 </since_tizen>
79         protected void AddPrivilege(string privilege)
80         {
81             if (privilege == null)
82             {
83                 throw new ArgumentNullException(nameof(privilege));
84             }
85
86             Interop.LibRPCPort.Stub.AddPrivilege(_stub, privilege);
87         }
88
89         /// <summary>
90         /// Sets a trusted proxy to the stub.
91         /// </summary>
92         /// <param name="trusted">Whether stub allows only trusted proxy or not.</param>
93         /// <since_tizen> 5 </since_tizen>
94         protected void SetTrusted(bool trusted)
95         {
96             Interop.LibRPCPort.Stub.SetTrusted(_stub, trusted);
97         }
98
99         /// <summary>
100         /// Gets s port.
101         /// </summary>
102         /// <param name="t">The type of port.</param>
103         /// <param name="instance">The ID of the instance, which is connected.</param>
104         /// <returns>Port object.</returns>
105         /// <exception cref="InvalidIDException">Thrown when invalid instance is used.</exception>
106         /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
107         /// <since_tizen> 5 </since_tizen>
108         protected Port GetPort(Port.Type t, string instance)
109         {
110             var err = Interop.LibRPCPort.Stub.GetPort(_stub,
111                 (Interop.LibRPCPort.PortType)t, instance, out IntPtr port);
112             switch (err)
113             {
114                 case Interop.LibRPCPort.ErrorCode.InvalidParameter:
115                     throw new InvalidIDException();
116                 case Interop.LibRPCPort.ErrorCode.IoError:
117                     throw new InvalidIOException();
118             }
119
120             return new Port() { Handle = port };
121         }
122
123         /// <summary>
124         /// Abstract method for receiving connected event.
125         /// </summary>
126         /// <param name="sender">The target proxy app ID.</param>
127         /// <param name="instance">The information about the request.</param>
128         /// <since_tizen> 5 </since_tizen>
129         protected abstract void OnConnectedEvent(string sender, string instance);
130
131         /// <summary>
132         /// Abstract method for receiving disconnected event.
133         /// </summary>
134         /// <param name="sender">The target proxy app ID.</param>
135         /// <param name="instance">The information about the request.</param>
136         /// <since_tizen> 5 </since_tizen>
137         protected abstract void OnDisconnectedEvent(string sender, string instance);
138
139         /// <summary>
140         /// Abstract method called when the stub receives data from proxy.
141         /// </summary>
142         /// <param name="sender">The target proxy app ID.</param>
143         /// <param name="instance">The information about the request.</param>
144         /// <param name="port">Port object for reading and writing.</param>
145         /// <returns><c>true</c> to continue receiving data, otherwise <c>false</c> to disconnect from the port.</returns>
146         /// <since_tizen> 5 </since_tizen>
147         protected abstract bool OnReceivedEvent(string sender, string instance, Port port);
148
149         /// <summary>
150         /// Abstract method called immediately before disposing an object.
151         /// </summary>
152         /// <since_tizen> 5 </since_tizen>
153         protected abstract void OnTerminatedEvent();
154
155         private void OnConnectedEvent(string sender, string instance, IntPtr data)
156         {
157             OnConnectedEvent(sender, instance);
158         }
159
160         private void OnDisconnectedEvent(string sender, string instance, IntPtr data)
161         {
162             OnDisconnectedEvent(sender, instance);
163         }
164
165         private int OnReceivedEvent(string sender, string instance, IntPtr port, IntPtr data)
166         {
167             bool b = OnReceivedEvent(sender, instance, new Port() { Handle = port });
168             if (b)
169                 return 0;
170             return -1;
171         }
172
173         #region IDisposable Support
174         private bool disposedValue = false;
175
176         /// <summary>
177         /// Releases any unmanaged resources used by this object and disposes any other disposable objects.
178         /// </summary>
179         /// <param name="disposing">true to disposes any disposable objects, or false not to dispose disposable objects.</param>
180         /// <since_tizen> 6 </since_tizen>
181         protected virtual void Dispose(bool disposing)
182         {
183             if (!disposedValue)
184             {
185                 if (disposing)
186                 {
187                 }
188
189                 OnTerminatedEvent();
190
191                 if (_stub != IntPtr.Zero)
192                     Interop.LibRPCPort.Stub.Destroy(_stub);
193                 _stub = IntPtr.Zero;
194
195                 disposedValue = true;
196             }
197         }
198
199         /// <summary>
200         /// Finalizer of the class StubBase.
201         /// </summary>
202         ~StubBase()
203         {
204             Dispose(false);
205         }
206
207         /// <summary>
208         /// Release all the resources used by the class StubBase.
209         /// </summary>
210         public void Dispose()
211         {
212             Dispose(true);
213             GC.SuppressFinalize(this);
214         }
215         #endregion
216     }
217 }