public PingResponseEventArgs(IPAddress serverAddress, PingResponseType result, int responseTime, int byteCount, bool cancel) { this.serverAddress = serverAddress; this.result = result; this.responseTime = responseTime; this.byteCount = byteCount; this.cancel = cancel; }
private void OnPingError(PingResponseType errorType, string message) { PingErrorEventHandler temp = PingError; if (temp != null) { temp(this, new PingErrorEventArgs(errorType, message, DateTime.Now)); } }
private void OnPingResponse(IPAddress serverAddress, PingResponseType result, int responseTime, int byteCount, ref bool cancel) { PingResponseEventHandler temp = PingResponse; if (temp != null) { PingResponseEventArgs e = new PingResponseEventArgs(serverAddress, result, responseTime, byteCount, cancel); temp(this, e); cancel = e.Cancel; } }
private void OnPingResponse(IPAddress serverAddress, PingResponseType result, int responseTime, int byteCount, ref bool cancel) { PingResponseEventHandler temp = PingResponse; if (temp != null) { var e = new PingResponseEventArgs(serverAddress, result, responseTime, byteCount, cancel); temp(this, e); cancel = e.Cancel; } }
public PingErrorEventArgs(PingResponseType errorType, string message, DateTime errorDateTime) { this.errorType = errorType; this.message = message; this.errorDateTime = errorDateTime; }
private PingResponse SendPackets(Socket socket, EndPoint client, EndPoint server, byte[] sendBuffer, int pingCount) { //Initialize PingResponse object PingResponse response = new PingResponse(pingCount); PingResponseType result = PingResponseType.Ok; byte[] receiveBuffer = new Byte[256]; int byteCount = 0; int start = 0; int stop = 0; response.PacketsReceived = 0; response.PacketsSent = 0; response.ServerEndPoint = (IPEndPoint)server; response.ClientEndPoint = (IPEndPoint)client; try { OnPingStarted((IPEndPoint)server, IcmpPacket.ICMP_PACKET_SIZE); for (int i = 0; i < pingCount; i++) { if (cancel) { response.PingResult = PingResponseType.Canceled; break; } // Initialize the buffers. The receive buffer is the size of the // ICMP header plus the IP header (20 bytes) receiveBuffer = new Byte[256]; byteCount = 0; response.PacketsSent++; try { // Start timing start = Environment.TickCount; //send the Packet over the socket byteCount = socket.SendTo(sendBuffer, IcmpPacket.ICMP_PACKET_SIZE, SocketFlags.None, server); if (byteCount == SOCKET_ERROR) { result = PingResponseType.ConnectionError; response.ResponseTimes[i] = Constants.InvalidInt; } else { //ReceiveFrom will block while waiting for data byteCount = socket.ReceiveFrom(receiveBuffer, 256, SocketFlags.None, ref client); // stop timing stop = System.Environment.TickCount; if (byteCount == SOCKET_ERROR) { result = PingResponseType.ConnectionError; response.ResponseTimes[i] = Constants.InvalidInt; } else if ((stop - start) > pingTimeout) { result = PingResponseType.RequestTimedOut; response.ResponseTimes[i] = Constants.InvalidInt; } else if (byteCount > 0) { //Record time response.ResponseTimes[i] = stop - start; response.PacketsReceived++; } } OnPingResponse(((IPEndPoint)server).Address, result, response.ResponseTimes[i], byteCount, ref cancel); } catch (Exception ex) { OnPingError(PingResponseType.InternalError, ex.Message); } } } finally { //close the socket socket.Close(); OnPingCompleted(response); } response.PingResult = result; return(response); }