Example #1
0
        /// <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;
        }
Example #2
0
 private void ReceiveCallback(IAsyncResult ar)
 {
     /*
      * Jeżeli zatrzymamy serwer w trakcie kończenia odierania to będzie wyjątek
      */
     try
     {
         bytes    = client.EndReceive(ar, ref ep);
         message  = TaskMessage.Deserialize(bytes);
         received = true;
     }
     catch (Exception)
     {
         received = false;
     }
 }