示例#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
        public void writeMsgLength()
        {
            if (msgtype.msglen != -1)
            {
                return;
            }

            MemoryStream stream = this.stream;

            if (_curMsgStreamIndex > 0)
            {
                stream = streamList[streamList.Count - _curMsgStreamIndex];
            }
            stream.data()[2] = (Byte)(messageLength & 0xff);
            stream.data()[3] = (Byte)(messageLength >> 8 & 0xff);
        }
示例#3
0
        public override bool send(MemoryStream stream)
        {
            if (!valid())
            {
                throw new ArgumentException("invalid socket!");
            }

            return(kcp_.Send(stream.data(), stream.rpos, (int)stream.length()) >= 0);
        }
示例#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 bool send(MemoryStream stream)
        {
            if (!valid())
            {
                throw new ArgumentException("invalid socket!");
            }

            if (_filter != null)
            {
                _filter.encrypt(stream);
            }

            nextTickKcpUpdate = 0;
            return(kcp_.Send(stream.data(), stream.rpos, (int)stream.length()) >= 0);
        }
示例#6
0
 static int data(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         KBEngine.MemoryStream obj = (KBEngine.MemoryStream)ToLua.CheckObject <KBEngine.MemoryStream>(L, 1);
         byte[] o = obj.data();
         ToLua.Push(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
示例#7
0
        public void writeMsgLength()
        {
            if (msgtype.msglen != -1)
            {
                return;
            }

            if (stream.opsize() >= messageLength)
            {
                int idx = (int)stream.opsize() - messageLength - 2;
                stream.data()[idx]     = (Byte)(messageLength & 0xff);
                stream.data()[idx + 1] = (Byte)(messageLength >> 8 & 0xff);
            }
            else
            {
                int    size = messageLength - (int)stream.opsize();
                byte[] data = streamList[numMessage - 1].data();

                int idx = data.Length - size - 2;

                data[idx]     = (Byte)(messageLength & 0xff);
                data[idx + 1] = (Byte)(messageLength >> 8 & 0xff);
            }
        }
示例#8
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();
                }
            }
        }
示例#9
0
        public void Client_onUpdatePropertys(MemoryStream stream)
        {
            Int32 eid = stream.readInt32();
            Entity entity = null;

            if(!entities.TryGetValue(eid, out entity))
            {
                MemoryStream entityMessage = null;
                if(bufferedCreateEntityMessage.TryGetValue(eid, out entityMessage))
                {
                    Dbg.ERROR_MSG("KBEngine::Client_onUpdatePropertys: entity(" + eid + ") not found!");
                    return;
                }

                MemoryStream stream1 = new MemoryStream();
                stream1.wpos = stream.wpos;
                stream1.rpos = stream.rpos - 4;
                Array.Copy(stream.data(), stream1.data(), stream.data().Length);
                bufferedCreateEntityMessage[eid] = stream1;
                return;
            }

            Dictionary<UInt16, Property> pdatas = EntityDef.moduledefs[entity.classtype].idpropertys;
            while(stream.opsize() > 0)
            {
                UInt16 utype = stream.readUint16();
                Property propertydata = pdatas[utype];
                System.Reflection.MethodInfo setmethod = propertydata.setmethod;

                object val = propertydata.utype.createFromStream(stream);
                object oldval = entity.getDefinedProptertyByUType(utype);

                Dbg.DEBUG_MSG("KBEngine::Client_onUpdatePropertys: " + entity.classtype + "(id=" + eid  + " " + propertydata.name + "=" + val + "), hasSetMethod=" + setmethod + "!");

                entity.setDefinedProptertyByUType(utype, val);
                if(setmethod != null)
                {
                    setmethod.Invoke(entity, new object[]{oldval});
                }
            }
        }
        public bool send(MemoryStream stream)
        {
            int dataLength = (int)stream.length();
            if (dataLength <= 0)
                return true;

            if (0 == Interlocked.Add(ref _sending, 0))
            {
                if (_wpos == _spos)
                {
                    _wpos = 0;
                    _spos = 0;
                }
            }

            int t_spos = Interlocked.Add(ref _spos, 0);
            int space = 0;
            int tt_wpos = _wpos % _buffer.Length;
            int tt_spos = t_spos % _buffer.Length;

            if(tt_wpos >= tt_spos)
                space = _buffer.Length - tt_wpos + tt_spos - 1;
            else
                space = tt_spos - tt_wpos - 1;

            if (dataLength > space)
            {
                Dbg.ERROR_MSG("PacketSender::send(): no space, Please adjust 'SEND_BUFFER_MAX'! data(" + dataLength
                    + ") > space(" + space + "), wpos=" + _wpos + ", spos=" + t_spos);

                return false;
            }

            int expect_total = tt_wpos + dataLength;
            if(expect_total <= _buffer.Length)
            {
                Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, dataLength);
            }
            else
            {
                int remain = _buffer.Length - tt_wpos;
                Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, remain);
                Array.Copy(stream.data(), stream.rpos + remain, _buffer, 0, expect_total - _buffer.Length);
            }

            Interlocked.Add(ref _wpos, dataLength);

            if (Interlocked.CompareExchange(ref _sending, 1, 0) == 0)
            {
                _startSend();
            }

            return true;
        }
示例#11
0
        /*
            服务端错误描述导入了
        */
        public void Client_onImportServerErrorsDescr(MemoryStream stream)
        {
            byte[] datas = new byte[stream.wpos - stream.rpos];
            Array.Copy(stream.data(), stream.rpos, datas, 0, stream.wpos - stream.rpos);

            onImportServerErrorsDescr (stream);

            if(_persistentInofs != null)
                _persistentInofs.onImportServerErrorsDescr(datas);
        }
示例#12
0
        /*
            从服务端返回的二进制流导入客户端消息协议
        */
        public void Client_onImportClientMessages(MemoryStream stream)
        {
            byte[] datas = new byte[stream.wpos - stream.rpos];
            Array.Copy (stream.data (), stream.rpos, datas, 0, stream.wpos - stream.rpos);

            onImportClientMessages (stream);

            if(_persistentInofs != null)
                _persistentInofs.onImportClientMessages(currserver, datas);
        }
示例#13
0
        protected override void onAsyncConnect(ConnectState state)
        {
            try
            {
                //state.socket.Connect(state.connectIP, state.connectPort);

                byte[] helloPacket = System.Text.Encoding.ASCII.GetBytes(UDP_HELLO);
                state.socket.SendTo(helloPacket, helloPacket.Length, SocketFlags.None, new IPEndPoint(IPAddress.Parse(state.connectIP), state.connectPort));

                ArrayList readList = new ArrayList();
                readList.Add(state.socket);
                Socket.Select(readList, null, null, 3000000);

                if (readList.Count > 0)
                {
                    byte[] buffer = new byte[UDP_PACKET_MAX];
                    int    length = state.socket.Receive(buffer);

                    if (length <= 0)
                    {
                        Dbg.ERROR_MSG(string.Format("NetworkInterfaceKCP::_asyncConnect(), failed to connect to '{0}:{1}'! receive hello-ack error!", state.connectIP, state.connectPort));
                        state.error = "receive hello-ack error!";
                    }
                    else
                    {
                        MemoryStream stream = new MemoryStream();
                        Array.Copy(buffer, 0, stream.data(), stream.wpos, length);
                        stream.wpos = length;
                        string helloAck      = stream.readString();
                        string versionString = stream.readString();
                        uint   conv          = stream.readUint32();

                        if (helloAck != UDP_HELLO_ACK)
                        {
                            Dbg.ERROR_MSG(string.Format("NetworkInterfaceKCP::_asyncConnect(), failed to connect to '{0}:{1}'! receive hello-ack({2}!={3}) mismatch!",
                                                        state.connectIP, state.connectPort, helloAck, UDP_HELLO_ACK));

                            state.error = "hello-ack mismatch!";
                        }
                        else if (KBEngineApp.app.serverVersion != versionString)
                        {
                            Dbg.ERROR_MSG(string.Format("NetworkInterfaceKCP::_asyncConnect(), failed to connect to '{0}:{1}'! version({2}!={3}) mismatch!",
                                                        state.connectIP, state.connectPort, versionString, KBEngineApp.app.serverVersion));

                            state.error = "version mismatch!";
                        }
                        else if (conv == 0)
                        {
                            Dbg.ERROR_MSG(string.Format("NetworkInterfaceKCP::_asyncConnect(), failed to connect to '{0}:{1}'! conv is 0!",
                                                        state.connectIP, state.connectPort));

                            state.error = "kcp conv error!";
                        }

                        ((NetworkInterfaceKCP)state.networkInterface).connID = conv;
                    }
                }
                else
                {
                    Dbg.ERROR_MSG(string.Format("NetworkInterfaceKCP::_asyncConnect(), connect to '{0}:{1}' timeout!'", state.connectIP, state.connectPort));
                    state.error = "timeout!";
                }
            }
            catch (Exception e)
            {
                Dbg.ERROR_MSG(string.Format("NetworkInterfaceKCP::_asyncConnect(), connect to '{0}:{1}' fault! error = '{2}'", state.connectIP, state.connectPort, e));
                state.error = e.ToString();
            }
        }
示例#14
0
        public void process(byte[] datas, MessageLength length, Dictionary <uint, MessageHandler> flowHandler)
        {
            //Log.Net("process receive Data " + length + " state " + state);
            MessageLength totallen = 0;

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

                        flag = stream.readUint8();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGLEN;
                        expectSize = 4;
                    }
                    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.readUint32();
                        stream.clear();

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

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

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

                        moduleId = stream.readUint8();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGID;
                        expectSize = 2;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }
                else 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();

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

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

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

                        responseFlag = stream.readInt16();
                        stream.clear();

                        state      = READ_STATE.READ_STATE_BODY;
                        expectSize = msglen - 4 - 1 - 2 - 4 - 2;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += (int)length;
                        expectSize  -= length;
                        break;
                    }
                }

                /*
                 * body Can be empty
                 */
                if (state == READ_STATE.READ_STATE_BODY)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        totallen    += expectSize;
                        stream.wpos += (int)expectSize;
                        length      -= expectSize;

                        /*
                         * No Handler Or PushMessage  forward To IPacketHandler
                         * Call Who's RPC Method Or Register Many RPC Method to Handle It ?
                         * [PushHandler]
                         * void GCPushSpriteInfo(Packet packet) {
                         * }
                         *
                         * PacketHandler namespace
                         * IPacketHandler---->GCPushSpriteInfo
                         */
                        MessageHandler handler = null;
                        if (flowHandler == null)
                        {
                            handler = msgHandle;
                        }
                        else if (flowHandler.ContainsKey(flowId))
                        {
                            handler = flowHandler [flowId];
                            flowHandler.Remove(flowId);
                        }



                        //Message msg = new Message();
                        IMessageLite pbmsg    = KBEngine.Message.handlePB(moduleId, msgid, stream);
                        Packet       p        = new Packet(flag, msglen, flowId, moduleId, msgid, responseTime, responseFlag, pbmsg);
                        var          fullName = pbmsg.GetType().FullName;
                        //Bundle.recvMsg.Add("recvMsg " + fullName + " : " + flowId);
                        //Log.Net("RecvMsg: "+fullName+" f "+flowId);

                        if (fullName.Contains("Push"))
                        {
                            //Log.Net("MessageReader Handler PushMessage");
                            if (mainLoop != null)
                            {
                                mainLoop.queueInLoop(delegate
                                {
                                    var handlerName = fullName.Replace("ChuMeng", "PacketHandler");
                                    var tp          = Type.GetType(handlerName);
                                    if (tp == null)
                                    {
                                        Debug.LogError("PushMessage noHandler " + handlerName);
                                    }
                                    else
                                    {
                                        //Debug.Log("Handler Push Message here "+handlerName);
                                        var ph = (PacketHandler.IPacketHandler)Activator.CreateInstance(tp);
                                        ph.HandlePacket(p);
                                    }
                                });
                            }
                        }
                        else if (handler != null)
                        {
                            mainLoop.queueInLoop(() => {
                                handler(p);
                            });
                        }
                        else
                        {
                            //flowHandler.Remove(flowId);
                            Debug.LogError("MessageReader::process No handler for flow Message " + msgid + " " + flowId + " " + pbmsg.GetType() + " " + pbmsg);
                        }



                        stream.clear();

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

            if (responseFlag != 0)
            {
                Debug.LogError("MessageReader:: read Error Packet " + responseFlag);
            }

            //Log.Net("current state after " + state + " msglen " + msglen + " " + length);
            //Log.Net("MessageReader::  prop  flag" + flag + "  msglen " + msglen + " flowId " + flowId + " moduleId " + moduleId + " msgid " + msgid + " responseTime " + responseTime + " responseFlag " + responseFlag + " expectSize " + expectSize);
        }
示例#15
0
        public override bool send(MemoryStream stream)
        {
            int dataLength = (int)stream.length();

            if (dataLength <= 0)
            {
                return(true);
            }

            Monitor.Enter(_sending);
            if (!_sending)
            {
                if (_wpos == _spos)
                {
                    _wpos = 0;
                    _spos = 0;
                }
            }

            int t_spos  = _spos;
            int space   = 0;
            int tt_wpos = _wpos % _buffer.Length;
            int tt_spos = t_spos % _buffer.Length;

            if (tt_wpos >= tt_spos)
            {
                space = _buffer.Length - tt_wpos + tt_spos - 1;
            }
            else
            {
                space = tt_spos - tt_wpos - 1;
            }

            if (dataLength > space)
            {
                Dbg.ERROR_MSG("PacketSenderTCP::send(): no space, Please adjust 'SEND_BUFFER_MAX'! data(" + dataLength
                              + ") > space(" + space + "), wpos=" + _wpos + ", spos=" + t_spos);

                return(false);
            }

            int expect_total = tt_wpos + dataLength;

            if (expect_total <= _buffer.Length)
            {
                Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, dataLength);
            }
            else
            {
                int remain = _buffer.Length - tt_wpos;
                Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, remain);
                Array.Copy(stream.data(), stream.rpos + remain, _buffer, 0, expect_total - _buffer.Length);
            }

            _wpos += dataLength;

            if (!_sending)
            {
                _sending = true;
                Monitor.Exit(_sending);

                _startSend();
            }
            else
            {
                Monitor.Exit(_sending);
            }

            return(true);
        }
示例#16
0
 public override void decrypt(MemoryStream stream)
 {
     _blowfish.decipher(stream.data(), stream.rpos, (int)stream.length());
 }
示例#17
0
        public bool send(MemoryStream stream)
        {
            int dataLength = (int)stream.length();

            if (dataLength <= 0)
            {
                return(true);
            }

            //如果没有在发送的数据,且写位置和发送位置一样,则把写位置和发送位置都置0
            if (0 == Interlocked.Add(ref _sending, 0))
            {
                if (_wpos == _spos)
                {
                    _wpos = 0;
                    _spos = 0;
                }
            }

            int t_spos  = Interlocked.Add(ref _spos, 0);
            int space   = 0;
            int tt_wpos = _wpos % _buffer.Length;
            int tt_spos = t_spos % _buffer.Length;

            //                 _____________________________________________________________________________________________________
            // _buffer = | 000000000000000000000000000000000 0 00000000000000000000000000000000000 0 00000000000000000000000000000 |
            //                                                                                     ^                                                                        ^
            //                                                                                 _spos                                                                _wpos
            //                <----------------space----------------------> <----------------data-------------------------> <----------------space---------------->
            if (tt_wpos >= tt_spos)
            {
                space = _buffer.Length - tt_wpos + tt_spos - 1;
            }
            else
            {
                space = tt_spos - tt_wpos - 1;
            }

            if (dataLength > space)
            {
                Dbg.ERROR_MSG("PacketSender::send(): no space, Please adjust 'SEND_BUFFER_MAX'! data(" + dataLength
                              + ") > space(" + space + "), wpos=" + _wpos + ", spos=" + t_spos);

                return(false);
            }

            int expect_total = tt_wpos + dataLength;

            if (expect_total <= _buffer.Length)
            {
                Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, dataLength);
            }
            else
            {
                int remain = _buffer.Length - tt_wpos;
                Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, remain);
                Array.Copy(stream.data(), stream.rpos + remain, _buffer, 0, expect_total - _buffer.Length);
            }

            Interlocked.Add(ref _wpos, dataLength);

            if (Interlocked.CompareExchange(ref _sending, 1, 0) == 0)
            {
                _startSend();
            }

            return(true);
        }
示例#18
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);
        }
示例#19
0
 public void copy(MemoryStream ms)
 {
     this.wpos = ms.wpos;
     this.rpos = ms.rpos;
     Array.Copy(ms.data(), this.data(), ms.data().Length);
 }
示例#20
0
 public void copy(MemoryStream ms)
 {
     this.wpos = ms.wpos;
     this.rpos = ms.rpos;
     Array.Copy(ms.data(), this.data(), ms.data().Length);
 }
示例#21
0
        public void onUpdatePropertys_(Int32 eid, MemoryStream stream)
        {
            Entity entity = null;

            if(!entities.TryGetValue(eid, out entity))
            {
                MemoryStream entityMessage = null;
                if(_bufferedCreateEntityMessage.TryGetValue(eid, out entityMessage))
                {
                    Dbg.ERROR_MSG("KBEngine::Client_onUpdatePropertys: entity(" + eid + ") not found!");
                    return;
                }

                MemoryStream stream1 = new MemoryStream();
                stream1.wpos = stream.wpos;
                stream1.rpos = stream.rpos - 4;
                Array.Copy(stream.data(), stream1.data(), stream.data().Length);
                _bufferedCreateEntityMessage[eid] = stream1;
                return;
            }

            ScriptModule sm = EntityDef.moduledefs[entity.className];
            Dictionary<UInt16, Property> pdatas = sm.idpropertys;

            while(stream.length() > 0)
            {
                UInt16 utype = 0;

                if(sm.usePropertyDescrAlias)
                {
                    utype = stream.readUint8();
                }
                else
                {
                    utype = stream.readUint16();
                }

                Property propertydata = pdatas[utype];
                utype = propertydata.properUtype;
                System.Reflection.MethodInfo setmethod = propertydata.setmethod;

                object val = propertydata.utype.createFromStream(stream);
                object oldval = entity.getDefinedProptertyByUType(utype);

                 // Dbg.DEBUG_MSG("KBEngine::Client_onUpdatePropertys: " + entity.className + "(id=" + eid  + " " +
                 // propertydata.name + "=" + val + "), hasSetMethod=" + setmethod + "!");

                entity.setDefinedProptertyByUType(utype, val);
                if(setmethod != null)
                {
                    if(propertydata.isBase() || entity.inWorld)
                        setmethod.Invoke(entity, new object[]{oldval});
                }
            }
        }
示例#22
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;
                    }
                }
            }
        }
示例#23
0
        public void Client_onImportClientEntityDef(MemoryStream stream)
        {
            byte[] datas = new byte[stream.wpos - stream.rpos];
            Array.Copy (stream.data (), stream.rpos, datas, 0, stream.wpos - stream.rpos);

            onImportClientEntityDef (stream);

            if(_persistentInofs != null)
                _persistentInofs.onImportClientEntityDef(datas);
        }
示例#24
0
        public void process(byte[] datas, MessageLength length)
        {
            MessageLength totallen = 0;

            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 += expectSize;
                        length      -= expectSize;

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

                        Message msg = Message.clientMessages[msgid];

                        if (msg.msglen == -1)
                        {
                            state      = READ_STATE.READ_STATE_MSGLEN;
                            expectSize = 2;
                        }
                        else
                        {
                            state      = READ_STATE.READ_STATE_BODY;
                            expectSize = (MessageLength)msg.msglen;
                        }
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += 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 += expectSize;
                        length      -= expectSize;

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

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

                        Message msg = Message.clientMessages[msgid];

                        msg.handleMessage(stream);
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGID;
                        expectSize = 2;
                    }
                    else
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += length;
                        expectSize  -= length;
                        break;
                    }
                }
            }
        }
示例#25
0
        public void process(byte[] datas, MessageLength length)
        {
            MessageLength totallen = 0;

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

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

                        Message msg = Message.clientMessages[msgid];

                        if (msg.msglen == -1)
                        {
                            state      = READ_STATE.READ_STATE_MSGLEN;
                            expectSize = 2;
                        }
                        else
                        {
                            state      = READ_STATE.READ_STATE_BODY;
                            expectSize = (MessageLength)msg.msglen;
                        }

                        totallen += 2;
                        //Dbg.DEBUG_MSG(string.Format("MessageReader::process(): msgid={0}/{1}/{2}, msgname={3}!",
                        //	msg.id, msg.msglen, expectSize, msg.name));
                    }
                    else
                    {
                        expectSize -= length;
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        // totallen += length; // 要退出循环了无序这一步
                        length = 0;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_MSGLEN)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        length -= expectSize;

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

                        // Dbg.DEBUG_MSG(string.Format("MessageReader::process(): msglen={0}!", msglen));

                        state      = READ_STATE.READ_STATE_BODY;
                        expectSize = msglen;
                        totallen  += 2;
                    }
                    else
                    {
                        expectSize -= length;
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        // totallen += length; // 要退出循环了无序这一步
                        length = 0;
                        break;
                    }
                }
                else if (state == READ_STATE.READ_STATE_BODY)
                {
                    if (length >= expectSize)
                    {
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, expectSize);
                        length   -= expectSize;
                        totallen += expectSize;
                        Message msg = Message.clientMessages[msgid];
                        stream.wpos += expectSize;

                        // Dbg.DEBUG_MSG(string.Format("MessageReader::process(): handleMessage={0}, msglen={1}, rpos({2}), wpos({3}), msg={4}!",
                        //	msg.name, stream.opsize(), stream.rpos, stream.wpos, stream.toString()));

                        msg.handleMessage(stream);
                        stream.clear();

                        state      = READ_STATE.READ_STATE_MSGID;
                        expectSize = 2;
                    }
                    else
                    {
                        expectSize -= length;
                        Array.Copy(datas, totallen, stream.data(), stream.wpos, length);
                        stream.wpos += length;
                        // totallen += length; // 要退出循环了无序这一步
                        length = 0;
                        break;
                    }
                }
            }
        }