コード例 #1
0
ファイル: TcpIpAgent.cs プロジェクト: Rush102/OHTMiddler
        public void DecodReciveRawData(byte[] raw_data)
        {
            byte[] bytPacketDatas = null;
            int    data_coune     = raw_data.Length;
            int    PacketID       = 0;
            UInt16 SeqNum         = 0;
            object objPacket      = null;

            //while (data_coune >= LEN_MESSAGE_SIZE)
            //{
            string result = TCPUtility.PrcConvBytes2HexStr(raw_data, raw_data.Length, " ");

            Console.WriteLine(reciveCount++);
            Console.WriteLine(result);

            // Check Message Header
            if ((raw_data[0] == '@') && (raw_data[1] == 'P') &&
                (raw_data[2] == 'K') && (raw_data[3] == 'T'))
            {
                //bytPacketDatas = TCPUtility.PrcGetPacketFromMessage(raw_data);
                bytPacketDatas = TCPUtility.PrcGetPacketFromMessage_New(raw_data);
                PacketID       = TCPUtility.PrcGetPacketID(bytPacketDatas);
                SeqNum         = TCPUtility.PrcGetPacketSeqNum(bytPacketDatas);
                objPacket      = bytPacketDatas;
                TcpIpEventArgs arg = new TcpIpEventArgs(PacketID, SeqNum, objPacket);
                onTcpIpReceive(PacketID, arg);
                dataSeqNoOrderToComfirm(PacketID, SeqNum);
            }
            //data_coune -= LEN_MESSAGE_SIZE;
            //Array.Copy(raw_data, LEN_MESSAGE_SIZE, raw_data, 0, data_coune);
            //}
        }
コード例 #2
0
ファイル: TrxTcpIp.cs プロジェクト: Rush102/OHTMiddler
        public ReturnCode sendRecv <TSource, TSource2>(TSource inStr, out TSource2 outStr,
                                                       out string rtnMsg,
                                                       int timeoutMSec, int retryCnt)
        {
            byte[] bytPacketDatas = TCPUtility._Str2Packet(inStr);
            int    iPacketID      = TCPUtility.PrcGetPacketID(bytPacketDatas);
            ushort iSeqNum        = 0;

            if (!tcpipAgent.ImsgStateMachine.IsInState(TcpIpAgent.E_Msg_STS.Idle))
            {
                throw new TcpIpStateException(string.Format("The status is incorrect when sent.TcpIp name:{0} ,Pack ID:{1} ,crt state:{2}"
                                                            , tcpipAgent.Name
                                                            , iPacketID
                                                            , tcpipAgent.ImsgStateMachine.State));
            }

            int threadID = System.Threading.Thread.CurrentThread.ManagedThreadId;

            tcpipAgent.onMessageGenerated();
            hasRtn = false;

            outStr = (TSource2)TCPUtility.GetDefault(typeof(TSource2));


            EventHandler <TcpIpEventArgs> handler = null;

            rtnMsg = string.Empty;
            int iReplyPacketID = iPacketID + 100;

            try
            {
                handler = delegate(Object _sender, TcpIpEventArgs _e)
                {
                    replyHandler <TSource, TSource2>(_sender, _e, ref inStr);
                };

                tcpipAgent.addTcpIpReceivedHandler(iReplyPacketID, handler);

                int count = 0;
                do
                {
                    Console.WriteLine(string.Format("Send begin ID:{0}", iPacketID));
                    tcpipAgent.onMessageSent();
                    bool is_success = this.SendPrimary(inStr);
                    if (!is_success)
                    {
                        rtnMsg = "Send message fail.";
                        TcpIpExceptionEventArgs e = new TcpIpExceptionEventArgs(iPacketID, iSeqNum, bytPacketDatas, rtnMsg);
                        tcpipAgent.onErrorHappend(e);
                        return(ReturnCode.SendDataFail);
                    }
                    Console.WriteLine(string.Format("Send success ID:{0}", iPacketID));

                    tcpipAgent.onWaitForReply();

                    SpinWait.SpinUntil(() => hasRtn, timeoutMSec);

                    if (!hasRtn)
                    {
                        if (count++ >= retryCnt)
                        {
                            Console.WriteLine(string.Format("Replay timeout ID:{0}", iPacketID));
                            rtnMsg = string.Format("A response timeout occurred. retry count:{0}", count);
                            TcpIpExceptionEventArgs e = new TcpIpExceptionEventArgs(iPacketID, iSeqNum, bytPacketDatas, rtnMsg);
                            tcpipAgent.onReplyTimeout(e);
                            return(ReturnCode.Timeout);
                        }
                        Console.WriteLine(string.Format("Send aging ID:{0}, count:{1}", iPacketID, count));
                    }
                } while (!hasRtn);
                outStr = (TSource2)recvStr;
                tcpipAgent.onMessageSentCmp();
            }
            catch (Exception ex)
            {
                rtnMsg = ex.ToString();
                TcpIpExceptionEventArgs e = new TcpIpExceptionEventArgs(iPacketID, iSeqNum, bytPacketDatas, rtnMsg);
                tcpipAgent.onErrorHappend(e);
                return(ReturnCode.SendDataFail);
            }
            finally
            {
                tcpipAgent.removeTcpIpReceivedHandler(iReplyPacketID, handler);
                tcpipAgent.onMessageSentFinish();
                Console.WriteLine(string.Format("Send finish ID:{0}", iPacketID));
            }
            return(ReturnCode.Normal);
        }