Esempio n. 1
0
    // ------------------------------------------------------
    // 私有方法区
    // ------------------------------------------------------
    /// <summary>
    /// Socket 接收报文的主体函数,用于线程体的循环中
    /// </summary>
    /// <returns>接收字节数</returns>
    private int Receive()
    {
        int data_size = Marshal.SizeOf(typeof(T));

        byte[] buffer = new byte[1024];
        int    length = 0;

        try
        {
            length = m_socket.ReceiveFrom(buffer, ref m_remote_ep);
            //Console.WriteLine("{0} bytes received.", length);
        }
        catch (System.Exception e)
        {
            Console.WriteLine(e.Message);
        }

        if (length <= 0 || length != data_size)
        {
            return(length);
        }

        lock (data_lock)
        {
            m_data = BytesConverter.BytesToStruct <T>(buffer);
        }

        LastUpdateTime = DateTime.Now;

        return(length);
    }