게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
c# 문법배우면서 웹소켓채팅 소스 비트연산포함
게시물ID : programmer_15091짧은주소 복사하기
작성자 : 엿먹이기
추천 : 0
조회수 : 985회
댓글수 : 2개
등록시간 : 2015/12/20 20:19:23
옵션
  • 본인삭제금지
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;
        }
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호