예제 #1
0
        protected void RecvBytes()
        {
            if (!IsConnected())
            {
                LogManager.Debug("TcpSession::SendBytes => not connected");
                return;
            }

            if (_bRecving)
            {
                return;
            }

            try
            {
                DataBuff buff = null;
                lock (_recvLocker)
                {
                    if (_recvBuffList.Count > 0)
                    {
                        int index = _recvBuffList.Count - 1;
                        if (_recvBuffList[index] != null && _recvBuffList[index].GetUnwriteLen() > 0)
                        {
                            buff = _recvBuffList[index];
                            _recvBuffList.RemoveAt(index);
                        }
                    }
                }

                if (buff == null)
                {
                    buff = ObjectPool <DataBuff> .Pop();
                }

                _socket.BeginReceive(buff._data, 0, (int)buff.GetUnwriteLen(), SocketFlags.None, OnRecvBytesCallback, buff);
                _bRecving = true;
            }
            catch (SocketException e) { LogManager.Debug(string.Format("TcpSession::RecvBytes => error [{0}] [{1}]", e.ErrorCode, e.Message)); }
            catch { LogManager.Debug("TcpSession::RecvBytes => unknown error"); }
        }
예제 #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();
            }
        }