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 stub class for RPC.
24 /// <since_tizen> 5 </since_tizen>
25 public abstract class StubBase : IDisposable
27 private Interop.LibRPCPort.Stub.ConnectedEventCallback _connectedEventCallback;
28 private Interop.LibRPCPort.Stub.DisconnectedEventCallback _disconnectedEventCallback;
29 private Interop.LibRPCPort.Stub.ReceivedEventCallback _receivedEventCallback;
35 /// <since_tizen> 5 </since_tizen>
36 public string PortName { get; }
39 /// Constructor for this class.
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)
46 if (Interop.LibRPCPort.Stub.Create(out _stub, portName) != Interop.LibRPCPort.ErrorCode.None)
47 throw new InvalidIOException();
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);
58 /// Listens to the requests for connections.
60 /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
61 /// <since_tizen> 5 </since_tizen>
62 protected void Listen()
64 var err = Interop.LibRPCPort.Stub.Listen(_stub);
67 case Interop.LibRPCPort.ErrorCode.InvalidParameter:
68 case Interop.LibRPCPort.ErrorCode.IoError:
69 throw new InvalidIOException();
74 /// Adds a privilege to the stub.
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)
81 if (privilege == null)
83 throw new ArgumentNullException(nameof(privilege));
86 Interop.LibRPCPort.Stub.AddPrivilege(_stub, privilege);
90 /// Sets a trusted proxy to the stub.
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)
96 Interop.LibRPCPort.Stub.SetTrusted(_stub, trusted);
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)
110 var err = Interop.LibRPCPort.Stub.GetPort(_stub,
111 (Interop.LibRPCPort.PortType)t, instance, out IntPtr port);
114 case Interop.LibRPCPort.ErrorCode.InvalidParameter:
115 throw new InvalidIDException();
116 case Interop.LibRPCPort.ErrorCode.IoError:
117 throw new InvalidIOException();
120 return new Port() { Handle = port };
124 /// Abstract method for receiving connected event.
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);
132 /// Abstract method for receiving disconnected event.
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);
140 /// Abstract method called when the stub receives data from proxy.
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);
150 /// Abstract method called immediately before disposing an object.
152 /// <since_tizen> 5 </since_tizen>
153 protected abstract void OnTerminatedEvent();
155 private void OnConnectedEvent(string sender, string instance, IntPtr data)
157 OnConnectedEvent(sender, instance);
160 private void OnDisconnectedEvent(string sender, string instance, IntPtr data)
162 OnDisconnectedEvent(sender, instance);
165 private int OnReceivedEvent(string sender, string instance, IntPtr port, IntPtr data)
167 bool b = OnReceivedEvent(sender, instance, new Port() { Handle = port });
173 #region IDisposable Support
174 private bool disposedValue = false;
177 /// Releases any unmanaged resources used by this object and disposes any other disposable objects.
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)
191 if (_stub != IntPtr.Zero)
192 Interop.LibRPCPort.Stub.Destroy(_stub);
195 disposedValue = true;
200 /// Finalizer of the class StubBase.
208 /// Release all the resources used by the class StubBase.
210 public void Dispose()
213 GC.SuppressFinalize(this);