net: don't use get_message() when constructing datagram header

This is causing an unnecessary copy operation.
This commit is contained in:
rdb 2018-04-06 17:16:18 +02:00
parent 7125a3e587
commit de6d753f79
2 changed files with 14 additions and 14 deletions

View File

@ -24,23 +24,23 @@
*/
DatagramTCPHeader::
DatagramTCPHeader(const NetDatagram &datagram, int header_size) {
const string &str = datagram.get_message();
size_t length = datagram.get_length();
switch (header_size) {
case 0:
break;
case datagram_tcp16_header_size:
{
uint16_t size = str.length();
nassertv(size == str.length());
uint16_t size = (uint16_t)length;
nassertv((size_t)size == length);
_header.add_uint16(size);
}
break;
case datagram_tcp32_header_size:
{
uint32_t size = str.length();
nassertv(size == str.length());
uint32_t size = (uint32_t)length;
nassertv((size_t)size == length);
_header.add_uint32(size);
}
break;
@ -93,8 +93,7 @@ verify_datagram(const NetDatagram &datagram, int header_size) const {
return true;
}
const string &str = datagram.get_message();
int actual_size = str.length();
int actual_size = (int)datagram.get_length();
int expected_size = get_datagram_size(header_size);
if (actual_size == expected_size) {
return true;

View File

@ -24,10 +24,11 @@
*/
DatagramUDPHeader::
DatagramUDPHeader(const NetDatagram &datagram) {
const string &str = datagram.get_message();
const unsigned char *begin = (const unsigned char *)datagram.get_data();
const unsigned char *end = begin + datagram.get_length();
uint16_t checksum = 0;
for (size_t p = 0; p < str.size(); p++) {
checksum += (uint16_t)(uint8_t)str[p];
for (const unsigned char *p = begin; p != end; ++p) {
checksum += (uint16_t)(uint8_t)*p;
}
// Now pack the header.
@ -49,11 +50,11 @@ DatagramUDPHeader(const void *data) : _header(data, datagram_udp_header_size) {
*/
bool DatagramUDPHeader::
verify_datagram(const NetDatagram &datagram) const {
const string &str = datagram.get_message();
const unsigned char *begin = (const unsigned char *)datagram.get_data();
const unsigned char *end = begin + datagram.get_length();
uint16_t checksum = 0;
for (size_t p = 0; p < str.size(); p++) {
checksum += (uint16_t)(uint8_t)str[p];
for (const unsigned char *p = begin; p != end; ++p) {
checksum += (uint16_t)(uint8_t)*p;
}
if (checksum == get_datagram_checksum()) {