Ejemplo n.º 1
0
        public bool Start(string ip, int port, bool takon)
        {
            try
            {
                acallsend = new AsyncCallback(SendDataEnd);
                if (DT == DataType.json && (ReceiveServerEvent == null && ReceiveServerEventobj == null))
                {
                    throw new Exception("没有注册receiveServerEvent事件");
                }
                if (DT == DataType.bytes && (ReceiveServerEventbit == null && ReceiveServerEventbitobj == null))
                {
                    throw new Exception("没有注册receiveServerEventbit事件");
                }
                if (DT == DataType.custom && (ReceiveServerEventbit == null && ReceiveServerEventbitobj == null))
                {
                    throw new Exception("没有注册receiveeventbit事件");
                }
                IP   = ip;
                PORT = port;
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
                tcpc = new TcpClient
                {
                    // ExclusiveAddressUse = false,
                    ReceiveBufferSize = int.MaxValue
                };
                tcpc.Connect(ip, port);
                localprot = ((IPEndPoint)tcpc.Client.LocalEndPoint).Port.ToString();
                Isline    = true;
                isok      = true;

                timeout = DateTime.Now;
                if (!isreceives)
                {
                    isreceives = true;
                    Thread t = new Thread(new ParameterizedThreadStart(Receives));
                    t.Start();
                }
                int ss = 0;
                if (!takon)
                {
                    return(true);
                }
                while (Tokan == null)
                {
                    Thread.Sleep(1000);
                    ss++;
                    if (ss > 10)
                    {
                        return(false);
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                Isline = false;
                ErrorMge?.Invoke(1, e.Message);
                return(false);
            }
        }
Ejemplo n.º 2
0
        public bool Start(string ip, int port, bool takon)
        {
            try
            {
                if (DT == DataType.json && ReceiveServerEvent == null)
                {
                    throw new Exception("没有注册receiveServerEvent事件");
                }
                if (DT == DataType.bytes && ReceiveServerEventbit == null)
                {
                    throw new Exception("没有注册receiveServerEventbit事件");
                }
                IP   = ip;
                PORT = port;
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
                tcpc = new UdpClient
                {
                    ExclusiveAddressUse = false
                };

                Isline  = true;
                isok    = true;
                timeout = DateTime.Now;
                if (!isreceives)
                {
                    isreceives = true;
                    Thread t = new Thread(new ParameterizedThreadStart(Receives));
                    t.Start();
                    Thread t1 = new Thread(new ThreadStart(Unup));
                    t1.Start();
                    Thread t2 = new Thread(new ThreadStart(KeepAliveHander));
                    t2.Start();
                }
                int ss = 0;
                if (!takon)
                {
                    return(true);
                }
                while (Tokan == null)
                {
                    Thread.Sleep(1000);
                    ss++;
                    if (ss > 10)
                    {
                        return(false);
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                Isline = false;
                ErrorMge?.Invoke(1, e.Message);
                return(false);
            }
        }
Ejemplo n.º 3
0
 void Unup()
 {
     while (isok)
     {
         Thread.Sleep(10);
         try
         {
             int i     = 0;
             int count = ListData.Count;
             if (count > 0)
             {
                 int bytesRead = ListData[i] != null ? ListData[i].Length : 0;
                 if (bytesRead == 0)
                 {
                     continue;
                 }
                 byte[] tempbtye = new byte[bytesRead];
                 Array.Copy(ListData[i], tempbtye, tempbtye.Length);
                 try
                 {
                     ListData.RemoveAt(0);
                     Temppake str = new Temppake
                     {
                         command = Tokan,
                         date    = tempbtye
                     };
                     ReceiveServerEvent(Tokan, str.date);
                     continue;
                 }
                 catch (Exception e)
                 {
                     ErrorMge?.Invoke(3, "unup:" + e.Message);
                 }
             }
         }
         catch (Exception e)
         {
             ErrorMge?.Invoke(3, "unup:" + e.Message);
         }
     }
 }
Ejemplo n.º 4
0
 void Receives(object obj)
 {
     while (isok)
     {
         Thread.Sleep(150);
         try
         {
             int bytesRead = tcpc.Client.Available;
             if (bytesRead > 0)
             {
                 byte[] tempbtye = new byte[bytesRead];
                 tcpc.Client.Receive(tempbtye);
                 ListData.Add(tempbtye);
                 timeout = DateTime.Now;
             }
         }
         catch (Exception e)
         {
             ErrorMge?.Invoke(2, e.Message);
         }
     }
 }
Ejemplo n.º 5
0
        void Receives(object obj)
        {
            while (isok)
            {
                Thread.Sleep(50);
                try
                {
                    int bytesRead = tcpc.Available;
                    if (bytesRead > 0)
                    {
                        byte[] tempbtye = new byte[bytesRead];
                        try
                        {
                            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                            EndPoint   Remote = sender;
                            timeout = DateTime.Now;

                            tcpc.Client.ReceiveFrom(tempbtye, ref Remote);

_0x99:
                            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 { }
                                    tempbtye = b;
                                    goto _0x99;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                        catch (Exception ee)
                        {
                            ErrorMge(22, ee.Message);
                        }
                        ListData.Add(tempbtye);
                    }
                    else
                    {
                        try
                        {
                            TimeSpan ts = DateTime.Now - timeout;
                            if (ts.TotalSeconds > mytimeout)
                            {
                                Isline = false;
                                Stop();
                                Timeoutevent?.Invoke();
                                Timeoutobjevent?.Invoke(this);
                                ErrorMge?.Invoke(2, "连接超时,未收到服务器指令");
                                continue;
                            }
                        }
                        catch (Exception ee)
                        {
                            ErrorMge(21, ee.Message);
                        }
                    }
                }
                catch (Exception e)
                {
                    ErrorMge?.Invoke(2, e.Message);
                }
            }
        }
Ejemplo n.º 6
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 { }
                }
            }
        }
Ejemplo n.º 7
0
 private void Xmhelper_errorMessageEvent(Socket soc, WeaveSession _0x01, string message)
 {
     ErrorMge?.Invoke(0, message);
 }
Ejemplo n.º 8
0
        void Receives(object obj)
        {
            //  var w = new SpinWait();
            while (isok)
            {
                if (ReceivesSpeedMode != WeaveReceivesSpeedMode.high)
                {
                    //sleep(10)
                    System.Threading.Thread.Sleep((int)ReceivesSpeedMode);
                }
                try
                {
                    if (tcpc.Client == null)
                    {
                        continue;
                    }
                    int bytesRead = tcpc.Client.Available;
                    if (bytesRead > 0)
                    {
                        byte[] tempbtye = new byte[bytesRead];
                        try
                        {
                            timeout = DateTime.Now;

                            tcpc.Client.Receive(tempbtye);
                            tempp = new byte[alldata.Length];
                            alldata.CopyTo(tempp, 0);
                            int lle = alldata.Length;
                            bytesRead = tempbtye.Length;
                            byte[] temp = new byte[lle + bytesRead];
                            Array.Copy(alldata, 0, temp, 0, lle);
                            Array.Copy(tempbtye, 0, temp, lle, bytesRead);
                            alldata = temp;
                        }
                        catch (Exception ee)
                        {
                            ErrorMge(22, ee.Message);
                        }
                    }


                    if (alldata.Length > 3)
                    {
                        Unup();
                    }
                    else
                    {
                        if (tcpc.Client.Available == 0)
                        {
                            if (resttime > 0)
                            {
                                System.Threading.Thread.Sleep(1);
                            }
                            else
                            {
                                System.Threading.Thread.Yield();
                            }
                        }
                    }

                    try
                    {
                        TimeSpan ts = DateTime.Now - timeout;
                        if (ts.TotalSeconds > mytimeout)
                        {
                            Isline = false;
                            Stop();
                            Timeoutevent?.Invoke();
                            Timeoutobjevent?.Invoke(this);
                            ErrorMge?.Invoke(2, "连接超时,未收到服务器指令");
                            continue;
                        }
                    }
                    catch (Exception ee)
                    {
                        ErrorMge(21, ee.Message);
                    }
                }
                catch (Exception e)
                {
                    ErrorMge?.Invoke(2, e.Message);
                }
            }
        }
Ejemplo n.º 9
0
        void Unupbyte()
        {
            try
            {
                {
lb0x99:
                    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;
                    //        goto lb0x99;
                    //    }
                    //}

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

                            byte[] bbcrc = new byte[4 + a];
                            Array.Copy(tempbtye, 0, bbcrc, 0, 4 + a);
                            if (CRC.DataCRC(ref bbcrc, 4 + a))
                            {
                                byte[] bb = new byte[a];
                                Array.Copy(tempbtye, 2, bb, 0, a);
                                len = ConvertToInt(bb);
                            }
                            else
                            {
                                byte[] temps = new byte[tempbtye.Length - 1];
                                Array.Copy(tempbtye, 1, temps, 0, temps.Length);
                                alldata = temps;
                                //return;
                                goto lb0x99;
                            }
                            try
                            {
                                if ((len + 4 + a) > tempbtye.Length)
                                {
                                    return;
                                }
                                else if (tempbtye.Length > (len + 4 + a))
                                {
                                    byte[] temps = new byte[tempbtye.Length - (len + 4 + a)];
                                    Array.Copy(tempbtye, (len + 4 + a), temps, 0, temps.Length);
                                    alldata = temps;
                                    //return;
                                    //  goto lb0x99;
                                }
                                else if (tempbtye.Length == (len + 4 + 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
                            {
                                byte[] bs = new byte[len];
                                Array.Copy(tempbtye, (4 + a), bs, 0, bs.Length);
                                if (tempbtye[0] == 0x99)
                                {
                                    return;
                                }

                                //ReceiveServerEventbit?.BeginInvoke(tempbtye[0], bs,null, null);
                                //ReceiveServerEventbitobj?.BeginInvoke(tempbtye[0], bs, this, null, null);
                                command cc = new command();
                                cc.comm = tempbtye[0];
                                cc.bs   = bs;
                                System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(Eventbit), cc);

                                return;
                            }
                            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];
            }
        }
Ejemplo n.º 10
0
        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];
            }
        }