Exemplo n.º 1
0
        protected bool Invoke(FileDataBuff buff)
        {
            if (buff == null || _rpcCallbackArray == null)
            {
                return(false);
            }

            if (buff._dataLen < 4)
            {
                return(false);
            }

            uint readLen = 0;
            uint msgType = 0;
            uint msgLen  = 0;

            byte[] ushortBytes = new byte[2];

            readLen = 2;
            SerializeUtil.LEMemcpy(ushortBytes, 0, buff._data, buff.GetReadPos(), readLen);
            buff._readPos += readLen;
            msgType        = BitConverter.ToUInt16(ushortBytes, 0);

            if (msgType >= _rpcCallbackNum)
            {
                return(false);
            }

            readLen = 2;
            SerializeUtil.LEMemcpy(ushortBytes, 0, buff._data, buff.GetReadPos(), readLen);
            buff._readPos += readLen;
            msgLen         = BitConverter.ToUInt16(ushortBytes, 0);

            if (buff.GetUnreadLen() != msgLen)
            {
                return(false);
            }

            return(_rpcCallbackArray[msgType](buff._data, buff.GetReadPos(), msgLen));
        }
Exemplo n.º 2
0
        public void PushSendData(ushort msgType, ISerializableObj data)
        {
            if (!IsConnected())
            {
                return;
            }

            uint            dataLen      = data == null ? 0 : data.CalcDataLen();
            uint            needLen      = dataLen + 4;
            uint            copyLen      = 0;
            DataBuff        buff         = null;
            List <DataBuff> sendBuffList = new List <DataBuff>();

            if (needLen > FileDataBuff.MAX_FILE_SIZE)
            {
                return;
            }

            if (needLen > DataBuff.MAX_BUFF_LEN)
            {
                buff = ObjectPool <DataBuff> .Pop();

                if (buff == null)
                {
                    return;
                }

                sendBuffList.Add(buff);

                copyLen = 2;
                SerializeUtil.LEMemcpy(buff._data, buff.GetWritePos(), BitConverter.GetBytes(msgType), 0, copyLen);
                if (_bEncode)
                {
                    Encode(buff._data, buff.GetWritePos(), copyLen);
                }
                buff._dataLen += copyLen;

                copyLen = 2;
                SerializeUtil.LEMemcpy(buff._data, buff.GetWritePos(), BitConverter.GetBytes((ushort)dataLen), 0, copyLen);
                if (_bEncode)
                {
                    Encode(buff._data, buff.GetWritePos(), copyLen);
                }
                buff._dataLen += copyLen;

                if (data != null)
                {
                    _sendFileBuff.Clear();
                    data.Serialize(_sendFileBuff._data, _sendFileBuff.GetWritePos(), FileDataBuff.MAX_FILE_SIZE);

                    while (_sendFileBuff.GetUnreadLen() > 0)
                    {
                        if (buff.GetUnwriteLen() <= 0)
                        {
                            buff = ObjectPool <DataBuff> .Pop();

                            if (buff == null)
                            {
                                return;
                            }
                            _sendBuffList.Add(buff);
                        }

                        copyLen = Math.Min(buff.GetUnwriteLen(), _sendFileBuff.GetUnreadLen());
                        SerializeUtil.Memcpy(buff._data, buff.GetWritePos(), _sendFileBuff._data, _sendFileBuff.GetReadPos(), copyLen);
                        if (_bEncode)
                        {
                            Encode(buff._data, buff.GetWritePos(), copyLen);
                        }
                        buff._dataLen          += copyLen;
                        _sendFileBuff._readPos += copyLen;
                    }
                }
            }
            else
            {
                lock (_sendLocker)
                {
                    if (_sendBuffList.Count > 0)
                    {
                        int tailIndex = _sendBuffList.Count - 1;
                        sendBuffList.Add(_sendBuffList[tailIndex]);
                        _sendBuffList.RemoveAt(tailIndex);
                    }
                }

                bool needNew = sendBuffList.Count <= 0 || (DataBuff.MAX_BUFF_LEN - sendBuffList[sendBuffList.Count - 1]._dataLen < needLen);
                if (needNew)
                {
                    buff = ObjectPool <DataBuff> .Pop();
                }
                else
                {
                    buff = sendBuffList[sendBuffList.Count - 1];
                }

                if (buff != null)
                {
                    sendBuffList.Add(buff);

                    copyLen = 2;
                    SerializeUtil.LEMemcpy(buff._data, buff.GetWritePos(), BitConverter.GetBytes(msgType), 0, copyLen);
                    if (_bEncode)
                    {
                        Encode(buff._data, buff.GetWritePos(), copyLen);
                    }
                    buff._dataLen += copyLen;

                    copyLen = 2;
                    SerializeUtil.LEMemcpy(buff._data, buff.GetWritePos(), BitConverter.GetBytes((ushort)dataLen), 0, copyLen);
                    if (_bEncode)
                    {
                        Encode(buff._data, buff.GetWritePos(), copyLen);
                    }
                    buff._dataLen += copyLen;

                    if (data != null)
                    {
                        copyLen = data.Serialize(buff._data, buff.GetWritePos(), DataBuff.MAX_BUFF_LEN - buff._dataLen);
                        if (_bEncode)
                        {
                            Encode(buff._data, buff.GetWritePos(), copyLen);
                        }
                        buff._dataLen += copyLen;
                    }
                }
            }

            if (sendBuffList.Count > 0)
            {
                lock (_sendLocker)
                {
                    _sendBuffList.AddRange(sendBuffList);
                    sendBuffList.Clear();
                }
            }

            if (!IsConnected())
            {
                ClearSendBuffList();
            }
            else
            {
                SendBytes();
            }
        }