Ejemplo n.º 1
0
        private static bool ReceiveByProtocol(NetworkStream ns, ref NetStructure.NormalDataStruct dataInfo)
        {
            bool result = false;

            byte[] byteHead = new byte[Coder.PROTOCOL_HEAD_COUNT];

            if (!ReadBuffer(ns, Coder.PROTOCOL_HEAD_COUNT, byteHead))
            {
                db.InsertErrorInfo(enumSystemErrorCode.TcpRecieveErr, null, "数据头读取超时", byteHead);
                return(result);
            }
            //int headIdx = 0;
            //while (true)
            //{
            //    if (byteHead[headIdx] == Coder.PROTOCOL_HEAD_START[0] && byteHead[headIdx + 1] == Coder.PROTOCOL_HEAD_START[1] &&
            //        byteHead[headIdx + 2] == Coder.PROTOCOL_HEAD_START[2] && byteHead[headIdx + 3] == Coder.PROTOCOL_HEAD_START[3])
            //    {
            //        break;
            //    }
            //    headIdx++;
            //    if (headIdx > Coder.PROTOCOL_HEAD_COUNT - 4)
            //    {
            //        ReadBuffer(ns, Coder.PROTOCOL_HEAD_COUNT, byteHead);
            //        headIdx = 0;
            //    }
            //}
            //if (headIdx > 0)
            //{
            //    byte[] byteTemp = new byte[headIdx];
            //    ReadBuffer(ns, headIdx, byteTemp);
            //    for (int i = 0; i < Coder.PROTOCOL_HEAD_COUNT - headIdx; i++)
            //    {
            //        byteHead[i] = byteHead[i + headIdx];
            //    }
            //    for (int i = Coder.PROTOCOL_HEAD_COUNT - headIdx; i < Coder.PROTOCOL_HEAD_COUNT; i++)
            //    {
            //        byteHead[i] = byteTemp[i + headIdx - Coder.PROTOCOL_HEAD_COUNT];
            //    }
            //}
            Coder.DecodeData(byteHead, ref dataInfo);
            if (!ReadBuffer(ns, dataInfo.contentLen, dataInfo.Content))
            {
                db.InsertErrorInfo(enumSystemErrorCode.TcpRecieveErr, null, "数据主体读取超时", byteHead);
                return(result);
            }
            result = true;

            return(result);
        }
Ejemplo n.º 2
0
        private static void HandlerByProtocol(ref NetStructure.NormalDataStruct dataInfo)
        {
            try
            {
                interfaceClientHanlder clientHandler = GetHandlerByCommand(dataInfo.Code);
                if (clientHandler == null)
                {
                    db.InsertErrorInfo(enumSystemErrorCode.TcpDefaultHandlerErr, null, dataInfo.IpAddress, dataInfo.Content);
                    return;
                }
                byte[] byteResult = clientHandler.HandlerClientData(dataInfo.Content);
                //返回信息
                byte[] buffResp = null;
                if (clientHandler.ShouldResponse())
                {
                    Coder.EncodeServerResp(dataInfo.Code + 1, byteResult, out buffResp);
                    dataInfo.stream.Write(buffResp, 0, buffResp.Length);
                }
                int tempMachineId = dataInfo.Code == enumCommandType.UP_DEVICE_SETTING_SEND ?
                                    ConvertHelper.BytesToInt16(buffResp, buffResp.Length - 2, true) : //设置协议没有设备ID,所以用返回回去的ID
                                    ConvertHelper.BytesToInt16(dataInfo.Content, true);               //其它协议以设备ID开头

                dataInfo.MachineId = tempMachineId;
                if (netConnection.ContainsKey(tempMachineId))
                {
                    netConnection[tempMachineId] = dataInfo.stream;
                }
                else
                {
                    netConnection.Add(tempMachineId, dataInfo.stream);
                }
            }
            catch (Exception ex)
            {
                db.InsertErrorInfo(enumSystemErrorCode.TcpHandlerException, ex, dataInfo.IpAddress, dataInfo.Content);
            }
        }