Skip to content

Commit 5d924cd

Browse files
committed
don't copy received datagram
1 parent 27116b9 commit 5d924cd

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

libi2pd_client/SAM.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2020, The PurpleI2P Project
2+
* Copyright (c) 2013-2021, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -1115,19 +1115,9 @@ namespace client
11151115
if (ep)
11161116
{
11171117
// udp forward enabled
1118-
size_t bsz = base64.size();
1119-
size_t sz = bsz + 1 + len;
1120-
// build datagram body
1121-
uint8_t * data = new uint8_t[sz];
1122-
// Destination
1123-
memcpy(data, base64.c_str(), bsz);
1124-
// linefeed
1125-
data[bsz] = '\n';
1126-
// Payload
1127-
memcpy(data+bsz+1, buf, len);
1128-
// send to remote endpoint
1129-
m_Owner.SendTo(data, sz, ep);
1130-
delete [] data;
1118+
const char lf = '\n';
1119+
// send to remote endpoint, { destination, linefeed, payload }
1120+
m_Owner.SendTo({ {(const uint8_t *)base64.c_str(), base64.size()}, {(const uint8_t *)&lf, 1}, {buf, len} }, *ep);
11311121
}
11321122
else
11331123
{
@@ -1156,7 +1146,7 @@ namespace client
11561146
auto ep = session->UDPEndpoint;
11571147
if (ep)
11581148
// udp forward enabled
1159-
m_Owner.SendTo(buf, len, ep);
1149+
m_Owner.SendTo({ {buf, len} }, *ep);
11601150
else
11611151
{
11621152
#ifdef _MSC_VER
@@ -1453,14 +1443,11 @@ namespace client
14531443
return list;
14541444
}
14551445

1456-
void SAMBridge::SendTo(const uint8_t * buf, size_t len, std::shared_ptr<boost::asio::ip::udp::endpoint> remote)
1446+
void SAMBridge::SendTo (const std::vector<boost::asio::const_buffer>& bufs, const boost::asio::ip::udp::endpoint& ep)
14571447
{
1458-
if(remote)
1459-
{
1460-
m_DatagramSocket.send_to(boost::asio::buffer(buf, len), *remote);
1461-
}
1462-
}
1463-
1448+
m_DatagramSocket.send_to (bufs, ep);
1449+
}
1450+
14641451
void SAMBridge::ReceiveDatagram ()
14651452
{
14661453
m_DatagramSocket.async_receive_from (

libi2pd_client/SAM.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013-2020, The PurpleI2P Project
2+
* Copyright (c) 2013-2021, The PurpleI2P Project
33
*
44
* This file is part of Purple i2pd project and licensed under BSD3
55
*
@@ -249,8 +249,8 @@ namespace client
249249
std::list<std::shared_ptr<SAMSocket> > ListSockets(const std::string & id) const;
250250

251251
/** send raw data to remote endpoint from our UDP Socket */
252-
void SendTo(const uint8_t * buf, size_t len, std::shared_ptr<boost::asio::ip::udp::endpoint> remote);
253-
252+
void SendTo (const std::vector<boost::asio::const_buffer>& bufs, const boost::asio::ip::udp::endpoint& ep);
253+
254254
void AddSocket(std::shared_ptr<SAMSocket> socket);
255255
void RemoveSocket(const std::shared_ptr<SAMSocket> & socket);
256256

0 commit comments

Comments
 (0)