Exemplo n.º 1
0
        /// <summary>
        /// Each time passes <see cref="INTERVAL_BETWEEN_PETITIONS_MS"/> check if there's any petition in progress and send new petition
        /// </summary>
        /// <param name="sender"> - </param>
        /// <param name="e"> - </param>
        private static void TimerIntervalElapsed(object sender, ElapsedEventArgs e)
        {
            if (!PetitionInProgress)
            {
                PetitionInProgress = true;
                if (ServerQueue.Count > 0)
                {
                    ServerObject serverObject = ServerQueue.Dequeue();

                    Debugging.StatusBar.SetStatus(((ApiObject)serverObject.Data).apiFlag.GetDescription());

                    if (ServerQueue.Where(q => ((ApiObject)q.Data).apiFlag == ((ApiObject)serverObject.Data).apiFlag).Count() == 0)
                    {
                        Client.SendToServer(serverObject);
                        if (LoadingNotificator.LoadingPetitions == 0)
                        {
                            LoadingNotificator.LoadingInitialized();
                        }
                    }
                    else
                    {
                        PetitionInProgress = false;
                    }
                }
                if (ServerQueue.Count == 0)
                {
                    timer.Stop();
                }

                PetitionInProgress = false;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends ServerObject to the server
        /// </summary>
        /// <param name="data">Data to send</param>
        public static async void SendToServer(ServerObject data)
        {
            try
            {
                // If there is no server connection, stablish
                if (senderServer == null || senderServer.Connected == false)
                {
                    LoadingNotificator.LoadingInitialized();
                    try
                    {
                        senderServer = new TcpClient(serverAddress, SERVER_PORT);
                    }
                    catch (SocketException e)
                    {
                        if (reconnectingAttempts >= MAX_RECONNECTING_ATTEMPTS)
                        {
                            StatusBar.SetStatus($"{e.SocketErrorCode.ToString()}\nCannot stablish connection");
                            return;
                        }
                        StatusBar.SetStatus($"{e.SocketErrorCode.ToString()}\nTrying to reconnect in 10 seconds ({++reconnectingAttempts}/{MAX_RECONNECTING_ATTEMPTS} attempts)", 10000);
                        await Task.Delay(10000);

                        SendToServer(data);
                        LoadingNotificator.LoadingFinished();
                        return;
                    }
                    _ = Task.Run(() => ListenServer(senderServer));

                    LoadingNotificator.LoadingFinished();
                }

                lock (senderServer) // To not send too much data at same time
                {
                    if (data.ServerFlag == ServerFlag.Error)
                    {
                        MessageBox.Show("Error sendind data to server");
                    }

                    if (senderServer.Connected)
                    {
                        formatter.Serialize(senderServer.GetStream(), data);
                        senderServer.GetStream().Flush();
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
Exemplo n.º 3
0
 private static void DataRetrievedHandler()
 {
     PetitionInProgress = false;
     LoadingNotificator.LoadingFinished();
 }