/// <summary> /// Connects with the cion server. /// </summary> /// <param name="peer">The peer to connect.</param> /// <privilege>http://tizen.org/privilege/d2d.datasharing</privilege> /// <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."); } }
/// <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."); } }
/// <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) { throw CionErrorFactory.GetException(ret, "Failed to disconnect."); } _peer = null; }
/// <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; } }
/// <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.</exception> /// <since_tizen> 9 </since_tizen> public Task <PayloadAsyncResult> SendPayloadAsync(Payload payload, PeerInfo peerInfo) { if (payload?.Id.Length == 0 || 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); }
/// <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);
/// <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);
/// <summary> /// The callback invoked when payload received. /// </summary> /// <since_tizen> 9 </since_tizen> protected abstract void OnPayloadReceived(Payload payload, PeerInfo peer);
/// <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."); } }
/// <summary> /// The callback invoked when disconnected with cion client. /// </summary> /// <param name="peerInfo">The peer info of the cion server.</param> /// <since_tizen> 9 </since_tizen> protected abstract void OnDisconnected(PeerInfo peerInfo);
/// <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);
/// <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); }
/// <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); }
private PayloadAsyncResult(PayloadAsyncResultCode result, PeerInfo peer, string payloadId) { Result = result; PeerInfo = peer; PayloadId = payloadId; }
/// <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 OnConnentionRequest(PeerInfo peerInfo);
/// <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);
/// <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);