示例#1
0
        public void OnDataReceived(byte[] data, int byteCount)
        {
            int offset = 0;
            while (offset < byteCount)
            {
                if (m_header == null)
                {
                    if (m_headerBytesReceived < 5)
                    {
                        m_headerBytes[m_headerBytesReceived++] = data[offset++];
                    }
                    else if (m_headerBytesReceived == 5)
                    {
                        m_header = new VoicePacketHeader();
                        m_header.Parse(m_headerBytes);
                        if (m_header.length > 65535)
                        {
                            throw new Exception("Packet size " + m_header.length + " > 65535");
                        }

                        m_buffer = new byte[m_header.length];
                        m_offset = 0;
                        m_headerBytesReceived = 0;
                    }
                }
                else
                {
                    int bytesToCopy = m_header.length-m_offset;
                    if (bytesToCopy > byteCount - offset)
                        bytesToCopy = byteCount - offset;

                    Buffer.BlockCopy(data, offset, m_buffer, m_offset, bytesToCopy);

                    offset += bytesToCopy;
                    m_offset += bytesToCopy;

                    if (m_offset == m_header.length)
                    {
                        ParsePacket(m_header.type, m_buffer);
                        m_header = null;
                    }
                }
            }
        }
示例#2
0
        public byte[] GetBytes()
        {
            VoicePacketHeader header = new VoicePacketHeader();
            byte[] bytes = new byte[5+16+4+m_audioData.Length];

            header.length = bytes.Length-5;

            //ToClient packets are type 2
            header.type = 2;

            int pos = 0;
            header.CopyTo(bytes, pos); pos += 5;
            m_clientId.GetBytes().CopyTo(bytes, pos); pos += 16;

            bytes[pos++] = (byte)((m_codec) % 256);
            bytes[pos++] = (byte)((m_codec << 8) % 256);
            bytes[pos++] = (byte)((m_codec << 16) % 256);
            bytes[pos++] = (byte)((m_codec << 24) % 256);

            m_audioData.CopyTo(bytes, pos);
            return bytes;
        }