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

namespace ZeusServerEdgeJSTest
{

    class FastStreamComposer
    {

        private  const int LEFT_SHIFT_BITS = 1 << 7;

        private MemoryStream stream;
        public FastStreamComposer(string uid,byte[] data)
        {
            byte[] bytesUid = System.Text.UTF8Encoding.UTF8.GetBytes(uid);

            int contentSize = 6 + bytesUid.Length + data.Length;
            int lengthSize = calLengthSize(contentSize);

            this.stream = new MemoryStream(lengthSize + contentSize);

        
            //composer head
            writeLength(contentSize, lengthSize);
            //协议 head
            writeU16((ushort)bytesUid.Length);
            writeU32((uint)data.Length);

            //uid
            writeBytes(bytesUid);
            //data
            writeBytes(data);

        }

        public byte[] getBytes()
        {
            return stream.GetBuffer();
        }

        public static int calLengthSize(int length)
        {
            int res = 0;
            while (length > 0)
            {
                length = length >> 7;
                res++;
            }

            return res;

        }
      

        public void writeLength(int data,int size)
        {
            int offset = size - 1, b;           
            byte[] bytes = new byte[size];
            for (; offset >= 0; offset--)
            {
                b = data % LEFT_SHIFT_BITS;
                if (offset < size - 1)
                {
                    b |= 0x80;
                }
                bytes[offset] = (byte)b;       
                data = data >> 7;
            }
            stream.Write(bytes, 0, bytes.Length);
        }

        public void writeU16(UInt16 value)
        {
            stream.WriteByte((byte)(value));
            stream.WriteByte((byte)(value >> 8));
        }

        public void writeU32(UInt32 value)
        {
            stream.WriteByte((byte)(value));
            stream.WriteByte((byte)(value >> 8));
            stream.WriteByte((byte)(value >> 16));
            stream.WriteByte((byte)(value >> 24));
        }



        public void writeBytes(byte[] value)
        {
            stream.Write(value, 0, value.Length);
        }
    }
    class FastStreamTest
    {
        private Socket socket;
        public void connect(string host,int port)
        {
            IPAddress ipAddress = IPAddress.Parse(host);
            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint ie = new IPEndPoint(ipAddress, port);

            socket.BeginConnect(ie, new AsyncCallback((result) =>
            {
                try
                {
                    this.socket.EndConnect(result);
                    this.socket.NoDelay = true;

                    for (int i = 1; i <= 3; ++i )
                        send("rovesky", data);
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.Message);

                }
                finally
                {
                   
                }
            }), this.socket);
        }

        private byte[] data = new byte[]{1,134,0,0,239,219,3,98};


     
 
        public void send(string uid, byte[] data)
        {

            FastStreamComposer composer = new FastStreamComposer(uid, data);



            this.socket.Send(composer.getBytes());
           
          //  socket.Send()
        }



        public static void start()
        {
            FastStreamTest fastStreamTest = new FastStreamTest();
            fastStreamTest.connect("127.0.0.1", 3050);
            do
            {
                Thread.Sleep(10);
            }
            while (true);
        }


           
    }
}