Skip to content

Commit

Permalink
refactor: nuke a whole load of C style casts from orbit
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 6, 2024
1 parent 4a7829d commit c8774d4
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 48 deletions.
2 changes: 1 addition & 1 deletion include/dpp/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ uint32_t DPP_EXPORT hsl(int h, int s, int l);
* @param data The start of the data to display
* @param length The length of data to display
*/
std::string DPP_EXPORT debug_dump(uint8_t* data, size_t length);
std::string DPP_EXPORT debug_dump(const uint8_t* data, size_t length);

/**
* @brief Returns the length of a UTF-8 string in codepoints.
Expand Down
1 change: 0 additions & 1 deletion src/dpp/discordvoiceclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ void discord_voice_client::get_user_privacy_code(const dpp::snowflake user, priv
return;
}
mls_state->dave_session->get_pairwise_fingerprint(0x0000, user.str(), [callback](const std::vector<uint8_t> &data) {
std::cout << dpp::utility::debug_dump((uint8_t *) data.data(), data.size());
callback(data.size() == 64 ? generate_displayable_code(data, 45) : "");
});
#else
Expand Down
4 changes: 2 additions & 2 deletions src/dpp/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ image_data&& icon::as_image_data() && {
return std::move(std::get<image_data>(hash_or_data));
}

std::string debug_dump(uint8_t* data, size_t length) {
std::string debug_dump(const uint8_t* data, size_t length) {
std::ostringstream out;
size_t addr = (size_t)data;
size_t extra = addr % 16;
Expand All @@ -367,7 +367,7 @@ std::string debug_dump(uint8_t* data, size_t length) {
out << "-- ";
}
std::string ascii;
for (uint8_t* ptr = data; ptr < data + length; ++ptr) {
for (const uint8_t* ptr = data; ptr < data + length; ++ptr) {
if (((size_t)ptr % 16) == 0) {
out << ascii << "\n[" << to_hex((size_t)ptr) << "] : ";
ascii.clear();
Expand Down
8 changes: 4 additions & 4 deletions src/dpp/voice/enabled/discover_ip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ std::string discord_voice_client::discover_ip() {
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(0);
if (bind(socket.fd, (const sockaddr*)(&servaddr), sizeof(servaddr)) < 0) {
if (bind(socket.fd, reinterpret_cast<const sockaddr*>(&servaddr), sizeof(servaddr)) < 0) {
log(ll_warning, "Could not bind socket for IP discovery");
return "";
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(this->port);
servaddr.sin_addr.s_addr = inet_addr(this->ip.c_str());
if (::connect(socket.fd, (const sockaddr*)(&servaddr), sizeof(sockaddr_in)) < 0) {
if (::connect(socket.fd, reinterpret_cast<const sockaddr*>(&servaddr), sizeof(sockaddr_in)) < 0) {
log(ll_warning, "Could not connect socket for IP discovery");
return "";
}
if (::send(socket.fd, (const char*)&discovery, sizeof(discovery), 0) == -1) {
if (::send(socket.fd, reinterpret_cast<const char*>(&discovery), sizeof(discovery), 0) == -1) {
log(ll_warning, "Could not send packet for IP discovery");
return "";
}
Expand All @@ -120,7 +120,7 @@ std::string discord_voice_client::discover_ip() {
log(ll_warning, "Timed out in IP discovery");
return "";
default:
if (recv(socket.fd, (char*)&discovery, sizeof(discovery), 0) == -1) {
if (recv(socket.fd, reinterpret_cast<char*>(&discovery), sizeof(discovery), 0) == -1) {
log(ll_warning, "Could not receive packet for IP discovery");
return "";
}
Expand Down
8 changes: 4 additions & 4 deletions src/dpp/voice/enabled/handle_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod
break;
default:
log(ll_debug, "Unexpected DAVE frame opcode");
log(dpp::ll_trace, "R: " + dpp::utility::debug_dump((uint8_t*)(data.data()), data.length()));
log(dpp::ll_trace, "R: " + dpp::utility::debug_dump(reinterpret_cast<const uint8_t*>(data.data()), data.length()));
break;
}

Expand Down Expand Up @@ -390,7 +390,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod
}
log(ll_debug, "Voice websocket established; UDP endpoint: " + ip + ":" + std::to_string(port) + " [ssrc=" + std::to_string(ssrc) + "] with " + std::to_string(modes.size()) + " modes");

dpp::socket newfd;
dpp::socket newfd = 0;
if ((newfd = ::socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {

sockaddr_in servaddr{};
Expand All @@ -399,7 +399,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(0);

if (bind(newfd, (sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
if (bind(newfd, reinterpret_cast<sockaddr*>(&servaddr), sizeof(servaddr)) < 0) {
throw dpp::connection_exception(err_bind_failure, "Can't bind() client UDP socket");
}

Expand All @@ -417,7 +417,7 @@ bool discord_voice_client::handle_frame(const std::string &data, ws_opcode opcod
int bound_port = 0;
sockaddr_in sin{};
socklen_t len = sizeof(sin);
if (getsockname(this->fd, (sockaddr *)&sin, &len) > -1) {
if (getsockname(this->fd, reinterpret_cast<sockaddr *>(&sin), &len) > -1) {
bound_port = ntohs(sin.sin_port);
}

Expand Down
12 changes: 6 additions & 6 deletions src/dpp/voice/enabled/opus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ discord_voice_client& discord_voice_client::send_audio_raw(uint16_t* audio_data,
}

if (length > send_audio_raw_max_length) {
std::string s_audio_data((const char*)audio_data, length);
std::string s_audio_data(reinterpret_cast<const char*>(audio_data), length);

while (s_audio_data.length() > send_audio_raw_max_length) {
std::string packet(s_audio_data.substr(0, send_audio_raw_max_length));
const auto packet_size = static_cast<ptrdiff_t>(packet.size());

s_audio_data.erase(s_audio_data.begin(), s_audio_data.begin() + packet_size);

send_audio_raw((uint16_t*)packet.data(), packet_size);
send_audio_raw(reinterpret_cast<uint16_t*>(packet.data()), packet_size);
}

return *this;
}

if (length < send_audio_raw_max_length) {
std::string packet((const char*)audio_data, length);
std::string packet(reinterpret_cast<const char*>(audio_data), length);
packet.resize(send_audio_raw_max_length, 0);

return send_audio_raw((uint16_t*)packet.data(), packet.size());
return send_audio_raw(reinterpret_cast<uint16_t*>(packet.data()), packet.size());
}

opus_int32 encoded_audio_max_length = (opus_int32)length;
std::vector<uint8_t> encoded_audio(encoded_audio_max_length);
size_t encoded_audio_length = encoded_audio_max_length;
encoded_audio_length = this->encode((uint8_t*)audio_data, length, encoded_audio.data(), encoded_audio_length);
encoded_audio_length = this->encode(reinterpret_cast<uint8_t*>(audio_data), length, encoded_audio.data(), encoded_audio_length);
send_audio_opus(encoded_audio.data(), encoded_audio_length);
return *this;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ size_t discord_voice_client::encode(uint8_t *input, size_t inDataSize, uint8_t *
return outDataSize;
}
for (size_t i = 0; i < (inDataSize / mEncFrameBytes); ++ i) {
const opus_int16* pcm = (opus_int16*)(input + i * mEncFrameBytes);
const opus_int16* pcm = reinterpret_cast<opus_int16*>(input + i * mEncFrameBytes);
int ret = opus_encode(encoder, pcm, mEncFrameSize, out, 65536);
if (ret > 0) {
int retval = opus_repacketizer_cat(repacketizer, out, ret);
Expand Down
4 changes: 2 additions & 2 deletions src/dpp/voice/enabled/read_ready.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace dpp {
void discord_voice_client::read_ready()
{
uint8_t buffer[65535];
int packet_size = this->udp_recv((char*)buffer, sizeof(buffer));
int packet_size = this->udp_recv(reinterpret_cast<char*>(buffer), sizeof(buffer));

bool receive_handler_is_empty = creator->on_voice_receive.empty() && creator->on_voice_receive_combined.empty();
if (packet_size <= 0 || receive_handler_is_empty) {
Expand Down Expand Up @@ -67,7 +67,7 @@ void discord_voice_client::read_ready()

voice_payload vp{0, // seq, populate later
0, // timestamp, populate later
std::make_unique<voice_receive_t>(nullptr, std::string((char*)buffer, packet_size))};
std::make_unique<voice_receive_t>(nullptr, std::string(reinterpret_cast<char*>(buffer), packet_size))};

vp.vr->voice_client = this;

Expand Down
15 changes: 11 additions & 4 deletions src/dpp/voice/enabled/read_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ dpp::socket discord_voice_client::want_write() {
std::lock_guard<std::mutex> lock(this->stream_mutex);
if (!this->paused && !outbuf.empty()) {
return fd;
} else {
return INVALID_SOCKET;
}
return INVALID_SOCKET;

}

dpp::socket discord_voice_client::want_read() {
Expand All @@ -56,12 +56,19 @@ int discord_voice_client::udp_send(const char* data, size_t length) {
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(this->port);
servaddr.sin_addr.s_addr = inet_addr(this->ip.c_str());
return (int) sendto(this->fd, data, (int)length, 0, (const sockaddr*)&servaddr, (int)sizeof(sockaddr_in));
return static_cast<int>(sendto(
this->fd,
data,
static_cast<int>(length),
0,
reinterpret_cast<const sockaddr*>(&servaddr),
static_cast<int>(sizeof(sockaddr_in))
));
}

int discord_voice_client::udp_recv(char* data, size_t max_length)
{
return (int) recv(this->fd, data, (int)max_length, 0);
return static_cast<int>(recv(this->fd, data, static_cast<int>(max_length), 0));
}

}
3 changes: 0 additions & 3 deletions src/dpp/voice/enabled/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
#include <dpp/exception.h>
#include <dpp/isa_detection.h>
#include <dpp/discordvoiceclient.h>

#include <opus/opus.h>
#include "../../dave/encryptor.h"

#include "enabled.h"

namespace dpp {
Expand Down
20 changes: 0 additions & 20 deletions src/dpp/voice/enabled/voice_payload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,10 @@
*
************************************************************************************/

#ifdef _WIN32
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <io.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <string_view>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <dpp/exception.h>
#include <dpp/isa_detection.h>
#include <dpp/discordvoiceclient.h>
#include <dpp/json.h>

#include <sodium.h>
#include <opus/opus.h>
#include "../../dave/session.h"
#include "../../dave/decryptor.h"
#include "../../dave/encryptor.h"

namespace dpp {

Expand Down
2 changes: 1 addition & 1 deletion src/dpp/voice/enabled/write_ready.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void discord_voice_client::write_ready() {
std::lock_guard<std::mutex> lock(this->stream_mutex);
if (!this->paused && outbuf.size()) {
type = send_audio_type;
if (outbuf[0].packet.size() == sizeof(uint16_t) && (*((uint16_t*)(outbuf[0].packet.data()))) == AUDIO_TRACK_MARKER) {
if (outbuf[0].packet.size() == sizeof(uint16_t) && (*(reinterpret_cast<uint16_t*>(outbuf[0].packet.data()))) == AUDIO_TRACK_MARKER) {
outbuf.erase(outbuf.begin());
track_marker_found = true;
if (tracks > 0) {
Expand Down

0 comments on commit c8774d4

Please sign in to comment.