public async System.Threading.Tasks.Task ConnectToServerAsync() { try { await Client.ConnectAsync(new Uri(Uri), Cts.Token); OnConnected?.Invoke(this, EventArgs.Empty); await System.Threading.Tasks.Task.Factory.StartNew(async() => { while (true) { try { await ReadMessageAsync(); } catch (Exception ex) { OnReceiveError?.Invoke(this, ex); RecconectWithPeroidASync(); return; } } }, Cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); } catch (Exception ex) { OnReceiveError?.Invoke(this, ex); RecconectWithPeroidASync(); return; } }
private void RecThreadTask() { try { while (!m_Terminate) { if (m_Socket.Connected) { int byteCount = 0; try { byteCount = m_Socket.Receive(m_Buffer); } catch (Exception e) { Trace(EventType.Error, "Receiving socket data, connection will be closed: {0}", ConnID); Trace(e); OnReceiveError?.Invoke(this, e); } if (byteCount > 0) { byte[] data = new byte[byteCount]; Array.Copy(m_Buffer, data, byteCount); Trace(EventType.Full, "Received socket data. ConnID: {0} Length: {1} byte(s)", ConnID, byteCount); OnReceiveData?.Invoke(this, data); } else { Trace(EventType.Full, "Closing socket connection (from the other side!). ConnID: {0}", ConnID); Close(); } } else { Trace(EventType.Full, "Closing socket connection (!Connected). ConnID: {0}", ConnID); Close(); } } Trace(EventType.Full, "Finished receiving thread. ConnID: {0}", ConnID); } catch (Exception exc) { Trace(exc); } }
/// <summary> /// Конфигурация соединения /// </summary> protected override async Task ConfigureHubConnection() { //Событие потери соединения hubConnection.Closed += HubConnection_Closed; //Конфигурация прокси по определенному адресу proxy = hubConnection.CreateHubProxy("Formula"); //Перехватываем получение ответа и вызываем событие proxy.On <string, FormulaStatus, DateTime>("SendState", (name, status, date) => OnReceive?.Invoke(name, status, date)); proxy.On <string, string, DateTime>("SendError", (name, error, date) => OnReceiveError?.Invoke(name, error, date)); await hubConnection.Start(); Started?.Invoke(); }
public void Release(bool destroy = false) { m_jsonToSend = null; m_httpType = HttpType.Post; m_result = 0; m_callback = null; onReceiveError = null; onReceiveOver = null; m_receiveMsg = null; if (destroy) { m_httpClient = null; } }
public async Task StartReceiving(int bufferSize = 102400) { try { var buffer = new ArraySegment <byte>(new byte[bufferSize]); while (Connected) { var received = await Receive(buffer); OnReceived?.Invoke(received); switch (received.Type) { case ReceivedDataType.TextJson: OnReceivedTextJson?.Invoke(received.TextJson); break; case ReceivedDataType.TextString: OnReceivedTextString?.Invoke(received.TextString); break; case ReceivedDataType.BinaryBson: OnReceivedBinaryBson?.Invoke(received.BinaryBson); break; case ReceivedDataType.BinaryData: OnReceivedBinaryData?.Invoke(received.BinaryData); break; case ReceivedDataType.Close: OnReceivedClose?.Invoke(); break; } } } catch (Exception e) { OnReceiveError?.Invoke(e); } finally { OnDisconnected?.Invoke(); } }
private void SocketConn_ReceiveError(SocketConn scktConn, Exception scktExp) { OnReceiveError?.Invoke(scktExp); }
#pragma warning disable AsyncFixer03 // Avoid fire & forget async void methods private static async void ReceiveAsync(this ITelegramBotClient client, ReceiverOptions options, CancellationToken cancellationToken) { var sw = new Stopwatch(); IsReceiving = true; while (!cancellationToken.IsCancellationRequested) { var timeout = 30; var updates = EmptyUpdates; sw.Reset(); try { //let's see if Telegram is responding slowly.... Program.log.Info("Starting a getUpdates request"); sw.Start(); updates = await client.GetUpdatesAsync( MessageOffset, timeout : timeout, limit : options.Limit, allowedUpdates : options.AllowedUpdates, cancellationToken : cancellationToken ).ConfigureAwait(false); sw.Stop(); Program.log.Info($"Time to receive updates: {sw.ElapsedMilliseconds}ms"); } catch (OperationCanceledException opException) { Program.log.Error("Error getting updates", opException); } catch (ApiRequestException apiException) { Program.log.Error("Error getting updates", apiException); OnReceiveError?.Invoke("receiver", apiException); } catch (Exception generalException) { Program.log.Error("Error getting updates", generalException); OnReceiveGeneralError?.Invoke("receiver", generalException); } try { Program.log.Info($"Received {updates.Length} updates, processing"); MessagesReceived += updates.Length; new Task(() => { foreach (var update in updates) { OnUpdateReceived(new UpdateEventArgs(update)); } }).Start(); if (updates.Length > 0) { MessageOffset = updates[updates.Length - 1].Id + 1; } } catch (Exception e) { Program.log.Error("Error getting updates", e); IsReceiving = false; throw; } } IsReceiving = false; }