/// <summary>
 /// 接受消息.
 /// </summary>
 /// <returns></returns>
 public QueuingMachineMessage ReceiveMessage()
 {
     BinaryReader reader = new BinaryReader(stream);
     QueuingMachineMessage result = new QueuingMachineMessage();
     queuingMachineMessageCoding.Decode(reader, result);
     return result;
 }
        /// <summary>
        /// 消息处理.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public QueuingMachineMessage DoMessageProcess(QueuingMachineMessage message)
        {
            QueuingMachineMessage result = new QueuingMachineMessage()
            {
                Header = new MessageHeader()
                {
                    CommandID = message.Header.CommandID + 0x80000000,
                    SequenceNo = message.Header.SequenceNo,
                },
            };

            switch (message.Header.CommandID)
            {
                case MessageHeader.QM_NewWorkNumber_Req:
                    // 申请新号码.
                    result.Body = GetNewWorkNumberRespond(message.Body as NewWorkNumberRequest);
                    break;

                case MessageHeader.QM_AskWorkNumber_Req :
                    // 需要一个号码.
                    result.Body = GetAskWorkNumberRespond(message.Body as AskWorkNumberRequest);
                    break;
            }

            // 返回.
            return result;
        }
        /// <summary>
        /// 发送消息.
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(QueuingMachineMessage message)
        {
            // 消息转换为 byte 数组.
            BinaryWriter writer = new BinaryWriter(stream);
            queuingMachineMessageCoding.Encode(writer, message);

            // 通过 Socket  发送.
            Socket.Send(stream.GetBuffer(), message.Header.CommandLength, SocketFlags.None);
        }
Пример #4
0
        /// <summary>
        /// 覆盖 Equals 方法.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (obj is QueuingMachineMessage)
            {
                QueuingMachineMessage oMessageData = obj as QueuingMachineMessage;

                return(this.Header.Equals(oMessageData.Header) && this.Body.Equals(oMessageData.Body));
            }
            return(false);
        }
        /// <summary>
        /// 接受消息.
        /// </summary>
        /// <returns></returns>
        public QueuingMachineMessage ReceiveMessage()
        {
            byte[] bytes = new byte[256];
            Socket.Receive(bytes);
            stream = new MemoryStream(bytes);

            BinaryReader reader = new BinaryReader(stream);
            QueuingMachineMessage result = new QueuingMachineMessage();
            queuingMachineMessageCoding.Decode(reader, result);
            return result;
        }
 public void TestFixtureSetUp()
 {
     message = new QueuingMachineMessage()
     {
         Header = new MessageHeader()
         {
             CommandID = MessageHeader.QM_CloseWorkNumber_Resp ,
             SequenceNo = 1,
         },
         Body = new CloseWorkNumberRespond(),
     };
 }
 public void TestFixtureSetUp()
 {
     message = new QueuingMachineMessage()
     {
         Header = new MessageHeader()
         {
             CommandID = MessageHeader.QM_NewWorkNumber_Req,
             SequenceNo = 1,
         },
         Body = new NewWorkNumberRequest()
         {
             ServiceCode = "A",
         },
     };
 }
 public void TestFixtureSetUp()
 {
     message = new QueuingMachineMessage()
     {
         Header = new MessageHeader()
         {
             CommandID = MessageHeader.QM_AskWorkNumber_Resp,
             SequenceNo = 1,
         },
         Body = new AskWorkNumberRespond()
         {
             ResultStatus = 1,
             ResultWorkNumber = "00100",
         },
     };
 }
 public void TestFixtureSetUp()
 {
     message = new QueuingMachineMessage()
     {
         Header = new MessageHeader()
         {
             CommandID = MessageHeader.QM_NewWorkNumber_Resp,
             SequenceNo = 1,
         },
         Body = new NewWorkNumberRespond()
         {
             ResultWorkNumber = "A0009",
             TopWorkNumber = "A0005",
             QueueLength = 4,
         },
     };
 }
 /// <summary>
 /// 发送消息.
 /// </summary>
 /// <param name="message"></param>
 public void SendMessage(QueuingMachineMessage message)
 {
     BinaryWriter writer = new BinaryWriter(stream);
     queuingMachineMessageCoding.Encode(writer, message);
 }
Пример #11
0
        /// <summary>
        /// 发送请求消息.
        /// </summary>
        /// <param name="code"></param>
        private void SendMessage(string code)
        {
            QueuingMachineMessage message = new QueuingMachineMessage()
            {
                Header = new MessageHeader()
                {
                    CommandID = MessageHeader.QM_NewWorkNumber_Req ,
                    SequenceNo = sequenceNo ++,
                },
                Body = new NewWorkNumberRequest()
                {
                    ServiceCode = code,
                },
            };

            // 第一个参数:AddressFamily  = 指定 Socket 类的实例可以使用的寻址方案。
            //     Unspecified 未指定地址族。
            //     InterNetwork IP 版本 4 的地址。
            //     InterNetworkV6 IP 版本 6 的地址。
            //
            // 第二个参数:SocketType = 指定 Socket 类的实例表示的套接字类型。
            //     Stream 一个套接字类型,支持可靠、双向、基于连接的字节流,而不重复数据,也不保留边界。
            //            此类型的 Socket 与单个对方主机通信,并且在通信开始之前需要建立远程主机连接。
            //            此套接字类型使用传输控制协议 (Tcp),AddressFamily 可以是 InterNetwork,也可以是 InterNetworkV6。
            //
            // 第三个参数:ProtocolType = 指定 Socket 类支持的协议。
            //     Tcp 传输控制协议 (TCP)。
            Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                //  尝试连接主机.
                mySocket.Connect(ephost);

                // 发送消息.
                SocketMessageSender sender = new SocketMessageSender();
                sender.Socket = mySocket;
                sender.SendMessage(message);

                // 接收消息.
                SocketMessageReceiver receiver = new SocketMessageReceiver();
                receiver.Socket = mySocket;
                QueuingMachineMessage resultMessage = receiver.ReceiveMessage();

                NewWorkNumberRespond body = resultMessage.Body as NewWorkNumberRespond;

                txtResult.Text = String.Format(@"您的排队号码为{0},
            您前面共有{1}个用户处于等待中!",
                  body.ResultWorkNumber,
                  body.QueueLength -1
                  );

            }
            catch (Exception ex)
            {
                txtResult.AppendText("连接/发送/接收过程中,发生了错误!");
                txtResult.AppendText(ex.Message);
                txtResult.AppendText(ex.StackTrace);
            }
            finally
            {
                mySocket.Close();
            }
        }
        public void MessageProcessTest()
        {
            // 请求5个号码.
            for (uint i = 0; i < 5; i++)
            {

                QueuingMachineMessage message = new QueuingMachineMessage()
                {
                    Header = new MessageHeader()
                    {
                        CommandID = MessageHeader.QM_NewWorkNumber_Req,
                        SequenceNo = i,
                    },
                    Body = new NewWorkNumberRequest()
                    {
                        ServiceCode = "A",
                    },
                };

                // 处理消息.
                QueuingMachineMessage resultMessage = processer.DoMessageProcess(message);

                // 结果消息非空.
                Assert.IsNotNull(resultMessage);

                // 核对消息头.
                // 核对 消息反馈类型.
                Assert.AreEqual(MessageHeader.QM_NewWorkNumber_Resp, resultMessage.Header.CommandID);
                // 核对 流水号.
                Assert.AreEqual(i, resultMessage.Header.SequenceNo);

                // 核对消息体.
                Assert.IsInstanceOf<NewWorkNumberRespond>(resultMessage.Body);

                NewWorkNumberRespond body = resultMessage.Body as NewWorkNumberRespond;
                // 叫号代码.
                Assert.AreEqual("A0000" + (i + 1), body.ResultWorkNumber);
                // 队列首代码.
                Assert.AreEqual("A00001", body.TopWorkNumber);
                // 队列长度.
                Assert.AreEqual(i + 1, body.QueueLength);

            }

             // 消费5个号码.
            for (uint i = 0; i < 5; i++)
            {
                QueuingMachineMessage message = new QueuingMachineMessage()
                {
                    Header = new MessageHeader()
                    {
                        CommandID = MessageHeader.QM_AskWorkNumber_Req ,
                        SequenceNo = i,
                    },
                    Body = new AskWorkNumberRequest()
                    {
                        ServiceCode = "A",
                    },
                };

                // 处理消息.
                QueuingMachineMessage resultMessage = processer.DoMessageProcess(message);

                // 结果消息非空.
                Assert.IsNotNull(resultMessage);

                // 核对消息头.
                // 核对 消息反馈类型.
                Assert.AreEqual(MessageHeader.QM_AskWorkNumber_Resp, resultMessage.Header.CommandID);
                // 核对 流水号.
                Assert.AreEqual(i, resultMessage.Header.SequenceNo);

                // 核对消息体.
                Assert.IsInstanceOf<AskWorkNumberRespond>(resultMessage.Body);
                AskWorkNumberRespond body = resultMessage.Body as AskWorkNumberRespond;
                // 叫号代码.
                Assert.AreEqual("A0000" + (i + 1), body.ResultWorkNumber);
            }
        }
        /// <summary>
        /// 发送请求消息.
        /// </summary>
        /// <param name="code"></param>
        private int SendMessage()
        {
            QueuingMachineMessage message = new QueuingMachineMessage()
            {
                Header = new MessageHeader()
                {
                    CommandID = MessageHeader.QM_AskWorkNumber_Req,
                    SequenceNo = sequenceNo++,
                },
                Body = new AskWorkNumberRequest()
                {
                    ServiceCode = ServiceCode,
                },
            };

            // 第一个参数:AddressFamily  = 指定 Socket 类的实例可以使用的寻址方案。
            //     Unspecified 未指定地址族。
            //     InterNetwork IP 版本 4 的地址。
            //     InterNetworkV6 IP 版本 6 的地址。
            //
            // 第二个参数:SocketType = 指定 Socket 类的实例表示的套接字类型。
            //     Stream 一个套接字类型,支持可靠、双向、基于连接的字节流,而不重复数据,也不保留边界。
            //            此类型的 Socket 与单个对方主机通信,并且在通信开始之前需要建立远程主机连接。
            //            此套接字类型使用传输控制协议 (Tcp),AddressFamily 可以是 InterNetwork,也可以是 InterNetworkV6。
            //
            // 第三个参数:ProtocolType = 指定 Socket 类支持的协议。
            //     Tcp 传输控制协议 (TCP)。
            Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                //  尝试连接主机.
                mySocket.Connect(ephost);

                // 发送消息.
                SocketMessageSender sender = new SocketMessageSender();
                sender.Socket = mySocket;
                sender.SendMessage(message);

                // 接收消息.
                SocketMessageReceiver receiver = new SocketMessageReceiver();
                receiver.Socket = mySocket;
                QueuingMachineMessage resultMessage = receiver.ReceiveMessage();

                AskWorkNumberRespond body = resultMessage.Body as AskWorkNumberRespond;

                if (body.ResultStatus == 0)
                {
                    ShowProcessMessage(String.Empty, "休息中......");

                    this.maxWorkingTimes = 0;
                    this.workingTimes = 0;
                }
                else
                {
                    ShowProcessMessage(body.ResultWorkNumber, "服务中......");

                    // 随机服务时间.
                    this.maxWorkingTimes = random.Next(5, 15);
                    this.workingTimes = 0;

                }

                return body.ResultStatus;
            }
            catch (Exception ex)
            {
                ShowProcessMessage(String.Empty,  "连接/发送/接收过程中,发生了错误!" + ex.Message );

                return 0;
            }
            finally
            {
                mySocket.Close();
            }
        }