示例#1
0
        public void MonitorSocketStart(object source, System.Timers.ElapsedEventArgs e)
        {
            //轮巡SOCKET管理类,查数据接收超时
            AsyncSocketUserToken[] ListSoc = null;
            m_asyncSocketServer.AsyncSocketUserTokenList.CopyList(ref ListSoc);

            for (int i = 0; i < ListSoc.Length; i++)
            {
                try
                {
                    //SOCKET不为空,则可以发送消息
                    if (ListSoc[i].ConnectSocket != null)
                    {
                        //   Console.WriteLine("{0} is time out!!!", ListSoc[i].ConnectSocket.ToString());
                        //   //todo 服务器主动断掉这个链接,其他操作待添加
                        if (ListSoc[i].AsyncSocketInvokeElement != null)
                        {
                            ///当前端口发送不冲突

                            //byte[] bufferUTF8 = str2HexByte(ListSoc[i].StrQueryCommand);
                            byte[] bufferUTF8 = str2HexByte("01030000F1D8");

                            AsyncSendBufferManager asyncSendBufferManager = ListSoc[i].SendBuffer;
                            asyncSendBufferManager.StartPacket();
                            // asyncSendBufferManager.DynamicBufferManager.WriteInt(totalLength, false); //写入总大小
                            asyncSendBufferManager.DynamicBufferManager.WriteBuffer(bufferUTF8, 0, bufferUTF8.Length);

                            asyncSendBufferManager.EndPacket();
                            //
                            bool result = true;
                            //      Console.WriteLine("ListSoc[i].SendAsyncState = ?" + ListSoc[i].SendAsyncState);
                            //如果发送端口被占有
                            while (ListSoc[i].SendAsyncState)
                            {
                                Thread.Sleep(100);
                                Console.WriteLine("sleep 100");
                            }

                            int packetOffset = 0;
                            int packetCount  = 0;
                            if (asyncSendBufferManager.GetFirstPacket(ref packetOffset, ref packetCount))
                            {
                                ListSoc[i].SendAsyncState = true;
                                //    Console.WriteLine(System.DateTime.Now + "ListSoc[i].SendAsyncState = true;");
                                Program.Logger.Debug(System.DateTime.Now + "send:" + bufferUTF8);
                                result = m_asyncSocketServer.SendAsyncEvent(ListSoc[i].ConnectSocket,
                                                                            ListSoc[i].SendEventArgs,
                                                                            asyncSendBufferManager.DynamicBufferManager.Buffer, packetOffset,
                                                                            packetCount);
                            }
                        }
                    }
                    Thread.Sleep(100);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("##############" + ex);
                }
            }
        }