Esempio n. 1
0
        private void ReadCallback2(IAsyncResult ar)
        {
            NETcollection netc    = (NETcollection)ar.AsyncState;
            Socket        handler = netc.Soc;

            //if (!netc.Soc.Poll(100, SelectMode.SelectRead))
            //{
            //    listconn.Remove(netc);
            //    return;
            //}
            // Read data from the client socket.
            try
            {
                int bytesRead = 0;
                try
                {
                    bytesRead = handler.EndReceive(ar);
                }
                catch
                {
                    netc.Soc.Close();
                    listconn.Remove(netc);
                }
                byte[] tempbtye = new byte[bytesRead];

                //netc.Buffer.CopyTo(tempbtye, 0);
                Array.Copy(netc.Buffer, 0, tempbtye, 0, bytesRead);
                handler.Send(PackHandShakeData(GetSecKeyAccetp(tempbtye, bytesRead)));
            }
            catch
            {
            }
            //handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback), netc);
        }
Esempio n. 2
0
        void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                allDone.Set();
                Socket listener = (Socket)ar.AsyncState;
                if (listener == null)
                {
                    return;
                }
                Socket handler = listener.EndAccept(ar);

                // Create the state object.
                NETcollection netc = new NETcollection();
                netc.Soc = handler;
                listconn.Add(netc);
                System.Threading.Thread.Sleep(10);
                handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback2), netc);
                System.Threading.Thread.Sleep(50);
                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(UpdataConnSoc), handler);

                // netc.Buffer
            }
            catch { }
            // allDone.Set();
        }
Esempio n. 3
0
        void getbuffer(NETcollection[] netlist, int index, int len)
        {
            for (int i = index; i < len; i++)
            {
                if (i >= netlist.Length)
                {
                    return;
                }

                try
                {
                    NETcollection netc = netlist[i];
                    if (netc.Soc != null)
                    {
                        if (netc.Soc.Available > 0)
                        {
                            System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(ReadCallback), netc);
                            //  ReadCallback(netc);

                            // netc.Soc.ReceiveAsync(netc.Buffer = new byte[netc.Soc.Available], 0, netc.Buffer.Length, 0, new AsyncCallback(ReadCallback), netc);
                        }
                    }
                }
                catch
                { }
            }
        }
Esempio n. 4
0
        public void xintiao(object obj)
        {
            while (true)
            {
                try
                {
                    System.Threading.Thread.Sleep(8000);
                    //  ArrayList al = new ArrayList();
                    // al.Clone()
                    NETcollection[] netlist = new NETcollection[listconn.Count];
                    listconn.CopyTo(netlist);
                    foreach (NETcollection netc in netlist)
                    {
                        try
                        {
                            byte[] b = new byte[] { 0x99 };

                            netc.Soc.Send(b);
                        }
                        catch
                        {
                            try { netc.Soc.Close(); }
                            catch { }
                            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(DeleteConnSoc), netc.Soc);


                            listconn.Remove(netc);
                        }
                    }
                }
                catch { }
            }
        }
Esempio n. 5
0
        private void ReadCallback3(object ar)
        {
            NETcollection netc    = (NETcollection)ar;
            Socket        handler = netc.Soc;

            //if (!netc.Soc.Poll(100, SelectMode.SelectRead))
            //{
            //    listconn.Remove(netc);
            //    return;
            //}
            // Read data from the client socket.
            try
            {
                handler.Receive(netc.Buffer = new byte[netc.Soc.Available]);
                //if (bytesRead > 0)
                //{
                //    // There  might be more data, so store the data received so far.
                byte[] tempbtye = new byte[netc.Buffer.Length];

                //netc.Buffer.CopyTo(tempbtye, 0);
                Array.Copy(netc.Buffer, 0, tempbtye, 0, tempbtye.Length);
                netc.Datalist.Add(tempbtye);
                //  System.Threading.Thread.Sleep(10);
            }
            catch
            {
            }
            //handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback), netc);
        }
Esempio n. 6
0
        void Accept(object ias)
        {
            while (true)
            {
                try
                {
                    Socket handler = listener.Accept();
                    //if (listener == null)
                    //    return;
                    //Socket handler = listener.EndAccept(ar);
                    //listener.BeginAccept(new AsyncCallback(Accept), listener);
                    // Create the state object.

                    NETcollection netc = new NETcollection();
                    netc.Soc   = handler;
                    netc.State = 0;
                    // listconn.Add(netc);
                    connlist.Add(netc);
                    // new receiveconndele(setherd2).BeginInvoke(handler, null, null);
                    // System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(setherd2), handler);
                    //handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback2), netc);
                }
                catch (Exception ex)
                {
                }
            }
        }
Esempio n. 7
0
 void receivepage(object ias)
 {
     while (true)
     {
         try
         {
             NETcollection[] netlist = new NETcollection[listconn.Count];
             listconn.CopyTo(netlist);
             foreach (NETcollection netc in netlist)
             {
                 if (netc.Datalist.Count > 0)
                 {
                     if (!netc.Ispage)
                     {
                         netc.Ispage = true;
                         System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(packageData), netc);
                         //System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(packageData));
                         //t.Start(netc);
                         //Webp2psever.packageDataHandler pdh = new Webp2psever.packageDataHandler(packageData);
                         //pdh.BeginInvoke(netc, endpackageData, null);
                     }
                 }
             }
             System.Threading.Thread.Sleep(10);
         }
         catch { }
     }
 }
Esempio n. 8
0
        void receive(object ias)
        {
            while (true)
            {
                try
                {
                    int c     = listconn.Count;
                    int count = (c / Partition) + 1;
                    getbufferdelegate[] iagbd = new getbufferdelegate[count];
                    IAsyncResult[]      ia    = new IAsyncResult[count];

                    if (c > 0)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            c = c - (i * Partition) > Partition ? Partition : c - (i * Partition);
                            NETcollection[] netlist = new NETcollection[c];
                            listconn.CopyTo(i * Partition, netlist, 0, c);

                            iagbd[i] = new getbufferdelegate(getbuffer);
                            ia[i]    = iagbd[i].BeginInvoke(netlist, 0, Partition, null, null);
                        }
                        for (int i = 0; i < count; i++)
                        {
                            iagbd[i].EndInvoke(ia[i]);
                        }
                    }
                    //NETcollection[] netlist = new NETcollection[c];
                    //listconn.CopyTo(0, netlist, 0, c);
                    //getbuffer(netlist, 0, c);
                }
                catch { }
                System.Threading.Thread.Sleep(1);
            }
        }
Esempio n. 9
0
        void Accept(object ias)
        {
            while (true)
            {
                Socket handler = listener.Accept();
                //if (listener == null)
                //    return;
                //Socket handler = listener.EndAccept(ar);

                // Create the state object.
                NETcollection netc = new NETcollection();
                netc.Soc = handler;
                listconn.Add(netc);
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(UpdataConnSoc));
                t.Start(handler);
                //System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(UpdataConnSoc), handler);

                // Set the event to nonsignaled state.
                //allDone.Reset();

                ////开启异步监听socket
                ////    Console.WriteLine("Waiting for a connection");
                //try
                //{
                //    listener.BeginAccept(
                //              new AsyncCallback(AcceptCallback),
                //              listener);
                System.Threading.Thread.Sleep(1);
                //}
                //catch { }
                //// 让程序等待,直到连接任务完成。在AcceptCallback里的适当位置放置allDone.Set()语句.
                //allDone.WaitOne();
            }
        }
Esempio n. 10
0
 void getbuffer(NETcollection[] netlist, int index, int len, int state, int num)
 {
     for (int i = index; i < len; i++)
     {
         NETcollection netc = netlist[i];
         try
         {
             if (netc.Soc != null)
             {
                 if (netc.Soc.Available > 0)
                 {
                     if (netc.State == state)
                     {
                         if (netc.Soc.Available > num)
                         {
                             if (state == 0)
                             {
                                 netc.Soc.BeginReceive(netc.Buffer = new byte[netc.Soc.Available], 0, netc.Buffer.Length, 0, new AsyncCallback(ReadCallback2), netc);
                             }
                             if (state == 1)
                             {
                                 System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReadCallback3), netc);
                             }
                             // netc.Soc.BeginReceive(netc.Buffer = new byte[netc.Soc.Available], 0, netc.Buffer.Length, 0, new AsyncCallback(ReadCallback), netc);
                             // listconn.Find(p=>p==netc).State = 1;
                         }
                     }
                 }
             }
         }
         catch
         { }
     }
 }
Esempio n. 11
0
        public void xintiao(object obj)
        {
            while (true)
            {
                try
                {
                    //  ArrayList al = new ArrayList();
                    // al.Clone()
                    NETcollection[] netlist = new NETcollection[listconn.Count];
                    listconn.CopyTo(netlist);
                    foreach (NETcollection netc in netlist)
                    {
                        if (netc == null)
                        {
                            continue;
                        }
                        System.Threading.Thread.Sleep(1);
                        try
                        {
                            byte[] b = new byte[] { 0x99 };

                            netc.Soc.Send(b);

                            netc.Errornum = 0;
                        }
                        catch
                        {
                            netc.Errornum += 1;
                            if (netc.Errornum > 3)
                            {
                                try
                                {
                                    netc.Soc.Dispose();
                                }
                                catch { }
                                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(DeleteConnSoc), netc.Soc);
                                listconn.Remove(netc);
                            }
                            //try
                            //{
                            //    if (EventDeleteConnSoc != null)
                            //        EventDeleteConnSoc.BeginInvoke(netc.Soc, null, null);
                            //}
                            //catch { }
                        }
                    }
                    System.Threading.Thread.Sleep(5000);
                    // GC.Collect();
                }
                catch { }
            }
        }
Esempio n. 12
0
        void receive(object ias)
        {
            while (true)
            {
                try
                {
                    //int c = listconn.Count;
                    //int count = (c / Partition) + 1;
                    //getbufferdelegate[] iagbd = new getbufferdelegate[count];
                    //IAsyncResult[] ia = new IAsyncResult[count];

                    //if (c > 0)
                    //{
                    //    for (int i = 0; i < count; i++)
                    //    {

                    //        c = c - (i * Partition) > Partition ? Partition : c - (i * Partition);
                    //        NETcollection[] netlist = new NETcollection[c];
                    //        listconn.CopyTo(i * Partition, netlist, 0, c);

                    //        iagbd[i] = new getbufferdelegate(getbuffer);
                    //        ia[i] = iagbd[i].BeginInvoke(netlist, 0, Partition,1,30, null, null);


                    //    }
                    //    for (int i = 0; i < count; i++)
                    //    {
                    //        iagbd[i].EndInvoke(ia[i]);
                    //    }
                    //}
                    int c = listconn.Count;
                    //int count = (c / 2000) + 1;
                    //if (c > 0)
                    //    for (int i = 0; i < count; i++)
                    //    {

                    //        c = c - (i * 2000) > 2000 ? 2000 : c - (i * 2000);
                    //        NETcollection[] netlist2 = new NETcollection[c];
                    //        listconn.CopyTo(i * 2000, netlist2, 0, c);
                    //        System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(gg), netlist2);
                    //        //new getbufferdelegate(getbuffer).BeginInvoke(netlist, 0, 2000,null,null);
                    //    }
                    NETcollection[] netlist = new NETcollection[c];
                    listconn.CopyTo(0, netlist, 0, c);
                    getbuffer(netlist, 0, c, 1, 30);
                    System.Threading.Thread.Sleep(1);
                }
                catch (Exception ex)
                { }
            }
        }
Esempio n. 13
0
        public void xintiao(object obj)
        {
            while (true)
            {
                try
                {
                    //  ArrayList al = new ArrayList();
                    // al.Clone()
                    NETcollection[] netlist = new NETcollection[listconn.Count];
                    listconn.CopyTo(netlist);
                    foreach (NETcollection netc in netlist)
                    {
                        try
                        {
                            if (netc == null)
                            {
                                continue;
                            }
                            if (netc.State != 0)
                            {
                                DataFrame df = new DataFrame();
                                df.setByte(new byte[] { 0x99 });


                                netc.Soc.Send(df.GetBytes());
                                netc.Errornum = 0;
                            }
                        }
                        catch
                        {
                            netc.Errornum += 1;
                            if (netc.Errornum > 3)
                            {
                                try { netc.Soc.Dispose(); }
                                catch { }
                                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(DeleteConnSoc), netc.Soc);
                                // EventDeleteConnSoc.BeginInvoke(netc.Soc, null, null);

                                listconn.Remove(netc);
                            }
                        }
                    }
                    System.Threading.Thread.Sleep(8000);
                }
                catch { }
            }
        }
Esempio n. 14
0
        private void ReadCallback2(object ar)
        {
            NETcollection netc    = (NETcollection)ar;
            Socket        handler = netc.Soc;
            //if (!netc.Soc.Poll(100, SelectMode.SelectRead))
            //{
            //    listconn.Remove(netc);
            //    return;
            //}
            // Read data from the client socket.
            int bytesRead = 0;

            try
            {
                try
                {
                    bytesRead = handler.Available;
                }
                catch
                {
                    netc.Soc.Dispose();
                    listconn.Remove(netc);
                }

                byte[] tempbtye = new byte[bytesRead];
                handler.Receive(tempbtye);
                //   System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback())
                netc.State = 1;
                //  System.Threading.Thread.Sleep(50);
                sendheaddele sh = new sendheaddele(sendhead);
                IAsyncResult ia = sh.BeginInvoke(handler, tempbtye, null, null);
                sh.EndInvoke(ia);
                //if (EventUpdataConnSoc != null)
                //    EventUpdataConnSoc.BeginInvoke(handler, null, null);
                System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(UpdataConnSoc), handler);
                return;
                //System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(UpdataConnSoc));
                //t.Start(handler);
                // handler.BeginSend(aaa, 0, aaa.Length, 0, HandshakeFinished, handler);
            }
            catch (Exception e)
            {
            }
            //handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback), netc);
        }
Esempio n. 15
0
        void setherd(object obj)
        {
            try
            {
                NETcollection netc = obj as NETcollection;

                sendhead(netc.Soc, netc.Buffer);
                //  System.Threading.Thread.Sleep(50);
                // new sendheaddele(sendhead).BeginInvoke(netc.Soc, netc.Buffer, null, null);

                netc.State = 1;
                if (EventUpdataConnSoc != null)
                {
                    EventUpdataConnSoc(netc.Soc);
                }
                // EventUpdataConnSoc.BeginInvoke(netc.Soc, null, null);
            }
            catch { }
        }
Esempio n. 16
0
 void getbuffer(NETcollection[] netlist, int index, int len)
 {
     for (int i = index; i < len; i++)
     {
         NETcollection netc = netlist[i];
         try
         {
             if (netc.Soc != null)
             {
                 if (netc.Soc.Available > 0)
                 {
                     netc.Soc.BeginReceive(netc.Buffer = new byte[netc.Soc.Available], 0, netc.Buffer.Length, 0, new AsyncCallback(ReadCallback), netc);
                 }
             }
         }
         catch
         { }
     }
 }
Esempio n. 17
0
 void receive(object ias)
 {
     while (true)
     {
         try
         {
             NETcollection[] netlist = new NETcollection[listconn.Count];
             listconn.CopyTo(netlist);
             foreach (NETcollection netc in netlist)
             {
                 if (netc.Soc.Available > 0)
                 {
                     netc.Buffer = new byte[netc.Soc.Available];
                     netc.Soc.BeginReceive(netc.Buffer, 0, netc.Soc.Available, 0, new AsyncCallback(ReadCallback), netc);
                 }
             }
         }
         catch { }
         System.Threading.Thread.Sleep(100);
     }
 }
Esempio n. 18
0
        private void ReadCallback(IAsyncResult ar)
        {
            NETcollection netc    = (NETcollection)ar.AsyncState;
            Socket        handler = netc.Soc;

            //if (!netc.Soc.Poll(100, SelectMode.SelectRead))
            //{
            //    listconn.Remove(netc);
            //    return;
            //}
            // Read data from the client socket.
            try
            {
                int bytesRead = 0;
                try
                {
                    bytesRead = handler.EndReceive(ar);
                }
                catch
                {
                    netc.Soc.Close();
                    listconn.Remove(netc);
                }

                //if (bytesRead > 0)
                //{
                //    // There  might be more data, so store the data received so far.
                byte[] tempbtye = new byte[bytesRead];

                //netc.Buffer.CopyTo(tempbtye, 0);
                Array.Copy(netc.Buffer, 0, tempbtye, 0, bytesRead);
                netc.Datalist.Add(tempbtye);
                //  System.Threading.Thread.Sleep(10);
            }
            catch
            {
            }
            //handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback), netc);
        }
Esempio n. 19
0
        private void ReadCallback(object ar)
        {
            NETcollection netc    = (NETcollection)ar;
            Socket        handler = netc.Soc;

            //if (!netc.Soc.Poll(100, SelectMode.SelectRead))
            //{
            //    listconn.Remove(netc);
            //    return;
            //}
            // Read data from the client socket.
            try
            {
                int bytesRead = 0;
                try
                {
                    bytesRead = handler.Available;
                }
                catch
                {
                    //netc.Soc.Close();
                    //listconn.Remove(netc);
                }
                byte[] tempbtye = new byte[bytesRead];

                //netc.Buffer.CopyTo(tempbtye, 0);
                if (bytesRead > 0)
                {
                    handler.Receive(tempbtye);
                    netc.Datalist.Add(tempbtye);
                }
            }
            catch
            {
            }
            //handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback), netc);
        }
Esempio n. 20
0
        void receiveconn(object ias)
        {
            //  NETcollection netcs = ias as NETcollection;
            while (true)
            {
                int c = connlist.Count;

                NETcollection[] netlist = new NETcollection[c];
                connlist.CopyTo(0, netlist, 0, c);
                foreach (NETcollection netc in netlist)
                {
                    try
                    {
                        if (netc != null)
                        {
                            if (netc.Soc != null)
                            {
                                if (netc.State == 0)
                                {
                                    if (netc.Soc.Available > 200)
                                    {
                                        netc.Soc.Receive(netc.Buffer = new byte[netc.Soc.Available]);
                                        // setherd(netc);
                                        //new receiveconndele(setherd).BeginInvoke(netc, null, null);
                                        System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(setherd), netc);
                                        connlist.Remove(netc);
                                        listconn.Add(netc);
                                    }
                                }
                            }
                        }
                    }
                    catch { }
                }
                System.Threading.Thread.Sleep(1);
            }
        }
Esempio n. 21
0
        public void xintiao(object obj)
        {
            while (true)
            {
                try
                {
                    System.Threading.Thread.Sleep(8000);
                    //  ArrayList al = new ArrayList();
                    // al.Clone()
                    NETcollection[] netlist = new NETcollection[listconn.Count];
                    listconn.CopyTo(netlist);
                    foreach (NETcollection netc in netlist)
                    {
                        try
                        {
                           byte[] bp = PackData(new byte[] { 0x99});
                            netc.Soc.Send(bp);
                           
                          

                        }
                        catch
                        {

                            try { netc.Soc.Close(); }
                            catch { }
                            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(DeleteConnSoc), netc.Soc);


                            listconn.Remove(netc);
                        }

                    }
                }
                catch { }
            }
        }
Esempio n. 22
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;
            }
        }
Esempio n. 23
0
        private void packageData(object obj)
        {
            NETcollection netc     = obj as NETcollection;
            List <byte[]> ListData = netc.Datalist;

            try
            {
                int i     = 0;
                int count = ListData.Count;

                if (count > 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];
                        if (bytesRead > 2 + a)
                        {
                            String temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a);
                            int    len  = int.Parse(temp);


                            if ((len + 2 + a) > tempbtye.Length)
                            {
                                try
                                {
                                    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];
                                        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;
                            }
                            else if (tempbtye.Length > (len + 2 + a))
                            {
                                try
                                {
                                    byte[] temps = new byte[tempbtye.Length - (len + 2 + a)];
                                    Array.Copy(tempbtye, (len + 2 + a), temps, 0, temps.Length);
                                    ListData[i] = temps;
                                }
                                catch
                                { }
                                // netc.Ispage = false; return;
                            }
                            else if (tempbtye.Length == (len + 2 + a))
                            {
                                if (ListData.Count > 0)
                                {
                                    ListData.RemoveAt(i);
                                }
                            }
                            try
                            {
                                temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len);
                            }
                            catch
                            { }
                            try
                            {
                                modelevent me = new modelevent();
                                me.Command = tempbtye[0];
                                me.Data    = temp;
                                me.Soc     = netc.Soc;
                                if (receiveevent != null)
                                {
                                    System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventto), me);
                                }
                                //receiveeventto(me);
                                //if (receiveevent != null)
                                //    receiveevent.BeginInvoke(tempbtye[0], temp, netc.Soc, null, null);
                                //if (ListData.Count > 0) ListData.RemoveAt(i);
                                netc.Ispage = false; return;
                            }
                            catch (Exception e)
                            {
                                netc.Ispage = false; return;
                            }
                        }
                        else
                        {
                            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];
                                Array.Copy(temps, tempbtyes, temps.Length);
                                Array.Copy(ListData[i], 0, tempbtyes, temps.Length, ListData[i].Length);
                                ListData[i] = tempbtyes;
                            }
                            netc.Ispage = false; return;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (netc.Datalist.Count > 0)
                {
                    netc.Datalist.RemoveAt(0);
                }
                netc.Ispage = false;
                return;
            }
            finally { netc.Ispage = false; }
        }
Esempio n. 24
0
        void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                allDone.Set();
                Socket listener = (Socket)ar.AsyncState;
                if (listener == null)
                    return;
                Socket handler = listener.EndAccept(ar);

                // Create the state object.
                NETcollection netc = new NETcollection();
                netc.Soc = handler;
                listconn.Add(netc);
                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(UpdataConnSoc), handler);

               // netc.Buffer 

                handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback), netc);
            }
            catch { }
           // allDone.Set();

        }
Esempio n. 25
0
        private void ReadCallback(IAsyncResult ar)
        {
            NETcollection netc    = (NETcollection)ar.AsyncState;
            Socket        handler = netc.Soc;

            //if (!netc.Soc.Poll(100, SelectMode.SelectRead))
            //{
            //    listconn.Remove(netc);
            //    return;
            //}
            // Read data from the client socket.
            try
            {
                int bytesRead = 0;
                try
                {
                    bytesRead = handler.EndReceive(ar);
                }
                catch
                {
                    netc.Soc.Close();
                    listconn.Remove(netc);
                }

                if (bytesRead > 0)
                {
                    // There  might be more data, so store the data received so far.
                    byte[] tempbtye = new byte[bytesRead];

                    //netc.Buffer.CopyTo(tempbtye, 0);
                    Array.Copy(netc.Buffer, 0, tempbtye, 0, bytesRead);
                    tempbtye  = AnalyticData(tempbtye, bytesRead);
                    bytesRead = tempbtye.Length;
labe881:
                    int a = tempbtye[1];
                    String temp2 = System.Text.Encoding.UTF8.GetString(tempbtye, 2, a);
                    int    len   = int.Parse(temp2);
                    //int b = netc.Buffer[2 + a+1];
                    //temp = System.Text.ASCIIEncoding.ASCII.GetString(netc.Buffer, 2 + a + 1, b);
                    //len = int.Parse(temp);
                    String     temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + a, len);
                    modelevent me   = new modelevent();
                    me.Command = tempbtye[0];
                    me.Data    = temp;
                    me.Soc     = netc.Soc;
                    System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(receiveeventto), me);

                    if (bytesRead > (2 + a + len))
                    {
                        byte[] b = new byte[bytesRead - (2 + a + len)];
                        byte[] t = tempbtye;
                        Array.Copy(t, (2 + a + len), b, 0, b.Length);

                        tempbtye  = b;
                        bytesRead = bytesRead - (2 + a + len);
                        goto labe881;
                    }
                }
            }
            catch
            {
            }
            //handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback), netc);
        }
Esempio n. 26
0
 void receive(object ias)
 {
     while (true)
     {
         try
         {
             NETcollection[] netlist = new NETcollection[listconn.Count];
             listconn.CopyTo(netlist);
             foreach (NETcollection netc in netlist)
             {
                 if (netc.Soc.Available > 0)
                 {
                     netc.Buffer = new byte[netc.Soc.Available];
                     netc.Soc.BeginReceive(netc.Buffer, 0, netc.Soc.Available, 0, new AsyncCallback(ReadCallback), netc);
                 }
             }
         }
         catch { }
         System.Threading.Thread.Sleep(100);
     }
 }
Esempio n. 27
0
        void Accept(object ias)
        {
            while (true)
            {

                Socket handler = listener.Accept();
                //if (listener == null)
                //    return;
                //Socket handler = listener.EndAccept(ar);

                // Create the state object.
                NETcollection netc = new NETcollection();
                netc.Soc = handler;
                listconn.Add(netc);
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(UpdataConnSoc));
                t.Start(handler);
                System.Threading.Thread.Sleep(50);
              //  handler.BeginReceive(netc.Buffer, 0, netc.BufferSize, 0, new AsyncCallback(ReadCallback2), netc);
              
                //System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(UpdataConnSoc), handler);

                // Set the event to nonsignaled state.
                //allDone.Reset();

                ////开启异步监听socket
                ////    Console.WriteLine("Waiting for a connection");
                //try
                //{
                //    listener.BeginAccept(
                //              new AsyncCallback(AcceptCallback),
                //              listener);
                System.Threading.Thread.Sleep(1);
                //}
                //catch { }
                //// 让程序等待,直到连接任务完成。在AcceptCallback里的适当位置放置allDone.Set()语句.
                //allDone.WaitOne();
            }

        }