/// <summary> /// Authenticates this connection via a platform-specific license key. This may very well include "" /// if the server chooses to allow anonymous logon. /// </summary> /// <param name="key">The license key to use for authentication</param> /// <returns>True if the login succeeded, otherwise false.</returns> public async Task <bool> AuthenticateWithLicenseKey(string key) { var tcs = new TaskCompletionSource <bool>(); RPC.AttachHandlerForNextMessage(packet => { var result = packet as AuthenticateResultMessage; if (result == null) { return; } if (result.Result != 0) { tcs.SetResult(false); return; } LoginId = result.NPID; SessionToken = result.SessionToken; tcs.SetResult(true); }); RPC.Send(new AuthenticateWithKeyMessage { LicenseKey = key }); return(await tcs.Task); }
/// <summary> /// Downloads a publisher file and returns its contents. /// </summary> /// <param name="filename">The file to download</param> /// <returns>File contents as byte array</returns> public async Task <byte[]> GetPublisherFile(string filename) { var tcs = new TaskCompletionSource <byte[]>(); RPC.AttachHandlerForNextMessage(packet => { var result = (StoragePublisherFileMessage)packet; if (result.Result != 0) { tcs.SetException(new NpFileException(result.Result)); return; } tcs.SetResult(result.FileData); }); RPC.Send(new StorageGetPublisherFileMessage { FileName = filename }); return(await tcs.Task); }
// TODO: Try to use an exception for failed action instead /// <summary> /// Uploads a user file. /// </summary> /// <param name="filename">The file name to save the contents to on the server</param> /// <param name="contents">The raw byte contents</param> /// <returns>True if the upload succeeded, otherwise false.</returns> public async Task <bool> UploadUserFile(string filename, byte[] contents) { var tcs = new TaskCompletionSource <bool>(); RPC.AttachHandlerForNextMessage(packet => { var result = (StorageWriteUserFileResultMessage)packet; if (result.Result != 0) { tcs.SetResult(false); return; } tcs.SetResult(true); }); RPC.Send(new StorageWriteUserFileMessage { FileData = contents, FileName = filename, NPID = LoginId }); return(await tcs.Task); }
/// <summary> /// Authenticates a server ticket. /// </summary> /// <returns>True if the ticket validation succeeded, otherwise false.</returns> public async Task <TicketValidationResult> ValidateTicket(IPAddress clientIP, ulong guid, Ticket ticket) { var tcs = new TaskCompletionSource <TicketValidationResult>(); RPC.AttachHandlerForNextMessage(packet => { var result = packet as AuthenticateValidateTicketResultMessage; if (result == null) { return; } tcs.SetResult(new TicketValidationResult(result)); }); RPC.Send(new AuthenticateValidateTicketMessage { ClientIP = (uint)IPAddress.HostToNetworkOrder((int)BitConverter.ToUInt32(clientIP.GetAddressBytes(), 0)), Ticket = ticket.Serialize(), NPID = guid }); return(await tcs.Task); }