Example #1
0
        public override bCAPPacket SendAndRecv(bCAPPacket send)
        {
            var recv = new bCAPPacket();

            lock (m_lock)
            {
                // シリアル番号を設定
                send.iSerial = m_iSerial;
                send.iReserv = 0;

                // パケット送信
                SendPacket(send);

                // シリアル番号を更新
                m_iSerial = (UInt16)((m_iSerial != UInt16.MaxValue) ? (m_iSerial + 1) : 1);

                // パケットを受信
                recv = RecvPacket(send.iSerial);
            }

            if (recv.iFuncID < 0)
            {
                throw new ORiN2Exception(recv.iFuncID, null);
            }

            return(recv);
        }
Example #2
0
        public override bCAPPacket RecvPacket(UInt16 iSerial)
        {
            byte[] byRecv;
            var    recv     = new bCAPPacket();
            var    ipEpRecv = new IPEndPoint(IPAddress.Any, 0);

            while (true)
            {
                try
                {
                    // データグラム受信
                    byRecv = m_udpStream.Receive(ref ipEpRecv);
                    if (!ipEpRecv.Equals(m_ipEpSend))
                    {
                        continue;
                    }

                    // 受信パケットが送信パケットの応答であり、かつ、S_EXECUTINGでなければ終わり
                    recv = m_conv.Deserialize(byRecv);
                    if ((recv.iSerial == iSerial) && (recv.iFuncID != HResult.S_EXECUTING))
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return(recv);
        }
Example #3
0
        public byte[] Serialize(bCAPPacket packet)
        {
            byte[] bRet = null;

            try
            {
                using (var ms = new MemoryStream())
                {
                    using (var bw = new BinaryWriter(ms))
                    {
                        // SOH
                        bw.Write(BCAP_SOH);

                        // Message Length(最後に正しい値を入力)
                        bw.Write((Int32)0);

                        // Serial Number
                        bw.Write(bCAPByteConverter.Value2Byte(packet.iSerial));

                        // Reserve Number
                        bw.Write(bCAPByteConverter.Value2Byte(packet.iReserv));

                        // Function ID
                        bw.Write(bCAPByteConverter.Value2Byte(packet.iFuncID));

                        // Number of arguments
                        bw.Write(bCAPByteConverter.Value2Byte((Int16)packet.aryArgs.Count));

                        // Arguments
                        Serialize_ObjectArray(bw, packet.aryArgs, true);

                        // EOH
                        bw.Write(BCAP_EOT);

                        // Message Length(正しい値を入力)
                        bw.BaseStream.Position = 1;
                        bw.Write(bCAPByteConverter.Value2Byte((Int32)ms.Length));
                    }

                    bRet = ms.ToArray();
                }
            }
            catch (Exception ex)
            {
                throw new ORiN2Exception(HResult.E_INVALIDARG, ex);
            }

            return(bRet);
        }
Example #4
0
        public override bCAPPacket SendAndRecv(bCAPPacket send)
        {
            var recv = new bCAPPacket();

            lock (m_lock)
            {
                // シリアル番号を設定
                send.iSerial = m_iSerial;
                send.iReserv = m_iSerial;

                int iRetry = 0;
retry_proc:
                try
                {
                    // シリアル番号を設定(リトライ)
                    send.iSerial = m_iSerial;

                    // パケットを送信
                    SendPacket(send);

                    // シリアル番号を更新
                    m_iSerial = (UInt16)((m_iSerial != UInt16.MaxValue) ? (m_iSerial + 1) : 1);

                    // パケットを受信
                    recv = RecvPacket(send.iSerial);
                }
                catch (Exception ex)
                {
                    // リトライ回数を超過するまで繰り返す
                    iRetry++;
                    if (iRetry < m_iRetry)
                    {
                        goto retry_proc;
                    }

                    // 例外をスロー
                    throw ex;
                }
            }

            if (recv.iFuncID < 0)
            {
                throw new ORiN2Exception(recv.iFuncID, null);
            }

            return(recv);
        }
Example #5
0
        public bCAPPacket Deserialize(byte[] buf)
        {
            var packet = new bCAPPacket();

            try
            {
                using (var ms = new MemoryStream(buf))
                {
                    using (var br = new BinaryReader(ms))
                    {
                        // Message Length
                        br.BaseStream.Position = 1;
                        Int32 len = (Int32)bCAPByteConverter.Byte2Value(br.ReadBytes(4), typeof(Int32));

                        // パケットの確認
                        if ((buf[0] != BCAP_SOH) ||
                            (len > buf.Length) || (buf[len - 1] != BCAP_EOT))
                        {
                            throw new ArgumentException();
                        }

                        // Serial Number
                        packet.iSerial = (UInt16)bCAPByteConverter.Byte2Value(br.ReadBytes(2), typeof(UInt16));

                        // Reserve Number
                        packet.iReserv = (UInt16)bCAPByteConverter.Byte2Value(br.ReadBytes(2), typeof(UInt16));

                        // Function ID
                        packet.iFuncID = (Int32)bCAPByteConverter.Byte2Value(br.ReadBytes(4), typeof(Int32));

                        // Number of arguments
                        Int16 argc = (Int16)bCAPByteConverter.Byte2Value(br.ReadBytes(2), typeof(Int16));

                        // Arguments
                        packet.aryArgs.AddRange(Deserialize_ObjectArray(br, argc, true));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ORiN2Exception(HResult.E_INVALIDPACKET, ex);
            }

            return(packet);
        }
Example #6
0
        public override bCAPPacket RecvPacket(UInt16 iSerial)
        {
            byte[] byRecv;
            var    recv = new bCAPPacket();

            using (var ms = new MemoryStream())
            {
                var br = new BinaryReader(m_tcpStream.GetStream());

                while (true)
                {
                    try
                    {
                        // ストリームを初期化
                        ms.Position = 0;
                        ms.SetLength(0);

                        // SOH
                        ms.WriteByte(br.ReadByte());

                        // Message Length
                        byRecv = br.ReadBytes(4);
                        ms.Write(byRecv, 0, byRecv.Length);

                        // 残りを受信
                        byRecv = br.ReadBytes((Int32)bCAPByteConverter.Byte2Value(byRecv, typeof(Int32)) - 5);
                        ms.Write(byRecv, 0, byRecv.Length);

                        // 受信パケットが送信パケットの応答であり、かつ、S_EXECUTINGでなければ終わり
                        recv = m_conv.Deserialize(ms.ToArray());
                        if ((recv.iSerial == iSerial) && (recv.iFuncID != HResult.S_EXECUTING))
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

            return(recv);
        }
Example #7
0
 public abstract bCAPPacket SendAndRecv(bCAPPacket send);
Example #8
0
 public abstract void SendPacket(bCAPPacket send);
Example #9
0
 public override void SendPacket(bCAPPacket send)
 {
     byte[] bySend = m_conv.Serialize(send);
     m_tcpStream.GetStream().Write(bySend, 0, bySend.Length);
 }
Example #10
0
 public override void SendPacket(bCAPPacket send)
 {
     byte[] bySend = m_conv.Serialize(send);
     m_udpStream.Send(bySend, bySend.Length, m_ipEpSend);
 }