void Eventrec(object obj)
        {
            command cc = obj as command;

            ReceiveServerEvent?.Invoke(cc.comm, cc.str);
            ReceiveServerEventobj?.Invoke(cc.comm, cc.str, this);
        }
Beispiel #2
0
        void Unup()
        {
            while (isok)
            {
                Thread.Sleep(10);
                try
                {
                    int count = ListData.Count;
                    if (count > 0)
                    {
                        int bytesRead = ListData[0] != null ? ListData[0].Length : 0;
                        if (bytesRead == 0)
                        {
                            continue;
                        }
                        byte[] tempbtye = new byte[bytesRead];
                        Array.Copy(ListData[0], tempbtye, tempbtye.Length);

                        if (bytesRead > 2)
                        {
                            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
                                {
                                    string temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a);
                                    len = 0;
                                    try
                                    {
                                        len = int.Parse(temp);
                                        if (len == 0)
                                        {
                                            ListData.RemoveAt(0); continue;
                                        }
                                    }
                                    catch
                                    { }
                                }

                                try
                                {
                                    if ((len + 2 + a) > tempbtye.Length)
                                    {
                                        if (ListData.Count > 1)
                                        {
                                            ListData.RemoveAt(0);
                                            byte[] temps = new byte[ListData[0].Length];
                                            Array.Copy(ListData[0], temps, temps.Length);
                                            byte[] temps2 = new byte[tempbtye.Length + temps.Length];
                                            Array.Copy(tempbtye, 0, temps2, 0, tempbtye.Length);
                                            Array.Copy(temps, 0, temps2, tempbtye.Length, temps.Length);
                                            ListData[0] = temps2;
                                        }
                                        else
                                        {
                                            Thread.Sleep(20);
                                        }
                                        continue;
                                    }
                                    else if (tempbtye.Length > (len + 2 + a))
                                    {
                                        byte[] temps = new byte[tempbtye.Length - (len + 2 + a)];
                                        Array.Copy(tempbtye, (len + 2 + a), temps, 0, temps.Length);
                                        ListData[0] = temps;
                                    }
                                    else if (tempbtye.Length == (len + 2 + a))
                                    {
                                        ListData.RemoveAt(0);
                                    }
                                }
                                catch (Exception e)
                                {
                                    ErrorMge?.Invoke(3, e.StackTrace + "unup001:" + e.Message + "2 + a" + 2 + a + "---len" + len + "--tempbtye" + tempbtye.Length);
                                }
                                try
                                {
                                    if (len == 0)
                                    {
                                        ListData.RemoveAt(0);
                                        return;
                                    }
                                    if (DT == DataType.json)
                                    {
                                        string   temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len);
                                        Temppake str  = new Temppake
                                        {
                                            command = tempbtye[0],
                                            date    = temp
                                        };
                                        if (tempbtye[0] == 0xff)
                                        {
                                            if (temp.IndexOf("token") >= 0)
                                            {
                                                Tokan = temp.Split('|')[1];
                                            }
                                            else if (temp.IndexOf("jump") >= 0)
                                            {
                                                Tokan = "连接数量满";
                                                JumpServerEvent(temp.Split('|')[1]);
                                            }
                                            else
                                            {
                                                ThreadPool.QueueUserWorkItem(new WaitCallback(Rec), str);
                                            }
                                        }
                                        else if (ReceiveServerEvent != null)
                                        {
                                            ThreadPool.QueueUserWorkItem(new WaitCallback(Rec), str);
                                        }
                                    }
                                    if (DT == DataType.bytes)
                                    {
                                        byte[] bs = new byte[len - 2 + a];
                                        Array.Copy(tempbtye, bs, bs.Length);
                                        Temppake str = new Temppake
                                        {
                                            command = tempbtye[0],
                                            datebit = bs
                                        };
                                        ReceiveServerEvent.BeginInvoke(str.command, str.date, null, null);
                                    }
                                    continue;
                                }
                                catch (Exception e)
                                {
                                    ErrorMge?.Invoke(3, e.StackTrace + "unup122:" + e.Message);
                                }
                            }
                        }
                        else
                        {
                            ListData.RemoveAt(0);
                        }
                    }
                }
                catch (Exception e)
                {
                    ErrorMge?.Invoke(3, "unup:" + e.Message + "---" + e.StackTrace);
                    try
                    {
                        ListData.RemoveAt(0);
                    }
                    catch { }
                }
            }
        }
        void Unupjson()
        {
            try
            {
                {
                    int bytesRead = alldata.Length;

                    if (bytesRead == 0)
                    {
                        return;
                    }


                    byte[] tempbtye = new byte[bytesRead];

                    Array.Copy(alldata, tempbtye, tempbtye.Length);
                    //if (tempbtye[0] == 0x99)
                    //{
                    //    timeout = DateTime.Now;
                    //    if (tempbtye.Length > 1)
                    //    {
                    //        byte[] b = new byte[bytesRead - 1];
                    //        try
                    //        {
                    //            Array.Copy(tempbtye, 1, b, 0, b.Length);
                    //        }
                    //        catch { }
                    //        alldata = b;
                    //        return;
                    //    }
                    //}

                    if (bytesRead > 2)
                    {
                        int a = tempbtye[1];
                        if (bytesRead > 2 + a)
                        {
                            int len = 0;

                            string temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a);
                            len = 0;
                            try
                            {
                                len = int.Parse(temp);
                                if (len == 0)
                                {
                                    alldata = new byte[0]; return;
                                }
                            }
                            catch
                            {
                                byte[] temps = new byte[tempbtye.Length - 1];
                                Array.Copy(tempbtye, 1, temps, 0, temps.Length);
                                alldata = temps;
                                return;
                            }

                            try
                            {
                                if ((len + 2 + a) > tempbtye.Length)
                                {
                                    return;
                                }
                                else if (tempbtye.Length > (len + 2 + a))
                                {
                                    byte[] temps = new byte[tempbtye.Length - (len + 2 + a)];
                                    Array.Copy(tempbtye, (len + 2 + a), temps, 0, temps.Length);
                                    alldata = temps;
                                    //  goto lb0x99;
                                }
                                else if (tempbtye.Length == (len + 2 + a))
                                {
                                    alldata = new byte[0];
                                }
                            }
                            catch (Exception e)
                            {
                                ErrorMge?.Invoke(3, e.StackTrace + "unup001:" + e.Message + "2 + a" + 2 + a + "---len" + len + "--tempbtye" + tempbtye.Length);
                                alldata = new byte[0];
                            }
                            try
                            {
                                temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len);

                                if (tempbtye[0] == 0xff)
                                {
                                    if (temp.IndexOf("token") >= 0)
                                    {
                                        Tokan = temp.Split('|')[1];
                                    }
                                    else if (temp.IndexOf("jump") >= 0)
                                    {
                                        Tokan = "连接数量满";
                                        JumpServerEvent(temp.Split('|')[1]);
                                    }
                                    else
                                    {
                                        //ReceiveServerEvent?.Invoke(tempbtye[0], temp);

                                        ReceiveServerEvent?.BeginInvoke(tempbtye[0], temp, null, null);
                                        ReceiveServerEventobj?.BeginInvoke(tempbtye[0], temp, this, null, null);
                                    }
                                }
                                else if (tempbtye[0] == 0x99)
                                {
                                    return;
                                }
                                else if (ReceiveServerEvent != null || ReceiveServerEventobj != null)
                                {
                                    command cc = new command();
                                    cc.comm = tempbtye[0];
                                    cc.str  = temp;
                                    System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(Eventrec), cc);
                                    //ReceiveServerEvent?.BeginInvoke(tempbtye[0], temp, null, null);
                                    //ReceiveServerEventobj?.BeginInvoke(tempbtye[0], temp, this, null, null);
                                }
                            }
                            catch (Exception e)
                            {
                                ErrorMge?.Invoke(3, e.StackTrace + "unup122:" + e.Message);
                                alldata = new byte[0];
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorMge?.Invoke(3, "unup:" + e.Message + "---" + e.StackTrace);
                alldata = new byte[0];
            }
        }