/// <summary> /// Rozpoczynamy dzia³anie serwera /// </summary> public void Start() { _started = true; _StartTime = DateTime.Now; try { server = new UdpClient(_port); TaskMessage message = new TaskMessage(); do { byte[] bytes = null; try { bytes = server.Receive(ref ep); } catch (SocketException) { //Spróbuje zrestartowaæ serwer if (_started) { server.Close(); server = null; } } message = TaskMessage.Deserialize(bytes); OnServerUpdate(new ServerUpdateEventArgs("Czekam na wiadomoœæ...")); message = _scheduler.Schedule(message); OnServerUpdate(new ServerUpdateEventArgs("Wysy³am wiadomoœæ...")); bytes = TaskMessage.Serialize(message); server.Send(bytes, bytes.Length, ep); } while (_started); } catch (Exception ex) { if (_started) { MessageBox.Show("B³¹d serwera: " + ex.ToString()); } //else to jest zatrzymanie u¿ytkownika } finally { if (server != null && _started) { server.Close(); } } _started = false; DateTime endTime = DateTime.Now; }
public void Start() { TimeSpan waitTime = new TimeSpan(0); int brakTime = 5; int stepTime = 1; int stopTime = 5; message = new TaskMessage(); started = true; try { do { OnClientUpdate(new ClientUpdateEventArgs("Wysyłanie wiadomości...")); bytes = TaskMessage.Serialize(message); client.Send(bytes, bytes.Length, _host, _port); //Odbierz wiadomość received = false; OnClientUpdate(new ClientUpdateEventArgs("Czekam na odpowiedź...")); IAsyncResult iresult = client.BeginReceive(new AsyncCallback(ReceiveCallback), null); int index = WaitHandle.WaitAny(new WaitHandle[] { iresult.AsyncWaitHandle }, timeout, false); if (index == WaitHandle.WaitTimeout) { OnClientUpdate(new ClientUpdateEventArgs("Serwer nie odpowiada, czekam " + stopTime + "s...")); if (stopTime < 60) { stopTime += stepTime; } waitTime = new TimeSpan(0, 0, stopTime); Thread.Sleep(waitTime); //break; } else { stopTime = 5; //Trzeba chwilę poczekać... OnClientUpdate(new ClientUpdateEventArgs("Kończę odbieranie wiadomości...")); DateTime startTime = DateTime.Now; waitTime = new TimeSpan(0, 0, 1); //1s while (!received && DateTime.Now - startTime < waitTime) { continue; } if (!received) { OnClientUpdate(new ClientUpdateEventArgs("Nie udało się odebrać wiadomości :(")); break; } OnClientUpdate(new ClientUpdateEventArgs("Wiadomość odebrana")); if (message.TaskId < 0) { if (brakTime < 60) { brakTime += stepTime; } OnClientUpdate(new ClientUpdateEventArgs("Chwilowo brak aktywnych zadań, czekam " + brakTime.ToString() + "s...")); waitTime = new TimeSpan(0, 0, brakTime); Thread.Sleep(waitTime); } else if (message.Task != null) { brakTime = 5; _task = message.Task; _id = message.TaskId; OnClientUpdate(new ClientUpdateEventArgs("Wykonuję zadanie...")); ITaskResult result = _task.Execute(); message.Result = result; OnClientUpdate( new ClientUpdateEventArgs("Zakończyłem szukanie") ); } } } while (started); } catch (SocketException) { OnClientUpdate(new ClientUpdateEventArgs("Serwer nie odpowiada")); } catch (Exception ex) { MessageBox.Show("Błąd aplikacji klienta: " + ex.ToString()); } finally { started = false; } }