Beispiel #1
0
        /// <summary>Stops the connection and message listeners.</summary>
        /// <exception cref="ApplicationException">Thrown for any unexpected exception.</exception>
        public void StopServer()
        {
            try {
                try {
                    this.mReading = false;
                    Thread.Sleep(2000);
                    if (this.mMessageThread.IsAlive)
                    {
                        this.mMessageThread.Abort();
                        this.mMessageThread.Join(1000);
                    }
                    this.mMessageThread = null;
                }
                catch (ThreadStateException) { this.mMessageThread.Resume(); }
                catch (Exception) { }

                try {
                    this.mListening = false;
                    Thread.Sleep(2000);
                    if (this.mListeningThread.IsAlive)
                    {
                        this.mListeningThread.Abort();
                        this.mListeningThread.Join(1000);
                    }
                    this.mListeningThread = null;
                }
                catch (ThreadStateException) { this.mListeningThread.Resume(); }
                catch (Exception) { }

                try {
                    //Clear out all client connections
                    foreach (DictionaryEntry entry in this.mClients)
                    {
                        TcpClientProxy client = (TcpClientProxy)entry.Value;
                        client.Close();
                    }
                }
                catch (Exception) { }
                this.mClients.Clear();

                //Stop the base listener
                base.Stop();
            }
            catch (ApplicationException ex) { throw ex; }
            catch (Exception ex) { throw new ApplicationException("Unexpected error while stopping (closing) the listener.", ex); }
            finally { if (this.ListeningStateChanged != null)
                      {
                          this.ListeningStateChanged(this, EventArgs.Empty);
                      }
            }
        }
Beispiel #2
0
 private void OnConnected(object sender, ClientEventArgs e)
 {
     //Raise an event that a client is connected
     try {
         //Add the new client to the collection of attached clients
         TcpClientProxy client = e.Client;
         lock (this.mClients) {
             this.mClients.Add(client.SessionID, client);
         }
     }
     catch (Exception ex) { if (this.Error != null)
                            {
                                this.Error(this, new ErrorEventArgs(ex));
                            }
     }
     finally { if (this.ClientConnected != null)
               {
                   this.ClientConnected(this, e);
               }
     }
 }
Beispiel #3
0
 private void listenForMessages()
 {
     //Listen for client messages
     while (this.mReading)
     {
         try { if (this.mMessageThread.IsAlive)
               {
                   Thread.Sleep(TcpClientProxy.ReadTimeout);
               }
         } catch {}
         try {
             //Request a read for each connected clients
             lock (this.mClients) {
                 foreach (DictionaryEntry entry in this.mClients)
                 {
                     //Validate client is still active (no session timeout)
                     TcpClientProxy client = (TcpClientProxy)entry.Value;
                     if (client.SessionTimedOut)
                     {
                         //Client timed-out; dispose the object
                         this.mTimeoutDelegate.BeginInvoke(null, new ClientEventArgs(client), null, null);
                     }
                     else
                     {
                         //Check for a message from this client
                         byte[] message = client.Read();
                         if (message != null)
                         {
                             this.mReceivedDelegate.BeginInvoke(null, new ClientMessageEventArgs(client, message), null, null);
                         }
                     }
                 }
             }
             this.mMessageHeartbeatDelegate.BeginInvoke(this, EventArgs.Empty, null, null);
         }
         catch (Exception ex) { this.mReceiveErrorDelegate.BeginInvoke(this, new ErrorEventArgs(new ApplicationException("ArgixTcpListener::listenForMessages() exception.", ex)), null, null); }
     }
 }
Beispiel #4
0
 private void listenForConnections()
 {
     //Listen for client connections
     while (this.mListening)
     {
         try { if (this.mListeningThread.IsAlive)
               {
                   Thread.Sleep(3000);
               }
         } catch {}
         try {
             //Check for pending connection requests
             if (base.Pending())
             {
                 //Create a client to handle server requests
                 this.mPendingDelegate.BeginInvoke(null, EventArgs.Empty, null, null);
                 TcpClientProxy client = new TcpClientProxy(base.AcceptTcpClient());
                 this.mConnectedDelegate.BeginInvoke(null, new ClientEventArgs(client), null, null);
             }
             this.mListenerHeartbeatDelegate.BeginInvoke(null, EventArgs.Empty, null, null);
         }
         catch (Exception ex) { this.mConnectErrorDelegate.BeginInvoke(null, new ErrorEventArgs(ex), null, null); }
     }
 }
Beispiel #5
0
 /// <summary></summary>
 /// <param name="client"></param>
 /// <param name="message"></param>
 public ClientMessageEventArgs(TcpClientProxy client, byte[] message)
 {
     this._client = client; this._message = message;
 }
Beispiel #6
0
 /// <summary></summary>
 /// <param name="client"></param>
 public ClientEventArgs(TcpClientProxy client)
 {
     this._client = client;
 }