日期:2014-05-18 浏览次数:20984 次
        private void button1_Click(object sender, EventArgs e)
        {
            if (Listen.Text == "Listen")
            {
                Listen.Text = "UnListen";               
                
                con = new conVal();
                con.andon = new LinkAndon();
                con.sendmes = new Queue<byte[]>();
                con.buffer= new byte[1024];
                mt = new Mutex(true, "mymt");
                
              //  Thread T2 = new Thread(SendMessage);
                
                AsyncCallback ac = new AsyncCallback(AcceptData);
                Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Server.Bind(serverIP);
                Server.Listen(10);
                Server = Server.Accept();
                //erver.Receive(data);
                
                Server.BeginReceive(con.buffer, 0, con.buffer.Length, SocketFlags.None, ac, (object)con);
                
                System.Threading.Timer tm = new System.Threading.Timer(tm_Elapsed, (object)con, 0, 5000);
               // T2.Start((object)con);
            }
            else
            {
                Server.Disconnect(false);
                Listen.Text = "Listen";
            
            }
            
        }
        class conVal
        {
            //public Socket con;
            public ANDON.LinkAndon andon;
            public Queue<byte[]> sendmes;
            public bool go=true;
            public byte[] buffer;
        
        }
        private conVal con;
        void tm_Elapsed(object or)
        {
            conVal cc = (conVal)or;
            Dictionary<string, string>[] par = cc.andon.MachineStatusWrite();
            foreach (Dictionary<string, string> para in par)
            {
                string ex = "<Andon_alarm ";
                int i=1;
                string pa="";
                if (para["DEVICE_ID"] == "1")
                {
 
                    pa+="Bstate="+para["STATE"]+" ";
                
                }else pa+="Lstate="+para["STATE"]+" ";
                i++;
                ex += "tokens=" + i.ToString() + pa.ToString()+"comm='test'/>";
                Command cd = new Command(9, para["DEVICE_ID"], ex);
                byte[] combyte = Encoding.ASCII.GetBytes(cd.CommandText);
                byte[] ct = new byte[] { 0x0, 0x0, 0x0, (byte)combyte.Length };
                IEnumerable<byte> sou = ct.Concat(combyte);
                con.buffer=sou.ToArray();
                Server.BeginSend(con.buffer,0,con.buffer.Length, SocketFlags.None, SendMessage, (object)con.buffer);
               // cc.sendmes.Enqueue(sou.ToArray());
                cc.andon.updateEventState(para["ID"]);
               
            }
   
        }
        private ANDON.LinkAndon ad;
        private void SendMessage(IAsyncResult result)
        {
            try
            {
                //conVal con = (conVal)result.AsyncState;
                //while (con.go)
                //{
                //    if (con.sendmes.Count() != 0)
                //    {
                //       // mt.WaitOne();
                //        byte[] sou = con.buffer;
                //        //Server.Send(sou, sou.Length, SocketFlags.None);
                //       // mt.ReleaseMutex();
                //    }
                //}
            }
            catch (Exception e)
            { 
            
            
            }
        
        
        
        }
        private void AcceptData(IAsyncResult result)
        {