Exemplo n.º 1
0
        void _UnPack(SocketEvent ev, NetSocket socket, object param)
        {
            MsgProcesser <HeaderType, MsgType> msgProcesser = (MsgProcesser <HeaderType, MsgType>)param;

            socket.SetMsgProcesser(msgProcesser);
            msgProcesser.UnPack(ev, socket);
        }
Exemplo n.º 2
0
        private bool RecvRespUpgradeProtoco(NetSocket socket, HttpHeader header, ByteStream dataStream)
        {
            // DEBUG("接收到远端%s响应请求更换通信协议。", socket.remoteIP);

            if (header.isVaild == false)
            {
                return(true);
            }

            if (header.stateCode != 101)
            {
                return(true);
            }


            if (!header.fieldMap.ContainsKey("Upgrade") ||
                !header.fieldMap.ContainsKey("Connection") ||
                !header.fieldMap.ContainsKey("Sec-WebSocket-Accept") ||
                header.fieldMap["Connection"].CompareTo("Upgrade") != 0)
            {
                // DEBUG("远端%s响应请求更换通信协议不合法。", socket.remoteIP);
                socket.Close();
                return(true);
            }

            //
            string stroIn = header.fieldMap["Sec-WebSocket-Accept"];

            byte[] byteArray = Convert.FromBase64String(stroIn);
            string pOut      = Encoding.Default.GetString(byteArray);


            byte[] bytedata = Encoding.Default.GetBytes(Sec_WebSocket_Key);
            string seckey   = Convert.ToBase64String(bytedata);
            string key      = seckey + Sec_WebSocket_Key_2;


            SHA1 sha1 = new SHA1CryptoServiceProvider();

            byte[] bytes_in  = Encoding.Default.GetBytes(key);
            byte[] bytes_out = sha1.ComputeHash(bytes_in);
            string chSha1    = Encoding.Default.GetString(bytes_out);

            if (chSha1.CompareTo(pOut) != 0)
            {
                //DEBUG("远端%s响应请求更换通信协议不合法。", socket.remoteIP);
                socket.Close();
                return(true);
            }

            if (header.fieldMap["Upgrade"].CompareTo("GamePackProtocol") == 0)
            {
                socket.SetDePacketor(gamepackDePacketor);
                socket.SetMsgProcesser(gamepackMsgProcesser);
            }

            return(true);
        }
Exemplo n.º 3
0
        public void UnPack(SocketEvent ev, NetSocket socket)
        {
            BaseMsgProcesser msgProcesser = socket.GetMsgProcesser();

            if (msgProcesser != null)
            {
                msgProcesser.UnPack(ev, socket);
            }
            else if (unPack != null)
            {
                unPack(ev, socket);
                socket.SetMsgProcesser((BaseMsgProcesser)unPackParam);
            }
        }