/// <summary> /// Gets native handle for the Session. /// Throws ObjectDisposedException if the Session has been disposed. /// </summary> /// <returns>The native handle.</returns> /// <exception cref="ObjectDisposedException"></exception> internal IntPtr GetNativeHandle() { var h = handle.DangerousGetHandle(); if (handle.IsClosed || handle.IsInvalid) { throw new ObjectDisposedException("handle"); } return(h); }
/// <summary> /// Create a Session. /// </summary> /// <param name="owner"></param> /// <param name="email"></param> /// <param name="password"></param> /// <param name="attemptCreateProfile"></param> /// <returns></returns> internal static Session Create(NabtoClient owner, string email, string password, bool attemptCreateProfile) { if (email == null) { throw new ArgumentNullException("email"); } if (password == null) { throw new ArgumentNullException("password"); } IntPtr nativeHandle; try { PlatformAdapter.Instance.nabtoOpenSession(out nativeHandle, email, password); var safeHandle = new SafeSessionHandle(nativeHandle); var buffer = new byte[1024]; int tokenLength; PlatformAdapter.Instance.nabtoGetSessionToken(safeHandle.DangerousGetHandle(), buffer, out tokenLength); var token = tokenEncoding.GetString(buffer, 0, tokenLength); var session = new Session(safeHandle, owner, token, email, password); owner.Register(session); return(session); } catch (MissingProfileException) { if (attemptCreateProfile) // attempt to create the profile { owner.CreateProfile(email, password); return(Create(owner, email, password, false)); } else // no go -> fail { throw; } } }