示例#1
0
        public override void encrypt(MemoryStream stream)
        {
            int padSize = 0;

            if (stream.length() % BLOCK_SIZE != 0)
            {
                padSize      = (int)(BLOCK_SIZE - (stream.length() % BLOCK_SIZE));
                stream.wpos += padSize;

                if (stream.wpos > MemoryStream.BUFFER_MAX)
                {
                    Debug.LogError("BlowfishFilter::encrypt: stream.wpos(" + stream.wpos + ") > MemoryStream.BUFFER_MAX(" + MemoryStream.BUFFER_MAX + ")!");
                }
            }

            _blowfish.encipher(stream.data(), (int)stream.length());

            UInt16 packLen = (UInt16)(stream.length() + 1);

            _enctyptStrem.writeUint16(packLen);
            _enctyptStrem.writeUint8((UINT8)padSize);
            _enctyptStrem.append(stream.data(), (UInt32)stream.rpos, stream.length());

            stream.swap(_enctyptStrem);
            _enctyptStrem.clear();
        }
示例#2
0
 static int append(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 4);
         KBEngine.MemoryStream obj = (KBEngine.MemoryStream)ToLua.CheckObject <KBEngine.MemoryStream>(L, 1);
         byte[] arg0 = ToLua.CheckByteBuffer(L, 2);
         uint   arg1 = (uint)LuaDLL.luaL_checknumber(L, 3);
         uint   arg2 = (uint)LuaDLL.luaL_checknumber(L, 4);
         obj.append(arg0, arg1, arg2);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
示例#3
0
        public void onImportClientSDK(int remainingFiles, string fileName, int fileSize, byte[] fileDatas)
        {
            if (sdkFileStream == null)
            {
                sdkFileStream = MemoryStream.createObject();
            }

            sdkFileStream.append(fileDatas, (uint)sdkFileStream.rpos, (uint)fileDatas.Length);

            warnUpdateSDK = "Download:" + fileName + " -> " + sdkFileStream.length() + "/" + fileSize + "bytes! " + (int)(((float)downloadFiles / (float)(downloadFiles + remainingFiles)) * 100) + "%";
            Debug.Log(warnUpdateSDK);

            if (sdkFileStream.length() == fileSize)
            {
                Debug.Log("onImportClientSDK: " + fileName + "->" + fileSize + "bytes success!");

                string path = Path.GetDirectoryName(sdkTempPath + "//" + fileName);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                StreamWriter sw;
                FileInfo     t    = new FileInfo(sdkTempPath + "//" + fileName);
                string       data = System.Text.Encoding.UTF8.GetString(sdkFileStream.data(), 0, fileSize);
                sw = t.CreateText();
                sw.WriteLine(data);
                sw.Close();
                sw.Dispose();

                sdkFileStream.reclaimObject();
                sdkFileStream  = null;
                downloadFiles += 1;

                if (remainingFiles == 0)
                {
                    warnUpdateSDK = "";
                    downloadFiles = 0;
                    replaceNewSDK();
                }
            }
        }
示例#4
0
        public override void encrypt(MemoryStream stream)
        {
            int padSize = 0;

            if (stream.length() % BLOCK_SIZE != 0)
            {
                padSize      = (int)(BLOCK_SIZE - (stream.length() % BLOCK_SIZE));
                stream.wpos += padSize;
                Debug.Assert(stream.wpos <= MemoryStream.BUFFER_MAX);
            }

            _blowfish.encipher(stream.data(), (int)stream.length());

            UInt16 packLen = (UInt16)(stream.length() + 1);

            _enctyptStrem.writeUint16(packLen);
            _enctyptStrem.writeUint8((UINT8)padSize);
            _enctyptStrem.append(stream.data(), (UInt32)stream.rpos, stream.length());

            stream.swap(_enctyptStrem);
            _enctyptStrem.clear();
        }
示例#5
0
        public override void process(byte[] datas, MessageLengthEx offset, MessageLengthEx length)
        {
            MessageLengthEx totallen = offset;

            while (length > 0 && expectSize > 0)
            {
                if (state == READ_STATE.READ_STATE_MSGID)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;
                        msgid        = stream.readUint16();
                        stream.clear();

                        Message msg = Messages.clientMessages[msgid];

                        if (msg.msglen == -1)
                        {
                            state      = READ_STATE.READ_STATE_MSGLEN;
                            expectSize = 2;
                        }
                        else if (msg.msglen == 0)
                        {
                            // 如果是0个参数的消息,那么没有后续内容可读了,处理本条消息并且直接跳到下一条消息
                                                        #if UNITY_EDITOR
                            Dbg.profileStart(msg.name);
                                                        #endif

                            msg.handleMessage(stream);

                                                        #if UNITY_EDITOR
                            Dbg.profileEnd(msg.name);
                                                        #endif

                            state      = READ_STATE.READ_STATE_MSGID;
                            expectSize = 2;
                        }
                        else
                        {
                            expectSize = (MessageLengthEx)msg.msglen;
                            state      = READ_STATE.READ_STATE_BODY;
                        }
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MSGLEN)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        msglen = stream.readUint16();
                        stream.clear();

                        // 长度扩展
                        if (msglen >= 65535)
                        {
                            state      = READ_STATE.READ_STATE_MSGLEN_EX;
                            expectSize = 4;
                        }
                        else
                        {
                            state      = READ_STATE.READ_STATE_BODY;
                            expectSize = msglen;
                        }
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MSGLEN_EX)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        expectSize = stream.readUint32();
                        stream.clear();

                        state = READ_STATE.READ_STATE_BODY;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_BODY)
                {
                    if (length >= expectSize)
                    {
                        stream.append(datas, totallen, expectSize);
                        totallen += expectSize;
                        length   -= expectSize;

                        Message msg = Messages.clientMessages[msgid];

#if UNITY_EDITOR
                        Dbg.profileStart(msg.name);
#endif

                        msg.handleMessage(stream);

#if UNITY_EDITOR
                        Dbg.profileEnd(msg.name);
#endif

                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGID;
                        expectSize = 2;
                    }
                    else
                    {
                        stream.append(datas, totallen, length);
                        expectSize -= length;
                        break;
                    }
                }
            }
        }
示例#6
0
        public override bool recv(MessageReader reader, byte[] buffer, UInt32 rpos, UInt32 len)
        {
            if (!_blowfish.isGood())
            {
                Dbg.ERROR_MSG("BlowfishFilter::recv: Dropping packet, due to invalid filter");
                return(false);
            }

            if (_packet.length() == 0 && len >= MIN_PACKET_SIZE && BitConverter.ToUInt16(buffer, (int)rpos) - 1 == len - 3)
            {
                int packLen = BitConverter.ToUInt16(buffer, (int)rpos) - 1;
                int padSize = buffer[rpos + 2];

                decrypt(buffer, (int)(rpos + 3), (int)(len - 3));

                int length = packLen - padSize;
                if (reader != null)
                {
                    reader.process(buffer, rpos + 3, (UInt32)length);
                }

                return(true);
            }

            _packet.append(buffer, rpos, len);

            while (_packet.length() > 0)
            {
                UInt32 currLen = 0;
                int    oldwpos = 0;
                if (_packLen <= 0)
                {
                    // 如果满足一个最小包则尝试解包, 否则缓存这个包待与下一个包合并然后解包
                    if (_packet.length() >= MIN_PACKET_SIZE)
                    {
                        _packLen = _packet.readUint16();
                        _padSize = _packet.readUint8();

                        _packLen -= 1;

                        if (_packet.length() > _packLen)
                        {
                            currLen      = (UInt32)(_packet.rpos + _packLen);
                            oldwpos      = _packet.wpos;
                            _packet.wpos = (int)currLen;
                        }
                        else if (_packet.length() < _packLen)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    // 如果上一次有做过解包行为但包还没有完整则继续处理
                    // 如果包是完整的下面流程会解密, 如果有多余的内容需要将其剪裁出来待与下一个包合并
                    if (_packet.length() > _packLen)
                    {
                        currLen      = (UInt32)(_packet.rpos + _packLen);
                        oldwpos      = _packet.wpos;
                        _packet.wpos = (int)currLen;
                    }
                    else if (_packet.length() < _packLen)
                    {
                        return(false);
                    }
                }

                decrypt(_packet);
                _packet.wpos -= _padSize;

                // 上面的流程能保证wpos之后不会有多余的包
                // 如果有多余的包数据会放在_recvStream
                if (reader != null)
                {
                    reader.process(_packet.data(), (UInt32)_packet.rpos, _packet.length());
                }

                if (currLen > 0)
                {
                    _packet.rpos = (int)currLen;
                    _packet.wpos = oldwpos;
                }
                else
                {
                    _packet.clear();
                }

                _packLen = 0;
                _padSize = 0;
            }

            return(true);
        }
        /*
            从二进制流导入消息协议
        */
        public bool importMessagesFromMemoryStream(byte[] loginapp_clientMessages, byte[] baseapp_clientMessages, byte[] entitydefMessages, byte[] serverErrorsDescr)
        {
            resetMessages();

            loadingLocalMessages_ = true;
            MemoryStream stream = new MemoryStream();
            stream.append(loginapp_clientMessages, (UInt32)0, (UInt32)loginapp_clientMessages.Length);
            currserver = "loginapp";
            onImportClientMessages(stream);

            stream = new MemoryStream();
            stream.append(baseapp_clientMessages, (UInt32)0, (UInt32)baseapp_clientMessages.Length);
            currserver = "baseapp";
            onImportClientMessages(stream);
            currserver = "loginapp";

            stream = new MemoryStream();
            stream.append(serverErrorsDescr, (UInt32)0, (UInt32)serverErrorsDescr.Length);
            onImportServerErrorsDescr(stream);

            stream = new MemoryStream();
            stream.append(entitydefMessages, (UInt32)0, (UInt32)entitydefMessages.Length);
            onImportClientEntityDef(stream);

            loadingLocalMessages_ = false;
            loginappMessageImported_ = true;
            baseappMessageImported_ = true;
            entitydefImported_ = true;
            isImportServerErrorsDescr_ = true;

            currserver = "";
            Dbg.DEBUG_MSG("KBEngine::importMessagesFromMemoryStream(): is successfully!");
            return true;
        }