/// <summary> /// Incoming connection callback /// </summary> /// <param name="ar"></param> void DoAcceptUdpDataCallback(IAsyncResult ar) { UdpClient listener = null; UDPData udp_data = null; byte[] rx_buffer = null; IPEndPoint remote_ep = null; try { List <byte> send_buffer = new List <byte>(); List <byte> receive_buffer = new List <byte>(); // Copy listener instance listener = (UdpClient)ar.AsyncState; // Get input frame and remote endpoint rx_buffer = listener.EndReceive(ar, ref remote_ep); // Istance UDPData class udp_data = new UDPData(listener, rx_buffer, remote_ep); // Set event _manualResetEvent.Set(); // Process incoming call IncomingMessagePolling(send_buffer, receive_buffer, udp_data); } catch { } finally { if (udp_data != null) { udp_data.Close(); } } }
/// <summary> /// Read a byte from a specified flux /// </summary> /// <param name="flux">Flux of reading bytes</param> /// <returns>Readed byte or -1 if no data is present</returns> int ReadByte(object flux) { int ret = -1; switch (_connectionType) { case ConnectionType.UDP_IP: UDPData udpd = (UDPData)flux; if (udpd.Length > 0) { ret = udpd.ReadByte(); } break; case ConnectionType.TCP_IP: NetworkStream ns = (NetworkStream)flux; if (ns.DataAvailable) { ret = ns.ReadByte(); } break; case ConnectionType.SERIAL_ASCII: case ConnectionType.SERIAL_RTU: SerialPort sp = (SerialPort)flux; bool done = false; // Await 1 char... if (!_timeoutStopwatch.IsRunning) { _timeoutStopwatch.Start(); } do { if (sp.BytesToRead > 0) { ret = sp.ReadByte(); done = true; } else { ret = -1; } } while ((!done) && ((_timeoutStopwatch.ElapsedMilliseconds - _charTimeout) < _intercharDelay)); if (done) { _charTimeout = _timeoutStopwatch.ElapsedMilliseconds; // Char received with no errors...reset timeout counter for next char! } break; } return(ret); }
/// <summary> /// Write a byte on a specified flux /// </summary> /// <param name="flux">Writing byte flux</param> /// <param name="buffer">Buffer of bytes</param> /// <param name="offset">Buffer starting offset</param> /// <param name="size">Number of bytes to write</param> void WriteBuffer(object flux, byte[] buffer, int offset, int size) { switch (_connectionType) { case ConnectionType.UDP_IP: UDPData udpd = (UDPData)flux; udpd.WriteResp(buffer, offset, size); break; case ConnectionType.TCP_IP: NetworkStream ns = (NetworkStream)flux; ns.Write(buffer, offset, size); break; case ConnectionType.SERIAL_ASCII: case ConnectionType.SERIAL_RTU: SerialPort sp = (SerialPort)flux; sp.Write(buffer, offset, size); break; } }
/// <summary> /// Incoming connection callback /// </summary> /// <param name="ar"></param> void DoAcceptUdpDataCallback(IAsyncResult ar) { UdpClient listener = null; UDPData udp_data = null; byte[] rx_buffer = null; IPEndPoint remote_ep = null; try { List<byte> send_buffer = new List<byte>(); List<byte> receive_buffer = new List<byte>(); // Copy listener instance listener = (UdpClient)ar.AsyncState; // Get input frame and remote endpoint rx_buffer = listener.EndReceive(ar, ref remote_ep); // Istance UDPData class udp_data = new UDPData(listener, rx_buffer, remote_ep); // Set event mre.Set(); // Process incoming call IncomingMessagePolling(send_buffer, receive_buffer, udp_data); } catch { } finally { if (udp_data != null) udp_data.Close(); } }