Пример #1
0
        private void appServer_NewRequestReceived(ProtocolSession session, ProtocolRequestInfo requestInfo)
        {
            if (requestInfo == null)
            {
                Console.Write(DateTime.Now.ToString() + "   ");
                Console.WriteLine("非正常包 " + session.SessionID);
                return;
            }
            //心跳
            if (requestInfo.Length == 0)
            {
                Console.Write(DateTime.Now.ToString() + "   ");
                Console.WriteLine("心跳 " + session.SessionID);
                return;
            }
            else if (requestInfo.Length < 0)
            {
                Console.Write(DateTime.Now.ToString() + "   ");
                Console.WriteLine("异常数据,算作心跳 " + session.SessionID);
                return;
            }
            Console.Write(DateTime.Now.ToString() + "   ");
            Console.WriteLine("收到了 " + session.SessionID);

            sendReplyMsg(session, getRsponseMsg(requestInfo.Data));

            //保存数据
            ReceiveController.UpdateOrSaveSession(session.SessionID, session.StartTime, requestInfo.Data);
        }
Пример #2
0
        /// <summary>
        /// 往仪器端回复消息
        /// </summary>
        /// <param name="session">socket连接</param>
        /// <param name="msg">消息内容</param>
        public static ResultInfo sendReplyMsg(ProtocolSession session, string msg)
        {
            ResultInfo result = new ResultInfo();

            byte[] dataByte    = Encoding.UTF8.GetBytes(msg);
            byte[] startByte   = HexStrTobyte("aa 55");
            byte[] controlByte = HexStrTobyte("02");
            byte[] lenByte     = BitConverter.GetBytes(dataByte.Length);
            if (BitConverter.IsLittleEndian)//判断计算机结构的endian设置(高低位转换)
            {
                Array.Reverse(lenByte);
            }
            byte[] verfiByte = HexStrTobyte("1c 1d");
            int    len       = 7 + dataByte.Length + 2;

            byte[] b = new byte[len];
            Buffer.BlockCopy(startByte, 0, b, 0, 2);
            Buffer.BlockCopy(controlByte, 0, b, 2, 1);
            Buffer.BlockCopy(lenByte, 0, b, 3, 4);
            Buffer.BlockCopy(dataByte, 0, b, 7, dataByte.Length);
            Buffer.BlockCopy(verfiByte, 0, b, 7 + dataByte.Length, 2);

            bool bl = session.TrySend(b, 0, b.Length);

            if (!bl)
            {
                result.code = 100;
                result.msg  = "发送消息失败.";
            }


            return(result);
        }
Пример #3
0
        void appServer_NewSessionConnected(ProtocolSession session)
        {
            Console.Write(DateTime.Now.ToString() + "   ");
            Console.WriteLine("连上了 " + session.SessionID);
            dic.Add(session.SessionID, session);
            ReceiveController.SessionConnect(session.SessionID, session.StartTime);

            testIds.Add(session.SessionID);
        }
Пример #4
0
        void appServer_SessionClosed(ProtocolSession session, CloseReason value)
        {
            Console.Write(DateTime.Now.ToString() + "   ");
            Console.WriteLine("断开了 " + session.SessionID + " " + value.ToString());
            dic.Remove(session.SessionID);
            ReceiveController.SessionClose(session.SessionID);

            testIds.Remove(session.SessionID);
        }