Exemplo n.º 1
0
        /// <summary>
        /// 通信処理本体
        /// </summary>
        /// <param name="stream">送受信ストリーム</param>
        private void communicationMain(NetworkStream stream)
        {
            while (true)
            {
                Thread.Sleep(10);

                if (m_StopRequest == null)
                {
                    break;
                }

                if (m_StopRequest.WaitOne(10) == true)
                {
                    m_TcpClient.Close();
                    m_TcpClient.Dispose();
                    break;
                }

                if (m_RequestQueue.Count <= 0)
                {
                    continue;
                }

                CommandBase command = m_RequestQueue.Dequeue();

                int    expectedSize = 0;
                byte[] sendData     = command.CreateCommand(out expectedSize);
                byte[] response     = new byte[expectedSize];

                try
                {
                    stream.Write(sendData, 0, sendData.Length);
                }
                catch (Exception)
                {
                    goto FINISH;
                }

                try
                {
                    int index  = 0;
                    int remain = expectedSize;
                    while (0 < remain)
                    {
                        int once = stream.Read(response, index, remain);
                        index  += once;
                        remain -= once;
                    }
                }
                catch (Exception)
                {
                    goto FINISH;
                }

                command.Analyze(response);
            }

FINISH:
            return;
        }