using System;
using System.ComponentModel;
using System.Diagnostics;
+using System.Threading.Tasks;
using static Interop;
namespace Tizen.Multimedia.Remoting
/// <summary>
/// Provides the ability to control WebRTCSignalingServer.
/// </summary>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public class WebRTCSignalingServer : IDisposable
{
/// Initializes a new instance of the <see cref="WebRTCSignalingServer"/> class.
/// </summary>
/// <param name="port">The server port.</param>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public WebRTCSignalingServer(int port)
{
/// <summary>
/// Starts the signaling server.
/// </summary>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public void Start()
{
/// <summary>
/// Stops the signaling server.
/// </summary>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public void Stop()
{
/// Releases all resources used by the current instance.
/// </summary>
/// <exception cref="ObjectDisposedException">The WebRTCSignalingServer has already been disposed.</exception>
- /// <since_tizen> 9 </since_tizen>
public void Dispose()
{
Dispose(true);
/// true to release both managed and unmanaged resources;
/// false to release only unmanaged resources.
/// </param>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual void Dispose(bool disposing)
{
/// <summary>
/// Provides the ability to control WebRTCSignalingClient.
/// </summary>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public class WebRTCSignalingClient : IDisposable
{
- private readonly IntPtr _handle;
+ private IntPtr _handle;
private bool _isConnected;
+ private (string serverIp, int port) _serverInfo;
private SignalingClient.SignalingMessageCallback _signalingMessageCallback;
private bool _disposed;
/// </summary>
/// <param name="serverIp">The server IP.</param>
/// <param name="port">The server port.</param>
- /// <seealso cref="GetID"/>
- /// <since_tizen> 9 </since_tizen>
+ /// <seealso cref="Connect"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public WebRTCSignalingClient(string serverIp, int port)
{
- IntPtr zero = IntPtr.Zero;
-
ValidationUtil.ValidateIsNullOrEmpty(serverIp, nameof(serverIp));
- _signalingMessageCallback = (type, message, _) =>
+ if (port < 0)
{
- Log.Info(WebRTCLog.Tag, $"type:{type}, message:{message}");
-
- if (type == SignalingMessageType.Connected)
- {
- _isConnected = true;
- }
-
- SignalingMessage?.Invoke(this, new WebRTCSignalingEventArgs(type, message));
- };
+ throw new ArgumentException("port should be greater than zero.");
+ }
- SignalingClient.Connect(serverIp, port, _signalingMessageCallback, zero, out _handle).
- ThrowIfFailed("Failed to connect to server");
+ _serverInfo.serverIp = serverIp;
+ _serverInfo.port = port;
}
/// <summary>
/// Occurs when a message to be handled is sent from the remote peer or the signaling server.
/// </summary>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public event EventHandler<WebRTCSignalingEventArgs> SignalingMessage;
/// <summary>
- /// Gets the state whether signaling client is connected to remote peer or not.
+ /// Connect to signaling server and return client id.
/// </summary>
- /// <since_tizen> 9 </since_tizen>
- [EditorBrowsable(EditorBrowsableState.Never)]
- public bool IsConnected => _isConnected;
-
- /// <summary>
- /// Gets the signaling client ID.
- /// </summary>
- /// <remarks>
- /// This method must be called after <see cref="SignalingMessage"/> event is occurred with <see cref="SignalingMessageType.Connected"/>.
- /// </remarks>
/// <returns>The signaling client ID.</returns>
/// <exception cref="ObjectDisposedException">The WebRTCSignalingClient has already been disposed.</exception>
- /// <exception cref="InvalidOperationException">The signaling client is not connected yet.</exception>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
- public int GetID()
+ public async Task<int> Connect()
{
ValidateNotDisposed();
- if (!IsConnected)
+ var tcsConnected = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
+
+ _signalingMessageCallback = (type, message, _) =>
{
- throw new InvalidOperationException("Client is not connected to server yet.");
- }
+ Log.Info(WebRTCLog.Tag, $"type:{type}, message:{message}");
+
+ if (!_isConnected && type == SignalingMessageType.Connected)
+ {
+ _isConnected = true;
- SignalingClient.GetID(_handle, out int id).
- ThrowIfFailed("Failed to get signaling client ID");
+ SignalingClient.GetID(_handle, out int id).ThrowIfFailed("Failed to get signaling client ID");
+ Log.Info(WebRTCLog.Tag, $"Client ID[{id}]");
+
+ tcsConnected.TrySetResult(id);
+ }
+
+ SignalingMessage?.Invoke(this, new WebRTCSignalingEventArgs(type, message));
+ };
+
+ SignalingClient.Connect(_serverInfo.serverIp, _serverInfo.port, _signalingMessageCallback, IntPtr.Zero, out _handle).
+ ThrowIfFailed("Failed to connect to server");
- return id;
+ return await tcsConnected.Task;
}
/// <summary>
/// <param name="peerId">The ID of remote peer.</param>
/// <exception cref="ObjectDisposedException">The WebRTCSignalingClient has already been disposed.</exception>
/// <see cref="SignalingMessageType.SessionEstablished"/>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public void RequestSession(int peerId)
{
/// </summary>
/// <param name="message"></param>
/// <exception cref="ObjectDisposedException">The WebRTCSignalingClient has already been disposed.</exception>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendMessage(string message)
{
/// Releases all resources used by the current instance.
/// </summary>
/// <exception cref="ObjectDisposedException">The WebRTCSignalingClient has already been disposed.</exception>
- /// <since_tizen> 9 </since_tizen>
public void Dispose()
{
Dispose(true);
/// true to release both managed and unmanaged resources;
/// false to release only unmanaged resources.
/// </param>
- /// <since_tizen> 9 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual void Dispose(bool disposing)
{