Example #1
0
        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;
                    }
                }
            }
        }
Example #2
0
        String combine(string temp, byte[] tempbtyes, List <byte[]> ListData)
        {
            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 ex)
            {
                string ems = ex.Message;
            }
            return(temp);
        }
Example #3
0
 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);
 }
Example #4
0
        private void packageData(object obj)
        {
            WeaveNetWorkItems netc = obj as WeaveNetWorkItems;

            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)
                {
                    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;
                    }

                    if (tempbtye.Length > 0)
                    {
                        #region MyRegion
                        String temp = "";
                        int    a    = tempbtye[1];
                        if (bytesRead > 2 + a)
                        {
                            int len = 0;
                            if (DT == WeaveDataTypeEnum.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 == WeaveDataTypeEnum.Json)
                                {
                                    temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len);
                                }
                            }
                            else
                            {
                                len = tempbtye.Length - 2 - a;
                                if (DT == WeaveDataTypeEnum.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 == WeaveDataTypeEnum.Json)
                                {
                                    WeaveEvent me = new WeaveEvent();
                                    me.Command = tempbtye[0];
                                    me.Data    = temp;
                                    me.Soc     = netc.SocketSession;
                                    me.Masks   = masks;
                                    //System.Threading.Thread t = new Thread(new ParameterizedThreadStart(receiveeventto));
                                    //t.Start(me);
                                    //receiveeventto(me);
                                    if (waveReceiveEvent != null)
                                    {
                                        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveToEventHander), me);
                                    }
                                    //if (receiveevent != null)
                                    //    receiveevent(me.Command, me.Data, me.Soc);
                                }
                                else if (DT == WeaveDataTypeEnum.Bytes)
                                {
                                    byte[] bs = new byte[len - (2 + a)];
                                    Array.Copy(tempbtye, 2 + a, bs, 0, bs.Length);
                                    WeaveEvent me = new WeaveEvent();
                                    me.Command = tempbtye[0];
                                    me.Data    = "";
                                    me.Databit = bs;
                                    me.Soc     = netc.SocketSession;
                                    if (weaveReceiveBitEvent != null)
                                    {
                                        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveToBitEventHander), me);
                                    }
                                }
                                netc.IsPage = false; return;
                            }
                            catch (Exception ex)
                            {
                                string ems = ex.Message;
                                netc.IsPage = false; return;
                            }
                        }
                        #endregion
                    }
                }
                // }
            }
            catch (Exception ex)
            {
                string ems = ex.Message;
                if (netc.DataList.Count > 0)
                {
                    netc.DataList.RemoveAt(0);
                }
                netc.IsPage = false;
                return;
            }
        }