在我的应用程序中,我收到一个 UDP 流,并将其转发到另外两个 UDP 端点。我的问题是我需要在两个流之间设置延迟,所以我想使用 Timer。这是我的代码(我只显示相关方法):
public void Start()
{
Socket udpSocket = m_udpLocalClient.Client;
udpSocket.ReceiveBufferSize = BUFFER_SIZE;
// Enable broadcast
udpSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast,
1);
// Enable reusing address
udpSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress,
true);
// Bind the socket to the local endpoint and listen for incoming connections.
udpSocket.Bind(new IPEndPoint(IPAddress.Any, m_ListenPort));
m_udpLocalClient.JoinMulticastGroup(IPAddress.Parse(m_ListenIP));
udpSocket.BeginReceiveFrom(m_data,
0,
m_data.Length,
SocketFlags.None,
ref m_ListenEndPoint,
new AsyncCallback(ReceiveData),
m_udpLocalClient);
}
public void TimerCallback(object sender, ElapsedEventArgs e)
{
byte[] Data = new byte[m_BytesReceived];
m_Timer.Stop();
m_Mutex.WaitOne();
m_Buffer.Get(Data);
m_Mutex.ReleaseMutex();
//Send Packets lo LocalHost EndPoint
m_udpLocalClient.Send(Data, Data.Length, m_LocalEndPoint);
}
void ReceiveData(IAsyncResult iar)
{
int recv = 0;
try
{
UdpClient udpReceiver = (UdpClient)iar.AsyncState;
recv = udpReceiver.Client.EndReceiveFrom(iar, ref m_ListenEndPoint);
if (recv == 0) return;
//Forward packets on the FwEndpoint
m_udpFwClient.Send(m_data, recv, m_FwEndPoint);
//Send Packets lo LocalHost EndPoint
//m_udpLocalClient.Send(m_data, recv, m_LocalEndPoint);
m_Mutex.WaitOne();
m_Buffer.Put(m_data);
m_Mutex.ReleaseMutex();
if (!m_Timer.Enabled) m_Timer.Start();
udpReceiver.Client.BeginReceiveFrom(m_data,
0,
m_data.Length,
SocketFlags.None,
ref m_ListenEndPoint,
new AsyncCallback(ReceiveData),
udpReceiver);
}
catch (Exception e)
{
//...handle....
}
}
笔记。m_Buffer 成员是CodePlex的 CircularBuffer 。
这里的问题是调用
m_udpLocalClient.Send(Data, Data.Length, m_LocalEndPoint);
进入 TimerCallBack 似乎没有效果,而如果我在 ReceiveData 中取消注释同一个,它可以工作。
我也尝试使用异步方法 BeginSend() 但没有成功。我哪里错了?
问候,
丹尼尔。