Exemplo n.º 1
0
        public bool Send(Socket socket, byte command, string text)
        {
            try
            {
                //byte[] sendb = System.Text.Encoding.UTF8.GetBytes(text);
                //byte[] lens = System.Text.Encoding.UTF8.GetBytes(sendb.Length.ToString());
                //byte[] b = new byte[2 + lens.Length + sendb.Length];
                //b[0] = command;
                //b[1] = (byte)lens.Length;
                //从指定的目标数组索引处开始,将当前一维数组的所有元素复制到指定的一维数组中 ,将lens所有元素复制到b中,从b的第2个元素开始
                //lens.CopyTo(b, 2);
                //将sendb所有元素复制到b中,从(2+lens长度)开始
                // sendb.CopyTo(b, 2 + lens.Length);

                //最终b的结构是[0] 是command的byte
                // [1]是text数据转换为byte后的长度length
                //[2]从2往后是实际的text数据转换为byte的数据

                byte[] b = WeaveServerHelper.CodingProtocol(command, text);

                int slen = 40960;  //40kb左右
                if (socketLisener.ProtocolType == ProtocolType.Udp)
                {
                    slen = 520;
                }

                int count = (b.Length <= slen ? b.Length / slen : (b.Length / slen) + 1);
                if (count == 0)
                {
                    socket.Send(b);
                }
                else
                {
                    //只有在slen 大于40959,约40kb 才会进入这个方法,实现数据分页发送
                    for (int i = 0; i < count; i++)
                    {
                        int    zz   = b.Length - (i * slen) > slen ? slen : b.Length - (i * slen);
                        byte[] temp = new byte[zz];
                        Array.Copy(b, i * slen, temp, 0, zz);
                        socket.Send(temp);
                        System.Threading.Thread.Sleep(1);
                    }
                }
            }
            catch { return(false); }
            // tcpc.Close();
            return(true);
        }
Exemplo n.º 2
0
        public bool Send(Socket socket, byte command, byte[] text)
        {
            try
            {
                int slen = 40960;
                if (socketLisener.ProtocolType == ProtocolType.Udp)
                {
                    slen = 520;
                }

                //byte[] sendb = text;
                //byte[] lens = WeaveServerHelper.ConvertToByteList(sendb.Length);
                //byte[] b = new byte[2 + lens.Length + sendb.Length];
                //b[0] = command;
                //b[1] = (byte)lens.Length;
                //lens.CopyTo(b, 2);
                //sendb.CopyTo(b, 2 + lens.Length);
                //最终b的结构是[0] 是command的byte
                // [1]是text数据转换为byte后的长度length
                //[2]从2往后是实际的text数据转换为byte的数据
                byte[] b = WeaveServerHelper.CodingProtocol(command, text);

                int count = (b.Length <= slen ? b.Length / slen : (b.Length / slen) + 1);
                if (count == 0)
                {
                    socket.Send(b);
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        int    zz   = b.Length - (i * slen) > slen ? slen : b.Length - (i * slen);
                        byte[] temp = new byte[zz];
                        Array.Copy(b, i * slen, temp, 0, zz);
                        socket.Send(temp);
                        System.Threading.Thread.Sleep(1);
                    }
                }
            }
            catch { return(false); }
            // tcpc.Close();
            return(true);
        }
Exemplo n.º 3
0
        public bool send(int index, byte command, string text)
        {
            try
            {
                Socket socket = weaveNetworkItems[index].SocketSession;
                //byte[] sendb = Encoding.UTF8.GetBytes(text);
                //byte[] lens = Encoding.UTF8.GetBytes(sendb.Length.ToString());
                //byte[] b = new byte[2 + lens.Length + sendb.Length];
                //b[0] = command;
                //b[1] = (byte)lens.Length;
                //lens.CopyTo(b, 2);
                //sendb.CopyTo(b, 2 + lens.Length);
                byte[] b = WeaveServerHelper.CodingProtocol(command, text);

                socket.Send(b);
            }
            catch { return(false); }
            // tcpc.Close();
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        private void packageData(object obj)
        {
            WeaveNetWorkItems netc     = obj as WeaveNetWorkItems;
            List <byte[]>     ListData = netc.DataList; //当前传入的网络连接的socket对应,其发送过来的数据

            try
            {
                int i     = 0;
                int count = ListData.Count;
                if (count > 0)
                {
                    //ListData[0] 发过来数据的Length长度,,如果为0
                    int bytesRead = ListData[i] != null ? ListData[i].Length : 0;
                    if (bytesRead == 0)
                    {
                        if (ListData.Count > 0)
                        {
                            ListData.RemoveAt(0);
                        }

                        netc.IsPage = false;
                        return;
                    }
                    ;
                    byte[] tempbtye = new byte[bytesRead];
                    Array.Copy(ListData[i], tempbtye, tempbtye.Length);
                    if (bytesRead > 2)
                    {
                        //int a = tempbtye[1];
                        int part3_Length = tempbtye[1];
                        if (bytesRead > 2 + part3_Length)
                        {
                            //说明 第四段有数据,,不然不会大于【 命令+ 长度+第三段】
                            //int len = 0;
                            int part4_Length = 0; //第四段数据的长度,,【既是第三段的内容】
                            if (weaveDataType == WeaveDataTypeEnum.Bytes)
                            {
                                byte[] part3_byte = new byte[part3_Length];
                                Array.Copy(tempbtye, 2, part3_byte, 0, part3_Length);
                                //第三段 数据表示的是 第四段数据的长度,,所以这里。。
                                part4_Length = WeaveServerHelper.ConvertToInt(part3_byte);
                            }
                            else
                            {  //Json 类型的话
                                String part3_string = System.Text.Encoding.UTF8.GetString(tempbtye, 2, part3_Length);
                                part4_Length = int.Parse(part3_string);
                            }
                            if ((part4_Length + 2 + part3_Length) > tempbtye.Length)
                            {    //说明数据发送发生错误,,断链了???
                                try
                                {
                                    if (ListData.Count > 1)
                                    {
                                        ListData.RemoveAt(i);  //删除收到的第一个包
                                        byte[] temps = new byte[tempbtye.Length];
                                        Array.Copy(tempbtye, temps, temps.Length);
                                        //第一个包全部数据,先复制到temps数组里
                                        byte[] tempbtyes = new byte[temps.Length + ListData[i].Length];
                                        Array.Copy(temps, tempbtyes, temps.Length);
                                        //再把后面的一个数据包,,接到tempbtyes数组后面
                                        Array.Copy(ListData[i], 0, tempbtyes, temps.Length, ListData[i].Length);
                                        //再把后面的一个数据包,,接到tempbtyes数组后面
                                        ListData[i] = tempbtyes;
                                    }
                                }
                                catch
                                {
                                }
                                netc.IsPage = false;
                                return;
                            }
                            else if (tempbtye.Length > (part4_Length + 2 + part3_Length))
                            {
                                try
                                {
                                    byte[] temps = new byte[tempbtye.Length - (part4_Length + 2 + part3_Length)];
                                    Array.Copy(tempbtye, (part4_Length + 2 + part3_Length), temps, 0, temps.Length);
                                    ListData[i] = temps;
                                }
                                catch
                                { }
                                // netc.Ispage = false; return;
                            }
                            else if (tempbtye.Length == (part4_Length + 2 + part3_Length))
                            {    //正好匹配数据包格式
                                if (ListData.Count > 0)
                                {
                                    ListData.RemoveAt(i);
                                }
                            }
                            try
                            {
                                if (weaveDataType == WeaveDataTypeEnum.Json)
                                {
                                    //第四部分的数据......
                                    String     part4_Data = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + part3_Length, part4_Length);
                                    WeaveEvent me         = new WeaveEvent();
                                    me.Command = tempbtye[0];
                                    me.Data    = part4_Data;
                                    me.Soc     = netc.SocketSession;
                                    if (waveReceiveEvent != null)
                                    {
                                        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveEventHander), me);
                                    }
                                    //receiveeventto(me);
                                    //if (receiveevent != null)
                                    //    receiveevent.BeginInvoke(tempbtye[0], temp, netc.Soc, null, null);
                                    //if (ListData.Count > 0) ListData.RemoveAt(i);
                                }
                                else if (weaveDataType == WeaveDataTypeEnum.Bytes)
                                {
                                    //  temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len);
                                    byte[] part4_byteData = new byte[part4_Length];
                                    Array.Copy(tempbtye, (2 + part3_Length), part4_byteData, 0, part4_byteData.Length);
                                    //获得第四段数据的byte实际数据
                                    WeaveEvent me = new WeaveEvent();
                                    me.Command = tempbtye[0];
                                    me.Data    = "";
                                    me.Databit = part4_byteData;
                                    me.Soc     = netc.SocketSession;
                                    if (weaveReceiveBitEvent != null)
                                    {
                                        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveBitEventHander), me);
                                    }
                                }
                                netc.IsPage = false;
                                return;
                            }
                            catch (Exception ex)
                            {
                                string ems = ex.Message;
                                netc.IsPage = false;
                                return;
                            }
                        }
                        else
                        {   //bytesRead > 2 + part3_Length  不成立,,
                            // 说明发生了,,断链,断包情况....
                            if (ListData.Count > 0)
                            {
                                ListData.RemoveAt(i);
                                //先把第一个数据删除,,,
                                byte[] temps = new byte[tempbtye.Length];
                                Array.Copy(tempbtye, temps, temps.Length);
                                //创建一个扩充长度的数据,加上  第二个包的数据长度
                                byte[] tempbtyes = new byte[temps.Length + ListData[i].Length];
                                //复制temps数组数据到tempbtyes里面
                                Array.Copy(temps, tempbtyes, temps.Length);
                                //把第二个包的数据扩充到,tempbtyes后面(从刚才后面的位置开始)
                                Array.Copy(ListData[i], 0, tempbtyes, temps.Length, ListData[i].Length);
                                //更新--当前数据为 第一个包数据
                                ListData[i] = tempbtyes;
                            }
                            netc.IsPage = false;
                            return;
                        }
                    }
                    else
                    {  // 收到的第一个包数据ListData[0].Length =1 或 2
                        try
                        {
                            if (ListData.Count > 1)
                            {
                                ListData.RemoveAt(i);
                                byte[] temps = new byte[tempbtye.Length];
                                Array.Copy(tempbtye, temps, temps.Length);
                                byte[] 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;
                            }
                        }
                        catch
                        {
                        }
                        netc.IsPage = false;
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                string ems = ex.Message;
                if (netc.DataList.Count > 0)
                {
                    netc.DataList.RemoveAt(0);
                }
                netc.IsPage = false;
                return;
            }
            finally {
                netc.IsPage = false;
            }
        }