/// <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."); } }
/// <summary> /// The constructor of GroupBase class. /// </summary> /// <param name="topicName">The topic of group.</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 GroupBase(string topicName, Cion.SecurityInfo security) { Topic = topicName; Cion.SecuritySafeHandle handle = security?._handle; Interop.Cion.ErrorCode ret = Interop.CionGroup.CionGroupCreate(out _handle, topicName, handle?.DangerousGetHandle() ?? IntPtr.Zero); if (ret != Interop.Cion.ErrorCode.None) { throw CionErrorFactory.GetException(ret, "Failed to create group."); } _payloadReceivedCb = new Interop.CionGroup.CionGroupPayloadReceivedCb( (IntPtr group, IntPtr peerInfo, IntPtr payload, 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: throw new ArgumentException("Invalid payload type received."); } OnPayloadReceived(receivedPayload, new PeerInfo(new PeerInfoSafeHandle(peerInfo, false))); }); ret = Interop.CionGroup.CionGroupAddPayloadReceivedCb(_handle, _payloadReceivedCb, IntPtr.Zero); if (ret != Interop.Cion.ErrorCode.None) { _handle.Dispose(); throw CionErrorFactory.GetException(ret, "Failed to add payload received callback."); } _joinedCb = new Interop.CionGroup.CionGroupJoinedCb( (string name, IntPtr peerInfo, IntPtr userData) => { Interop.Cion.ErrorCode clone_ret = Interop.CionPeerInfo.CionPeerInfoClone(peerInfo, out PeerInfoSafeHandle clone); if (clone_ret != Interop.Cion.ErrorCode.None) { return; } OnJoined(new PeerInfo(clone)); }); ret = Interop.CionGroup.CionGroupAddJoinedCb(_handle, _joinedCb, IntPtr.Zero); if (ret != Interop.Cion.ErrorCode.None) { _handle.Dispose(); throw CionErrorFactory.GetException(ret, "Failed to add joined callback."); } _leftCb = new Interop.CionGroup.CionGroupLeftCb( (string name, IntPtr peerInfo, IntPtr userData) => { Interop.Cion.ErrorCode clone_ret = Interop.CionPeerInfo.CionPeerInfoClone(peerInfo, out PeerInfoSafeHandle clone); if (clone_ret != Interop.Cion.ErrorCode.None) { return; } OnLeft(new PeerInfo(clone)); }); ret = Interop.CionGroup.CionGroupAddLeftCb(_handle, _leftCb, IntPtr.Zero); if (ret != Interop.Cion.ErrorCode.None) { _handle.Dispose(); throw CionErrorFactory.GetException(ret, "Failed to add joined callback."); } }
/// <summary> /// The constructor of ServerBase class. /// </summary> /// <param name="serviceName">The name of service.</param> /// <param name="displayName">The display 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 ServerBase(string serviceName, string displayName, Cion.SecurityInfo security) { ServiceName = serviceName; DisplayName = displayName; Cion.SecuritySafeHandle handle = security?._handle; Interop.Cion.ErrorCode ret = Interop.CionServer.CionServerCreate(out _handle, serviceName, displayName, handle?.DangerousGetHandle() ?? IntPtr.Zero); if (ret != Interop.Cion.ErrorCode.None) { throw CionErrorFactory.GetException(ret, "Failed to create server handle."); } _connectionResultCb = new Interop.CionServer.CionServerConnectionResultCb( (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, "Failed to clone peer info."); return; } OnConnectionResult(new PeerInfo(clone), new ConnectionResult(result)); }); ret = Interop.CionServer.CionServerAddConnectionResultCb(_handle, _connectionResultCb, IntPtr.Zero); if (ret != Interop.Cion.ErrorCode.None) { _handle.Dispose(); throw CionErrorFactory.GetException(ret, "Failed to add connection status changed callback."); } _dataReceivedCb = new Interop.CionServer.CionServerDataReceivedCb( (string service, IntPtr peerInfo, byte[] data, int dataSize, out byte[] returnData, out int returnDataSize, IntPtr userData) => { returnData = OnDataReceived(data, new PeerInfo(new PeerInfoSafeHandle(peerInfo, false))); returnDataSize = returnData.Length; }); ret = Interop.CionServer.CionServerSetDataReceivedCb(_handle, _dataReceivedCb, IntPtr.Zero); if (ret != Interop.Cion.ErrorCode.None) { _handle.Dispose(); throw CionErrorFactory.GetException(ret, "Failed to set data received callback."); } _payloadRecievedCb = new Interop.CionServer.CionServerPayloadRecievedCb( (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, new PeerInfo(new PeerInfoSafeHandle(peerInfo, false)), (PayloadTransferStatus)status); }); ret = Interop.CionServer.CionServerAddPayloadReceivedCb(_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.CionServer.CionServerDisconnectedCb( (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.CionServer.CionServerAddDisconnectedCb(_handle, _disconnectedCb, IntPtr.Zero); if (ret != Interop.Cion.ErrorCode.None) { _handle.Dispose(); throw CionErrorFactory.GetException(ret, "Failed to add disconnected callback."); } }