| 1088 | } |
| 1089 | |
| 1090 | void I2CPSession::SendMessagePayloadMessage (const uint8_t * payload, size_t len) |
| 1091 | { |
| 1092 | // we don't use SendI2CPMessage to eliminate additional copy |
| 1093 | auto l = len + 10 + I2CP_HEADER_SIZE; |
| 1094 | if (l > I2CP_MAX_MESSAGE_LENGTH) |
| 1095 | { |
| 1096 | LogPrint (eLogError, "I2CP: Message to send is too long ", l); |
| 1097 | return; |
| 1098 | } |
| 1099 | auto sendBuf = m_IsSending ? std::make_shared<i2p::stream::SendBuffer> (l) : nullptr; |
| 1100 | uint8_t * buf = sendBuf ? sendBuf->buf : m_SendBuffer; |
| 1101 | htobe32buf (buf + I2CP_HEADER_LENGTH_OFFSET, len + 10); |
| 1102 | buf[I2CP_HEADER_TYPE_OFFSET] = I2CP_MESSAGE_PAYLOAD_MESSAGE; |
| 1103 | htobe16buf (buf + I2CP_HEADER_SIZE, m_SessionID); |
| 1104 | htobe32buf (buf + I2CP_HEADER_SIZE + 2, m_MessageID++); |
| 1105 | htobe32buf (buf + I2CP_HEADER_SIZE + 6, len); |
| 1106 | memcpy (buf + I2CP_HEADER_SIZE + 10, payload, len); |
| 1107 | if (sendBuf) |
| 1108 | { |
| 1109 | if (m_SendQueue.GetSize () < I2CP_MAX_SEND_QUEUE_SIZE) |
| 1110 | m_SendQueue.Add (std::move(sendBuf)); |
| 1111 | else |
| 1112 | { |
| 1113 | LogPrint (eLogWarning, "I2CP: Send queue size exceeds ", I2CP_MAX_SEND_QUEUE_SIZE); |
| 1114 | return; |
| 1115 | } |
| 1116 | } |
| 1117 | else |
| 1118 | { |
| 1119 | auto socket = m_Socket; |
| 1120 | if (socket) |
| 1121 | { |
| 1122 | m_IsSending = true; |
| 1123 | boost::asio::async_write (*socket, boost::asio::buffer (m_SendBuffer, l), |
| 1124 | boost::asio::transfer_all (), std::bind(&I2CPSession::HandleI2CPMessageSent, |
| 1125 | shared_from_this (), std::placeholders::_1, std::placeholders::_2)); |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | I2CPServer::I2CPServer (const std::string& interface, uint16_t port, bool isSingleThread): |
| 1131 | RunnableService ("I2CP"), m_IsSingleThread (isSingleThread), |
no test coverage detected