00001 using System;
00002 using System.Net;
00003 using System.Net.Sockets;
00004 using System.Threading;
00005
00006
00007 namespace SmartDeviceGUIClient
00008 {
00012 public class SegmentReceiver {
00013 Thread processingThread;
00014 StackInterface thisStack;
00015 UdpClient udpListener;
00016
00021 public SegmentReceiver(StackInterface thisStack) {
00022 this.thisStack = thisStack;
00023 }
00024
00025 public void Start() {
00026 thisStack.msgLog.LogStartBegin("Segment Receiver");
00027 processingThread = new Thread(new ThreadStart(Listen));
00028 processingThread.Start();
00029 thisStack.msgLog.LogStartComplete("Segment Receiver");
00030 }
00031
00033 private void Listen() {
00034 IPEndPoint currentEndPoint;
00035 Segment receivedSegment;
00036 int localPortNumber = -1;
00037
00038
00039
00040
00041 localPortNumber = thisStack.settings.ClientReceivePortNumber;
00042
00043
00044 udpListener = new UdpClient(new IPEndPoint(IPAddress.Any, localPortNumber));
00045 try {
00046 while(true) {
00047 try {
00048
00049
00050 thisStack.msgLog.Log(udpListener.ToString());
00051 currentEndPoint = new IPEndPoint(IPAddress.Any, localPortNumber);
00052 receivedSegment = new Segment(udpListener.Receive(ref currentEndPoint));
00053 receivedSegment.Headers.TimeReceived = DateTime.Now.ToUniversalTime();
00054 thisStack.msgLog.Log("Got #{0}", receivedSegment.Headers.SequenceNumber);
00055
00056 } catch (Exception ex) {
00057 thisStack.msgLog.LogError("Error in Protocol Stack\n\rUnable to correctly receive segment.\n\rSkipping over and continuting. Details:\n\r(" + ex + ")");
00058 }
00059 }
00060 } finally {
00061 if (udpListener != null) { udpListener.Close(); }
00062 }
00063 }
00064 }
00065
00066
00067 }