private async Task ConnectRequested(ConnectRequested @event) { // Connect logic here await Task.Delay(@event.Delay); // Connection succeeded this.EventManager.RaiseEvent(new Connected{Address = @event.Address}); }
private async Task RunClient() { var connectRequestedEvent = new ConnectRequested {Address = "http://localhost"}; // We can raise event before starting waiting for result, because it will be processed in same synchronization context (after await call) this.EventManager.RaiseEvent(connectRequestedEvent); // Thread: Main // Waits for connected or cancel event. Address of connect event must be http://localhost. // This call blocks execution, so it'll continue in client thread var @event = await this.EventManager.WaitFor<Connected, CancelRequested>(TimeSpan.FromMilliseconds(50), e => !(e is Connected) || ((Connected) e).Address == "http://localhost"); // Thread: Client // Expected result: Connected CheckStatus(@event); connectRequestedEvent.Delay = TimeSpan.FromMilliseconds(100); this.EventManager.RaiseEvent(connectRequestedEvent); @event = await this.EventManager.WaitFor<Connected, CancelRequested>(TimeSpan.FromMilliseconds(50)); // Expected result: Timeout, reason: connect delay > 50 ms CheckStatus(@event); this.EventManager.RaiseEvent(connectRequestedEvent); var eventWaitTask = this.EventManager.WaitFor<Connected, CancelRequested>(TimeSpan.FromMilliseconds(50)); this.EventManager.RaiseEvent(new CancelRequested()); @event = await eventWaitTask; // Expected result: cancelled, reason: cancel event raised before connected event CheckStatus(@event); }
private void _conn_MessageReceived(Connection source, string data) { RequestModel model = JsonConvert.DeserializeObject <RequestModel>(data); switch (model.Action) { case "TurnData": Turn = (TurnState)JsonConvert.DeserializeObject <TurnRequest>(data).Turn; break; case "Connect": ConnectRequested?.Invoke(this, null); break; case "Settings": try { Send(new SettingsResponseModel() { Settings = Config.Constants }); } catch (ConnectionBusyException) { _modelsQueue.Enqueue(new SettingsResponseModel() { Settings = Config.Constants }); } break; } }
public static bool InvokeConnectRequested(string comPort, int baudRate) { if (ConnectRequested != null) { return(ConnectRequested.Invoke(comPort, baudRate)); } else { return(false); } }
void OnConnectRequest(P2PSessionRequest_t ev) { ConnectRequested?.Invoke(ev); }