예제 #1
0
        protected virtual void replyHandler <TSource, TSource2>(object sender, TcpIpEventArgs e, ref TSource inStr)
        {
            int threadID = System.Threading.Thread.CurrentThread.ManagedThreadId;

            lock (handlerLock)
            {
                try
                {
                    if (hasRtn)
                    {
                        return;
                    }
                    byte[] bytPacketDatas = TCPUtility._Str2Packet(inStr);
                    ushort iSeqNum        = TCPUtility.PrcGetPacketSeqNum(bytPacketDatas);

                    if (iSeqNum != e.iSeqNum)
                    {
                        return;
                    }
                    TSource2 tmpSXFY = (TSource2)TCPUtility._Packet2Str <TSource2>((byte[])e.objPacket, tcpipAgent.Name);

                    recvStr = (Object)tmpSXFY;
                    hasRtn  = true;
                }
                catch (Exception ex)
                {
                    //todo recode log
                }
            }
        }
예제 #2
0
        //const UInt16 SeqNoBeginByte = 8;
        public void SendMessage <T>(T msgStructure)
        {
            byte[] bytPacketDatas;
            byte[] bytSendMsgDatas;
            //byte[] bytSeqNoDatas;
            //bytSeqNoDatas = BitConverter.GetBytes(getSendSeqNum());
            bytPacketDatas  = TCPUtility._Str2Packet(msgStructure);
            bytSendMsgDatas = TCPUtility.PrcCreateSendMessage_New(bytPacketDatas);

            LogCollection.TrxMsgInfo(TCPUtility.toStuctureString(msgStructure, Name, true));
            Console.WriteLine("Send Data:{0}", TCPUtility.PrcConvBytes2HexStr(bytSendMsgDatas, bytSendMsgDatas.Length, " "));

            //Array.Copy(bytSeqNoDatas, 0, bytSendMsgDatas, SeqNoBeginByte, bytSeqNoDatas.Length);
            Task taskSendMsg = null;

            switch (ConnectMode)
            {
            case TCPIP_AGENT_COMM_MODE.SERVER_MODE:
                taskSendMsg = session.SendAsync(bytSendMsgDatas);
                break;

            case TCPIP_AGENT_COMM_MODE.CLINET_MODE:
                taskSendMsg = ClientSession.SendAsync(bytSendMsgDatas);
                break;
            }
            taskSendMsg.Wait();
        }
예제 #3
0
        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);
        }