int Connection::recv(inp::INFPacket& dest){
        ScopedMutexLock(pimpl_->dataAccess);
        //make sure thers something to recieved
        if( (pimpl_->group == NULL)       ||
            (pimpl_->userSocket == NULL)  || (pimpl_->userSocket == NULL)  ||
            (SDLNet_CheckSockets(pimpl_->group, 0) == -1)  )
        {
            pimpl_->active = false;
            pimpl_->connectTime = 0;
            return -1;
        }

        if( !SDLNet_SocketReady(pimpl_->userSocket) ){ return 0; } //no activity

        dest.clear();
        Uint8 temp = 0;
        Uint8 len_buf[2];
        Uint16 len = 0;
        bool recieving = true;

        //get header byte
        if( recvOne( &temp, 1000, false ) == -1 ){ return -1; }

        //  check if the header byte is a start packet
        if( temp != ControlByte::START  ){ return -1; }

        //begin getting body of data
        while( recieving ){
            //  This should be 2 bytes stating length of segment
            for(unsigned i = 0; i < 2; ++i){
                if( recvOne( &len_buf[i], 1000, false ) == -1 ){
                    return -1;
                }
            }
            len  = SDLNet_Read16(len_buf);

            //try to get "len" amount of bytes
            for(unsigned i = 0; i < len; ++i){
                if( recvOne( &temp, 1000, false ) == -1 ){
                    return -1;
                } else {
                    dest.data.push_back(temp);
                }
            }

            //  This shud be a wait_more/end
            if( recvOne( &temp, 1000, false ) == -1 ){ return -1; }
            if( (temp == ControlByte::CONTINUE ) ||
                (temp == ControlByte::WAIT_MORE )  )
            {
                continue;
            } else
            // Slab already started.
            //   this is a malformed transmission
            if( (temp == ControlByte::START) ||
                (temp == ControlByte::END)  )  //stop if this is the end
            {
                recieving = false;
            } else {
                // some bull shit happened here =/
                recieving = false;
            }
        }
        pimpl_->lastRecv = dest;
        return 1;
    }
Example #2
0
void NetworkConnexion::recvLoop() {
	while(m_running) {
		recvOne();
		pause(100);
	}
}