Example #1
0
 public void Set(RecvDataStruct oo)
 {
     m_RecvLst.Clear();
     m_RecvLst.Add(oo);
 }
Example #2
0
        /// <summary>

        /// TCP读数据的回调函数

        /// </summary>

        /// <param name="ar"></param>

        private void TCPReadCallBack(IAsyncResult ar)
        {
            StateObject state = (StateObject)ar.AsyncState;

            //主动断开时

            if ((state.client == null) || (!state.client.Connected))
            {
                return;
            }
            int           numberOfBytesRead = 0;
            NetworkStream mas  = state.client.GetStream();
            string        type = null;

            try
            {
                numberOfBytesRead = mas.EndRead(ar);
            }
            catch (Exception dd)
            {
            }

            state.totalBytesRead += numberOfBytesRead;

            // string strRev = numberOfBytesRead.ToString();
            if (numberOfBytesRead > 0)
            {
                byte[] dd = new byte[numberOfBytesRead];
                Array.Copy(state.buffer, 0, dd, 0, numberOfBytesRead);
                //  OnGetData(dd);
                char [] szchar = new char[dd.Length];
                for (int i = 0; i < dd.Length; i++)
                {
                    szchar[i] = (char)dd[i];
                }

                string sdata = new string(szchar);


                if (sdata.Length >= 12 && sdata[0] == 'W' && sdata[1] == 'J')
                {
                    uint iClientNo;
                    uint iBodylen = 0;
                    int  iFuncNo;
                    if (JsonOp.UnPacketHead(sdata, out iFuncNo, out iClientNo, out iBodylen))
                    {
                        if (iBodylen + 12 > sdata.Length)
                        {
                            RecvDataStruct oRecvDataStruct = new RecvDataStruct();
                            oRecvDataStruct.ifd        = 0;
                            oRecvDataStruct.itotallen  = iBodylen + 12;
                            oRecvDataStruct.iRemainlen = iBodylen + 12;
                            lock (DataMgr.Get_DataMgr().m_RecvLst)
                            {
                                DataMgr.Get_DataMgr().m_RecvLst.Add(oRecvDataStruct);
                            }
                        }
                    }
                }

                bool bRecOK = true;
                lock (DataMgr.Get_DataMgr().m_RecvLst)
                {
                    if (DataMgr.Get_DataMgr().m_RecvLst.Count > 0)
                    {
                        List <RecvDataStruct> lst             = DataMgr.Get_DataMgr().Get();
                        RecvDataStruct        oRecvDataStruct = lst[0];
                        oRecvDataStruct.iRemainlen  -= (uint)sdata.Length;
                        oRecvDataStruct.strrecvData += sdata;

                        if (oRecvDataStruct.iRemainlen <= 0)
                        {
                            sdata = oRecvDataStruct.strrecvData;
                            DataMgr.Get_DataMgr().Remove();
                        }
                        else
                        {
                            DataMgr.Get_DataMgr().Set(oRecvDataStruct);
                            bRecOK = false;
                        }
                    }
                }

                if (bRecOK)
                {
                    OnRecvData(sdata);
                }

                mas.BeginRead(state.buffer, 0, StateObject.BufferSize,
                              new AsyncCallback(TCPReadCallBack), state);
            }
            else
            {
                //被动断开时

                mas.Close();
                state.client.Close();
                m_Form1.DispMsg("connect close");
                mas   = null;
                state = null;
            }
        }