[Mediaserver]mini_Mediaserver_1-코드설명/code description-amf0.cpp(1-9)

👉🏻 코드 설명 / Code Explanation

✔️ void Amf0::write_string(std::vector& buf, const std::string& str)

— 버퍼만 키우고 값을 반환하지 않습니다.
It only expands the buffer and does not return a value.

— push_back : vector의 현재 끝에 그 값을 복사해서 붙입니다.
push_back: Copies the value and appends it to the current end of the vector.

— AMF0에서는 이 첫 바이트 0x02가 “지금부터는 문자열이다”라는 타입 마커 역할을 합니다.
In AMF0, this first byte, 0x02, acts as a type marker indicating “what follows is a string.”

— 그래서 디코더가 뒤에 오는 길이를 문자열로 해석하게 만드는 신호입니다.
So, it is a signal that tells the decoder to interpret the length that follows as a string.

buf.push_back(0x02);

— UTF-8 기준: str.size()는 바이트 수입니다.
In UTF-8: str.size() represents the number of bytes.

— 문자 개수가 아닙니다.
It is not the number of characters.

uint16_t len = str.size();

— len의 값이 6인 경우라고 가정 할 경우
Assuming the value of len is 6

— ex: len(16bit) = 0000 0000 0000 0110₂ = 6

— len >> 8 = {0000 0000 }{0000 0000 0000 0110 } → 8칸 이동/Move 8 spaces →

— 0xFF = 1111 1111₂

— 비트 연산하면 0000 0110 → 0x06
Performing a bitwise operation yields 0000 0110 → 0x06.

— len → 0000 0000 0000 0110 → 8비트이동 → 오른쪽으로 모두 밀려서 0000 0000 0000 0000
0xFF → 0000 0000 1111 1111 ← 앞에 0을 자동으로 채움

len → 0000 0000 0000 0110 → 8-bit shift → shifted entirely to the right, resulting in 0000 0000 0000 0000
0xFF → 0000 0000 1111 1111 ← leading zeros are automatically filled in

— 그래서 같인 and연산하면 둘다1이어야 1이되니까 값은 그냥 0000 0000
So, when you perform an AND operation, the result is 1 only if both bits are 1; therefore, the value is simply 0000 0000.

— 백터가 8비트 이므로 8비트까지만입력 할 수 있고 16비트의 경우 뒤에서 8자리수(8비트)까지만 입력되고 나머지는잘림
Since the vector is 8-bit, only up to 8 bits can be entered, and in the case of 16-bit, only the last 8 digits (8 bits) are entered and the rest is truncated.

— 그래서 백터에는 0이 입력됩니다.
Therefore, 0 is input into the vector.

buf.push_back((len >> 8) & 0xFF);

— 위와 같지만 백터에 8비트를 입력하면 되기 때문에 0000 00110이 값을 만들면됩니다.
It is the same as above, but since you need to input 8 bits into the vector, you simply need to create the value 0000 0110.

— 즉 len = 0000 00110 ,0xFF = 1111 1111 이 두 값을 and 연산하면 동일하게 0000 00110입니다.
In other words, performing an AND operation on the two values—len = 0000 00110 and 0xFF = 1111 1111—results in 0000 00110.

— 이 값은 십진수로 6이고 백터에 저장합니다.
This value is 6 in decimal, and it is stored in the vector.

— 그러면 최종 0,6이 차례대로 백터에 저장한 결과를 수행합니다.
Then, the final result of 0.6 is stored in the vector in sequence.

— 이렇게 바이트로 쪼개는 이유는 리틀엔디언 CPU에서도, 빅엔디언 CPU에서도 똑같이 빅엔디언 바이트열을 만들게 하기 위해서입니다.
The reason for breaking the data down into bytes in this way is to ensure that a big-endian byte sequence is produced consistently, regardless of whether the CPU is little-endian or big-endian.

buf.push_back(len & 0xFF);

— buf백터에 문자열 추가
Append a string to the buf vector.

— 문자열이 string인 경우 s,t,r,i,n,g로 추가됩니다.
If the string is “string”, it is added as s, t, r, i, n, g.

buf.insert(buf.end(), str.begin(), str.end());

Leave a Reply