|
///////////////////////////////////////////////////////////////////////
//
// Audio Manager for the PRISMIQ Media Player
// Written by Bruce Waeyen
// Copyright (c) 2004. All Rights Reserved.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name and all copyright
// notices remains intact.
//
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
//
//////////////////////////////////////////////////////////////////////
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace UdpClientServer
{
/// <summary>
/// Handles the UDP communications for both the client and the server
/// </summary>
public class UdpComm
{
public const string DefaultIp = "255.255.255.255";
public const string DefaultPort = "5999";
public delegate void ReceiveHandler(UdpComm pSocket, Byte[] data);
private ReceiveHandler m_pfnReceiveHandler;
private UdpClient m_Client;
private int m_Port = 5999;
private string m_Ip = "255.255.255.255";
private IPAddress m_GroupAddress;
private IPEndPoint m_RemoteEP;
private ManualResetEvent m_Exit;
private Thread m_ReceiverThread;
private bool m_Running = false;
public UdpComm(ReceiveHandler pfnReceiveHandler)
{
m_pfnReceiveHandler = pfnReceiveHandler;
}
/// <summary>
/// Set the port and address
/// </summary>
public void SetConnection(string Ip, int nPort)
{
Terminate();
m_Port = nPort;
m_Ip = Ip;
Initialize();
}
/// <summary>
/// Create the UdpClient and start receiver
/// </summary>
public void Initialize()
{
try
{
m_Client = new UdpClient(m_Port);
m_GroupAddress = IPAddress.Parse(m_Ip);
m_RemoteEP = new IPEndPoint( m_GroupAddress, m_Port);
m_ReceiverThread = new Thread(new ThreadStart(Receiver));
m_ReceiverThread.Start();
m_Running = true;
}
catch
{
System.Windows.Forms.MessageBox.Show("Could not create UDP connection");
}
}
/// <summary>
/// non blocking receive
/// </summary>
public void Receiver()
{
Thread.Sleep(2000);
while(true)
{
Byte[] Data;
try
{
Data = m_Client.Receive(ref m_RemoteEP);
}
catch
{
if(m_Running) continue;
// exception caused by a Terminate()
m_Exit.Set();
return;
}
m_pfnReceiveHandler(this, Data);
}
}
/// <summary>
/// Terminates the Listner by closing the Client this will cause an exception
/// in UdpClient Receive function unblocking the thread
/// </summary>
public void Terminate()
{
m_Running = false;
if(m_Client == null) return;
m_Exit = new ManualResetEvent(false); // event set when thread exits
try
{
m_Client.Close();
}
catch
{
}
m_Exit.WaitOne();
}
/// <summary>
/// blocking send
/// </summary>
public void Send(byte[] by)
{
m_Client.Send(by, by.Length, m_RemoteEP);
}
public void Send(byte b)
{
byte[] by = new byte[1];
by[0] = b;
m_Client.Send(by, by.Length, m_RemoteEP);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.