Beispiel #1
0
        public DataFrame(string content)
        {
            _content = Encoding.UTF8.GetBytes(content);
            int length = _content.Length;

            if (length < 126)
            {
                _extend = new byte[0];
                _header = new DataFrameHeader(true, false, false, false, 1, false, length);
            }
            else if (length < 65536)
            {
                _extend = new byte[2];
                _header = new DataFrameHeader(true, false, false, false, 1, false, 126);
                _extend[0] = (byte)(length / 256);
                _extend[1] = (byte)(length % 256);
            }
            else
            {
                _extend = new byte[8];
                _header = new DataFrameHeader(true, false, false, false, 1, false, 127);

                int left = length;
                int unit = 256;

                for (int i = 7; i > 1; i--)
                {
                    _extend[i] = (byte)(left % unit);
                    left = left / unit;

                    if (left == 0)
                        break;
                }
            }
        }
Beispiel #2
0
        //RFC6455 5.2基本帧协议
        public DataFrame(byte[] buffer)
        {
            //帧头
            _header = new DataFrameHeader(buffer);

            //扩展长度:if payload len==126/127) Extended payload length。第3、4字节
            if (_header.Length == 126)
            {
                _extend = new byte[2];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 2);
            }
            else if (_header.Length == 127)
            {
                _extend = new byte[8];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 8);
            }

            //是否有掩码
            if (_header.HasMask)
            {
                _mask = new byte[4];
                Buffer.BlockCopy(buffer, _extend.Length + 2, _mask, 0, 4);
            }

            //消息体。
            //负载长度=“扩展数据”长度+“应用数据”长度
            //“应用数据”的长度 = 负载长度减去“扩展数据”长度。
            if (_extend.Length == 0)
            {
                _content = new byte[_header.Length];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
            }
            else if (_extend.Length == 2)
            {
                int contentLength = (int)_extend[0] * 256 + (int)_extend[1];
                _content = new byte[contentLength];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, contentLength > 1024 * 100 ? 1024 * 100 : contentLength);
            }
            else
            {
                long len = 0;
                int  n   = 1;
                for (int i = 7; i >= 0; i--)
                {
                    len += (int)_extend[i] * n;
                    n   *= 256;
                }
                _content = new byte[len];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
            }

            if (_header.HasMask)
            {
                _content = GetDecodedPayloadData(_content, _mask);
            }
        }
        public DataFrame(byte[] buffer)
        {
            //帧头
            _header = new DataFrameHeader(buffer);

            //扩展长度
            if (_header.Length == 126)
            {
                _extend = new byte[2];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 2);
            }
            else if (_header.Length == 127)
            {
                _extend = new byte[8];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 8);
            }

            //是否有掩码
            if (_header.HasMask)
            {
                _mask = new byte[4];
                Buffer.BlockCopy(buffer, _extend.Length + 2, _mask, 0, 4);
            }

            //消息体
            if (_extend.Length == 0)
            {
                _content = new byte[_header.Length];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
            }
            else if (_extend.Length == 2)
            {
                int contentLength = (int)_extend[0] * 256 + (int)_extend[1];
                _content = new byte[contentLength];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, contentLength > 1024 * 100 ? 1024 * 100 : contentLength);
            }
            else
            {
                long len = 0;
                int  n   = 1;
                for (int i = 7; i >= 0; i--)
                {
                    len += (int)_extend[i] * n;
                    n   *= 256;
                }
                _content = new byte[len];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
            }

            if (_header.HasMask)
            {
                _content = Mask(_content, _mask);
            }
        }
Beispiel #4
0
        public DataFrame(byte[] buffer)
        {
            //帧头
            _header = new DataFrameHeader(buffer);

            //扩展长度
            if (_header.Length == 126)
            {
                _extend = new byte[2];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 2);
            }
            else if (_header.Length == 127)
            {
                _extend = new byte[8];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 8);
            }

            //是否有掩码
            if (_header.HasMask)
            {
                _mask = new byte[4];
                Buffer.BlockCopy(buffer, _extend.Length + 2, _mask, 0, 4);
            }         
   
            //消息体
            if (_extend.Length == 0)
            {
                _content = new byte[_header.Length];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2 , _content, 0, _content.Length);
            }
            else if (_extend.Length == 2)
            {
                int contentLength = (int)_extend[0] * 256 + (int)_extend[1];
                _content = new byte[contentLength];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, contentLength > 1024 * 100 ? 1024 * 100 : contentLength);
            }
            else
            {
                long len = 0;
                int n = 1;
                for (int i = 7; i >= 0; i--)
                {
                    len += (int)_extend[i] * n;
                    n *= 256;
                }
                _content = new byte[len];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
            }

            if (_header.HasMask) _content = Mask(_content, _mask);

        }
        public DataFrame(string content)
        {
            _content = Encoding.UTF8.GetBytes(content);
            int length = _content.Length;

            if (length < 126)
            {
                _extend = new byte[0];
                _header = new DataFrameHeader(true, false, false, false, 1, false, length);
            }
            else if (length < 65536)
            {
                _extend    = new byte[2];
                _header    = new DataFrameHeader(true, false, false, false, 1, false, 126);
                _extend[0] = (byte)(length / 256);
                _extend[1] = (byte)(length % 256);
            }
            else
            {
                _extend = new byte[8];
                _header = new DataFrameHeader(true, false, false, false, 1, false, 127);

                int left = length;
                int unit = 256;

                for (int i = 7; i > 1; i--)
                {
                    _extend[i] = (byte)(left % unit);
                    left       = left / unit;

                    if (left == 0)
                    {
                        break;
                    }
                }
            }
        }
        public void setByte(byte[] contents)
        {
            _content = contents;
            int length = _content.Length;

            if (length < 126)
            {
                _extend = new byte[0];
                _header = new DataFrameHeader(true, false, false, false, 2, false, length);
            }
            else if (length < 65536)
            {
                _extend    = new byte[2];
                _header    = new DataFrameHeader(true, false, false, false, 2, false, 126);
                _extend[0] = (byte)(length / 256);
                _extend[1] = (byte)(length % 256);
            }
            else
            {
                _extend = new byte[8];
                _header = new DataFrameHeader(true, false, false, false, 2, false, 127);

                int left = length;
                int unit = 256;

                for (int i = 7; i > 1; i--)
                {
                    _extend[i] = (byte)(left % unit);
                    left       = left / unit;

                    if (left == 0)
                    {
                        break;
                    }
                }
            }
        }
Beispiel #7
0
        private void packageData(object obj)
        {
            NETcollection netc = obj as NETcollection;

            try {
                //  Array.Copy(netc.Datalist, ListData, count);

                //while (true)
                //{
                int           count    = netc.Datalist.Count;
                List <Byte[]> ListData = netc.Datalist;
                int           i        = 0;


                if (netc.Datalist.Count > 0)
                {
                    WebSocketServer.DataFrameHeader dfh = null;
                    int bytesRead = ListData[i] != null ? ListData[i].Length : 0;
                    if (bytesRead == 0)
                    {
                        if (ListData.Count > 0)
                        {
                            ListData.RemoveAt(0);
                        }
                        netc.Ispage = false;  return;
                    }
                    ;
                    byte[] tempbtyes = new byte[bytesRead];
                    Array.Copy(ListData[i], tempbtyes, tempbtyes.Length);
                    byte[] masks = new byte[4];

                    int    lens     = 0;
                    int    paylen   = 0;
                    byte[] tempbtye = null;
                    try
                    {
                        DataFrame df = new DataFrame();
                        // AnalyticData(tempbtyes, bytesRead, ref masks, ref lens, ref paylen);

                        tempbtye = df.GetData(tempbtyes, ref masks, ref lens, ref paylen, ref dfh);
                        if (dfh.OpCode != 2)
                        {
                            ListData.RemoveAt(i);
                            netc.Ispage = false; return;
                        }
                    }
                    catch {
                        if (paylen > bytesRead)
                        {
                            ListData.RemoveAt(i);
                            byte[] temps = new byte[tempbtyes.Length];
                            Array.Copy(tempbtyes, temps, temps.Length);
                            tempbtyes = new byte[temps.Length + ListData[i].Length];
                            Array.Copy(temps, tempbtyes, temps.Length);
                            Array.Copy(ListData[i], 0, tempbtyes, temps.Length, ListData[i].Length);
                            ListData[i] = tempbtyes;
                        }
                        else
                        {
                            ListData.RemoveAt(i);
                        }
                        netc.Ispage = false; return;
                    }
                    if (tempbtye == null)
                    {
                        netc.Ispage = false; return;
                    }

labe881:
                    if (tempbtye.Length > 0)
                    {
                        #region MyRegion

                        String temp = "";
                        int    a    = tempbtye[1];
                        if (bytesRead > 2 + a)
                        {
                            int len = 0;
                            if (DT == DataType.bytes)
                            {
                                byte[] bb = new byte[a];
                                Array.Copy(tempbtye, 2, bb, 0, a);

                                len = ConvertToInt(bb);
                            }
                            else
                            {
                                temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a);
                                try
                                {
                                    len = int.Parse(temp);
                                }
                                catch
                                {
                                    if (bytesRead > tempbtye.Length + lens)
                                    {
                                        int    aa     = bytesRead - (tempbtye.Length + lens);
                                        byte[] temptt = new byte[aa];
                                        Array.Copy(tempbtyes, (tempbtye.Length + lens), temptt, 0, temptt.Length);
                                        ListData[i] = temptt;
                                        netc.Ispage = false; return;
                                    }
                                }
                            }


                            if (tempbtye.Length == (len + 2 + a))
                            {
                                if (bytesRead > tempbtye.Length + lens)
                                {
                                    int    aa     = bytesRead - (tempbtye.Length + lens);
                                    byte[] temptt = new byte[aa];
                                    Array.Copy(tempbtyes, (tempbtye.Length + lens), temptt, 0, temptt.Length);
                                    ListData[i] = temptt;
                                }
                                else if (bytesRead < tempbtye.Length + lens)
                                {
                                }
                                else
                                {
                                    ListData.RemoveAt(i);
                                }
                                if (DT == DataType.json)
                                {
                                    temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len);
                                }
                            }
                            else
                            {
                                len = tempbtye.Length - 2 - a;
                                if (DT == DataType.json)
                                {
                                    temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len);
                                }

                                if (bytesRead > tempbtye.Length + lens)
                                {
                                    int    aa     = bytesRead - (tempbtye.Length + lens);
                                    byte[] temptt = new byte[aa];


                                    Array.Copy(tempbtyes, (tempbtye.Length + lens), temptt, 0, temptt.Length);
                                    ListData[i] = temptt;

                                    temp = combine(temp, temptt, ListData);
                                }
                                else
                                {
                                    ListData.RemoveAt(i);
                                    while (!(ListData.Count > 0))
                                    {
                                        System.Threading.Thread.Sleep(100);
                                    }


                                    temp = combine(temp, ListData[i], ListData);
                                }

                                // netc.Ispage = false; return;
                            }



                            try
                            {
                                if (DT == DataType.json)
                                {
                                    modelevent me = new modelevent();
                                    me.Command = tempbtye[0];
                                    me.Data    = temp;
                                    me.Soc     = netc.Soc;
                                    me.Masks   = masks;

                                    //System.Threading.Thread t = new Thread(new ParameterizedThreadStart(receiveeventto));
                                    //t.Start(me);
                                    //receiveeventto(me);
                                    if (receiveevent != null)
                                    {
                                        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventto), me);
                                    }
                                    //if (receiveevent != null)
                                    //    receiveevent(me.Command, me.Data, me.Soc);
                                }
                                else if (DT == DataType.bytes)
                                {
                                    byte[] bs = new byte[len - (2 + a)];
                                    Array.Copy(tempbtye, 2 + a, bs, 0, bs.Length);
                                    modelevent me = new modelevent();
                                    me.Command = tempbtye[0];
                                    me.Data    = "";
                                    me.Databit = bs;
                                    me.Soc     = netc.Soc;
                                    if (receiveeventbit != null)
                                    {
                                        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventtobit), me);
                                    }
                                }
                                netc.Ispage = false; return;
                            }
                            catch (Exception e)
                            {
                                netc.Ispage = false; return;
                            }
                        }
                        #endregion
                    }
                }
                // }
            }
            catch (Exception ex)
            {
                if (netc.Datalist.Count > 0)
                {
                    netc.Datalist.RemoveAt(0);
                }
                netc.Ispage = false; return;
            }
        }
Beispiel #8
0
        String combine(string temp, byte[] tempbtyes, List <byte[]> ListData)
        {
            WebSocketServer.DataFrameHeader dfh = null;

            //  Array.Copy(ListData[0], tempbtyes, tempbtyes.Length);
            byte[] masks = new byte[4];

            int lens   = 0;
            int paylen = 0;

            byte[] tempbtye = null;

            DataFrame df = new DataFrame();

            // AnalyticData(tempbtyes, bytesRead, ref masks, ref lens, ref paylen);
            try
            {
                tempbtye = df.GetData(tempbtyes, ref masks, ref lens, ref paylen, ref dfh);
            }
            catch {
                if (paylen > tempbtyes.Length)
                {
                    ListData.RemoveAt(0);
                    byte[] temps = new byte[tempbtyes.Length];
                    Array.Copy(tempbtyes, temps, temps.Length);
                    tempbtyes = new byte[temps.Length + ListData[0].Length];
                    Array.Copy(temps, tempbtyes, temps.Length);
                    Array.Copy(ListData[0], 0, tempbtyes, temps.Length, ListData[0].Length);
                    ListData[0] = tempbtyes;
                    temp        = combine(temp, ListData[0], ListData);
                    return(temp);
                }
            }
            try
            {
                temp += System.Text.Encoding.UTF8.GetString(tempbtye);
                if (ListData[0].Length > tempbtye.Length + lens)
                {
                    int    aa     = ListData[0].Length - (tempbtye.Length + lens);
                    byte[] temptt = new byte[aa];
                    Array.Copy(tempbtyes, (tempbtye.Length + lens), temptt, 0, temptt.Length);
                    ListData[0] = temptt;
                }
                else
                {
                    ListData.RemoveAt(0);
                }
                if (!dfh.FIN)
                {
                    while (!(ListData.Count > 0))
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                    temp = combine(temp, ListData[0], ListData);
                }
            }
            catch (Exception e)
            {
            }

            return(temp);
        }
        public byte[] GetData(byte[] buffer, ref byte[] masks, ref int lens, ref int payload_len, ref DataFrameHeader dfh)
        {
            lens = 0;
            //帧头
            _header     = new DataFrameHeader(buffer);
            dfh         = _header;
            payload_len = 0;
            if (_header.OpCode != 2)
            {
            }
            //扩展长度
            if (_header.Length == 126)
            {
                lens    = 8;
                _extend = new byte[2];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 2);
            }
            else if (_header.Length == 127)
            {
                lens    = 14;
                _extend = new byte[8];
                Buffer.BlockCopy(buffer, 2, _extend, 0, 8);
            }

            //是否有掩码
            if (_header.HasMask)
            {
                _mask = new byte[4];
                Buffer.BlockCopy(buffer, _extend.Length + 2, _mask, 0, 4);
            }

            //消息体
            if (_extend.Length == 0)
            {
                payload_len = _content.Length;
                _content    = new byte[_header.Length];
                lens        = _extend.Length + _mask.Length + 2;
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
            }
            else if (_extend.Length == 2)
            {
                int contentLength = (int)_extend[0] * 256 + (int)_extend[1];
                payload_len = contentLength;
                lens        = _extend.Length + _mask.Length + 2;
                _content    = new byte[contentLength];
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, contentLength > 1024 * 100 ? 1024 * 100 : contentLength);
            }
            else
            {
                long len = 0;
                int  n   = 1;
                for (int i = 7; i >= 0; i--)
                {
                    len += (int)_extend[i] * n;
                    n   *= 256;
                }
                payload_len = (int)len;
                _content    = new byte[len];
                lens        = _extend.Length + _mask.Length + 2;
                Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
            }

            if (_header.HasMask)
            {
                _content = Mask(_content, _mask);
            }
            return(_content);
        }