In C ++ regarding bit-shift types and data casting

I recently asked here about a stack overflow on how to discard my data from a 16-bit integer, followed by an undetermined amount of void * -cast memory in std :: vector unsigned characters to use a socket library known as NetLink, which uses a function whose signature looks like to send raw data:

void rawSend(const vector<unsigned char>* data);

(for reference, here is this question: Embedding unsigned int + string in unsigned char vector )

The question was successfully satisfied, and I am grateful to those who answered. Mike DeSimone responded with an example of the send_message () function, which converts the data to a format that NetLink (std :: vector) accepts, which looks like this:

void send_message(NLSocket* socket, uint16_t opcode, const void* rawData, size_t rawDataSize)
{
    vector<unsigned char> buffer;
    buffer.reserve(sizeof(uint16_t) + rawDataSize);
    buffer.push_back(opcode >> 8);
    buffer.push_back(opcode & 0xFF);
    const unsigned char* base(reinterpret_cast<const unsigned char*>(rawData));
    buffer.insert(buffer.end(), base, base + rawDataSize);
    socket->rawSend(&buffer);
}

, , receive_message()...

... , , , . , - , , .

receive_message(), , , NetLink rawRead(), :

vector<unsigned char>* rawRead(unsigned bufferSize = DEFAULT_BUFFER_SIZE, string* hostFrom = NULL);

, - :

void receive_message(NLSocket* socket, uint16_t* opcode, const void** rawData)
{
    std::vector<unsigned char, std::allocator<unsigned char>>* buffer = socket->rawRead();
    std::allocator<unsigned char> allocator = buffer->get_allocator(); // do I even need this allocator?  I saw that one is returned as part of the above object, but...
    // ...
}

rawRead(), , , , * rawData * opcode. , ( , , , send_message() ), .

- , receive_message()? , - , , ( , ), .

!

+5
2

& ssquo; & hellip;

    void rawSend( const vector<unsigned char>* data );

std::vector , , . , std::vector. , , , .

& hellip;

    vector<unsigned char>* rawRead(unsigned bufferSize = DEFAULT_BUFFER_SIZE, string* hostFrom = NULL);

: std::string, "hostFrom" ( ), vector. , . , , .

. , . I.e., .


& hellip;

void send_message(NLSocket* socket, uint16_t opcode, const void* rawData, size_t rawDataSize)
{
    vector<unsigned char> buffer;
    buffer.reserve(sizeof(uint16_t) + rawDataSize);
    buffer.push_back(opcode >> 8);
    buffer.push_back(opcode & 0xFF);
    const unsigned char* base(reinterpret_cast<const unsigned char*>(rawData));
    buffer.insert(buffer.end(), base, base + rawDataSize);
    socket->rawSend(&buffer);
}

:

  • reserve - . vector ( ) . vector .

  • buffer.push_back(opcode >> 8) 8 () 16- opcode . , , -, endian. . , endian, endian. , , , , .

  • buffer.push_back(opcode & 0xFF) 8 opcode , endian.

  • const unsigned char* base(reinterpret_cast<const unsigned char*>(rawData)) , base. const unsigned char* , . const void* .

  • buffer.insert(buffer.end(), base, base + rawDataSize) . base + rawDataSize - , .

  • socket->rawSend(&buffer) SillyLibrary & ss rawSend.


SillyLibrary rawRead.

( , ):

typedef unsigned char Byte;
typedef ptrdiff_t Size;

, // ( ) SillyLibrary:

void deleteSillyLibVector( vector<Byte> const* p )
{
    // perhaps just "delete p", but it depends on the SillyLibrary
}

, std::vector, . . - , std::vector.

.

, SillyLibrary, , , . . , , , vector:

Size receive_append( NLSocket& socket, vector<Byte>& data )
{
    vector<Byte> const* const result = socket.raw_read();

    if( result == 0 )
    {
        return 0;
    }

    struct ScopeGuard
    {
        vector<Byte>* pDoomed;
        explicit ScopeGuard( vector<Byte>* p ): pDoomed( p ) {}
        ~ScopeGuard() { deleteSillyLibVector( pDoomed ); }
    };

    Size const nBytesRead = result->size();
    ScopeGuard cleanup( result );

    data.insert( data.end(), result->begin(), result->end() );
    return nBytesRead;
}

, . - std::bad_alloc, . , , , ( - , SillyLibrary , ).

, , , vector. , , . , .

: .

hth.,

+3

- , opcode >> 8 opcode / 256, opcode & 0xFF opcode - ((opcode / 256) * 256). /.

opcode , ophi oplo, 0..255. opcode == (ophi * 256) + oplo.

...

0xFF  == 255 == binary  11111111 == 2^8 - 1
0x100 == 256 == binary 100000000 == 2^8

              opcode
         /              \
Binary : 1010101010101010
         \      /\      /
           ophi    oplo

, , endian-fix . , " " , , - . send_message . , .

opcode = (ophi * 256) + oplo; opcode == (ophi << 8) | oplo; - , .

, , , . , vector - , , const void** rawData, , , , reserve . ( , ).

, - , ? receive_message, .

0

All Articles