Beispiel #1
0
 /// <summary>
 /// Connects with the cion server.
 /// </summary>
 /// <param name="peer">The peer to connect.</param>
 /// <privilege>http://tizen.org/privilege/d2d.datasharing</privilege>
 /// <privilege>http://tizen.org/privilege/internet</privilege>
 /// <privlevel>public</privlevel>
 /// <exception cref="InvalidOperationException">Thrown when the client cannot connect to server.</exception>
 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
 /// <since_tizen> 9 </since_tizen>
 public void Connect(PeerInfo peer)
 {
     Interop.Cion.ErrorCode ret = Interop.CionClient.CionClientConnect(_handle, peer?._handle);
     if (ret != Interop.Cion.ErrorCode.None)
     {
         throw CionErrorFactory.GetException(ret, "Failed to connect.");
     }
 }
Beispiel #2
0
 /// <summary>
 /// Disconnects with the peer.
 /// </summary>
 /// <param name="peerInfo">The peer to disconnect.</param>
 /// <exception cref="ArgumentException">Thrown when the given peer info is invalid.</exception>
 /// <since_tizen> 9 </since_tizen>
 public void Disconnect(PeerInfo peerInfo)
 {
     Interop.Cion.ErrorCode ret = Interop.CionServer.CionServerDisconnect(_handle, peerInfo?._handle);
     if (ret != Interop.Cion.ErrorCode.None)
     {
         throw CionErrorFactory.GetException(ret, "Failed to stop server.");
     }
 }
Beispiel #3
0
 /// <summary>
 /// Disconnects from the cion server.
 /// </summary>
 /// <since_tizen> 9 </since_tizen>
 public void Disconnect()
 {
     Interop.Cion.ErrorCode ret = Interop.CionClient.CionClientDisconnect(_handle);
     if (ret != Interop.Cion.ErrorCode.None)
     {
         Log.Error(LogTag, string.Format("Failed to disconnect: {0}", ret));
     }
     _peer = null;
 }
Beispiel #4
0
 /// <summary>
 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
 /// </summary>
 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
 /// <since_tizen> 9 </since_tizen>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             PeerInfo?.Dispose();
         }
         disposedValue = true;
     }
 }
Beispiel #5
0
        /// <summary>
        /// Sends the payload to a peer asynchronously.
        /// </summary>
        /// <param name="payload">The payload to send.</param>
        /// <param name="peerInfo">The peer to send payload.</param>
        /// <exception cref="ArgumentException">Thrown when the payload is not valid.</exception>
        /// <exception cref="InvalidOperationException">Thrown when there is no such connected cion client or failed to send payload.</exception>
        /// <since_tizen> 9 </since_tizen>
        public Task <PayloadAsyncResult> SendPayloadAsync(Payload payload, PeerInfo peerInfo)
        {
            if (payload == null || payload.Id.Length == 0 || peerInfo == null || peerInfo.UUID.Length == 0)
            {
                throw new ArgumentException("Payload or peerinfo is invalid.");
            }

            TaskCompletionSource <PayloadAsyncResult> tcs = new TaskCompletionSource <PayloadAsyncResult>();

            _tcsDictionary[Tuple.Create(payload.Id, peerInfo.UUID)] = tcs;

            if (_payloadAsyncResultCb == null)
            {
                Interop.CionServer.CionServerPayloadAsyncResultCb cb = new Interop.CionServer.CionServerPayloadAsyncResultCb(
                    (IntPtr result, IntPtr userData) =>
                {
                    PayloadAsyncResult resultPayload = null;
                    try
                    {
                        resultPayload = PayloadAsyncResult.CreateFromHandle(result);
                    }
                    catch (Exception e)
                    {
                        Log.Error(LogTag, string.Format("Failed to create PayloadAsyncResult from result handle: {0}.", e.Message));
                        return;
                    }
                    TaskCompletionSource <PayloadAsyncResult> tcsToReturn = _tcsDictionary[Tuple.Create(resultPayload.PayloadId, resultPayload.PeerInfo.UUID)];
                    tcsToReturn.SetResult(resultPayload);
                    _tcsDictionary.Remove(Tuple.Create(resultPayload.PayloadId, resultPayload.PeerInfo.UUID));
                });
                _payloadAsyncResultCb = cb;
            }

            Interop.Cion.ErrorCode ret = Interop.CionServer.CionServerSendPayloadAsync(_handle, peerInfo?._handle, payload?._handle, _payloadAsyncResultCb, IntPtr.Zero);
            if (ret != Interop.Cion.ErrorCode.None)
            {
                throw CionErrorFactory.GetException(ret, "Failed to send payload.");
            }

            return(tcs.Task);
        }
Beispiel #6
0
 /// <summary>
 /// The callback invoked when received payload.
 /// </summary>
 /// <param name="data">The received data.</param>
 /// <param name="peerInfo">The peer info of the cion client.</param>
 /// <param name="status">The status of payload transfer.</param>
 /// <since_tizen> 9 </since_tizen>
 protected abstract void OnPayloadReceived(Payload data, PeerInfo peerInfo, PayloadTransferStatus status);
Beispiel #7
0
 /// <summary>
 /// The callback invoked when received data.
 /// </summary>
 /// <param name="data">The received data.</param>
 /// <param name="peerInfo">The peer info of the cion client.</param>
 /// <since_tizen> 9 </since_tizen>
 protected abstract byte[] OnDataReceived(byte[] data, PeerInfo peerInfo);
Beispiel #8
0
 /// <summary>
 /// The result callback of connection request.
 /// </summary>
 /// <param name="peerInfo">The peer info of the cion client.</param>
 /// <param name="result">The result of the connection.</param>
 /// <since_tizen> 9 </since_tizen>
 protected abstract void OnConnectionResult(PeerInfo peerInfo, ConnectionResult result);
Beispiel #9
0
 /// <summary>
 /// Rejects the connection request from the peer.
 /// </summary>
 /// <param name="peerInfo">The peer to reject the connection request.</param>
 /// <param name="reason">The reason why reject the connection request.</param>
 /// <since_tizen> 9 </since_tizen>
 public void Reject(PeerInfo peerInfo, string reason)
 {
     Interop.CionServer.CionServerReject(_handle, peerInfo?._handle, reason);
 }
Beispiel #10
0
 /// <summary>
 /// Accepts the connection request from the peer.
 /// </summary>
 /// <param name="peerInfo">The peer to accept the connection request.</param>
 /// <since_tizen> 9 </since_tizen>
 public void Accept(PeerInfo peerInfo)
 {
     Interop.CionServer.CionServerAccept(_handle, peerInfo?._handle);
 }
Beispiel #11
0
 /// <summary>
 /// The callback invoked when another peer joined in the current group.
 /// </summary>
 /// <param name="peerInfo">The peer info of joined in the current group.</param>
 /// <since_tizen> 9 </since_tizen>
 protected abstract void OnJoined(PeerInfo peerInfo);
Beispiel #12
0
 private PayloadAsyncResult(PayloadAsyncResultCode result, PeerInfo peer, string payloadId)
 {
     Result    = result;
     PeerInfo  = peer;
     PayloadId = payloadId;
 }
Beispiel #13
0
        /// <summary>
        /// The constructor of ClientBase class.
        /// </summary>
        /// <param name="serviceName">The name of service.</param>
        /// <param name="security">The security configuration.</param>
        /// <remarks>The maximum length of service name is 512.</remarks>
        /// <exception cref="ArgumentException">Thrown when the given service name is too long.</exception>
        /// <exception cref="InvalidOperationException">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;

            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.CionClientPayloadReceivedCb(
                (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.");
            }
        }
Beispiel #14
0
 /// <summary>
 /// The callback invoked when another peer left from the current group.
 /// </summary>
 /// <param name="peerInfo">The peer info of left from the current group.</param>
 /// <since_tizen> 9 </since_tizen>
 protected abstract void OnLeft(PeerInfo peerInfo);
Beispiel #15
0
 /// <summary>
 /// The callback invoked when connection requested from the cion client.
 /// </summary>
 /// <param name="peerInfo">The peer info of the cion client.</param>
 /// <since_tizen> 9 </since_tizen>
 protected abstract void OnConnectionRequest(PeerInfo peerInfo);
Beispiel #16
0
 /// <summary>
 /// The callback invoked when disconnected with cion client.
 /// </summary>
 /// <param name="peerInfo">The peer info of the cion client.</param>
 /// <since_tizen> 9 </since_tizen>
 protected abstract void OnDisconnected(PeerInfo peerInfo);
Beispiel #17
0
 /// <summary>
 /// The callback invoked when payload received.
 /// </summary>
 /// <since_tizen> 9 </since_tizen>
 protected abstract void OnPayloadReceived(Payload payload, PeerInfo peer);