Пример #1
0
int Connections::CompleteSend(DWORD TransferedBytes)
{
    UNREFERENCED_PARAMETER(TransferedBytes);

    FUNCTION_BEGIN();


    IncreaseBytesSend(TransferedBytes);


    _mutexSend.Lock();
    _sendBuffer.IncreasePopPos(TransferedBytes);

    int rc = 0;
    if (IsSetControlFlag(CONTROL_FLAG_USE_SEND_QUEUE))
    {
        Packet * SendPacket = _sendQueue.PeekPacket();
        while (SendPacket)
        {
            if (_sendBuffer.GetPushAvailableSize() <= SendPacket->GetUsedSize())
            {
                break;
            }

            BYTE Sequence = (BYTE)(_PacketSendCount & PACKET_MAX_SEQUENCE);
            SetSequence(SendPacket->GetPacketHeader(), Sequence);

            if (_PacketEncoder)
            {
                rc = _PacketEncoder->TxEncrypt(*SendPacket);
                if (0 != rc)
                {
                    _mutexSend.Unlock();

                    Logger::Log("Session[%X] TxEncrypt Error[%d]\n", this, rc);
                    return 1; //ERR_NET_PACKET_ENCRYPT_FAIL
                }
            }

            memcpy(_sendBuffer.GetQueuePushPtr(), SendPacket->GetPacketBuffer(), SendPacket->GetUsedSize());
            _sendBuffer.IncreasePushPos(SendPacket->GetUsedSize());
            IncreasePacketSend();

            _sendQueue.PopPacket();
            SAFE_DELETE(SendPacket);

            SendPacket = _sendQueue.PeekPacket();
        }
    }


    if (_sendBuffer.GetCurSize())
    {
        rc = PostSend();
        if (0 != rc)
        {
            _IsSending = false;
            _mutexSend.Unlock();
            Disconnect(false);

            Logger::Log("Session[%X] PostSend Error[%d]\n", this, rc);
            return 0;
        }
    }
    else
    {
        _IsSending = false;
    }


    _mutexSend.Unlock();

    return 0;
}
Пример #2
0
int Connections::CompleteRecv(DWORD TransferedBytes)
{
    FUNCTION_BEGIN();

    if (0 == TransferedBytes)
    {
        return 1;
    }

    IncreaseBytesRecv(TransferedBytes);

    if (false == _recvBuffer.IncreasePushPos(TransferedBytes))
    {
        Logger::Log("Session[%X] Recv Buffer OverFlow : BufferCur[%d] BufferMax[%d] TransferedBytes[%u]\n", this, _recvBuffer.GetCurSize(), _recvBuffer.GetQueueSize(), TransferedBytes);
        return 2;
    }

    int rc = 0;
    WORD PacketLength = 0;

    Packet packet;
    do
    {
        rc = MakeSureCompletedPacket(&_recvBuffer, &PacketLength, _PacketEncoder);
        if (0 != rc)
        {
            if (2 == rc || 4 == rc)
            {
                break;
            }

            Logger::Log("Session[%X] MakeSureCompletedPacket Error[%d]\n", this, rc);
            return rc;
        }

        if (true != IsValidPacket(_recvBuffer.GetQueueWorkPtr(), PacketLength))
        {
            Logger::Log("true != IsValidPacket()\n");
        }

        packet.AttachData(_recvBuffer.GetQueueWorkPtr(), PacketLength);

        _recvBuffer.IncreaseWorkPos(packet.GetUsedSize());

        if (_PacketEncoder)
        {
            rc = _PacketEncoder->RxDecrypt(packet);
            if (0 != rc)
            {
                Logger::Log("Session[%X] RxDecrypt Error[%d]\n", this, rc);
                return rc;
            }
        }

        BYTE ValidSequence = (BYTE)(_PacketRecvCount & PACKET_MAX_SEQUENCE);
        if (false == HasValidSequence(packet.GetPacketHeader(), ValidSequence))
        {
            Logger::Log("Session[%X] Sequence Error[%d] PacketSEQ[%u] CurrentSEQ[%u]\n", this, rc, GetSequence(_recvBuffer.GetQueueWorkPtr()), ValidSequence);
            return 3; //ERR_NET_PACKET_SEQUENCE_FAIL
        }

        IncreasePacketRecv();

        _NetworkRef->PostNetEventMessage((WPARAM)NETEVENT_RECV, (LPARAM)this);


    } while (0 == rc);

    rc = PostRecv();
    if (0 != rc)
    {
        return rc;
    }

    return 0;
}