|
1 | 1 | /***************************************************************************** |
2 | 2 | * |
3 | | - * PROJECT: Multi Theft Auto v1.0 |
| 3 | + * PROJECT: Multi Theft Auto |
4 | 4 | * LICENSE: See LICENSE in the top level directory |
5 | 5 | * FILE: mods/deathmatch/logic/packets/CVoiceDataPacket.cpp |
6 | 6 | * PURPOSE: Voice data packet class |
7 | 7 | * |
8 | | - * Multi Theft Auto is available from https://www.multitheftauto.com/ |
| 8 | + * Multi Theft Auto is available from https://multitheftauto.com/ |
9 | 9 | * |
10 | 10 | *****************************************************************************/ |
11 | 11 |
|
|
15 | 15 |
|
16 | 16 | CVoiceDataPacket::CVoiceDataPacket() |
17 | 17 | { |
18 | | - m_pBuffer = NULL; |
19 | | - m_usDataBufferSize = 0; |
20 | | - m_usActualDataLength = 0; |
21 | | - |
22 | | - AllocateBuffer(1024); |
23 | | -} |
24 | | - |
25 | | -CVoiceDataPacket::CVoiceDataPacket(CPlayer* pPlayer, const unsigned char* pbSrcBuffer, unsigned short usLength) |
26 | | -{ |
27 | | - m_pBuffer = NULL; |
28 | | - m_usDataBufferSize = 0; |
29 | | - m_usActualDataLength = 0; |
30 | | - |
31 | | - SetSourceElement(pPlayer); |
32 | | - SetData(pbSrcBuffer, usLength); |
33 | | -} |
34 | | -CVoiceDataPacket::~CVoiceDataPacket() |
35 | | -{ |
36 | | - DeallocateBuffer(); |
| 18 | + m_voiceBuffer.reserve(2048); |
37 | 19 | } |
38 | 20 |
|
39 | 21 | bool CVoiceDataPacket::Read(NetBitStreamInterface& BitStream) |
40 | 22 | { |
41 | | - if (m_pBuffer) |
| 23 | + unsigned short voiceBufferLength{}; |
| 24 | + |
| 25 | + if (BitStream.Read(voiceBufferLength) && voiceBufferLength <= m_voiceBuffer.capacity()) |
42 | 26 | { |
43 | | - BitStream.Read(m_usActualDataLength); |
44 | | - if (m_usActualDataLength) |
45 | | - { |
46 | | - BitStream.Read(reinterpret_cast<char*>(m_pBuffer), m_usActualDataLength <= m_usDataBufferSize ? m_usActualDataLength : m_usDataBufferSize); |
47 | | - } |
48 | | - return true; |
| 27 | + m_voiceBuffer.resize(voiceBufferLength); |
| 28 | + return BitStream.Read(reinterpret_cast<char*>(m_voiceBuffer.data()), m_voiceBuffer.size()); |
49 | 29 | } |
50 | 30 |
|
51 | 31 | return false; |
52 | 32 | } |
53 | 33 |
|
54 | 34 | bool CVoiceDataPacket::Write(NetBitStreamInterface& BitStream) const |
55 | 35 | { |
56 | | - if (m_usActualDataLength) |
| 36 | + if (!m_voiceBuffer.empty()) |
57 | 37 | { |
| 38 | + const auto voiceBuffer = reinterpret_cast<const char*>(m_voiceBuffer.data()); |
| 39 | + const auto voiceBufferLength = static_cast<uint16_t>(m_voiceBuffer.size()); |
| 40 | + |
58 | 41 | // Write the source player |
59 | | - ElementID ID = m_pSourceElement->GetID(); |
60 | | - BitStream.Write(ID); |
| 42 | + BitStream.Write(m_pSourceElement->GetID()); |
61 | 43 | // Write the length as an unsigned short and then write the string |
62 | | - BitStream.Write(m_usActualDataLength); |
63 | | - BitStream.Write(reinterpret_cast<const char*>(m_pBuffer), m_usActualDataLength); |
| 44 | + BitStream.Write(voiceBufferLength); |
| 45 | + BitStream.Write(voiceBuffer, voiceBufferLength); |
64 | 46 | return true; |
65 | 47 | } |
66 | 48 |
|
67 | 49 | return false; |
68 | 50 | } |
69 | 51 |
|
70 | | -void CVoiceDataPacket::AllocateBuffer(unsigned short usBufferSize) |
71 | | -{ |
72 | | - // Test to see if we already have an allocated buffer |
73 | | - // that will hold the requested size. |
74 | | - if (m_usDataBufferSize < usBufferSize) |
75 | | - { |
76 | | - // It's not... resize the buffer. |
77 | | - if (m_pBuffer) |
78 | | - { |
79 | | - // Free the current buffer. |
80 | | - delete[] m_pBuffer; |
81 | | - } |
82 | | - |
83 | | - // Allocate new buffer. |
84 | | - m_pBuffer = new unsigned char[usBufferSize]; |
85 | | - |
86 | | - // Clear buffer. |
87 | | - memset(m_pBuffer, 0, usBufferSize); |
88 | | - |
89 | | - // Save the new size. |
90 | | - m_usDataBufferSize = usBufferSize; |
91 | | - } |
92 | | -} |
93 | | - |
94 | | -void CVoiceDataPacket::DeallocateBuffer() |
| 52 | +void CVoiceDataPacket::SetVoiceData(const unsigned char* voiceBuffer, unsigned short voiceBufferLength) |
95 | 53 | { |
96 | | - if (m_pBuffer) |
97 | | - { |
98 | | - delete[] m_pBuffer; |
99 | | - m_pBuffer = NULL; |
100 | | - m_usDataBufferSize = 0; |
101 | | - m_usActualDataLength = 0; |
102 | | - } |
103 | | -} |
| 54 | + m_voiceBuffer.clear(); |
104 | 55 |
|
105 | | -void CVoiceDataPacket::SetData(const unsigned char* pbSrcBuffer, unsigned short usLength) |
106 | | -{ |
107 | | - // Allocate new buffer. |
108 | | - AllocateBuffer(usLength); |
| 56 | + if (!voiceBuffer || !voiceBufferLength || voiceBufferLength > m_voiceBuffer.capacity()) |
| 57 | + return; |
109 | 58 |
|
110 | | - // Copy in the data. |
111 | | - if (m_pBuffer) |
112 | | - { |
113 | | - memcpy(m_pBuffer, pbSrcBuffer, usLength); |
114 | | - m_usActualDataLength = usLength; |
115 | | - } |
116 | | -} |
117 | | - |
118 | | -const unsigned char* CVoiceDataPacket::GetData() const |
119 | | -{ |
120 | | - return m_pBuffer; |
121 | | -} |
122 | | - |
123 | | -unsigned short CVoiceDataPacket::GetDataLength() const |
124 | | -{ |
125 | | - return m_usActualDataLength; |
| 59 | + m_voiceBuffer.resize(voiceBufferLength); |
| 60 | + std::copy_n(voiceBuffer, voiceBufferLength, m_voiceBuffer.data()); |
126 | 61 | } |
0 commit comments