Пример #1
0
        public void Tick()
        {
            FileDataBuff buff = PopRecvData();

            while (buff != null)
            {
                if (!Invoke(buff))
                {
                    LogManager.Debug("TcpSession::Tick => invoke error");
                    Stop();
                    return;
                }
                buff.Clear();
                buff = PopRecvData();
            }
        }
Пример #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();
            }
        }