public class Packet
{
public byte[] Buffer { get; private set; }
public Packet(byte[] buffer)
{
Buffer = buffer;
}
}
public class PacketEventArgs : EventArgs
{
public Packet Packet { get; set; }
}
public class UdpState
{
public UdpClient Client { get; set; }
public IPEndPoint EndPoint { get; set; }
}
public class Receiver
{
public event EventHandler<PacketEventArgs> PacketReceived;
private Thread _thread;
private ManualResetEvent _shutdownThread = new ManualResetEvent(false);
public void Start() { _thread.Start(); }
public void Stop() { _shutdownThread.Set(); }
public Receiver()
{
_thread = new Thread(
delegate()
{
// Create the client UDP socket. Listing on port 9003 IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 9003);
UdpClient client = new UdpClient(endPoint);
// Receive the packets asynchronously. client.BeginReceive(
new AsyncCallback(OnPacketReceived),
new UdpState() { Client = client, EndPoint = endPoint });
// Wait for the thread to end. _shutdownThread.WaitOne();
}
);
}
private void OnPacketReceived(IAsyncResult ar)
{
UdpState state = (UdpState)ar.AsyncState;
IPEndPoint endPoint = state.EndPoint;
byte[] bytes = state.Client.EndReceive(ar, ref endPoint);
// Create the packet. Packet packet = new Packet(bytes);
// Notify any listeners. EventHandler<PacketEventArgs> handler = PacketReceived;
if (handler != null)
{
handler(this, new PacketEventArgs() { Packet = packet });
}
// Read next packet. if (!_shutdownThread.WaitOne(0))
{
state.Client.BeginReceive(
new AsyncCallback(OnPacketReceived),
state);
}
}
}
public class IPCamReplyData
{
public string IPCamReply;
public string Version;
public string DevId;
public string Port;
public string DevName;
public string WiFiIP;
public string nSeq;
public string AppVersion;
public string HardwareVersion;
public string WebVersion;
public string IP;
public string CableConnect;
public string WifiConnect;
public string MacEth0;
public string MacWiFi;
public string IPMethod;
public string WiFiIPMethod;
public string NetMask;
public string GateWay;
public string DNS0;
public string DNS1;
public string Ddns;
public string AppUpTime;
}
public class IpCameraList
{
static IpCameraList _instance = null;
List<IPCamReplyData> _listIpCams;
private IpCameraList()
{
_listIpCams = new List<IPCamReplyData>();
}
public static IpCameraList Instance()
{
if (_instance == null)
_instance = new IpCameraList();
return _instance;
}
public void Add(IPCamReplyData ipcam)
{
_listIpCams.Add(ipcam);
}
public void Clear()
{
_listIpCams.Clear();
}
public List<IPCamReplyData> GetAllIpCameraList()
{
return _listIpCams;
}
}
public class Processor
{
// Passing ipCam data to UI //public delegate void AddIpCamToUiTableDelegate(); //public AddIpCamToUiTableDelegate ipCamUIDelegage;IPCamReplyData ipcam public delegate void AddIpCamToUiTableDelegate(IPCamReplyData ipcam);
public AddIpCamToUiTableDelegate ipCamUIDelegage;
private Thread _thread;
private object _sync = new object();
private ManualResetEvent _packetReceived = new ManualResetEvent(false);
private ManualResetEvent _shutdownThread = new ManualResetEvent(false);
private Queue<Packet> _packetQueue = new Queue<Packet>(); // shared data public void Start() { _thread.Start(); }
public void Stop() { _shutdownThread.Set(); }
public Processor()
{
_thread = new Thread(
delegate()
{
WaitHandle[] handles = new WaitHandle[] {
_shutdownThread,
_packetReceived
};
while (!_shutdownThread.WaitOne(0))
{
switch (WaitHandle.WaitAny(handles))
{
case 0: // Shutdown Thread Event break;
case 1: // Packet Received Event _packetReceived.Reset();
ProcessPackets();
break;
default:
Stop();
break;
}
}
}
);
}
private void ProcessPackets()
{
Queue<Packet> localPacketQueue = null;
Queue<Packet> newPacketQueue = new Queue<Packet>();
lock (_sync)
{
// Swap out the populated queue with the empty queue. localPacketQueue = _packetQueue;
_packetQueue = newPacketQueue;
}
foreach (Packet packet in localPacketQueue)
{
string s = System.Text.ASCIIEncoding.ASCII.GetString(packet.Buffer);
string[] lines = s.Split('\n');
for (int i = 0; i < lines.Length; i += 1)
lines[i] = lines[i].Trim();
//Console.WriteLine("Packet received with {0} bytes with content [{1}]",packet.Buffer.Length, s); IPCamReplyData ipcam = CovertDataToIpCamObject(lines);
IpCameraList.Instance().Add(ipcam);
ipCamUIDelegage(ipcam);
}
// Notify UI that new data is available //ipCamUIDelegage();
}
private IPCamReplyData CovertDataToIpCamObject(string[] lines)
{
IPCamReplyData ipcam = new IPCamReplyData();
for (int i = 0; i < lines.Length; i += 1)
{
string s = lines[i];
string[] s1 = s.Split(':');
if (s1.Length <= 1) continue;
string value = s1[1];
if (value.Length > 0)
{
switch (s1[0])
{
case "IPCamReply": ipcam.IPCamReply = value;
break;
case "Version": ipcam.Version = value;
break;
case "DevId": ipcam.DevId = value;
break;
case "Port": ipcam.Port = value;
break;
case "DevName": ipcam.DevName = value;
break;
case "WiFiIP": ipcam.WiFiIP = value;
break;
case "nSeq": ipcam.nSeq = value;
break;
case "AppVersion": ipcam.AppVersion = value;
break;
case "HardwareVersion": ipcam.HardwareVersion = value;
break;
case "WebVersion": ipcam.WebVersion = value;
break;
case "IP": ipcam.IP = value;
break;
case "CableConnect": ipcam.CableConnect = value;
break;
case "WifiConnect": ipcam.WifiConnect = value;
break;
case "MacEth0": ipcam.MacEth0 = value;
break;
case "MacWiFi": ipcam.MacWiFi = value;
break;
case "IPMethod": ipcam.IPMethod = value;
break;
case "WiFiIPMethod": ipcam.WiFiIPMethod = value;
break;
case "NetMask": ipcam.NetMask = value;
break;
case "GateWay": ipcam.GateWay = value;
break;
case "DNS0": ipcam.DNS0 = value;
break;
case "DNS1": ipcam.DNS1 = value;
break;
case "Ddns": ipcam.Ddns = value;
break;
case "AppUpTime": ipcam.AppUpTime = value;
break;
default:
break;
}
}
}
return ipcam;
}
public void OnPacketReceived(object sender, PacketEventArgs e)
{
// NOTE: This function executes on the Receiver thread. lock (_sync)
{
// Enqueue the packet. _packetQueue.Enqueue(e.Packet);
}
// Notify the Processor thread that a packet is available. _packetReceived.Set();
}
}
public class MessageSender
{
int GroupPort = 9002;
private UdpClient udp;
private IPEndPoint groupEP;
private byte[] sendBytes4;
public MessageSender()
{
udp = new UdpClient();
groupEP = new IPEndPoint(IPAddress.Parse("255.255.255.255"), GroupPort);
string str4 = "IPCamSearch\r\nVersion:1\r\nnSeq:1\r\n";
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
sendBytes4 = ASCII.GetBytes(str4);
//udp.Send(sendBytes4, sendBytes4.Length, groupEP); }
public void Send()
{
IpCameraList.Instance().Clear();
udp.Send(sendBytes4, sendBytes4.Length, groupEP);
}
}
点击打开链接 http://www.oschina.net/question/267845_54551