Skip to content

Commit

Permalink
Use a std::string_view for JSON data
Browse files Browse the repository at this point in the history
  • Loading branch information
Crayon2000 committed Dec 22, 2024
1 parent c31b24c commit 30e6b48
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
## [Unreleased]

- Hold direction buttons to change IP.
- Fix a hang that occurs after 30 minutes.

## [1.3.0] - 2024-03-18

Expand Down
9 changes: 3 additions & 6 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,9 @@ static int sendPadData() {
}
}

// The buffer sent to the computer
char msg_data[2048];
pad_to_json(pad_data, msg_data, sizeof(msg_data));

// Send the message
udp_print(msg_data);
// Convert the data to JSON and send it
std::string_view msg = pad_to_json(pad_data);
udp_print(msg);

// Make a small delay to prevent filling up the computer receive buffer
std::this_thread::sleep_for(std::chrono::milliseconds(10));
Expand Down
22 changes: 9 additions & 13 deletions source/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static volatile bool udp_lock = false;
* @param ipString The IP address to connect to.
* @param ipport The port to connect to.
*/
void udp_init(const char * ipString, uint16_t ipport)
void udp_init(std::string_view ipString, uint16_t ipport)
{
udp_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(udp_socket < 0) {
Expand All @@ -30,10 +30,9 @@ void udp_init(const char * ipString, uint16_t ipport)
memset(&connect_addr, 0, sizeof(connect_addr));
connect_addr.sin_family = AF_INET;
connect_addr.sin_port = ipport;
inet_aton(ipString, &connect_addr.sin_addr);
inet_aton(ipString.data(), &connect_addr.sin_addr);

if(connect(udp_socket, (struct sockaddr*)&connect_addr, sizeof(connect_addr)) < 0)
{
if(connect(udp_socket, (struct sockaddr*)&connect_addr, sizeof(connect_addr)) < 0) {
close(udp_socket);
udp_socket = -1;
}
Expand All @@ -44,8 +43,7 @@ void udp_init(const char * ipString, uint16_t ipport)
*/
void udp_deinit(void)
{
if(udp_socket >= 0)
{
if(udp_socket >= 0) {
close(udp_socket);
udp_socket = -1;
}
Expand All @@ -56,7 +54,7 @@ void udp_deinit(void)
* @param str The string to send.
* @return Returns true on success, false otherwise.
*/
bool udp_print(const char *str)
bool udp_print(std::string_view str)
{
// socket is always 0 initially as it is in the BSS
if(udp_socket < 0) {
Expand All @@ -70,17 +68,15 @@ bool udp_print(const char *str)

bool result = true;

int len = strlen(str);
while (len > 0) {
const int block = len < 1400 ? len : 1400; // take max 1400 bytes per UDP packet
const int ret = send(udp_socket, str, block, 0);
while (str.empty() == false) {
const size_t block = std::min(str.size(), static_cast<size_t>(1400)); // take max 1400 bytes per UDP packet
const ssize_t ret = send(udp_socket, str.data(), block, 0);
if(ret < 0) {
result = false;
break;
}

len -= ret;
str += ret;
str.remove_prefix(ret);
}

udp_lock = false;
Expand Down
8 changes: 4 additions & 4 deletions source/udp.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>
#include <cstdint>
#include <string_view>

#ifdef __cplusplus
extern "C" {
#endif

void udp_init(const char * ipString, uint16_t ipport);
void udp_init(std::string_view ipString, uint16_t ipport);
void udp_deinit(void);
bool udp_print(const char *str);
bool udp_print(std::string_view str);

#ifdef __cplusplus
}
Expand Down
9 changes: 4 additions & 5 deletions source/vpad_to_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ static const std::map gcmask = {
/**
* Convert GamePad data to JSON string used by UsendMii.
* @param[in] pad_data Controllers data.
* @param[out] out Buffer where to copy the formatted data.
* @param[in] out_size Size of the out buffer.
* @return Returns the JSON string.
*/
void pad_to_json(PADData pad_data, char* out, uint32_t out_size)
std::string_view pad_to_json(PADData pad_data)
{
VPADTouchData TPCalibrated;
VPADGetTPCalibratedPointEx(VPAD_CHAN_0, VPAD_TP_854X480, &TPCalibrated, &pad_data.vpad->tpNormal);
Expand Down Expand Up @@ -266,6 +265,6 @@ void pad_to_json(PADData pad_data, char* out, uint32_t out_size)

writer.EndObject(); // End root object

// Convert to string
strncpy(out, sb.GetString(), out_size);
// Convert to string_view
return std::string_view(sb.GetString(), sb.GetSize());
}
3 changes: 2 additions & 1 deletion source/vpad_to_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <padscore/kpad.h>
#include <vpad/input.h>
#include <nn/hpad/hpad.h>
#include <string_view>

#ifdef __cplusplus
extern "C" {
Expand All @@ -19,7 +20,7 @@ typedef struct {
HPADStatus* hpad[4]; /**< USB Gamecube Controller Adapter. */
} PADData;

void pad_to_json(PADData pad_data, char* out, uint32_t out_size);
std::string_view pad_to_json(PADData pad_data);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 30e6b48

Please sign in to comment.