Exemplo n.º 1
0
 // <summary>
 /// 添加一个字节数组到待发送消息队列
 /// </summary>
 /// <param name="news"></param>
 public static void AddSendNews(Byte[] news1, Byte[] news2, Byte[] news3, Byte[] news4, Byte[] news5, Byte[] news6)
 {
     waitToSendBytes.Enqueue(MessageProtocol.AddBytes(MessageProtocol.AddBytes(news1, news2, news3), MessageProtocol.AddBytes(news4, news5, news6)));
 }
Exemplo n.º 2
0
        /// <summary>
        /// 接收消息、解码并存储在 “待执行的功能块队列” 中
        /// 并发执行
        /// </summary>
        private static void ReceiveNewsFormServer()
        {
            //开始接收返回值
            Byte[]        realDataReceived = new Byte[0];
            FunctionWords functionWords    = new FunctionWords();

            try
            {
                //开始读取,遇到结束标识跳出
                while (true)
                {
                    //确保头部四字节数据完整
                    while (realDataReceived.Length < 4)
                    {
                        Byte[] RecvArray        = new Byte[bufferSize];            //用于存放接收信息
                        int    dataLengthInFact = clientSocket.Receive(RecvArray); //接收信息
                        if (dataLengthInFact <= 0)                                 //服务器主动断开连接
                        {
                            errorTimes++;
                            if (errorTimes > 3)
                            {
                                MessageBox.Show("远程服务器主动退出,立即中断操作。错误:7");
                                return;
                            }
                        }
                        else
                        {
                            errorTimes = 0;
                        }
                        realDataReceived = MessageProtocol.AddBytes(realDataReceived, RecvArray.Take(dataLengthInFact).ToArray());
                    }
                    BlockInformation blockInformation = realDataReceived.ReadBlockInformation(0);

                    //确保尾部数据完整
                    while (realDataReceived.Length < blockInformation.lengthOfAll)
                    {
                        Byte[] RecvArray        = new Byte[bufferSize];            //用于存放接收信息
                        int    dataLengthInFact = clientSocket.Receive(RecvArray); //接收信息
                        realDataReceived = MessageProtocol.AddBytes(realDataReceived, RecvArray.Take(dataLengthInFact).ToArray());
                    }

                    switch (blockInformation.blockKind)
                    {
                    case BlockKind.Start:
                        functionWords.functionKind = blockInformation.functionKind;
                        break;

                    case BlockKind.Txt:     //读取字符串
                        functionWords.stringList.Add(realDataReceived.ToTxt(4, blockInformation.LengthOfValue));
                        break;

                    case BlockKind.Image:
                        functionWords.bitmapList.Add(realDataReceived.ToBitmap(4, blockInformation.LengthOfValue));
                        break;

                    case BlockKind.File:     //预留接口
                        Console.WriteLine("文件传输功能尚未开放,错误:8");
                        break;

                    case BlockKind.End:     //读取到块结尾,说明一个功能语句读取完毕
                        receivedFunctionWords.Enqueue(functionWords);
                        functionWords = new FunctionWords();
                        break;
                    }
                    realDataReceived = realDataReceived.Skip(blockInformation.lengthOfAll).ToArray();   //去除字节数组中已经读过的部分
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接错误,异常信息:9" + ex.Message);
                return;
            }
        }
Exemplo n.º 3
0
 // <summary>
 /// 添加一个字节数组到待发送消息队列
 /// </summary>
 /// <param name="news"></param>
 public static void AddSendNews(Byte[] news1, Byte[] news2)
 {
     waitToSendBytes.Enqueue(MessageProtocol.AddBytes(news1, news2));
 }