110,539
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// Sends this <see cref="GetRequestMessage"/> and handles the response from agent.
/// </summary>
/// <param name="timeout">Timeout.</param>
/// <param name="port">Port number.</param>
/// <returns></returns>
public IList<Variable> Send(int timeout, int port)
{
byte[] bytes = _bytes;
IPEndPoint agent = new IPEndPoint(_agent, port);
using (UdpClient udp = new UdpClient())
{
udp.Send(bytes, bytes.Length, agent);
IPEndPoint from = new IPEndPoint(IPAddress.Any, 0);
IAsyncResult result = udp.BeginReceive(null, this);
result.AsyncWaitHandle.WaitOne(timeout, false);
if (!result.IsCompleted)
{
throw SharpTimeoutException.Create(_agent, timeout);
}
bytes = udp.EndReceive(result, ref from);
udp.Close();
}
MemoryStream m = new MemoryStream(bytes, false);
ISnmpMessage message = MessageFactory.ParseMessages(m)[0];
if (message.TypeCode != SnmpType.GetResponsePdu)
{
throw SharpOperationException.Create("wrong response type", _agent);
}
GetResponseMessage response = (GetResponseMessage)message;
if (response.SequenceNumber != SequenceNumber)
{
throw SharpOperationException.Create("wrong response sequence", _agent);
}
if (response.ErrorStatus != ErrorCode.NoError)
{
throw SharpErrorException.Create(
"error in response",
_agent,
response.ErrorStatus,
response.ErrorIndex,
response.Variables[response.ErrorIndex - 1].Id);
}
return response.Variables;
}
if (!result.IsCompleted)
{
throw SharpTimeoutException.Create(_agent, timeout);
}