c# 문법배우면서 웹소켓채팅 소스 구해서 살펴보고있는중입니다
서버쪽에서 소켓으로 데이타를 받아서 처리하는코드인데
이해하기가 어려워서 가르침부탁합니다
클라이언트에서는 그냥 입력된텍스트를 전송할뿐인데
서버에서는 텍스트로 처리하면될것같은데
비트연산도하고 왜 이렇게 어렵게 처리하는지
코드가 어떤의미인지 주석좀 부탁합니다
뭔지 모르지만 이해해보고싶은 코딩이라서...
public WebSocketFrame Read(NetworkStream stream)
{
// read the first byte. If the connection has been terminated abnormally then this would return an FF and we should exit
byte byte1 = (byte)stream.ReadByte();
// this condition will happen if the connection has terminated unexpectedly
if (!stream.DataAvailable && byte1 == 0xFF)
{
return new WebSocketFrame(true, WebSocketOpCode.ConnectionClose, new byte[0], false);
}
// process first byte
byte finBitFlag = 0x80;
byte opCodeFlag = 0x0F;
bool isFinBitSet = (byte1 & finBitFlag) == finBitFlag;
WebSocketOpCode opCode = (WebSocketOpCode)(byte1 & opCodeFlag);
// read and process second byte
byte byte2 = (byte)stream.ReadByte();
byte maskFlag = 0x80;
bool isMaskBitSet = (byte2 & maskFlag) == maskFlag;
uint len = ReadLength(byte2, stream);
byte[] decodedPayload;
// use the masking key to decode the data if needed
if (isMaskBitSet)
{
const int maskKeyLen = 4;
byte[] maskKey = BinaryReaderWriter.ReadExactly(maskKeyLen, stream);
byte[] encodedPayload = BinaryReaderWriter.ReadExactly((int)len, stream);
decodedPayload = new byte[len];
// apply the mask key
for (int i = 0; i < encodedPayload.Length; i++)
{
decodedPayload[i] = (Byte)(encodedPayload[i] ^ maskKey[i % maskKeyLen]);
}
}
else
{
decodedPayload = BinaryReaderWriter.ReadExactly((int)len, stream);
}
WebSocketFrame frame = new WebSocketFrame(isFinBitSet, opCode, decodedPayload, true);
return frame;
}
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.