TcpReceiver.cs :  » Network-Clients » GROF » grof » protocols » membership » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » GROF 
GROF » grof » protocols » membership » TcpReceiver.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace grof.protocols.membership{
  /// <summary>
  /// The <code>TcpReceiver</code> class is responsible
  /// for listening to incoming TCP connections and 
  /// reading messages from them. The received messages
  /// are delegated to the passed <code>IMessageListener</code>
  /// object.
  /// </summary>
  public class TcpReceiver
  {    
    
    /// <summary>
    /// The port the TCP receiver listens for
    /// incoming messages.
    /// </summary>
    private int listenerPort;
    
    /// <summary>
    /// The IP address the TCP receiver is bound to.
    /// </summary>
    private IPAddress address;
    
    /// <summary>
    /// The delegate the received message is 
    /// forwarded.
    /// </summary>
    private MessageReceivedDelegate msgListener;
    
    /// <summary>
    /// Listens for incoming messages.
    /// </summary>
    private TcpListener server;
    
    /// <summary>
    /// Indicates whether the TCP receiver
    /// was stopped.
    /// </summary>
    private bool stopped = false;
    
    /// <summary>
    /// The TCP client.
    /// </summary>
    private TcpClient client;
    
    /// <summary>
    /// Listening for incoming messages in 
    /// a specific thread. 
    /// </summary>
    private Thread receiverThread;
    
    /// <summary>
    /// The name of the group member.
    /// </summary>
    private string memberName;
    
    /// <summary>
    /// Creates instances of class <code>TcpReceiver</code>.
    /// </summary>
    /// <param name="address">The IP address the receiver is bound.</param>
    /// <param name="port">The port the receiver listens.</param>
    /// <param name="listener">The <code>IMessageListener</code> object where
    ///     the received messages are delegated.</param>
    /// <returns></returns>
    public TcpReceiver( string address, int port, string memberName,
                       MessageReceivedDelegate listener )
    {
      this.listenerPort = port;
      this.address = IPAddress.Parse( address );
      this.msgListener = listener;
      this.memberName = memberName;
      this.receiverThread = new Thread( new ThreadStart( this.Receive ) );
      Debug.WriteLine( "[TcpReceiver#constructor] " + this.memberName + ": TCP receiver created" );            
    }
    
    /// <summary>
    /// Starts the <code>TcpReceiver</code>
    /// thread in its own thread and waits
    /// for incoming connections.
    /// </summary>
    public void Start()
    {      
      this.receiverThread.Start();
      Debug.WriteLine( "[TcpReceiver#Start] " + this.memberName + ": TcpReceiver thread was started." );
    }
    
    /// <summary>
    /// Starts receiving messages from 
    /// incoming network connections.
    /// </summary>
    void Receive()
    {    
      Debug.WriteLine( "[TcpReceiver] " + this.memberName + ": Receive method entered." );      
      try
      {                
        server = new TcpListener( this.address, this.listenerPort);
        
        // Start listening for client requests.
        server.Start();           
        while( !this.isStopped() )
        {

          // Enter the listening loop.
          Debug.WriteLine("[TcpReceiver#Receive] " + this.memberName + ": Waiting for a connection... ");
          
          // Perform a blocking call to accept requests.
          // You could also user server.AcceptSocket() here.
          client = server.AcceptTcpClient();    
          
          try 
          {
            Debug.WriteLine("[TcpReceiver#Receive] " + this.memberName + ": Connected!");    
            
            // Get a stream object for reading and writing
            NetworkStream stream = client.GetStream();
            
            
            // Loop to receive all the data sent by the client.
            byte[] buffer = new byte[4096];
            MemoryStream ms = new MemoryStream();      
              while ( true )
              {
                Debug.WriteLine( "[TcpReceiver#Receive] " + this.memberName + ": Reading message..." );
                  int read = stream.Read (buffer, 0, buffer.Length);
                  Debug.WriteLine( "[TcpReceiver#Receive] " + buffer.Length + " bytes read." );
                  if (read <= 0)
                      break;
                  ms.Write (buffer, 0, read);
              }
            Message msg = Message.FromBytes( ms.ToArray() );
            this.msgListener( msg );
            Debug.WriteLine( "[TcpReceiver#Receive] " + this.memberName + ": Received message: " + msg );                                   
          } 
          catch( SocketException se )
          {
            // this exception is thrown when something went wrong with
            // reading the incoming message from stream.
            Trace.WriteLine( "[TcpReceiver#Receive] " + this.memberName + ": Network error during reading message:" +
                            se.ToString() );
          } catch( Exception e )
          {
            Debug.WriteLine( "[TcpReceiver#Receive] " + this.memberName + ": Error occurred during reading message: " + 
                            e.ToString() );
          }
          finally {
            // Shutdown and end connection
            client.Close();
          }
          }
      }
      catch( SocketException e )
      {
        // This exception is thrown when something went wrong during
        // starting the TcpListener
        Trace.WriteLine("[TcpReceiver#Receive] " + this.memberName + ": SocketException, Message: " + 
                        e.Message + ", Error code: " + e.ErrorCode );
      }
      catch( ThreadAbortException ta ) 
      {
        // This exception is thrown when TcpReceiver thread was stopped
        // by Stop() method.
        Trace.WriteLine( "[TcpReceiver#Receive] " + this.memberName + ": TcpReceiver thread was stopped." );
      }
      Trace.WriteLine( "[TcpReceiver#Receive] " + this.memberName + ": Receive method left." );
              
    }
    
    /// <summary>
    /// Helper method which indicates whether
    /// receiver was stopped.
    /// </summary>
    /// <returns></returns>
    private bool isStopped()
    {
      lock( this )
      {
        return this.stopped;
      }
    }
    
    /// <summary>
    /// Stops the <code>TcpReceiver</code>
    /// thread and cleans resources.
    /// </summary>
    public void Stop()
    {
      lock( this )
      {
        this.stopped = true;
      }
      
      if ( this.server != null )
      {
        this.server.Stop();
      }
      if ( this.client != null )
      {
        this.client.Close();
      }
      this.receiverThread.Abort();
      Debug.WriteLine( "[TcpReceiver#Stop] " + this.memberName + ": called." );
    }

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.