- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/Networking/InfoLinkReceiver.cs

    00001 ﻿//-----------------------------------------------------------------------------
    00002 // InfoLinkReceiver.cs
    00003 // (A. Schiffler, 2009)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace NewGamePhysics.Networking
    00007 {
    00008     using System;
    00009     using System.Net;
    00010     using System.Net.Sockets;
    00011     using System.Text;
    00012     using System.Threading;
    00013
    00017     public class InfoLinkReceiver : InfoLinkNetBase
    00018     {
    00022         private volatile bool running = false;
    00023
    00027         Thread listenerThread;
    00028
    00032         ReceiverHandler handler;
    00033
    00038         public delegate void ReceiverHandler(InfoLink infoLink);
    00039
    00045         public InfoLinkReceiver(ReceiverHandler handler)
    00046         {
    00047             this.handler = handler;
    00048             this.port = InfoLinkNetDefaultPort;
    00049         }
    00050
    00057         public InfoLinkReceiver(ReceiverHandler handler, int port)
    00058         {
    00059             this.handler = handler;
    00060             this.port = port;
    00061         }
    00062
    00066         ~InfoLinkReceiver()
    00067         {
    00068             if (this.Connected)
    00069             {
    00070                 this.Close();
    00071             }
    00072         }
    00073
    00077         public bool Running
    00078         {
    00079             get { return this.running; }
    00080             set { this.running = value; }
    00081         }
    00082
    00086         public void StartListener()
    00087         {
    00088             if (this.Connected)
    00089             {
    00090                 this.Close();
    00091             }
    00092
    00093             this.Open();
    00094             this.listenerThread = new Thread(new ThreadStart(RunListenerThread));
    00095             this.listenerThread.Name = "InfoLink Receiver";
    00096             this.listenerThread.IsBackground = true;
    00097             this.listenerThread.Start();
    00098         }
    00099
    00103         public void StopListener()
    00104         {
    00105             if (this.Connected)
    00106             {
    00107                 this.Close();
    00108             }
    00109         }
    00110
    00114         private void Open()
    00115         {
    00116             this.udpClient = new UdpClient(this.port);
    00117             this.endPoint = new IPEndPoint(IPAddress.Any, this.port);
    00118             this.Connected = true;
    00119         }
    00120
    00124         private void Close()
    00125         {
    00126             try
    00127             {
    00128                 this.running = false;
    00129                 this.udpClient.Close();
    00130                 this.listenerThread = null;
    00131             }
    00132             finally
    00133             {
    00134                 this.Connected = false;
    00135             }
    00136         }
    00137
    00142         private void RunListenerThread()
    00143         {
    00144             this.running = this.Connected;
    00145
    00146             while (this.running)
    00147             {
    00148                 // Receive a datagram
    00149                 byte[] bytes = null;
    00150                 try
    00151                 {
    00152                     bytes = this.udpClient.Receive(ref this.endPoint);
    00153                 }
    00154                 catch
    00155                 {
    00156                     // Assume the socket was closed,
    00157                     // so we can exit the thread loop
    00158                     this.running = false;
    00159                 }
    00160
    00161                 // Process datagram
    00162                 if (null != bytes && this.running)
    00163                 {
    00164                     string message =
    00165                         Encoding.UTF8.GetString(bytes, 0, bytes.Length);
    00166
    00167                     InfoLink infoLink = null;
    00168                     try
    00169                     {
    00170                         infoLink = InfoLinkSerializer.Deserialize(message);
    00171                     }
    00172                     finally
    00173                     {
    00174                         if (null != infoLink)
    00175                         {
    00176                             // Use data via delegate callback
    00177                             this.handler(infoLink);
    00178                         }
    00179                     }
    00180                 }
    00181             }
    00182         }
    00183     }
    00184 }