/// <summary> /// On client-side, we only fire readCompletionDelegate once all messages have been read /// and status has been received. /// </summary> protected override void ProcessLastRead(AsyncCompletionDelegate <TResponse> completionDelegate) { if (completionDelegate != null && readingDone && finishedStatus.HasValue) { bool shouldComplete; lock (myLock) { shouldComplete = !readObserverCompleted; readObserverCompleted = true; } if (shouldComplete) { var status = finishedStatus.Value.Status; if (status.StatusCode != StatusCode.OK) { FireCompletion(completionDelegate, default(TResponse), new RpcException(status)); } else { FireCompletion(completionDelegate, default(TResponse), null); } } } }
/// <summary> /// Handles streaming read completion. /// </summary> protected void HandleReadFinished(bool success, byte[] receivedMessage) { AsyncCompletionDelegate <TRead> origCompletionDelegate = null; lock (myLock) { origCompletionDelegate = readCompletionDelegate; readCompletionDelegate = null; if (receivedMessage == null) { // This was the last read. readingDone = true; } ReleaseResourcesIfPossible(); } // TODO: handle the case when error occured... if (receivedMessage != null) { // TODO: handle deserialization error TRead msg; TryDeserialize(receivedMessage, out msg); FireCompletion(origCompletionDelegate, msg, null); } else { FireCompletion(origCompletionDelegate, default(TRead), null); } }
public void GetEventsAsync(int year, int month, AsyncCompletionDelegate resultDelegate) { List<StableSession.YearMonth> months = new List<StableSession.YearMonth> { new StableSession.YearMonth() { Year = year, Month = month } }; GetEventsAsync(months, resultDelegate); }
protected void FireCompletion <T>(AsyncCompletionDelegate <T> completionDelegate, T value, Exception error) { try { completionDelegate(value, error); } catch (Exception e) { Console.WriteLine("Exception occured while invoking completion delegate: " + e); } }
/// <summary> /// Initiates reading a message. Only one read operation can be active at a time. /// completionDelegate is invoked upon completion. /// </summary> protected void StartReadMessageInternal(AsyncCompletionDelegate <TRead> completionDelegate) { lock (myLock) { Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckReadingAllowed(); call.StartReceiveMessage(HandleReadFinished); readCompletionDelegate = completionDelegate; } }
protected void FireCompletion <T>(AsyncCompletionDelegate <T> completionDelegate, T value, Exception error) { try { completionDelegate(value, error); } catch (Exception e) { Logger.Error(e, "Exception occured while invoking completion delegate."); } }
/// <summary> /// Initiates reading a message. Only one read operation can be active at a time. /// completionDelegate is invoked upon completion. /// </summary> protected void StartReadMessageInternal(AsyncCompletionDelegate <TRead> completionDelegate) { lock (myLock) { Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckReadingAllowed(); // I feel like a prerequisite variable should be set before entering that code path readCompletionDelegate = completionDelegate; call.StartReceiveMessage(HandleReadFinished); } }
/// <summary> /// Sends call result status, also indicating server is done with streaming responses. /// Only one pending send action is allowed at any given time. /// completionDelegate is called when the operation finishes. /// </summary> public void StartSendStatusFromServer(Status status, AsyncCompletionDelegate <object> completionDelegate) { lock (myLock) { Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(); call.StartSendStatusFromServer(status, halfclosedHandler); halfcloseRequested = true; sendCompletionDelegate = completionDelegate; } }
/// <summary> /// Sends halfclose, indicating client is done with streaming requests. /// Only one pending send action is allowed at any given time. /// completionDelegate is called when the operation finishes. /// </summary> public void StartSendCloseFromClient(AsyncCompletionDelegate <object> completionDelegate) { lock (myLock) { Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(); call.StartSendCloseFromClient(HandleHalfclosed); halfcloseRequested = true; sendCompletionDelegate = completionDelegate; } }
public void GetPendingEventsAsync(AsyncCompletionDelegate resultDelegate) { StableSession.Instance.GetPendingEventsAsync((events, reason) => { PendingEvents = events; if (resultDelegate != null) resultDelegate(events != null, reason); if (events != null) OnPendingEventsChanged(); }); }
/// <summary> /// Initiates sending a message. Only one send operation can be active at a time. /// completionDelegate is invoked upon completion. /// </summary> protected void StartSendMessageInternal(TWrite msg, AsyncCompletionDelegate <object> completionDelegate) { byte[] payload = UnsafeSerialize(msg); lock (myLock) { Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(); call.StartSendMessage(payload, HandleSendFinished); sendCompletionDelegate = completionDelegate; } }
/// <summary> /// Sends call result status, also indicating server is done with streaming responses. /// Only one pending send action is allowed at any given time. /// completionDelegate is called when the operation finishes. /// </summary> public void StartSendStatusFromServer(Status status, Metadata trailers, AsyncCompletionDelegate <object> completionDelegate) { lock (myLock) { Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(); using (var metadataArray = MetadataArraySafeHandle.Create(trailers)) { call.StartSendStatusFromServer(HandleHalfclosed, status, metadataArray, !initialMetadataSent); } halfcloseRequested = true; readingDone = true; sendCompletionDelegate = completionDelegate; } }
/// <summary> /// Initiates sending a message. Only one send operation can be active at a time. /// completionDelegate is invoked upon completion. /// </summary> protected void StartSendMessageInternal(TWrite msg, WriteFlags writeFlags, AsyncCompletionDelegate <object> completionDelegate) { byte[] payload = UnsafeSerialize(msg); lock (myLock) { GrpcPreconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(allowFinished: false); call.StartSendMessage(HandleSendFinished, payload, writeFlags, !initialMetadataSent); sendCompletionDelegate = completionDelegate; initialMetadataSent = true; streamingWritesCounter++; } }
public void GetMailAsync(AsyncCompletionDelegate resultDelegate) { LoadingMail = true; StableSession.Instance.GetMailAsync((messages, reason) => { Messages = messages; LoadingMail = false; if (resultDelegate != null) resultDelegate(Messages != null, reason); OnMessagesChanged(); OnUnreadCountChanged(); }); }
/// <summary> /// Handles receive status completion for calls with streaming response. /// </summary> private void HandleFinished(bool success, BatchContextSafeHandle ctx) { var fullStatus = ctx.GetReceivedStatusOnClient(); AsyncCompletionDelegate <TResponse> origReadCompletionDelegate = null; lock (myLock) { finished = true; finishedStatus = fullStatus; origReadCompletionDelegate = readCompletionDelegate; ReleaseResourcesIfPossible(); } ProcessLastRead(origReadCompletionDelegate); }
/// <summary> /// Handles receive status completion for calls with streaming response. /// </summary> private void HandleFinished(bool wasError, BatchContextSafeHandleNotOwned ctx) { var status = ctx.GetReceivedStatus(); AsyncCompletionDelegate <TResponse> origReadCompletionDelegate = null; lock (myLock) { finished = true; finishedStatus = status; origReadCompletionDelegate = readCompletionDelegate; ReleaseResourcesIfPossible(); } ProcessLastRead(origReadCompletionDelegate); }
/// <summary> /// Handles send completion. /// </summary> protected void HandleSendFinished(bool success, BatchContextSafeHandle ctx) { AsyncCompletionDelegate <object> origCompletionDelegate = null; lock (myLock) { origCompletionDelegate = sendCompletionDelegate; sendCompletionDelegate = null; ReleaseResourcesIfPossible(); } if (!success) { FireCompletion(origCompletionDelegate, null, new InvalidOperationException("Send failed")); } else { FireCompletion(origCompletionDelegate, null, null); } }
/// <summary> /// Handles send status from server completion. /// </summary> protected void HandleSendStatusFromServerFinished(bool success) { AsyncCompletionDelegate <object> origCompletionDelegate = null; lock (myLock) { origCompletionDelegate = sendCompletionDelegate; sendCompletionDelegate = null; ReleaseResourcesIfPossible(); } if (!success) { FireCompletion(origCompletionDelegate, null, new InvalidOperationException("Error sending status from server.")); } else { FireCompletion(origCompletionDelegate, null, null); } }
/// <summary> /// Handles send completion. /// </summary> private void HandleSendFinished(bool wasError, BatchContextSafeHandleNotOwned ctx) { AsyncCompletionDelegate origCompletionDelegate = null; lock (myLock) { origCompletionDelegate = sendCompletionDelegate; sendCompletionDelegate = null; ReleaseResourcesIfPossible(); } if (wasError) { FireCompletion(origCompletionDelegate, new OperationFailedException("Send failed")); } else { FireCompletion(origCompletionDelegate, null); } }
/// <summary> /// Handles halfclose completion. /// </summary> protected void HandleHalfclosed(bool success) { AsyncCompletionDelegate <object> origCompletionDelegate = null; lock (myLock) { origCompletionDelegate = sendCompletionDelegate; sendCompletionDelegate = null; ReleaseResourcesIfPossible(); } if (!success) { FireCompletion(origCompletionDelegate, null, new InvalidOperationException("Halfclose failed")); } else { FireCompletion(origCompletionDelegate, null, null); } }
/// <summary> /// Handles halfclose (send close from client) completion. /// </summary> protected void HandleSendCloseFromClientFinished(bool success) { AsyncCompletionDelegate <object> origCompletionDelegate = null; lock (myLock) { origCompletionDelegate = sendCompletionDelegate; sendCompletionDelegate = null; ReleaseResourcesIfPossible(); } if (!success) { FireCompletion(origCompletionDelegate, null, new InvalidOperationException("Sending close from client has failed.")); } else { FireCompletion(origCompletionDelegate, null, null); } }
/// <summary> /// Handles streaming read completion. /// </summary> protected void HandleReadFinished(bool success, BatchContextSafeHandle ctx) { var payload = ctx.GetReceivedMessage(); AsyncCompletionDelegate <TRead> origCompletionDelegate = null; lock (myLock) { origCompletionDelegate = readCompletionDelegate; if (payload != null) { readCompletionDelegate = null; } else { // This was the last read. Keeping the readCompletionDelegate // to be either fired by this handler or by client-side finished // handler. readingDone = true; } ReleaseResourcesIfPossible(); } // TODO: handle the case when error occured... if (payload != null) { // TODO: handle deserialization error TRead msg; TryDeserialize(payload, out msg); FireCompletion(origCompletionDelegate, msg, null); } else { ProcessLastRead(origCompletionDelegate); } }
/// <summary> /// Initiates sending a initial metadata. /// Even though C-core allows sending metadata in parallel to sending messages, we will treat sending metadata as a send message operation /// to make things simpler. /// completionDelegate is invoked upon completion. /// </summary> public void StartSendInitialMetadata(Metadata headers, AsyncCompletionDelegate <object> completionDelegate) { lock (myLock) { Preconditions.CheckNotNull(headers, "metadata"); Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); Preconditions.CheckState(!initialMetadataSent, "Response headers can only be sent once per call."); Preconditions.CheckState(streamingWritesCounter == 0, "Response headers can only be sent before the first write starts."); CheckSendingAllowed(); Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); using (var metadataArray = MetadataArraySafeHandle.Create(headers)) { call.StartSendInitialMetadata(HandleSendFinished, metadataArray); } this.initialMetadataSent = true; sendCompletionDelegate = completionDelegate; } }
/// <summary> /// Handles halfclose completion. /// </summary> protected void HandleHalfclosed(bool success, BatchContextSafeHandle ctx) { AsyncCompletionDelegate <object> origCompletionDelegate = null; lock (myLock) { halfclosed = true; origCompletionDelegate = sendCompletionDelegate; sendCompletionDelegate = null; ReleaseResourcesIfPossible(); } if (!success) { FireCompletion(origCompletionDelegate, null, new OperationFailedException("Halfclose failed")); } else { FireCompletion(origCompletionDelegate, null, null); } }
public IntPtr GetAsyncCompletionDelegate() { if (IntPtr.Zero == _asyncCompletionDelegatePointer) { lock (_delegatelock) { if (IntPtr.Zero == _asyncCompletionDelegatePointer) { AsyncCompletionDelegate d = new AsyncCompletionDelegate(AsyncCompletionHandler); if (null != d) { IntPtr p = Marshal.GetFunctionPointerForDelegate(d); if (IntPtr.Zero != p) { _asyncCompletionDelegate = d; _asyncCompletionDelegatePointer = p; } } } } } return(_asyncCompletionDelegatePointer); }
/// <summary> /// Handles halfclose completion. /// </summary> private void HandleHalfclosed(bool wasError, BatchContextSafeHandleNotOwned ctx) { AsyncCompletionDelegate <object> origCompletionDelegate = null; lock (myLock) { halfclosed = true; origCompletionDelegate = sendCompletionDelegate; sendCompletionDelegate = null; ReleaseResourcesIfPossible(); } if (wasError) { FireCompletion(origCompletionDelegate, null, new OperationFailedException("Halfclose failed")); } else { FireCompletion(origCompletionDelegate, null, null); } }
/// <summary> /// Sends halfclose, indicating client is done with streaming requests. /// Only one pending send action is allowed at any given time. /// completionDelegate is called when the operation finishes. /// </summary> public void StartSendCloseFromClient(AsyncCompletionDelegate <object> completionDelegate) { lock (myLock) { GrpcPreconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(allowFinished: true); if (!disposed && !finished) { call.StartSendCloseFromClient(HandleSendCloseFromClientFinished); } else { // In case the call has already been finished by the serverside, // the halfclose has already been done implicitly, so we only // emit the notification for the completion delegate. Task.Run(() => HandleSendCloseFromClientFinished(true)); } halfcloseRequested = true; sendCompletionDelegate = completionDelegate; } }
/// <summary> /// Handles streaming read completion. /// </summary> protected void HandleReadFinished(bool success, byte[] receivedMessage) { TRead msg = default(TRead); var deserializeException = (success && receivedMessage != null) ? TryDeserialize(receivedMessage, out msg) : null; AsyncCompletionDelegate <TRead> origCompletionDelegate = null; lock (myLock) { origCompletionDelegate = readCompletionDelegate; readCompletionDelegate = null; if (receivedMessage == null) { // This was the last read. readingDone = true; } if (deserializeException != null && IsClient) { readingDone = true; CancelWithStatus(DeserializeResponseFailureStatus); } ReleaseResourcesIfPossible(); } // TODO: handle the case when success==false if (deserializeException != null && !IsClient) { FireCompletion(origCompletionDelegate, default(TRead), new IOException("Failed to deserialize request message.", deserializeException)); return; } FireCompletion(origCompletionDelegate, msg, null); }
// TODO(jtattermusch): find more fitting name for this method. /// <summary> /// Default behavior just completes the read observer, but more sofisticated behavior might be required /// by subclasses. /// </summary> protected virtual void ProcessLastRead(AsyncCompletionDelegate <TRead> completionDelegate) { FireCompletion(completionDelegate, default(TRead), null); }
public void MarkMailReadAsync(CharacterMailMessage message, AsyncCompletionDelegate resultDelegate) { StableSession.Instance.MarkMailReadAsync(message.MailId, (bool result, string reason) => { if (result) { message.ReadDate = DateTime.UtcNow; } if (resultDelegate != null) resultDelegate(result, reason); if (!result) return; OnMessagesChanged(); OnUnreadCountChanged(); }); }
public void DeleteMailAsync(CharacterMailMessage message, AsyncCompletionDelegate resultDelegate) { StableSession.Instance.DeleteMailAsync(message.MailId, (bool result, string reason) => { if (result) { Messages.Remove(message); } if (resultDelegate != null) resultDelegate(result, reason); if (!result) return; OnMessagesChanged(); OnUnreadCountChanged(); }); }
/// <summary> /// Receives a streaming response. Only one pending read action is allowed at any given time. /// completionDelegate is called when the operation finishes. /// </summary> public void StartReadMessage(AsyncCompletionDelegate <TResponse> completionDelegate) { StartReadMessageInternal(completionDelegate); }
/// <summary> /// Sends a streaming request. Only one pending send action is allowed at any given time. /// completionDelegate is called when the operation finishes. /// </summary> public void StartSendMessage(TRequest msg, WriteFlags writeFlags, AsyncCompletionDelegate <object> completionDelegate) { StartSendMessageInternal(msg, writeFlags, completionDelegate); }
/// <summary> /// Sends a streaming response. Only one pending send action is allowed at any given time. /// completionDelegate is called when the operation finishes. /// </summary> public void StartSendMessage(TResponse msg, AsyncCompletionDelegate <object> completionDelegate) { StartSendMessageInternal(msg, completionDelegate); }
public AsyncCompletionTaskSource() { completionDelegate = new AsyncCompletionDelegate(HandleCompletion); }
public IntPtr GetAsyncCompletionDelegate() { if (IntPtr.Zero == _asyncCompletionDelegatePointer) { lock (_delegatelock) { if (IntPtr.Zero == _asyncCompletionDelegatePointer) { AsyncCompletionDelegate d = new AsyncCompletionDelegate(AsyncCompletionHandler); if (null != d) { IntPtr p = Marshal.GetFunctionPointerForDelegate(d); if (IntPtr.Zero != p) { _asyncCompletionDelegate = d; _asyncCompletionDelegatePointer = p; } } } } } return _asyncCompletionDelegatePointer; }
public void GetEventsAsync(List<StableSession.YearMonth> months, AsyncCompletionDelegate resultDelegate) { List<StableSession.YearMonth> newMonths = months.Where(month => _cachedMonths.Contains(month) != true).ToList(); if (newMonths.Count <= 0) return; StableSession.Instance.GetEventsAsync(newMonths, (events, reason) => { if (events != null) { if (Events != null) { foreach (StableSession.YearMonth month in newMonths) { DateTime startDate = new DateTime(month.Year, month.Month, 1); DateTime endDate = new DateTime(month.Year, month.Month, DateTime.DaysInMonth(month.Year, month.Month)); Events.RemoveAll(@event => @event.IsIn(startDate, endDate)); } } _cachedMonths.UnionWith(months); Events = Events != null ? Events.Union(events, new EventComparer()).ToList() : events; Events.Sort(new EventDateComparer()); } if (resultDelegate != null) resultDelegate(events != null, reason); if (events != null) OnEventsChanged(); }); }
/// <summary> /// Sends a streaming request. Only one pending send action is allowed at any given time. /// completionDelegate is called when the operation finishes. /// </summary> public void StartSendMessage(TRequest msg, AsyncCompletionDelegate completionDelegate) { StartSendMessageInternal(msg, completionDelegate); }
public void DeleteEventAsync(CalendarEvent eventData, AsyncCompletionDelegate resultDelegate) { StableSession.Instance.DeleteEventAsync(eventData.EventId.Value, (result, reason) => { if (result) { PendingEvents.RemoveAll(@event => @event.EventId == eventData.EventId); Events.RemoveAll(@event => @event.EventId == eventData.EventId); } if (resultDelegate != null) { resultDelegate(result, reason); } if (result) { OnEventsChanged(); OnPendingEventsChanged(); } }); }
public void ReplyInvitationAsync(UInt64 eventId, bool accept, AsyncCompletionDelegate resultDelegate) { StableSession.Instance.ReplyInvitationAsync(eventId, accept, (eventData, reason) => { if (eventData != null) { PendingEvents.RemoveAll(@event => @event.EventId == eventId); if (accept) { Events.Add(eventData); Events.Sort(new EventDateComparer()); } } if (resultDelegate != null) { resultDelegate(eventData != null, reason); } if (eventData != null) { OnEventsChanged(); OnPendingEventsChanged(); } }); }