Exemple #1
0
        /// <summary>
        /// The constructor of ClientBase class.
        /// </summary>
        /// <param name="serviceName">The name of service.</param>
        /// <param name="security">The security configuration.</param>
        /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory to continue the execution of the method.</exception>
        /// <since_tizen> 9 </since_tizen>
        public ClientBase(string serviceName, Cion.SecurityInfo security)
        {
            ServiceName = serviceName;

            Cion.SecuritySafeHandle handle = security?._handle;
            Interop.Cion.ErrorCode  ret    = Interop.CionClient.CionClientCreate(out _handle, serviceName, handle?.DangerousGetHandle() ?? IntPtr.Zero);
            if (ret != Interop.Cion.ErrorCode.None)
            {
                throw CionErrorFactory.GetException(ret, "Failed to create client.");
            }

            _connectionResultCb = new Interop.CionClient.CionClientConnectionResultCb(
                (string service, IntPtr peerInfo, IntPtr result, IntPtr userData) =>
            {
                Interop.Cion.ErrorCode clone_ret = Interop.CionPeerInfo.CionPeerInfoClone(peerInfo, out PeerInfoSafeHandle clone);
                if (clone_ret != Interop.Cion.ErrorCode.None)
                {
                    Log.Error(LogTag, string.Format("Failed to clone peer info."));
                    return;
                }

                PeerInfo peer = new PeerInfo(clone);
                ConnectionResult connectionResult = new ConnectionResult(result);
                if (connectionResult.Status == ConnectionStatus.OK)
                {
                    _peer = peer;
                }

                OnConnectionResult(peer, connectionResult);
            });
            ret = Interop.CionClient.CionClientAddConnectionResultCb(_handle, _connectionResultCb, IntPtr.Zero);
            if (ret != Interop.Cion.ErrorCode.None)
            {
                _handle.Dispose();
                throw CionErrorFactory.GetException(ret, "Failed to add connection status changed callback.");
            }

            _payloadRecievedCb = new Interop.CionClient.CionClientPayloadRecievedCb(
                (string service, IntPtr peerInfo, IntPtr payload, int status, IntPtr userData) =>
            {
                Payload receivedPayload;
                Interop.CionPayload.CionPayloadGetType(payload, out Interop.CionPayload.PayloadType type);
                switch (type)
                {
                case Interop.CionPayload.PayloadType.Data:
                    receivedPayload = new DataPayload(new PayloadSafeHandle(payload, false));
                    break;

                case Interop.CionPayload.PayloadType.File:
                    receivedPayload = new FilePayload(new PayloadSafeHandle(payload, false));
                    break;

                default:
                    Log.Error(LogTag, "Invalid payload type received.");
                    return;
                }
                OnPayloadReceived(receivedPayload, (PayloadTransferStatus)status);
            });
            ret = Interop.CionClient.CionClientAddPayloadReceivedCb(_handle, _payloadRecievedCb, IntPtr.Zero);
            if (ret != Interop.Cion.ErrorCode.None)
            {
                _handle.Dispose();
                throw CionErrorFactory.GetException(ret, "Failed to add payload received callback.");
            }

            _disconnectedCb = new Interop.CionClient.CionClientDisconnectedCb(
                (string service, IntPtr peerInfo, IntPtr userData) =>
            {
                Interop.Cion.ErrorCode clone_ret = Interop.CionPeerInfo.CionPeerInfoClone(peerInfo, out PeerInfoSafeHandle clone);
                if (clone_ret != Interop.Cion.ErrorCode.None)
                {
                    Log.Error(LogTag, string.Format("Failed to clone peer info."));
                    return;
                }
                OnDisconnected(new PeerInfo(clone));
            });
            ret = Interop.CionClient.CionClientAddDisconnectedCb(_handle, _disconnectedCb, IntPtr.Zero);
            if (ret != Interop.Cion.ErrorCode.None)
            {
                _handle.Dispose();
                throw CionErrorFactory.GetException(ret, "Failed to add disconnected callback.");
            }
        }
Exemple #2
0
 /// <summary>
 /// The result callback of connection request.
 /// </summary>
 /// <param name="peerInfo">The peer info of the cion server.</param>
 /// <param name="result">The result of the connection.</param>
 /// <since_tizen> 9 </since_tizen>
 protected abstract void OnConnectionResult(PeerInfo peerInfo, ConnectionResult result);