public void Class1SendO2T(SequencedAddressItem Item)
 {
     if (UdpListener != null)
     {
         UdpListener.Send(Item, epUdp);
     }
 }
        // Coming from an udp class1 device, with a previous ForwardOpen action
        public void On_ItemMessageReceived(object sender, byte[] packet, SequencedAddressItem ItemPacket, int offset, int msg_length, IPEndPoint remote_address)
        {
            if (ItemPacket.ConnectionId != T2O_ConnectionId)
            {
                return;
            }

            if ((msg_length - offset) == 0)
            {
                return;
            }

            RawData = new byte[msg_length - offset];
            Array.Copy(packet, offset, RawData, 0, RawData.Length);

            if (DecodedMembers != null)
            {
                int Idx = 0;
                try
                {
                    DecodedMembers.DecodeAttr(Id, ref Idx, RawData);
                }
                catch { }
            }


            if (T2OEvent != null)
            {
                T2OEvent(this);
            }
        }
        private void OnReceiveData(IAsyncResult asyncResult)
        {
            System.Net.Sockets.UdpClient conn = (System.Net.Sockets.UdpClient)asyncResult.AsyncState;
            try
            {
                System.Net.IPEndPoint ep = new IPEndPoint(System.Net.IPAddress.Any, 0);
                byte[] local_buffer;
                int    rx = 0;

                try
                {
                    local_buffer = conn.EndReceive(asyncResult, ref ep);
                    rx           = local_buffer.Length;
                }
                catch (Exception) // ICMP port unreachable
                {
                    //restart data receive
                    conn.BeginReceive(OnReceiveData, conn);
                    return;
                }

                if (rx < 14)    // Sure it's too small
                {
                    //restart data receive
                    conn.BeginReceive(OnReceiveData, conn);
                    return;
                }

                try
                {
                    int Offset = 0;
                    Encapsulation_Packet Encapacket = new Encapsulation_Packet(local_buffer, ref Offset, rx);
                    //verify message
                    if (Encapacket.IsOK)
                    {
                        if (EncapMessageReceived != null)
                        {
                            EncapMessageReceived(this, local_buffer, Encapacket, Offset, rx, ep);
                        }
                    }
                    else
                    {
                        SequencedAddressItem Itempacket = new SequencedAddressItem(local_buffer, ref Offset, rx);
                        if (Itempacket.IsOK && (ItemMessageReceived != null))
                        {
                            ItemMessageReceived(this, local_buffer, Itempacket, Offset, rx, ep);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Exception in udp recieve: " + ex.Message);
                }
                finally
                {
                    //restart data receive
                    conn.BeginReceive(OnReceiveData, conn);
                }
            }
            catch (Exception ex)
            {
                //restart data receive
                if (conn.Client != null)
                {
                    Trace.TraceError("Exception in Ip OnRecieveData: " + ex.Message);
                    conn.BeginReceive(OnReceiveData, conn);
                }
            }
        }
 public void Send(SequencedAddressItem Packet, IPEndPoint ep)
 {
     byte[] b = Packet.toByteArray();
     m_exclusive_conn.Send(b, b.Length, ep);
 }