using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading.Tasks;

public class UDPLogServer
{
    private static DateTime Sendtime = DateTime.Now;

    private static UdpClient udpClient;
    //private static IPEndPoint endPoint;
    private static Object _lock = new Object();

    public static void Init()
    {
        //endPoint = new IPEndPoint(IPAddress.Parse("122.152.232.170"), 45000);
        if (null != udpClient)
        {
            udpClient.Close();
            udpClient = null;
        }
        udpClient = new UdpClient("122.152.232.170", 45000);
    }
    /// <summary>
    /// AddMessage 添加数据
    /// </summary>
    public static void AddMessage(long custom, int type, string body)
    {
        if (null == udpClient)
        {
            return;
        }
        System.Threading.Tasks.Task.Factory.StartNew(() =>
        {
            List<byte> data = new List<byte>();
            data.Add(0xAA);//head
            data.AddRange(uint16tobyte((new MessagesData()).Id));// 添加id  2个字节
            // 添加时间  8个字节
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            data.AddRange(BitConverter.GetBytes(ts.TotalMilliseconds));
            // 自定义 只取 3 Byte
            var bCustom = uint32tobyte(custom);
            data.Add(bCustom[1]);
            data.Add(bCustom[2]);
            data.Add(bCustom[3]);
            data.Add((byte)type);
            var body_str = System.Text.Encoding.UTF8.GetBytes(body);
            data.AddRange(uint16tobyte(body_str.Length));// 添加长度  2 个字节                    
            data.AddRange(body_str);// 添加内容
            data.Add(0x55);//end

            byte[] sendData = data.ToArray();
            //udpClient.BeginSend(sendData, sendData.Length, new AsyncCallback(SendCallback), null);
            lock (_lock)
            {
                udpClient.Send(sendData, sendData.Length);
            }
        }, System.Threading.CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
    }

    private static void SendCallback(IAsyncResult ar)
    {
        if (ar.IsCompleted)
        {
            udpClient.EndSend(ar);
        }
    }

    private static byte[] uint16tobyte(int data)
    {
        byte[] res = new byte[2];

        res[0] = byte.Parse((data / 0x100).ToString());
        res[1] = byte.Parse((data % 0x100).ToString());

        return res;
    }

    private static byte[] uint32tobyte(long data)
    {
        byte[] res = new byte[4];
        long temp_data = data;
        // int temp_2 = 0;

        res[0] = byte.Parse((temp_data / 0x1000000).ToString());
        temp_data = temp_data % 0x1000000;
        res[1] = byte.Parse((temp_data / 0x10000).ToString());
        //temp_2 = int.Parse( (temp_data % 0x10000 ).ToString());
        temp_data = temp_data % 0x10000;
        res[2] = byte.Parse((temp_data / 0x100).ToString());
        temp_data = temp_data % 0x100;
        res[3] = byte.Parse((temp_data % 0x100).ToString());

        return res;
    }
}

public class MessagesData
{
    static object locks = new object();
    public MessagesData()
    {
        lock (locks)
        {
            MAX_Id++;
            if (MAX_Id > 65535)
            {
                MAX_Id = 1;
            }
        }
        this.Id = MAX_Id;
    }
    internal static int MAX_Id = 0;
    internal int Id = MAX_Id;
    public int type = 1;
    public DateTime time = DateTime.Now;
    public string body = "";
    public int about_id = 0;
    public long custom = 0;
}
